diff -r 000000000000 -r 82d1d1de1a01 carbidecpp20devenv/plugins/org.eclipse.test.source_3.3.0.v20080507/src/org.junit4_4.3.1/junitsrc/org/junit/runners/Parameterized.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/carbidecpp20devenv/plugins/org.eclipse.test.source_3.3.0.v20080507/src/org.junit4_4.3.1/junitsrc/org/junit/runners/Parameterized.java Wed Mar 18 17:21:00 2009 -0500 @@ -0,0 +1,150 @@ +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; +import java.lang.annotation.Target; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.junit.internal.runners.CompositeRunner; +import org.junit.internal.runners.MethodValidator; +import org.junit.internal.runners.TestClassMethodsRunner; +import org.junit.internal.runners.TestClassRunner; + +/**

The custom runner Parameterized implements parameterized + * tests. When running a parameterized test class, instances are created for the + * cross-product of the test methods and the test data elements.

+ * + * For example, to test a Fibonacci function, write: + *
+ * @RunWith(Parameterized.class)
+ * public class FibonacciTest {
+ *    @Parameters
+ *    public static Collection data() {
+ *          return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
+ *             { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
+ *    }
+ *
+ *    private int fInput;
+ *    private int fExpected;
+ *
+ *    public FibonacciTest(int input, int expected) {
+ *       fInput= input;
+ *       fExpected= expected;
+ *    }
+ *
+ *    @Test public void test() {
+ *       assertEquals(fExpected, Fibonacci.compute(fInput));
+ *    }
+ * }
+ * 
+ * + *

Each instance of FibonacciTest will be constructed using the two-argument + * constructor and the data values in the @Parameters method.

+ */ +public class Parameterized extends TestClassRunner { + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.METHOD) + public static @interface Parameters { + } + + public static Collection eachOne(Object... params) { + List results= new ArrayList(); + for (Object param : params) + results.add(new Object[] { param }); + return results; + } + + // TODO: single-class this extension + + private static class TestClassRunnerForParameters extends TestClassMethodsRunner { + private final Object[] fParameters; + + private final int fParameterSetNumber; + + private final Constructor fConstructor; + + private TestClassRunnerForParameters(Class klass, Object[] parameters, int i) { + super(klass); + fParameters= parameters; + fParameterSetNumber= i; + fConstructor= getOnlyConstructor(); + } + + @Override + protected Object createTest() throws Exception { + return fConstructor.newInstance(fParameters); + } + + @Override + protected String getName() { + return String.format("[%s]", fParameterSetNumber); + } + + @Override + protected String testName(final Method method) { + return String.format("%s[%s]", method.getName(), fParameterSetNumber); + } + + private Constructor getOnlyConstructor() { + Constructor[] constructors= getTestClass().getConstructors(); + assertEquals(1, constructors.length); + return constructors[0]; + } + } + + // 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())); + } + } + + private Collection getParametersList() throws IllegalAccessException, InvocationTargetException, Exception { + return (Collection) getParametersMethod().invoke(null); + } + + 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()); + } + } + + public Parameterized(final Class klass) throws Exception { + super(klass, new RunAllParameterMethods(klass)); + } + + @Override + protected void validate(MethodValidator methodValidator) { + methodValidator.validateStaticMethods(); + methodValidator.validateInstanceMethods(); + } +}