Java Mailing List Archive

http://www.ant-tasks.com/

Home » Ant Developers List »

Re: The Cactus/JUnit-Task problem

Stefan Bodewig

2008-02-18

Replies: Find Java Web Hosting

Author LoginPost Reply
On Fri, 15 Feb 2008, Petar Tahchiev <paranoiabla@(protected):

> in the execute(JUnitTest arg) method, too. Well, as Stefan
> suggested, we can put the
> if (delegate == null) {
>   setupJUnitDelegate();
> }
>
> in executeInVM() method, that we call when we don't fork the JUnit
> execution. Well, as you see a little bit
> down in the code there is a piece that states:
>         if (splitJunit) {
>           classLoader = (AntClassLoader) delegate.getClass
> ().getClassLoader();
>         } else {
>           createClassLoader();
>         }
>
> since I have no control over splitJUnit it is false, and then we
> invoke the createClassLoader(); method, which, invokes
> deleteClassLoader() and it, again, nullify-s the delegate object.

Ouch, my fault.

Actually I added the "delegate = null" statement to
deleteClassLoader after running my tests and didn't rerun the
tests after the change. I know I shouldn't do that, even if a
change looks simple.

Appended is a revised patch that doesn't exhibit the same
problem.

Stefan

Index: src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
===================================================================
--- src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java  (revision 628687)
+++ src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java  (working copy)
@@(protected) @@

  private boolean splitJunit = false;
  private JUnitTaskMirror delegate;
+   private ClassLoader mirrorLoader;

  /** A boolean on whether to get the forked path for ant classes */
  private boolean forkedPathChecked = false;
@@(protected) @@
  }

  /**
-   * Runs the testcase.
-   *
-   * @throws BuildException in case of test failures or errors
-   * @since Ant 1.2
+   * Sets up the delegate that will actually run the tests.
   */
-   public void execute() throws BuildException {
+   protected void setupJUnitDelegate() {
     ClassLoader myLoader = JUnitTask.class.getClassLoader();
-     ClassLoader mirrorLoader;
     if (splitJunit) {
        Path path = new Path(getProject());
        path.add(antRuntimeClasses);
@@(protected) @@
        mirrorLoader = myLoader;
     }
     delegate = createMirror(this, mirrorLoader);
+   }

+   /**
+   * Runs the testcase.
+   *
+   * @throws BuildException in case of test failures or errors
+   * @since Ant 1.2
+   */
+   public void execute() throws BuildException {
+     setupJUnitDelegate();
+
     List testLists = new ArrayList();

     boolean forkPerTest = forkMode.getValue().equals(ForkMode.PER_TEST);
@@(protected) @@
          }
        }
     } finally {
-        deleteClassLoader();
-        if (mirrorLoader instanceof SplitLoader) {
-           ((SplitLoader) mirrorLoader).cleanup();
-        }
-        delegate = null;
+        cleanup();
     }
  }

@@(protected) @@
   * @return the results
   */
  private TestResultHolder executeInVM(JUnitTest arg) throws BuildException {
+     if (delegate == null) {
+        setupJUnitDelegate();
+     }
+
     JUnitTest test = (JUnitTest) arg.clone();
     test.setProperties(getProject().getProperties());
     if (dir != null) {
@@(protected) @@
   */
  private void logVmExit(FormatterElement[] feArray, JUnitTest test,
                  String message, String testCase) {
+     if (delegate == null) {
+        setupJUnitDelegate();
+     }
+
     try {
        log("Using System properties " + System.getProperties(),
          Project.MSG_VERBOSE);
@@(protected) @@
  }

  /**
+   * Removes resources.
+   */
+   protected void cleanup() {
+     deleteClassLoader();
+     delegate = null;
+   }
+
+   /**
   * Removes a classloader if needed.
   * @since Ant 1.7
   */
@@(protected) @@
        classLoader.cleanup();
        classLoader = null;
     }
+     if (mirrorLoader instanceof SplitLoader) {
+        ((SplitLoader) mirrorLoader).cleanup();
+     }
+     mirrorLoader = null;
  }

  /**

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@(protected)
For additional commands, e-mail: dev-help@(protected)

©2008 ant-tasks.com - Jax Systems, LLC, U.S.A.