Author: krosenvold
Date: Sun Jan 2 21:20:02 2011
New Revision: 1054473
URL: http://svn.apache.org/viewvc?rev=1054473&view=rev
Log:
[SUREFIRE-424] Added support for suite-only tests to Junit 3/4
Modified:
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/ReflectionUtils.java
maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3TestChecker.java
maven/surefire/trunk/surefire-providers/surefire-junit4/src/main/java/org/apache/maven/surefire/junit4/JUnit4TestChecker.java
maven/surefire/trunk/surefire-providers/surefire-junit4/src/test/java/org/apache/maven/surefire/junit4/JUnit4TestCheckerTest.java
Modified:
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/ReflectionUtils.java
URL:
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/ReflectionUtils.java?rev=1054473&r1=1054472&r2=1054473&view=diff
==============================================================================
---
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/ReflectionUtils.java
(original)
+++
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/ReflectionUtils.java
Sun Jan 2 21:20:02 2011
@@ -35,9 +35,14 @@ public class ReflectionUtils
public static Method getMethod( Object instance, String methodName,
Class[] parameters )
{
+ return getMethod( instance.getClass(), methodName, parameters );
+ }
+
+ public static Method getMethod( Class clazz, String methodName, Class[]
parameters )
+ {
try
{
- return instance.getClass().getMethod( methodName, parameters );
+ return clazz.getMethod( methodName, parameters );
}
catch ( NoSuchMethodException e )
{
@@ -45,6 +50,19 @@ public class ReflectionUtils
}
}
+ public static Method tryGetMethod( Class clazz, String methodName, Class[]
parameters )
+ {
+ try
+ {
+ return clazz.getMethod( methodName, parameters );
+ }
+ catch ( NoSuchMethodException e )
+ {
+ return null;
+ }
+ }
+
+
public static Object invokeGetter( Object instance, String methodName )
{
final Method method = getMethod( instance, methodName, NO_ARGS );
@@ -88,8 +106,7 @@ public class ReflectionUtils
try
{
-
- Class clazz = loadClass( classLoader, classname );
+ Class clazz = loadClass( classLoader, classname );
return clazz.newInstance();
}
catch ( InstantiationException e )
@@ -180,7 +197,6 @@ public class ReflectionUtils
}
}
-
Constructor constructor = getConstructor( clazz, paramTypes );
object = newInstance( constructor, params );
Modified:
maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3TestChecker.java
URL:
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3TestChecker.java?rev=1054473&r1=1054472&r2=1054473&view=diff
==============================================================================
---
maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3TestChecker.java
(original)
+++
maven/surefire/trunk/surefire-providers/surefire-junit/src/main/java/org/apache/maven/surefire/junit/JUnit3TestChecker.java
Sun Jan 2 21:20:02 2011
@@ -23,7 +23,15 @@ import org.apache.maven.surefire.NonAbst
import org.apache.maven.surefire.util.ReflectionUtils;
import org.apache.maven.surefire.util.ScannerFilter;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
/**
+ * Missing tests ? This class is basically a subset of the JUnit4TestChecker,
which is tested
+ * to boredom and back. Unfortunately we don't have any common module between
these providers,
+ * so this stuff is duplicated. We should probably make some modules and just
shade the content
+ * into the providers.
+ *
* @author Kristian Rosenvold
*/
public class JUnit3TestChecker
@@ -31,6 +39,9 @@ public class JUnit3TestChecker
{
private final Class junitClass;
+ private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
+
+
private final NonAbstractClassFilter nonAbstractClassFilter = new
NonAbstractClassFilter();
@@ -41,7 +52,7 @@ public class JUnit3TestChecker
public boolean accept( Class testClass )
{
- return nonAbstractClassFilter.accept( testClass ) &&
isValidJUnit3Test( testClass );
+ return nonAbstractClassFilter.accept( testClass ) &&
isValidJUnit3Test( testClass );
}
public boolean isValidJUnit3Test( Class testClass )
@@ -49,9 +60,24 @@ public class JUnit3TestChecker
return isJunit3Test( testClass ) || isPojoTest( testClass );
}
+ public boolean isSuiteOnly( Class testClass )
+ {
+ final Method suite = ReflectionUtils.tryGetMethod( testClass, "suite",
EMPTY_CLASS_ARRAY );
+ if ( suite != null )
+ {
+
+ final int modifiers = suite.getModifiers();
+ if ( Modifier.isPublic( modifiers ) && Modifier.isStatic(
modifiers ) )
+ {
+ return junit.framework.Test.class.isAssignableFrom(
suite.getReturnType() );
+ }
+ }
+ return false;
+ }
+
public boolean isJunit3Test( Class testClass )
{
- return junitClass != null && junitClass.isAssignableFrom( testClass );
+ return junitClass != null && ( junitClass.isAssignableFrom( testClass
) || isSuiteOnly( testClass ) );
}
private boolean isPojoTest( Class testClass )
Modified:
maven/surefire/trunk/surefire-providers/surefire-junit4/src/main/java/org/apache/maven/surefire/junit4/JUnit4TestChecker.java
URL:
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-junit4/src/main/java/org/apache/maven/surefire/junit4/JUnit4TestChecker.java?rev=1054473&r1=1054472&r2=1054473&view=diff
==============================================================================
---
maven/surefire/trunk/surefire-providers/surefire-junit4/src/main/java/org/apache/maven/surefire/junit4/JUnit4TestChecker.java
(original)
+++
maven/surefire/trunk/surefire-providers/surefire-junit4/src/main/java/org/apache/maven/surefire/junit4/JUnit4TestChecker.java
Sun Jan 2 21:20:02 2011
@@ -25,6 +25,7 @@ import org.apache.maven.surefire.util.Sc
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
/**
* @author Kristian Rosenvold
@@ -38,6 +39,9 @@ public class JUnit4TestChecker
private final Class runWith;
+ private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
+
+
public JUnit4TestChecker( ClassLoader testClassLoader )
{
this.junitClass = getJUnitClass( testClassLoader,
junit.framework.Test.class.getName() );
@@ -57,7 +61,7 @@ public class JUnit4TestChecker
{
return false;
}
- if ( junitClass != null && junitClass.isAssignableFrom( testClass ) )
+ if ( junitClass != null && ( junitClass.isAssignableFrom( testClass )
|| isSuiteOnly( testClass ) ) )
{
return true;
}
@@ -95,6 +99,21 @@ public class JUnit4TestChecker
return false;
}
+ public boolean isSuiteOnly( Class testClass )
+ {
+ final Method suite = ReflectionUtils.tryGetMethod( testClass, "suite",
EMPTY_CLASS_ARRAY );
+ if ( suite != null )
+ {
+
+ final int modifiers = suite.getModifiers();
+ if ( Modifier.isPublic( modifiers ) && Modifier.isStatic(
modifiers ) )
+ {
+ return junit.framework.Test.class.isAssignableFrom(
suite.getReturnType() );
+ }
+ }
+ return false;
+ }
+
private Class getJUnitClass( ClassLoader classLoader, String className )
{
return ReflectionUtils.tryLoadClass( classLoader, className );
Modified:
maven/surefire/trunk/surefire-providers/surefire-junit4/src/test/java/org/apache/maven/surefire/junit4/JUnit4TestCheckerTest.java
URL:
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-providers/surefire-junit4/src/test/java/org/apache/maven/surefire/junit4/JUnit4TestCheckerTest.java?rev=1054473&r1=1054472&r2=1054473&view=diff
==============================================================================
---
maven/surefire/trunk/surefire-providers/surefire-junit4/src/test/java/org/apache/maven/surefire/junit4/JUnit4TestCheckerTest.java
(original)
+++
maven/surefire/trunk/surefire-providers/surefire-junit4/src/test/java/org/apache/maven/surefire/junit4/JUnit4TestCheckerTest.java
Sun Jan 2 21:20:02 2011
@@ -19,6 +19,7 @@ package org.apache.maven.surefire.junit4
*/
import junit.framework.TestCase;
+import junit.framework.TestResult;
import org.apache.maven.surefire.testset.TestSetFailedException;
import org.junit.Test;
import org.junit.internal.runners.InitializationError;
@@ -65,6 +66,7 @@ public class JUnit4TestCheckerTest
{
assertTrue( jUnit4TestChecker.isValidJUnit4Test( SuiteValid1.class ) );
}
+
@Test
public void validCustomSuite()
throws TestSetFailedException
@@ -87,10 +89,23 @@ public class JUnit4TestCheckerTest
}
@Test
- public void dontAcceptAbstractClasses(){
+ public void dontAcceptAbstractClasses()
+ {
assertFalse( jUnit4TestChecker.isValidJUnit4Test(
BaseClassWithTest.class ) );
}
+ @Test
+ public void suiteOnlyTest()
+ {
+ assertTrue( jUnit4TestChecker.isValidJUnit4Test( SuiteOnlyTest.class )
);
+ }
+
+ @Test
+ public void customSuiteOnlyTest()
+ {
+ assertTrue( jUnit4TestChecker.isValidJUnit4Test(
CustomSuiteOnlyTest.class ) );
+ }
+
public static class AlsoValid
extends TestCase
@@ -101,6 +116,36 @@ public class JUnit4TestCheckerTest
}
}
+ public static class SuiteOnlyTest
+ {
+ public static junit.framework.Test suite()
+ {
+ return null;
+ }
+ }
+
+ public static class CustomSuiteOnlyTest
+ {
+ public static MySuite2 suite()
+ {
+ return null;
+ }
+ }
+
+ public static class MySuite2
+ implements junit.framework.Test
+ {
+ public int countTestCases()
+ {
+ return 0;
+ }
+
+ public void run( TestResult testResult )
+ {
+ }
+ }
+
+
public static class NotValidTest
{
public void testSomething()
@@ -115,11 +160,13 @@ public class JUnit4TestCheckerTest
{
}
}
- public static class SubClassWithoutOwnTestMethods extends BaseClassWithTest
+
+ public static class SubClassWithoutOwnTestMethods
+ extends BaseClassWithTest
{
}
- @RunWith(Suite.class)
+ @RunWith( Suite.class )
public static class SuiteValid1
{
public void testSomething()
@@ -129,7 +176,8 @@ public class JUnit4TestCheckerTest
}
class CustomRunner
- extends Runner {
+ extends Runner
+ {
@Override
public Description getDescription()
{
@@ -142,7 +190,7 @@ public class JUnit4TestCheckerTest
}
}
- @RunWith(CustomRunner.class)
+ @RunWith( CustomRunner.class )
public static class SuiteValidCustomRunner
{
public void testSomething()
@@ -152,7 +200,7 @@ public class JUnit4TestCheckerTest
}
- @RunWith(MySuite.class)
+ @RunWith( MySuite.class )
public static class SuiteValid2
{
public void testSomething()
@@ -161,7 +209,9 @@ public class JUnit4TestCheckerTest
}
}
- class MySuite extends Suite {
+ class MySuite
+ extends Suite
+ {
MySuite( Class<?> klass )
throws InitializationError
{