antoine     2003/06/20 09:29:54

  Modified:    src/main/org/apache/tools/ant/taskdefs/optional/junit
                        JUnitTask.java
               docs/manual/OptionalTasks junit.html
               .        WHATSNEW
  Log:
  Allow JUnit to reuse the same classloader when fork is set to false
  Submitted by: michael_beauregard at transcanada dot com
  
  Revision  Changes    Path
  1.68      +40 -32    
ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
  
  Index: JUnitTask.java
  ===================================================================
  RCS file: 
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java,v
  retrieving revision 1.67
  retrieving revision 1.68
  diff -u -r1.67 -r1.68
  --- JUnitTask.java    28 May 2003 13:12:03 -0000      1.67
  +++ JUnitTask.java    20 Jun 2003 16:29:52 -0000      1.68
  @@ -166,6 +166,7 @@
   
       private Integer timeout = null;
       private boolean summary = false;
  +    private boolean reloading = true;
       private String summaryValue = "";
       private JUnitTestRunner runner = null;
   
  @@ -177,6 +178,15 @@
   
       private boolean showOutput = false;
       private File tmpDir;
  +    private AntClassLoader classLoader = null;
  +    /**
  +    * If true, force ant to re-classload all classes for each JUnit TestCase
  +    *
  +    * @param value force class reloading for each test case
  +    */
  +    public void setReloading(boolean value) {
  +        reloading = value;
  +    }
   
       /**
        * If true, smartly filter the stack frames of
  @@ -846,17 +856,17 @@
           if (sysProperties != null) {
               sysProperties.setSystem();
           }
  -        AntClassLoader cl = null;
  +
           try {
               log("Using System properties " + System.getProperties(),
                   Project.MSG_VERBOSE);
  -            cl = createClassLoader();
  -            if (cl != null) {
  -                cl.setThreadContextLoader();
  +            createClassLoader();
  +            if (classLoader != null) {
  +                classLoader.setThreadContextLoader();
               }
               runner = new JUnitTestRunner(test, test.getHaltonerror(),
                                            test.getFiltertrace(),
  -                                         test.getHaltonfailure(), cl);
  +                                         test.getHaltonfailure(), 
classLoader);
               if (summary) {
                   log("Running " + test.getName(), Project.MSG_INFO);
   
  @@ -878,7 +888,7 @@
                       } else {
                           fe.setOutput(getDefaultOutput());
                       }
  -                    runner.addFormatter(fe.createFormatter(cl));
  +                    runner.addFormatter(fe.createFormatter(classLoader));
                   }
               }
   
  @@ -888,8 +898,8 @@
               if (sysProperties != null) {
                   sysProperties.restoreSystem();
               }
  -            if (cl != null) {
  -                cl.resetThreadContextLoader();
  +            if (classLoader != null) {
  +                classLoader.resetThreadContextLoader();
               }
           }
       }
  @@ -1012,11 +1022,11 @@
        */
   
       private void logTimeout(FormatterElement[] feArray, JUnitTest test) {
  -        AntClassLoader cl = createClassLoader();
  +        createClassLoader();
           for (int i = 0; i < feArray.length; i++) {
               FormatterElement fe = feArray[i];
               File outFile = getOutput(fe, test);
  -            JUnitResultFormatter formatter = fe.createFormatter(cl);
  +            JUnitResultFormatter formatter = fe.createFormatter(classLoader);
               if (outFile != null && formatter != null) {
                   try {
                       OutputStream out = new FileOutputStream(outFile);
  @@ -1044,31 +1054,29 @@
        * Creates and configures an AntClassLoader instance from the
        * nested classpath element.
        *
  -     * @return null if there is no user-specified classpath.
  -     *
        * @since Ant 1.6
        */
  -    private AntClassLoader createClassLoader() {
  -        AntClassLoader cl = null;
  +    private void createClassLoader() {
           Path userClasspath = commandline.getClasspath();
           if (userClasspath != null) {
  -            Path classpath = (Path) userClasspath.clone();
  -            if (includeAntRuntime) {
  -                log("Implicitly adding " + antRuntimeClasses 
  -                    + " to CLASSPATH", Project.MSG_VERBOSE);
  -                classpath.append(antRuntimeClasses);
  -            }
  -
  -            cl = getProject().createClassLoader(classpath);
  -            cl.setParentFirst(false);
  -            cl.addJavaLibraries();
  -            log("Using CLASSPATH " + cl.getClasspath(), Project.MSG_VERBOSE);
  -
  -            // make sure the test will be accepted as a TestCase
  -            cl.addSystemPackageRoot("junit");
  -            // will cause trouble in JDK 1.1 if omitted
  -            cl.addSystemPackageRoot("org.apache.tools.ant");
  +            if (reloading || classLoader == null) {
  +                Path classpath = (Path) userClasspath.clone();
  +                if (includeAntRuntime) {
  +                    log("Implicitly adding " + antRuntimeClasses
  +                        + " to CLASSPATH", Project.MSG_VERBOSE);
  +                    classpath.append(antRuntimeClasses);
  +                }
  +                classLoader = getProject().createClassLoader(classpath);
  +                log("Using CLASSPATH " + classLoader.getClasspath(),
  +                Project.MSG_VERBOSE);
  +                classLoader.setParentFirst(false);
  +                classLoader.addJavaLibraries();
  +                log("Using CLASSPATH " + classLoader.getClasspath(), 
Project.MSG_VERBOSE);
  +                // make sure the test will be accepted as a TestCase
  +                classLoader.addSystemPackageRoot("junit");
  +                // will cause trouble in JDK 1.1 if omitted
  +                classLoader.addSystemPackageRoot("org.apache.tools.ant");
  +            }
           }
  -        return cl;
       }
   }
  
  
  
  1.28      +7 -0      ant/docs/manual/OptionalTasks/junit.html
  
  Index: junit.html
  ===================================================================
  RCS file: /home/cvs/ant/docs/manual/OptionalTasks/junit.html,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- junit.html        28 May 2003 13:12:03 -0000      1.27
  +++ junit.html        20 Jun 2003 16:29:53 -0000      1.28
  @@ -149,6 +149,13 @@
       <td align="center" valign="top">No; default is the project's base
         directory.</td>
     </tr>
  +  <tr>
  +    <td valign="top">reloading</td>
  +    <td valign="top">Whether or not a new classloader should be instantiated 
for each test case.<br/>
  +    Ignore if <code>fork</code> is set to true.
  +      <em>Since Ant 1.6</em>.</td>
  +    <td align="center" valign="top">No; default is <code>true</code>.</td>
  +  </tr>
   </table>
   
   <p>By using the <code>errorproperty</code> and <code>failureproperty</code>
  
  
  
  1.440     +3 -0      ant/WHATSNEW
  
  Index: WHATSNEW
  ===================================================================
  RCS file: /home/cvs/ant/WHATSNEW,v
  retrieving revision 1.439
  retrieving revision 1.440
  diff -u -r1.439 -r1.440
  --- WHATSNEW  20 Jun 2003 11:09:35 -0000      1.439
  +++ WHATSNEW  20 Jun 2003 16:29:53 -0000      1.440
  @@ -297,6 +297,9 @@
   * The xml formatter for JUnit will now honor test case names set with
     setName.  Bugzilla Report 17040.
   
  +* JUnit now has an attribute reloading, which, when set to false,
  +  makes the task reuse the same class loader for a series of tests.
  +
   * <concat> now supports filtering and can check timestamps before
     overriding a file.  Bugzilla Report 18166.
   
  
  
  

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

Reply via email to