Permalink
Browse files

[SUREFIRE-876] avoid using Description.getTestClass()

Description.getTestClass() uses Class.forName() which
breaks classloader isolation [1,2].
Refactor JUnitCoreRunListener where we only
need the class name anyway to avoid calling
Description.getTestClass().

[1] https://github.com/KentBeck/junit/issues/364
[2] https://bugs.eclipse.org/bugs/show_bug.cgi?id=318299#c8
  • Loading branch information...
1 parent db069d5 commit 5a4bbd9a99a654c5b29a52ae26b390de2226ea4b @jsievers committed Jun 15, 2012
View
10 ...efire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreRunListener.java
@@ -66,15 +66,15 @@ private void fillTestCountMap( Description description )
final ArrayList<Description> children = description.getChildren();
TestSet testSet = new TestSet( description );
- Class<?> itemTestClass = null;
+ String itemTestClassName = null;
for ( Description item : children )
{
if ( item.isTest() && item.getMethodName() != null )
{
testSet.incrementTestMethodCount();
- if ( itemTestClass == null )
+ if ( itemTestClassName == null )
{
- itemTestClass = item.getTestClass();
+ itemTestClassName = item.getClassName();
}
}
else if ( item.getChildren().size() > 0 )
@@ -86,9 +86,9 @@ else if ( item.getChildren().size() > 0 )
classMethodCounts.put( item.getClassName(), testSet );
}
}
- if ( itemTestClass != null )
+ if ( itemTestClassName != null )
{
- classMethodCounts.put( itemTestClass.getName(), testSet );
+ classMethodCounts.put( itemTestClassName, testSet );
}
}
View
22 ...e-junit47/src/test/java/org/apache/maven/surefire/junitcore/JUnitCoreRunListenerTest.java
@@ -20,12 +20,15 @@
*/
import java.util.HashMap;
+import java.util.Map;
+
import org.apache.maven.surefire.junit4.MockReporter;
import junit.framework.TestCase;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.Computer;
+import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.RunListener;
@@ -60,6 +63,25 @@ public void testFailedAssumption()
assertEquals( 1, result.getRunCount() );
}
+ public void testTestClassNotLoadableFromJUnitClassLoader()
+ throws Exception
+ {
+ // can't use Description.createTestDescription() methods as these require a loaded Class
+ Description testDescription = Description.createSuiteDescription( "testMethod(cannot.be.loaded.by.junit.Test)" );
+ assertEquals( "testMethod", testDescription.getMethodName() );
+ assertEquals( "cannot.be.loaded.by.junit.Test", testDescription.getClassName() );
+ // assert that the test class is not visible by the JUnit classloader
+ assertNull( testDescription.getTestClass() );
+ Description suiteDescription = Description.createSuiteDescription( "testSuite" );
+ suiteDescription.addChild( testDescription );
+ Map<String, TestSet> classMethodCounts = new HashMap<String, TestSet>();
+ JUnitCoreRunListener listener = new JUnitCoreRunListener( new MockReporter(), classMethodCounts );
+ listener.testRunStarted( suiteDescription );
+ assertEquals( 1, classMethodCounts.size() );
+ TestSet testSet = classMethodCounts.get( "cannot.be.loaded.by.junit.Test" );
+ assertNotNull( testSet );
+ }
+
public static class STest1
{
@Test

0 comments on commit 5a4bbd9

Please sign in to comment.