#207886: Cannot run TestNG tests in Ant project
authorLukas Jungmann <jungi@netbeans.org>
Sun, 05 Feb 2012 02:28:57 +0100
changeset 17777becd301cfcbf
parent 17776 2234fd472308
child 17778 412979d47b30
#207886: Cannot run TestNG tests in Ant project
testng/src/org/netbeans/modules/contrib/testng/output/TestNGAntLogger.java
testng/src/org/netbeans/modules/contrib/testng/output/TestNGOutputReader.java
     1.1 --- a/testng/src/org/netbeans/modules/contrib/testng/output/TestNGAntLogger.java	Sat Feb 04 21:12:07 2012 +0100
     1.2 +++ b/testng/src/org/netbeans/modules/contrib/testng/output/TestNGAntLogger.java	Sun Feb 05 02:28:57 2012 +0100
     1.3 @@ -41,10 +41,7 @@
     1.4  package org.netbeans.modules.contrib.testng.output;
     1.5  
     1.6  import java.io.File;
     1.7 -import java.util.ArrayList;
     1.8 -import java.util.Collections;
     1.9 -import java.util.List;
    1.10 -import java.util.Properties;
    1.11 +import java.util.*;
    1.12  import org.apache.tools.ant.module.spi.AntEvent;
    1.13  import org.apache.tools.ant.module.spi.AntLogger;
    1.14  import org.apache.tools.ant.module.spi.AntSession;
    1.15 @@ -301,33 +298,44 @@
    1.16              if (sessionInfo.getSessionType() == null) {
    1.17                  sessionInfo.setSessionType(sessionType);
    1.18              }
    1.19 -            //TODO: do the same for java/java debug tasks
    1.20 +            String suiteName = null;
    1.21 +            String logLevel = null;
    1.22              TaskStructure struct = event.getTaskStructure();
    1.23 -            String tmp = struct.getAttribute("suitename");
    1.24 -            if (tmp != null) {
    1.25 -                sessionInfo.setSessionName(event.evaluate(tmp));
    1.26 +            if (TASK_TESTNG.equals(struct.getName())) {
    1.27 +                suiteName = struct.getAttribute("suitename");
    1.28 +                logLevel = struct.getAttribute("verbose");
    1.29 +                if (logLevel == null) {
    1.30 +                    logLevel = struct.getAttribute("log");
    1.31 +                }
    1.32 +            } else if (TASK_JAVA.equals(struct.getName())) {
    1.33 +                TaskStructure[] nestedElems = struct.getChildren();
    1.34 +                for (TaskStructure ts : nestedElems) {
    1.35 +                    if (ts.getName().equals("arg")) {                //NOI18N
    1.36 +                        String a = ts.getAttribute("line");
    1.37 +                        if (a != null) {
    1.38 +                            String[] args = event.evaluate(a).split(" ");
    1.39 +                            int size = args.length;
    1.40 +                            for (int i = 0; i < size; i++) {
    1.41 +                                String curr = args[i];
    1.42 +                                if ("-suitename".equals(curr)) {
    1.43 +                                    suiteName = i + 1 < size ? args[i + 1] : null;
    1.44 +                                    i++;
    1.45 +                                } else if ("-log".equals(curr) || "-verbose".equals(curr)) {
    1.46 +                                    logLevel = i + 1 < size ? args[i + 1] : null;
    1.47 +                                    i++;
    1.48 +                                }
    1.49 +                            }
    1.50 +                        }
    1.51 +                    }
    1.52 +                }
    1.53 +
    1.54 +            } else {
    1.55 +                assert false : "Unexpeted task " + struct.getName();
    1.56              }
    1.57 -            tmp = struct.getAttribute("verbose");
    1.58 -            if (tmp == null) {
    1.59 -                tmp = struct.getAttribute("log");
    1.60 -            }
    1.61 -            boolean offline = false;
    1.62 -            if (tmp != null) {
    1.63 -                int logLevel;
    1.64 -                try {
    1.65 -                    logLevel = Integer.valueOf(event.evaluate(tmp));
    1.66 -                } catch (NumberFormatException nfe) {
    1.67 -                    logLevel = -1;
    1.68 -                }
    1.69 -                //logging is explicitly turned off by the user, so show only final
    1.70 -                //results computed off-line from testng-results.xml file
    1.71 -                offline = logLevel == 0;
    1.72 -            }
    1.73 -
    1.74              /*
    1.75               * Count the test classes in the try-catch block so that
    1.76 -             * 'testTaskStarted(...)' is called even if counting fails
    1.77 -             * (throws an exception):
    1.78 +             * 'testTaskStarted(...)' is called even if counting fails (throws
    1.79 +             * an exception):
    1.80               */
    1.81              //would have to parse all incoming xmls, take includes/excludes
    1.82              //into accout, dependencies between tests, groups etc
    1.83 @@ -339,8 +347,23 @@
    1.84  //                Logger.getLogger(TestNGAntLogger.class.getName()).log(Level.SEVERE, null, ex);
    1.85  //            }
    1.86  
    1.87 +            if (suiteName != null) {
    1.88 +                sessionInfo.setSessionName(event.evaluate(suiteName));
    1.89 +            }
    1.90 +            boolean offline = false;
    1.91 +            if (logLevel != null) {
    1.92 +                int lvl;
    1.93 +                try {
    1.94 +                    lvl = Integer.valueOf(event.evaluate(logLevel));
    1.95 +                } catch (NumberFormatException nfe) {
    1.96 +                    lvl = -1;
    1.97 +                }
    1.98 +                //logging is explicitly turned off by the user, so show only final
    1.99 +                //results computed off-line from testng-results.xml file
   1.100 +                offline = lvl == 0;
   1.101 +            }
   1.102 +            getOutputReader(event).testTaskStarted(offline, event);
   1.103  //            getOutputReader(event).testTaskStarted(testClassCount, hasXmlOutput, event);
   1.104 -            getOutputReader(event).testTaskStarted(offline, event);
   1.105          }
   1.106      }
   1.107  
   1.108 @@ -399,8 +422,12 @@
   1.109              } catch (Exception e) {
   1.110              }
   1.111              Properties props = new Properties();
   1.112 -            for (String propName : event.getPropertyNames()) {
   1.113 -                props.setProperty(propName, event.getProperty(propName));
   1.114 +            String[] propsOfInterest = {"javac.includes", "classname", "methodname", "work.dir", "classpath", "platform.java"};//NOI18N
   1.115 +            for(String prop:propsOfInterest) {
   1.116 +                String val = event.getProperty(prop);
   1.117 +                if (val!=null) {
   1.118 +                    props.setProperty(prop, val);
   1.119 +                }
   1.120              }
   1.121              outputReader = new TestNGOutputReader(
   1.122                      session,
     2.1 --- a/testng/src/org/netbeans/modules/contrib/testng/output/TestNGOutputReader.java	Sat Feb 04 21:12:07 2012 +0100
     2.2 +++ b/testng/src/org/netbeans/modules/contrib/testng/output/TestNGOutputReader.java	Sun Feb 05 02:28:57 2012 +0100
     2.3 @@ -92,7 +92,8 @@
     2.4      /**
     2.5       * whether XML report is expected
     2.6       */
     2.7 -    private boolean expectXmlReport;
     2.8 +    private boolean offline;
     2.9 +    private boolean noresults = true;
    2.10      /**
    2.11       *
    2.12       */
    2.13 @@ -161,10 +162,11 @@
    2.14          if (msg == null) {
    2.15              return;
    2.16          }
    2.17 -        if (!msg.startsWith(RegexpUtils.TEST_LISTENER_PREFIX) || expectXmlReport) {
    2.18 +        if (!msg.startsWith(RegexpUtils.TEST_LISTENER_PREFIX) || offline) {
    2.19              //this message is not for us...
    2.20              return;
    2.21          }
    2.22 +        if (noresults) noresults = false;
    2.23          verboseMessageLogged(msg);
    2.24  //        displayOutput(msg, event.getLogLevel() == AntEvent.LOG_WARN);
    2.25      }
    2.26 @@ -309,9 +311,10 @@
    2.27          if (tc != null) {
    2.28              tc.getOutput().add(new OutputLine(msg, false));
    2.29          }
    2.30 -        if (!expectXmlReport) {
    2.31 +        if (!offline) {
    2.32              //log/verbose level = 0 so don't show output
    2.33              displayOutput(msg, event.getLogLevel() == AntEvent.LOG_WARN);
    2.34 +            verboseMessageLogged(event);
    2.35          }
    2.36      }
    2.37  
    2.38 @@ -346,6 +349,8 @@
    2.39                  resultsDir = determineTestNGTaskResultsDir(event);
    2.40              } else if (taskName.equals("java")) {                       //NOI18N
    2.41                  resultsDir = determineJavaTaskResultsDir(event);
    2.42 +            } else {
    2.43 +                assert false : "Unexpected task: " + taskName;
    2.44              }
    2.45          }
    2.46  
    2.47 @@ -472,8 +477,8 @@
    2.48       * Notifies that a test (Ant) task was just started.
    2.49       */
    2.50      void testTaskStarted(boolean expectXmlOutput, AntEvent event) {
    2.51 -        this.expectXmlReport = expectXmlOutput;
    2.52 -        if (!expectXmlReport) {
    2.53 +        this.offline = expectXmlOutput;
    2.54 +        if (!offline) {
    2.55              manager.testStarted(testSession);
    2.56          }
    2.57          resultsDir = determineResultsDir(event);
    2.58 @@ -482,8 +487,10 @@
    2.59      /**
    2.60       */
    2.61      void testTaskFinished() {
    2.62 -        if (expectXmlReport) {
    2.63 +        if (offline) {
    2.64              manager.testStarted(testSession);
    2.65 +        }
    2.66 +        if (offline || noresults) {
    2.67              //get results from report xml file
    2.68              if (resultsDir != null) {
    2.69                  File reportFile = findReportFile();