Author: brett
Date: Fri Apr 28 20:01:14 2006
New Revision: 398071

URL: http://svn.apache.org/viewcvs?rev=398071&view=rev
Log:
[SUREFIRE-40] fix memory leak in JUnit runner by not retaining the test class 
instance

Modified:
    
maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java

Modified: 
maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java
URL: 
http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java?rev=398071&r1=398070&r2=398071&view=diff
==============================================================================
--- 
maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java
 (original)
+++ 
maven/surefire/branches/surefire-testng/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnitTestSet.java
 Fri Apr 28 20:01:14 2006
@@ -50,8 +50,6 @@
 
     private static final String TEST_SUITE = "junit.framework.TestSuite";
 
-    private Object testObject;
-
     private Class[] interfacesImplementedByDynamicProxy;
 
     private Class testResultClass;
@@ -71,19 +69,18 @@
     {
         super( testClass );
 
-        processTestClass( testClass.getClassLoader() );
+        processTestClass();
     }
 
-    private void processTestClass( ClassLoader loader )
+    private void processTestClass()
         throws TestSetFailedException
     {
         try
         {
-            testResultClass = loader.loadClass( TEST_RESULT );
-
-            Class testCaseClass = loader.loadClass( TEST_CASE );
+            Class testClass = getTestClass();
+            ClassLoader loader = testClass.getClassLoader();
 
-            Class testSuiteClass = loader.loadClass( TEST_SUITE );
+            testResultClass = loader.loadClass( TEST_RESULT );
 
             Class testListenerInterface = loader.loadClass( TEST_LISTENER );
 
@@ -100,35 +97,6 @@
             // o look for test classes that only implement the Test interface
             // 
----------------------------------------------------------------------
 
-            Object testObject = createInstanceFromSuiteMethod();
-
-            Class testClass = getTestClass();
-            if ( testObject == null && testCaseClass.isAssignableFrom( 
testClass ) )
-            {
-                Class[] constructorParamTypes = {Class.class};
-
-                Constructor constructor = testSuiteClass.getConstructor( 
constructorParamTypes );
-
-                Object[] constructorParams = {testClass};
-
-                testObject = constructor.newInstance( constructorParams );
-            }
-
-            if ( testObject == null )
-            {
-                Constructor testConstructor = getTestConstructor( testClass );
-
-                if ( testConstructor.getParameterTypes().length == 0 )
-                {
-                    testObject = testConstructor.newInstance( 
EMPTY_OBJECT_ARRAY );
-                }
-                else
-                {
-                    testObject = testConstructor.newInstance( new 
Object[]{testClass.getName()} );
-                }
-            }
-            this.testObject = testObject;
-
             interfacesImplementedByDynamicProxy = new Class[1];
 
             interfacesImplementedByDynamicProxy[0] = testListenerInterface;
@@ -157,31 +125,53 @@
         {
             throw new TestSetFailedException( "JUnit classes not available", e 
);
         }
-        catch ( IllegalAccessException e )
-        {
-            throw new TestSetFailedException( "Unknown access exception 
creating JUnit classes", e );
-        }
-        catch ( InvocationTargetException e )
+        catch ( NoSuchMethodException e )
         {
-            throw new TestSetFailedException( "Unknown invocation exception 
creating JUnit classes", e );
+            throw new TestSetFailedException( "Class is not a JUnit TestCase", 
e );
         }
-        catch ( InstantiationException e )
+    }
+
+    private static Object constructTestObject( Class testClass )
+        throws IllegalAccessException, InvocationTargetException, 
NoSuchMethodException, InstantiationException,
+        ClassNotFoundException
+    {
+        Object testObject = createInstanceFromSuiteMethod( testClass );
+
+        if ( testObject == null && testClass.getClassLoader().loadClass( 
TEST_CASE ).isAssignableFrom( testClass ) )
         {
-            throw new TestSetFailedException( "Unknown instantiation exception 
creating JUnit classes", e );
+            Class[] constructorParamTypes = {Class.class};
+
+            Constructor constructor =
+                testClass.getClassLoader().loadClass( TEST_SUITE 
).getConstructor( constructorParamTypes );
+
+            Object[] constructorParams = {testClass};
+
+            testObject = constructor.newInstance( constructorParams );
         }
-        catch ( NoSuchMethodException e )
+
+        if ( testObject == null )
         {
-            throw new TestSetFailedException( "Class is not a JUnit TestCase", 
e );
+            Constructor testConstructor = getTestConstructor( testClass );
+
+            if ( testConstructor.getParameterTypes().length == 0 )
+            {
+                testObject = testConstructor.newInstance( EMPTY_OBJECT_ARRAY );
+            }
+            else
+            {
+                testObject = testConstructor.newInstance( new 
Object[]{testClass.getName()} );
+            }
         }
+        return testObject;
     }
 
-    private Object createInstanceFromSuiteMethod()
+    private static Object createInstanceFromSuiteMethod( Class testClass )
         throws IllegalAccessException, InvocationTargetException
     {
         Object testObject = null;
         try
         {
-            Method suiteMethod = getTestClass().getMethod( "suite", 
EMPTY_CLASS_ARRAY );
+            Method suiteMethod = testClass.getMethod( "suite", 
EMPTY_CLASS_ARRAY );
 
             if ( Modifier.isPublic( suiteMethod.getModifiers() ) && 
Modifier.isStatic( suiteMethod.getModifiers() ) )
             {
@@ -198,8 +188,12 @@
     public void execute( ReporterManager reportManager, ClassLoader loader )
         throws TestSetFailedException
     {
+        Class testClass = getTestClass();
+
         try
         {
+            Object testObject = constructTestObject( testClass );
+
             Object instanceOfTestResult = testResultClass.newInstance();
 
             TestListenerInvocationHandler invocationHandler =
@@ -218,46 +212,69 @@
         }
         catch ( IllegalArgumentException e )
         {
-            throw new TestSetFailedException( testObject.getClass().getName(), 
e );
+            throw new TestSetFailedException( testClass.getName(), e );
         }
         catch ( InstantiationException e )
         {
-            throw new TestSetFailedException( testObject.getClass().getName(), 
e );
+            throw new TestSetFailedException( testClass.getName(), e );
         }
         catch ( IllegalAccessException e )
         {
-            throw new TestSetFailedException( testObject.getClass().getName(), 
e );
+            throw new TestSetFailedException( testClass.getName(), e );
         }
         catch ( InvocationTargetException e )
         {
-            throw new TestSetFailedException( testObject.getClass().getName(), 
e.getTargetException() );
+            throw new TestSetFailedException( testClass.getName(), 
e.getTargetException() );
+        }
+        catch ( ClassNotFoundException e )
+        {
+            throw new TestSetFailedException( "JUnit classes not available", e 
);
+        }
+        catch ( NoSuchMethodException e )
+        {
+            throw new TestSetFailedException( "Class is not a JUnit TestCase", 
e );
         }
     }
 
     public int getTestCount()
         throws TestSetFailedException
     {
+        Class testClass = getTestClass();
         try
         {
+            Object testObject = constructTestObject( testClass );
+
             Integer integer = (Integer) countTestCasesMethod.invoke( 
testObject, EMPTY_CLASS_ARRAY );
 
             return integer.intValue();
         }
         catch ( IllegalAccessException e )
         {
-            throw new TestSetFailedException( testObject.getClass().getName(), 
e );
+            throw new TestSetFailedException( testClass.getName(), e );
         }
         catch ( IllegalArgumentException e )
         {
-            throw new TestSetFailedException( testObject.getClass().getName(), 
e );
+            throw new TestSetFailedException( testClass.getName(), e );
         }
         catch ( InvocationTargetException e )
         {
-            throw new TestSetFailedException( testObject.getClass().getName(), 
e.getTargetException() );
+            throw new TestSetFailedException( testClass.getName(), 
e.getTargetException() );
+        }
+        catch ( InstantiationException e )
+        {
+            throw new TestSetFailedException( testClass.getName(), e );
+        }
+        catch ( ClassNotFoundException e )
+        {
+            throw new TestSetFailedException( "JUnit classes not available", e 
);
+        }
+        catch ( NoSuchMethodException e )
+        {
+            throw new TestSetFailedException( "Class is not a JUnit TestCase", 
e );
         }
     }
 
-    private Constructor getTestConstructor( Class testClass )
+    private static Constructor getTestConstructor( Class testClass )
         throws NoSuchMethodException
     {
         Constructor constructor;


Reply via email to