From: David S. <ds...@us...> - 2006-10-18 18:22:21
|
Update of /cvsroot/junit/junit/org/junit/runners In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv9576/org/junit/runners Modified Files: Tag: saff_r41_runner_refactoring ParameterizedInterpreter.java Parameterized.java Enclosed.java Suite.java Log Message: Parameterized works entirely with JavaTestInterpreter now Index: ParameterizedInterpreter.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/runners/Attic/ParameterizedInterpreter.java,v retrieving revision 1.1.2.1 retrieving revision 1.1.2.2 diff -u -d -r1.1.2.1 -r1.1.2.2 --- ParameterizedInterpreter.java 18 Oct 2006 17:07:05 -0000 1.1.2.1 +++ ParameterizedInterpreter.java 18 Oct 2006 18:22:14 -0000 1.1.2.2 @@ -8,56 +8,115 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Collection; +import java.util.List; +import org.junit.internal.runners.CompositeRunner; +import org.junit.internal.runners.InitializationError; import org.junit.internal.runners.JavaClass; import org.junit.internal.runners.JavaMethod; import org.junit.internal.runners.JavaTestInterpreter; +import org.junit.internal.runners.MethodValidator; +import org.junit.internal.runners.TestClassMethodsRunner; +import org.junit.runner.Runner; +import org.junit.runners.Parameterized.Parameters; class ParameterizedInterpreter extends JavaTestInterpreter { - private final Object fEach; + private static class ParameterizedJavaClass extends JavaClass { + private final Object[] fParameters; - private final int fNumber; + private final int fNumber; - ParameterizedInterpreter(Object each, int number) { - fEach= each; - fNumber= number; + private ParameterizedJavaClass(Class<?> type, Object[] parameters, + int number) { + super(type); + fParameters= parameters; + fNumber= number; + } + + // TODO: too many exceptions + @Override + protected Object newInstance() throws InstantiationException, + IllegalAccessException, InvocationTargetException, + NoSuchMethodException { + return getOnlyConstructor().newInstance(fParameters); + } + + @Override + public String getName() { + return String.format("[%s]", fNumber); + } + + private Constructor getOnlyConstructor() { + Constructor[] constructors= getTestClass().getConstructors(); + assertEquals(1, constructors.length); + return constructors[0]; + } } @Override - public JavaClass interpretJavaClass(Class<?> superclass) { - return new JavaClass(superclass) { - // TODO: too many exceptions - @Override - protected Object newInstance() - throws InstantiationException, - IllegalAccessException, InvocationTargetException, - NoSuchMethodException { - return getOnlyConstructor().newInstance( - (Object[]) fEach); - } - + protected JavaMethod interpretJavaMethod(final JavaClass klass, + Method method) { + return new JavaMethod(klass, method) { @Override public String getName() { - return String.format("[%s]", fNumber); + return String.format("%s%s", super.getName(), klass.getName()); } + }; + } - @Override - protected JavaMethod makeJavaMethod(Method method) { - return new JavaMethod(this, method) { - @Override - public String getName() { - return String.format("%s[%s]", super.getName(), - fNumber); - } - }; - } + // TODO: I think this now eagerly reads parameters, which was never the + // point. - private Constructor getOnlyConstructor() { - Constructor[] constructors= getTestClass() - .getConstructors(); - assertEquals(1, constructors.length); - return constructors[0]; + // TODO: pull interpreter back in? + @Override + public Runner runnerFor(Class klass) throws InitializationError { + CompositeRunner runner= new CompositeRunner(klass.getName()); + int i= 0; + for (final Object each : getParametersList(new JavaClass(klass))) { + if (each instanceof Object[]) { + runner + .add(new TestClassMethodsRunner( + new ParameterizedJavaClass(klass, + (Object[]) each, i++), this)); + } else + throw new InitializationError(String.format( + "%s.%s() must return a Collection of arrays.", klass + .getName(), getParametersMethod( + new JavaClass(klass)).getName())); + } + return runner; + } + + @Override + protected void validate(MethodValidator methodValidator) { + methodValidator.validateStaticMethods(); + methodValidator.validateInstanceMethods(); + } + + private Collection getParametersList(JavaClass javaClass) + throws InitializationError { + JavaMethod parametersMethod= getParametersMethod(javaClass); + try { + return (Collection) parametersMethod.invoke(null); + } catch (Exception e) { + throw new InitializationError(e); + } + } + + private JavaMethod getParametersMethod(JavaClass javaClass) + throws InitializationError { + // TODO: is this DUP? + List<JavaMethod> methods= javaClass.getMethods(Parameters.class, + new JavaTestInterpreter()); + + for (JavaMethod each : methods) { + if (each.isStatic() && each.isPublic()) { + return each; } - }; + } + throw new InitializationError( + "No public static parameters method on class " + + javaClass.getName()); } } \ No newline at end of file Index: Parameterized.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/runners/Parameterized.java,v retrieving revision 1.5.2.2 retrieving revision 1.5.2.3 diff -u -d -r1.5.2.2 -r1.5.2.3 --- Parameterized.java 18 Oct 2006 17:07:05 -0000 1.5.2.2 +++ Parameterized.java 18 Oct 2006 18:22:14 -0000 1.5.2.3 @@ -4,17 +4,10 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import org.junit.internal.runners.CompositeRunner; -import org.junit.internal.runners.JavaClass; -import org.junit.internal.runners.JavaMethod; -import org.junit.internal.runners.JavaTestInterpreter; -import org.junit.internal.runners.MethodValidator; -import org.junit.internal.runners.TestClassMethodsRunner; import org.junit.internal.runners.TestClassRunner; /** @@ -71,59 +64,6 @@ } public Parameterized(final Class<?> klass) throws Exception { - super(klass, buildCompositeRunner(new JavaClass(klass))); - } - - // TODO: I think this now eagerly reads parameters, which was never the - // point. - - // TODO: pull interpreter back in? - private static CompositeRunner buildCompositeRunner( - final JavaClass klass) throws Exception { - CompositeRunner runner= new CompositeRunner(klass.getName()); - int i= 0; - for (final Object each : getParametersList(klass)) { - if (each instanceof Object[]) { - final int parameterSetNumber= i++; - JavaTestInterpreter interpreter= new ParameterizedInterpreter( - each, parameterSetNumber); - runner - .add(new TestClassMethodsRunner(interpreter - .interpretJavaClass(klass.getTestClass()), - interpreter)); - } else - throw new Exception(String.format( - "%s.%s() must return a Collection of arrays.", klass - .getName(), getParametersMethod(klass) - .getName())); - } - return runner; - } - - @Override - protected void validate(MethodValidator methodValidator) { - methodValidator.validateStaticMethods(); - methodValidator.validateInstanceMethods(); - } - - // TODO: make non-static - static private Collection getParametersList(JavaClass javaClass) - throws IllegalAccessException, InvocationTargetException, Exception { - return (Collection) getParametersMethod(javaClass).invoke(null); - } - - static private JavaMethod getParametersMethod(JavaClass javaClass) - throws Exception { - // TODO: is this DUP? - List<JavaMethod> methods= javaClass.getMethods(Parameters.class, - new JavaTestInterpreter()); - - for (JavaMethod each : methods) { - if (each.isStatic() && each.isPublic()) { - return each; - } - } - throw new Exception("No public static parameters method on class " - + javaClass.getName()); + super(klass, new ParameterizedInterpreter()); } } Index: Enclosed.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/runners/Enclosed.java,v retrieving revision 1.4 retrieving revision 1.4.2.1 diff -u -d -r1.4 -r1.4.2.1 --- Enclosed.java 16 Mar 2006 22:09:12 -0000 1.4 +++ Enclosed.java 18 Oct 2006 18:22:14 -0000 1.4.2.1 @@ -2,6 +2,7 @@ import org.junit.internal.runners.InitializationError; + public class Enclosed extends Suite { public Enclosed(Class<?> klass) throws InitializationError { super(klass, klass.getClasses()); Index: Suite.java =================================================================== RCS file: /cvsroot/junit/junit/org/junit/runners/Suite.java,v retrieving revision 1.4 retrieving revision 1.4.2.1 diff -u -d -r1.4 -r1.4.2.1 --- Suite.java 25 Aug 2006 14:43:02 -0000 1.4 +++ Suite.java 18 Oct 2006 18:22:14 -0000 1.4.2.1 @@ -6,20 +6,24 @@ import java.lang.annotation.Target; import org.junit.internal.runners.InitializationError; +import org.junit.internal.runners.JavaTestInterpreter; import org.junit.internal.runners.TestClassRunner; import org.junit.runner.Request; +import org.junit.runner.Runner; /** - * Using <code>Suite</code> as a runner allows you to manually - * build a suite containing tests from many classes. It is the JUnit 4 equivalent of the JUnit 3.8.x - * static {@link junit.framework.Test} <code>suite()</code> method. To use it, annotate a class - * 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. + * Using <code>Suite</code> as a runner allows you to manually build a suite + * containing tests from many classes. It is the JUnit 4 equivalent of the JUnit + * 3.8.x static {@link junit.framework.Test} <code>suite()</code> method. To + * use it, annotate a class 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 { /** - * The <code>SuiteClasses</code> annotation specifies the classes to be run when a class - * annotated with <code>@RunWith(Suite.class)</code> is run. + * The <code>SuiteClasses</code> annotation specifies the classes to be + * run when a class annotated with <code>@RunWith(Suite.class)</code> is run. */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @@ -36,15 +40,25 @@ /** * Internal use only. + * @throws InitializationError */ - public Suite(Class<?> klass, Class[] annotatedClasses) throws InitializationError { - super(klass, Request.classes(klass.getName(), annotatedClasses).getRunner()); + public Suite(Class<?> klass, final Class[] annotatedClasses) throws InitializationError { + super(klass, new JavaTestInterpreter() { + @Override + public Runner runnerFor(Class<?> klass) throws InitializationError { + return Request.classes(klass.getName(), annotatedClasses) + .getRunner(); + } + }); } - private static Class[] getAnnotatedClasses(Class<?> klass) throws InitializationError { + private static Class[] getAnnotatedClasses(Class<?> klass) + throws InitializationError { SuiteClasses annotation= klass.getAnnotation(SuiteClasses.class); if (annotation == null) - throw new InitializationError(String.format("class '%s' must have a SuiteClasses annotation", klass.getName())); + throw new InitializationError(String.format( + "class '%s' must have a SuiteClasses annotation", klass + .getName())); return annotation.value(); } } |