bodewig     2005/04/11 03:36:53

  Modified:    src/main/org/apache/tools/ant/taskdefs/optional/junit Tag:
                        ANT_16_BRANCH JUnitTestRunner.java
  Log:
  Merge fix for 30798
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.42.2.10 +98 -99    
ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
  
  Index: JUnitTestRunner.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java,v
  retrieving revision 1.42.2.9
  retrieving revision 1.42.2.10
  diff -u -r1.42.2.9 -r1.42.2.10
  --- JUnitTestRunner.java      24 Mar 2005 08:36:40 -0000      1.42.2.9
  +++ JUnitTestRunner.java      11 Apr 2005 10:36:53 -0000      1.42.2.10
  @@ -137,16 +137,6 @@
       private boolean haltOnFailure = false;
   
       /**
  -     * The corresponding testsuite.
  -     */
  -    private Test suite = null;
  -
  -    /**
  -     * Exception caught in constructor.
  -     */
  -    private Exception exception;
  -
  -    /**
        * Returncode
        */
       private int retCode = SUCCESS;
  @@ -168,6 +158,9 @@
       /** Running more than one test suite? */
       private static boolean multipleTests = false;
   
  +    /** ClassLoader passed in in non-forked mode. */
  +    private ClassLoader loader;
  +
       /**
        * Constructor for fork=true or when the user hasn't specified a
        * classpath.
  @@ -207,38 +200,7 @@
           this.haltOnError = haltOnError;
           this.haltOnFailure = haltOnFailure;
           this.showOutput = showOutput;
  -
  -        try {
  -            Class testClass = null;
  -            if (loader == null) {
  -                testClass = Class.forName(test.getName());
  -            } else {
  -                testClass = Class.forName(test.getName(), true, loader);
  -            }
  -
  -            Method suiteMethod = null;
  -            try {
  -                // check if there is a suite method
  -                suiteMethod = testClass.getMethod("suite", new Class[0]);
  -            } catch (NoSuchMethodException e) {
  -                // no appropriate suite method found. We don't report any
  -                // error here since it might be perfectly normal.
  -            }
  -            if (suiteMethod != null) {
  -                // if there is a suite method available, then try
  -                // to extract the suite from it. If there is an error
  -                // here it will be caught below and reported.
  -                suite = (Test) suiteMethod.invoke(null, new Class[0]);
  -            } else {
  -                // try to extract a test suite automatically
  -                // this will generate warnings if the class is no suitable 
Test
  -                suite = new TestSuite(testClass);
  -            }
  -
  -        } catch (Exception e) {
  -            retCode = ERRORS;
  -            exception = e;
  -        }
  +        this.loader = loader;
       }
   
       public void run() {
  @@ -248,77 +210,114 @@
               res.addListener((TestListener) formatters.elementAt(i));
           }
   
  -        long start = System.currentTimeMillis();
  +        ByteArrayOutputStream errStrm = new ByteArrayOutputStream();
  +        systemError = new PrintStream(errStrm);
   
  -        fireStartTestSuite();
  -        if (exception != null) { // had an exception in the constructor
  -            for (int i = 0; i < formatters.size(); i++) {
  -                ((TestListener) formatters.elementAt(i)).addError(null,
  -                                                                 exception);
  -            }
  -            junitTest.setCounts(1, 0, 1);
  -            junitTest.setRunTime(0);
  -        } else {
  +        ByteArrayOutputStream outStrm = new ByteArrayOutputStream();
  +        systemOut = new PrintStream(outStrm);
   
  +        PrintStream savedErr = null;
  +        PrintStream savedOut = null;
   
  -            ByteArrayOutputStream errStrm = new ByteArrayOutputStream();
  -            systemError = new PrintStream(errStrm);
  +        if (forked) {
  +            savedOut = System.out;
  +            savedErr = System.err;
  +            if (!showOutput) {
  +                System.setOut(systemOut);
  +                System.setErr(systemError);
  +            } else {
  +                System.setOut(new PrintStream(
  +                                       new TeeOutputStream(savedOut, 
systemOut)
  +                                       )
  +                              );
  +                System.setErr(new PrintStream(
  +                                       new TeeOutputStream(savedErr,
  +                                                           systemError)
  +                                       )
  +                              );
  +            }
  +            perm = null;
  +        } else {
  +            if (perm != null) {
  +                perm.setSecurityManager();
  +            }
  +        }
   
  -            ByteArrayOutputStream outStrm = new ByteArrayOutputStream();
  -            systemOut = new PrintStream(outStrm);
  +        Test suite = null;
  +        Throwable exception = null;
   
  -            PrintStream savedOut = null;
  -            PrintStream savedErr = null;
  +        try {
   
  -            if (forked) {
  -                savedOut = System.out;
  -                savedErr = System.err;
  -                if (!showOutput) {
  -                    System.setOut(systemOut);
  -                    System.setErr(systemError);
  +            try {
  +                Class testClass = null;
  +                if (loader == null) {
  +                    testClass = Class.forName(junitTest.getName());
                   } else {
  -                    System.setOut(new PrintStream(
  -                                      new TeeOutputStream(savedOut, 
systemOut)
  -                                      )
  -                                  );
  -                    System.setErr(new PrintStream(
  -                                      new TeeOutputStream(savedErr,
  -                                                          systemError)
  -                                      )
  -                                  );
  +                    testClass = Class.forName(junitTest.getName(), true,
  +                                              loader);
                   }
  -                perm = null;
  -            } else {
  -                if (perm != null) {
  -                    perm.setSecurityManager();
  +
  +                Method suiteMethod = null;
  +                try {
  +                    // check if there is a suite method
  +                    suiteMethod = testClass.getMethod("suite", new Class[0]);
  +                } catch (NoSuchMethodException e) {
  +                    // no appropriate suite method found. We don't report any
  +                    // error here since it might be perfectly normal.
  +                }
  +                if (suiteMethod != null) {
  +                    // if there is a suite method available, then try
  +                    // to extract the suite from it. If there is an error
  +                    // here it will be caught below and reported.
  +                    suite = (Test) suiteMethod.invoke(null, new Class[0]);
  +                } else {
  +                    // try to extract a test suite automatically this
  +                    // will generate warnings if the class is no
  +                    // suitable Test
  +                    suite = new TestSuite(testClass);
                   }
  -            }
   
  +            } catch (Throwable e) {
  +                retCode = ERRORS;
  +                exception = e;
  +            }
   
  -            try {
  -                suite.run(res);
  -            } finally {
  -                if (perm != null) {
  -                    perm.restoreSecurityManager();
  +            long start = System.currentTimeMillis();
  +
  +            fireStartTestSuite();
  +            if (exception != null) { // had an exception constructing suite
  +                for (int i = 0; i < formatters.size(); i++) {
  +                    ((TestListener) formatters.elementAt(i))
  +                        .addError(null, exception);
                   }
  -                if (savedOut != null) {
  -                    System.setOut(savedOut);
  -                }
  -                if (savedErr != null) {
  -                    System.setErr(savedErr);
  +                junitTest.setCounts(1, 0, 1);
  +                junitTest.setRunTime(0);
  +            } else {
  +                try {
  +                    suite.run(res);
  +                } finally {
  +                    junitTest.setCounts(res.runCount(), res.failureCount(),
  +                                        res.errorCount());
  +                    junitTest.setRunTime(System.currentTimeMillis() - start);
                   }
  -
  -                systemError.close();
  -                systemError = null;
  -                systemOut.close();
  -                systemOut = null;
  -                sendOutAndErr(new String(outStrm.toByteArray()),
  -                              new String(errStrm.toByteArray()));
  -
  -                junitTest.setCounts(res.runCount(), res.failureCount(),
  -                                    res.errorCount());
  -                junitTest.setRunTime(System.currentTimeMillis() - start);
               }
  +        } finally {
  +            if (perm != null) {
  +                perm.restoreSecurityManager();
  +            }
  +            if (savedOut != null) {
  +                System.setOut(savedOut);
  +            }
  +            if (savedErr != null) {
  +                System.setErr(savedErr);
  +            }
  +
  +            systemError.close();
  +            systemError = null;
  +            systemOut.close();
  +            systemOut = null;
  +            sendOutAndErr(new String(outStrm.toByteArray()),
  +                          new String(errStrm.toByteArray()));
           }
           fireEndTestSuite();
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to