From: David S. <ds...@us...> - 2007-03-20 14:43:57
|
Update of /cvsroot/junit/junit/src/org/junit/runners In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv30152/src/org/junit/runners Modified Files: package-info.java Parameterized.java Suite.java AllTests.java Log Message: Runner rearchitecting to begin 4.4 development Index: package-info.java =================================================================== RCS file: /cvsroot/junit/junit/src/org/junit/runners/package-info.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- package-info.java 21 Nov 2006 18:53:41 -0000 1.1 +++ package-info.java 20 Mar 2007 14:43:50 -0000 1.2 @@ -3,6 +3,6 @@ * * @since 4.0 * @see org.junit.runner.Runner - * @see org.junit.internal.runners.TestClassRunner + * @see org.junit.internal.runners.TryToDoEverythingRunner */ package org.junit.runners; \ No newline at end of file Index: Parameterized.java =================================================================== RCS file: /cvsroot/junit/junit/src/org/junit/runners/Parameterized.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- Parameterized.java 27 Dec 2006 17:45:54 -0000 1.2 +++ Parameterized.java 20 Mar 2007 14:43:50 -0000 1.3 @@ -1,8 +1,5 @@ package org.junit.runners; -import static org.junit.Assert.assertEquals; - -import java.lang.annotation.Annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -15,10 +12,14 @@ import java.util.Collection; import java.util.List; +import org.junit.Assert; import org.junit.internal.runners.CompositeRunner; +import org.junit.internal.runners.InitializationError; import org.junit.internal.runners.MethodValidator; -import org.junit.internal.runners.TestClassMethodsRunner; -import org.junit.internal.runners.TestClassRunner; +import org.junit.internal.runners.TestClass; +import org.junit.internal.runners.JUnit4ClassRunner; +import org.junit.internal.runners.ClassRoadie; +import org.junit.runner.notification.RunNotifier; /** <p>The custom runner <code>Parameterized</code> implements parameterized * tests. When running a parameterized test class, instances are created for the @@ -51,30 +52,16 @@ * <p>Each instance of <code>FibonacciTest</code> will be constructed using the two-argument * constructor and the data values in the <code>@Parameters</code> method.</p> */ -public class Parameterized extends TestClassRunner { - @Retention(RetentionPolicy.RUNTIME) - @Target(ElementType.METHOD) - public static @interface Parameters { - } - - public static Collection<Object[]> eachOne(Object... params) { - List<Object[]> results= new ArrayList<Object[]>(); - for (Object param : params) - results.add(new Object[] { param }); - return results; - } - - // TODO: single-class this extension - - private static class TestClassRunnerForParameters extends TestClassMethodsRunner { +public class Parameterized extends CompositeRunner { + static class TestClassRunnerForParameters extends JUnit4ClassRunner { private final Object[] fParameters; private final int fParameterSetNumber; private final Constructor<?> fConstructor; - private TestClassRunnerForParameters(Class<?> klass, Object[] parameters, int i) { - super(klass); + TestClassRunnerForParameters(TestClass testClass, Object[] parameters, int i) throws InitializationError { + super(testClass.getJavaClass()); //todo fParameters= parameters; fParameterSetNumber= i; fConstructor= getOnlyConstructor(); @@ -96,55 +83,78 @@ } private Constructor<?> getOnlyConstructor() { - Constructor<?>[] constructors= getTestClass().getConstructors(); - assertEquals(1, constructors.length); + Constructor<?>[] constructors= getTestClass().getJavaClass().getConstructors(); + Assert.assertEquals(1, constructors.length); return constructors[0]; } + + @Override + protected void validate() throws InitializationError { + // do nothing: validated before. + } + + @Override + public void run(RunNotifier notifier) { + runMethods(notifier); + } } - - // TODO: I think this now eagerly reads parameters, which was never the point. - - public static class RunAllParameterMethods extends CompositeRunner { - private final Class<?> fKlass; - public RunAllParameterMethods(Class<?> klass) throws Exception { - super(klass.getName()); - fKlass= klass; - int i= 0; - for (final Object each : getParametersList()) { - if (each instanceof Object[]) - super.add(new TestClassRunnerForParameters(klass, (Object[])each, i++)); - else - throw new Exception(String.format("%s.%s() must return a Collection of arrays.", fKlass.getName(), getParametersMethod().getName())); - } - } + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.METHOD) + public static @interface Parameters { + } + + private final TestClass fTestClass; - private Collection<?> getParametersList() throws IllegalAccessException, InvocationTargetException, Exception { - return (Collection<?>) getParametersMethod().invoke(null); - } + public Parameterized(Class<?> klass) throws Exception { + super(klass.getName()); + fTestClass= new TestClass(klass); - private Method getParametersMethod() throws Exception { - for (Method each : fKlass.getMethods()) { - if (Modifier.isStatic(each.getModifiers())) { - Annotation[] annotations= each.getAnnotations(); - for (Annotation annotation : annotations) { - if (annotation.annotationType() == Parameters.class) - return each; - } - } - } - throw new Exception("No public static parameters method on class " - + getName()); + MethodValidator methodValidator= new MethodValidator(fTestClass); + methodValidator.validateStaticMethods(); + methodValidator.validateInstanceMethods(); + methodValidator.assertValid(); + + int i= 0; + for (final Object each : getParametersList()) { + if (each instanceof Object[]) + add(new TestClassRunnerForParameters(fTestClass, (Object[])each, i++)); + else + throw new Exception(String.format("%s.%s() must return a Collection of arrays.", fTestClass.getName(), getParametersMethod().getName())); } } - public Parameterized(final Class<?> klass) throws Exception { - super(klass, new RunAllParameterMethods(klass)); + @Override + public void run(final RunNotifier notifier) { + new ClassRoadie(notifier, fTestClass, getDescription(), new Runnable() { + public void run() { + runChildren(notifier); + } + }).runProtected(); } - @Override - protected void validate(MethodValidator methodValidator) { - methodValidator.validateStaticMethods(); - methodValidator.validateInstanceMethods(); + private Collection<?> getParametersList() throws IllegalAccessException, InvocationTargetException, Exception { + return (Collection<?>) getParametersMethod().invoke(null); + } + + private Method getParametersMethod() throws Exception { + List<Method> methods= fTestClass.getAnnotatedMethods(Parameters.class); + for (Method each : methods) { + int modifiers= each.getModifiers(); + if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) + return each; + } + + throw new Exception("No public static parameters method on class " + getName()); + } + + public static Collection<Object[]> eachOne(Object... params) { + List<Object[]> results= new ArrayList<Object[]>(); + for (Object param : params) + results.add(new Object[] { param }); + return results; } } + +// TODO: single-class this extension + Index: Suite.java =================================================================== RCS file: /cvsroot/junit/junit/src/org/junit/runners/Suite.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- Suite.java 21 Feb 2007 20:26:29 -0000 1.3 +++ Suite.java 20 Mar 2007 14:43:50 -0000 1.4 @@ -7,10 +7,14 @@ import java.util.HashSet; import java.util.Set; +import org.junit.internal.runners.CompositeRunner; import org.junit.internal.runners.InitializationError; import org.junit.internal.runners.MethodValidator; -import org.junit.internal.runners.TestClassRunner; +import org.junit.internal.runners.TestClass; +import org.junit.internal.runners.ClassRoadie; import org.junit.runner.Request; +import org.junit.runner.Runner; +import org.junit.runner.notification.RunNotifier; /** * Using <code>Suite</code> as a runner allows you to manually @@ -19,7 +23,7 @@ * with <code>@RunWith(Suite.class)</code> and <code>@SuiteClasses(TestClass1.class, ...)</code>. * When you run this class, it will run all the tests in all the suite classes. */ -public class Suite extends TestClassRunner { +public class Suite extends CompositeRunner { /** * The <code>SuiteClasses</code> annotation specifies the classes to be run when a class * annotated with <code>@RunWith(Suite.class)</code> is run. @@ -40,15 +44,33 @@ // This won't work correctly in the face of concurrency. For that we need to // add parameters to getRunner(), which would be much more complicated. private static Set<Class<?>> parents = new HashSet<Class<?>>(); + private TestClass fTestClass; - private static Class<?> addParent(Class<?> parent) throws InitializationError { + protected Suite(Class<?> klass, Class<?>[] annotatedClasses) throws InitializationError { + // we need to add parent be + super(klass.getName()); + + addParent(klass); + for (Class<?> each : annotatedClasses) { + Runner childRunner= Request.aClass(each).getRunner(); + if (childRunner != null) + add(childRunner); + } + removeParent(klass); + + fTestClass= new TestClass(klass); + MethodValidator methodValidator= new MethodValidator(fTestClass); + methodValidator.validateStaticMethods(); + methodValidator.assertValid(); + } + + private Class<?> addParent(Class<?> parent) throws InitializationError { if (!parents.add(parent)) throw new InitializationError(String.format("class '%s' (possibly indirectly) contains itself as a SuiteClass", parent.getName())); return parent; } - protected Suite(Class<?> klass, Class<?>[] annotatedClasses) throws InitializationError { - super(addParent(klass), Request.classes(klass.getName(), annotatedClasses).getRunner()); + private void removeParent(Class<?> klass) { parents.remove(klass); } @@ -59,9 +81,17 @@ return annotation.value(); } - @Override protected void validate(MethodValidator methodValidator) { methodValidator.validateStaticMethods(); methodValidator.validateInstanceMethods(); } + + @Override + public void run(final RunNotifier notifier) { + new ClassRoadie(notifier, fTestClass, getDescription(), new Runnable() { + public void run() { + runChildren(notifier); + } + }).runProtected(); + } } Index: AllTests.java =================================================================== RCS file: /cvsroot/junit/junit/src/org/junit/runners/AllTests.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- AllTests.java 21 Feb 2007 15:19:39 -0000 1.3 +++ AllTests.java 20 Mar 2007 14:43:50 -0000 1.4 @@ -5,7 +5,7 @@ import java.lang.reflect.Modifier; import junit.framework.Test; -import org.junit.internal.runners.OldTestClassRunner; +import org.junit.internal.runners.JUnit38ClassRunner; /** Runner for use with JUnit 3.8.x-style AllTests classes * (those that only implement a static <code>suite()</code> @@ -19,7 +19,7 @@ * } * </pre> */ -public class AllTests extends OldTestClassRunner { +public class AllTests extends JUnit38ClassRunner { @SuppressWarnings("unchecked") public AllTests(Class<?> klass) throws Throwable { super(testFromSuiteMethod(klass)); |