go to suite action
authorLukas Jungmann <jungi@netbeans.org>
Tue, 03 Jan 2012 23:04:24 +0100
changeset 177297b3b96bba65e
parent 17728 3a9aec79872e
child 17730 75a602db53b1
go to suite action
testng/src/org/netbeans/modules/contrib/testng/output/OutputUtils.java
testng/src/org/netbeans/modules/contrib/testng/output/XmlSuiteHandler.java
     1.1 --- a/testng/src/org/netbeans/modules/contrib/testng/output/OutputUtils.java	Mon Jan 02 21:25:38 2012 +0100
     1.2 +++ b/testng/src/org/netbeans/modules/contrib/testng/output/OutputUtils.java	Tue Jan 03 23:04:24 2012 +0100
     1.3 @@ -1,7 +1,7 @@
     1.4  /*
     1.5   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6   *
     1.7 - * Copyright © 1997-2011 Oracle and/or its affiliates. All rights reserved.
     1.8 + * Copyright © 1997-2012 Oracle and/or its affiliates. All rights reserved.
     1.9   *
    1.10   * The contents of this file are subject to the terms of either the GNU
    1.11   * General Public License Version 2 only ("GPL") or the Common
    1.12 @@ -57,6 +57,7 @@
    1.13  import org.netbeans.api.java.source.JavaSource;
    1.14  import org.netbeans.api.java.source.JavaSource.Phase;
    1.15  import org.netbeans.api.java.source.Task;
    1.16 +import org.netbeans.modules.contrib.testng.TestNGEntityResolver;
    1.17  import org.netbeans.modules.gsf.testrunner.api.TestSuite;
    1.18  import org.netbeans.modules.gsf.testrunner.api.Trouble;
    1.19  import org.openide.cookies.EditorCookie;
    1.20 @@ -67,6 +68,11 @@
    1.21  import org.openide.text.Line;
    1.22  import org.openide.text.Line.ShowOpenType;
    1.23  import org.openide.text.Line.ShowVisibilityType;
    1.24 +import org.openide.util.Exceptions;
    1.25 +import org.openide.xml.XMLUtil;
    1.26 +import org.xml.sax.InputSource;
    1.27 +import org.xml.sax.SAXException;
    1.28 +import org.xml.sax.XMLReader;
    1.29  
    1.30  /**
    1.31   *
    1.32 @@ -85,33 +91,8 @@
    1.33          if ((suite != null) && (suite instanceof TestSuite)) {
    1.34              final FileObject fo = ((TestNGTestSuite) suite).getSuiteFO();
    1.35              if (fo != null) {
    1.36 -                final long[] line = new long[]{0};
    1.37 -                JavaSource javaSource = JavaSource.forFileObject(fo);
    1.38 -                if (javaSource != null) {
    1.39 -                    try {
    1.40 -                        javaSource.runUserActionTask(new Task<CompilationController>() {
    1.41 -
    1.42 -                            public void run(CompilationController compilationController) throws Exception {
    1.43 -                                compilationController.toPhase(Phase.ELEMENTS_RESOLVED);
    1.44 -                                Trees trees = compilationController.getTrees();
    1.45 -                                CompilationUnitTree compilationUnitTree = compilationController.getCompilationUnit();
    1.46 -                                List<? extends Tree> typeDecls = compilationUnitTree.getTypeDecls();
    1.47 -                                for (Tree tree : typeDecls) {
    1.48 -                                    Element element = trees.getElement(trees.getPath(compilationUnitTree, tree));
    1.49 -                                    if (element != null && element.getKind() == ElementKind.CLASS && element.getSimpleName().contentEquals(fo.getName())) {
    1.50 -                                        long pos = trees.getSourcePositions().getStartPosition(compilationUnitTree, tree);
    1.51 -                                        line[0] = compilationUnitTree.getLineMap().getLineNumber(pos);
    1.52 -                                        break;
    1.53 -                                    }
    1.54 -                                }
    1.55 -                            }
    1.56 -                        }, true);
    1.57 -
    1.58 -                    } catch (IOException ioe) {
    1.59 -                        LOGGER.log(Level.WARNING, null, ioe);
    1.60 -                    }
    1.61 -                }
    1.62 -                openFile(fo, (int) line[0]);
    1.63 +                int[] location = XmlSuiteHandler.getSuiteLocation(fo, suite.getName());
    1.64 +                openFile(fo, location[0], location[1]);
    1.65              }
    1.66          }
    1.67      }
    1.68 @@ -302,6 +283,10 @@
    1.69      }
    1.70  
    1.71      static void openFile(FileObject file, int lineNum) {
    1.72 +        openFile(file, lineNum, Integer.MIN_VALUE);
    1.73 +    }
    1.74 +
    1.75 +    static void openFile(FileObject file, int lineNum, int columnNum) {
    1.76  
    1.77          /*
    1.78           * Most of the following code was copied from the Ant module, method
    1.79 @@ -326,7 +311,11 @@
    1.80                      try {
    1.81                          Line l = ed.getLineSet().getOriginal(lineNum - 1);
    1.82                          if (!l.isDeleted()) {
    1.83 -                            l.show(ShowOpenType.OPEN, ShowVisibilityType.FOCUS);
    1.84 +                            if (columnNum != Integer.MIN_VALUE) {
    1.85 +                                l.show(ShowOpenType.OPEN, ShowVisibilityType.FOCUS, columnNum);
    1.86 +                            } else {
    1.87 +                                l.show(ShowOpenType.OPEN, ShowVisibilityType.FOCUS);
    1.88 +                            }
    1.89                          }
    1.90                      } catch (IndexOutOfBoundsException ioobe) {
    1.91                          // Probably harmless. Bogus line number.
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/testng/src/org/netbeans/modules/contrib/testng/output/XmlSuiteHandler.java	Tue Jan 03 23:04:24 2012 +0100
     2.3 @@ -0,0 +1,108 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
     2.8 + *
     2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 + * Other names may be trademarks of their respective owners.
    2.11 + *
    2.12 + * The contents of this file are subject to the terms of either the GNU
    2.13 + * General Public License Version 2 only ("GPL") or the Common
    2.14 + * Development and Distribution License("CDDL") (collectively, the
    2.15 + * "License"). You may not use this file except in compliance with the
    2.16 + * License. You can obtain a copy of the License at
    2.17 + * http://www.netbeans.org/cddl-gplv2.html
    2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 + * specific language governing permissions and limitations under the
    2.20 + * License.  When distributing the software, include this License Header
    2.21 + * Notice in each file and include the License file at
    2.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 + * particular file as subject to the "Classpath" exception as provided
    2.24 + * by Oracle in the GPL Version 2 section of the License file that
    2.25 + * accompanied this code. If applicable, add the following below the
    2.26 + * License Header, with the fields enclosed by brackets [] replaced by
    2.27 + * your own identifying information:
    2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.29 + *
    2.30 + * If you wish your version of this file to be governed by only the CDDL
    2.31 + * or only the GPL Version 2, indicate your decision by adding
    2.32 + * "[Contributor] elects to include this software in this distribution
    2.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.34 + * single choice of license, a recipient has the option to distribute
    2.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.36 + * to extend the choice of license to its licensees as provided above.
    2.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.38 + * Version 2 license, then the option applies only if the new code is
    2.39 + * made subject to such option by the copyright holder.
    2.40 + *
    2.41 + * Contributor(s):
    2.42 + *
    2.43 + * Portions Copyrighted 2012 Sun Microsystems, Inc.
    2.44 + */
    2.45 +package org.netbeans.modules.contrib.testng.output;
    2.46 +
    2.47 +import java.io.IOException;
    2.48 +import java.util.logging.Level;
    2.49 +import java.util.logging.Logger;
    2.50 +import org.netbeans.modules.contrib.testng.TestNGEntityResolver;
    2.51 +import org.openide.filesystems.FileObject;
    2.52 +import org.openide.xml.XMLUtil;
    2.53 +import org.xml.sax.*;
    2.54 +import org.xml.sax.helpers.DefaultHandler;
    2.55 +
    2.56 +/**
    2.57 + *
    2.58 + * @author lukas
    2.59 + */
    2.60 +public class XmlSuiteHandler extends DefaultHandler {
    2.61 +
    2.62 +    private static final Logger LOGGER = Logger.getLogger(XmlSuiteHandler.class.getName());
    2.63 +    private Locator loc;
    2.64 +    private String suite;
    2.65 +    private int line;
    2.66 +    private int column;
    2.67 +
    2.68 +    private XmlSuiteHandler(String name) {
    2.69 +        suite = name;
    2.70 +    }
    2.71 +
    2.72 +    public static int[] getSuiteLocation(FileObject suiteFile, String suiteName) {
    2.73 +        int[] location = new int[]{0, 0};
    2.74 +        try {
    2.75 +            XMLReader r = XMLUtil.createXMLReader(false, false);
    2.76 +            r.setEntityResolver(new TestNGEntityResolver());
    2.77 +            XmlSuiteHandler sl = new XmlSuiteHandler(suiteName);
    2.78 +            r.setContentHandler(sl);
    2.79 +            r.parse(new InputSource(suiteFile.getInputStream()));
    2.80 +            location[0] = sl.getLine();
    2.81 +            location[1] = sl.getColumn();
    2.82 +        } catch (IOException ex) {
    2.83 +            LOGGER.log(Level.WARNING, null, ex);
    2.84 +        } catch (SAXException ex) {
    2.85 +            LOGGER.log(Level.WARNING, null, ex);
    2.86 +        }
    2.87 +        return location;
    2.88 +    }
    2.89 +
    2.90 +    @Override
    2.91 +    public void setDocumentLocator(Locator locator) {
    2.92 +        loc = locator;
    2.93 +    }
    2.94 +
    2.95 +    @Override
    2.96 +    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
    2.97 +        super.startElement(uri, localName, qName, attributes);
    2.98 +        if ("test".equals(qName) && attributes != null && suite.equals(attributes.getValue("name"))) {
    2.99 +            line = loc.getLineNumber();
   2.100 +            column = loc.getColumnNumber() - suite.length() - 3;
   2.101 +        }
   2.102 +    }
   2.103 +
   2.104 +    public int getLine() {
   2.105 +        return line;
   2.106 +    }
   2.107 +
   2.108 +    public int getColumn() {
   2.109 +        return column;
   2.110 +    }
   2.111 +}