Parameterized With Names

"el yoryo" <[email protected]> Wed, 10 Sep 2008 17:30:36 -0300
Newsgroups gmane.comp.java.junit.devel
Message-ID <[email protected]>
Hi Junit-devel,     I'm writting for give same sugest. I know few of every
thing, also English.
     Well. I use parameterized with Spring, so I mad distinct kind of test
and set values in a xml.
     The problem is the name of the test. All see the same when I run it.
     So a change the collection of parameters for a Map where the key is the
name.

     I try to doit, but eclipse make security exception.

Next it the code I fix. I know it is not  broken. It may be usefull.
Sory for may Bad English.... I'll learn more.
         Yory.

package org.junit.runners;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestClass;


/**
 * <p>
 * The custom runner <code>Parameterized</code> 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.
 * </p>
 *
 * For example, to test a Fibonacci function, write:
 *
 * <pre>
 * &#064;RunWith(Parameterized.class)
 * public class FibonacciTest {
 * &#064;Parameters
 * public static Collection&lt;Object[]&gt; data() {
 * return Arrays.asList(new Object[][] {
 * Fibonacci,
 * { { 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;
 * }
 *
 * &#064;Test
 * public void test(@HeresHowYouGetValue Type value) {
 * assertAnswerKey(new Object[][] {
 * Fibonacci,
 * { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 },
 * { 6, 8 } } });
 * assertEquals(fExpected, Fibonacci.compute(fInput));
 * }
 * }
 * </pre>
 *
 * <p>
 * Each instance of <code>FibonacciTest</code> will be constructed using the
 * two-argument constructor and the data values in the
 * <code>&#064;Parameters</code> method.
 * </p>
 */
public class Parameterized extends Suite {
/**
 * Annotation for a method which provides parameters to be injected into the
 * test class constructor by <code>Parameterized</code>
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public static @interface Parameters {
}

private class TestClassRunnerForParameters extends
BlockJUnit4ClassRunner {

private final Object[] fParameterList;
private final String name;

TestClassRunnerForParameters(Class<?> type,
Object[] parameterList, String name) throws InitializationError {
super(type);
fParameterList= parameterList;
this.name= name;
}

@Override
public Object createTest() throws Exception {
return getTestClass().getOnlyConstructor().newInstance(
computeParams());
}

private Object[] computeParams() throws Exception {
return fParameterList;
 }

@Override
protected String getName() {
return name;
}

@Override
protected String testName(final FrameworkMethod method) {
return method.getName();
}

@Override
protected void validateZeroArgConstructor(List<Throwable> errors) {
// constructor can, nay, should have args.
}

@Override
protected Statement classBlock(RunNotifier notifier) {
return childrenInvoker(notifier);
}
}

private final ArrayList<Runner> runners= new ArrayList<Runner>();

/**
 * Only called reflectively. Do not use programmatically.
 */

@SuppressWarnings("unchecked")
public Parameterized(Class<?> klass) throws Throwable {
super(klass, Collections.<Runner>emptyList());
Object parameters= getParametersList(getTestClass());
if (parameters instanceof Map<?, ?> ) {
Map<String, Object[]> parametersMap = (Map<String, Object[]>) parameters ;
for (Entry<String, Object[]> e: parametersMap.entrySet()) {
runners.add(new TestClassRunnerForParameters(getTestClass().getJavaClass(),
e.getValue(), e.getKey()));
}
} else if (parameters instanceof List<?> ) {
List<Object[]> parametersList = (List<Object[]>) parameters;
for (int i= 0; i < parametersList.size(); i++)
runners.add(new TestClassRunnerForParameters(getTestClass().getJavaClass(),
parametersList.get(i), Integer.toString(i)));
}
 }

@Override
protected List<Runner> getChildren() {
return runners;
}

@SuppressWarnings("unchecked")
private List<Object[]> getParametersList(TestClass klass)
throws Throwable {
return (List<Object[]>) getParametersMethod(klass).invokeExplosively(
null);
}

private FrameworkMethod getParametersMethod(TestClass testClass)
throws Exception {
List<FrameworkMethod> methods= testClass
.getAnnotatedMethods(Parameters.class);
for (FrameworkMethod each : methods) {
int modifiers= each.getMethod().getModifiers();
if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))
return each;
}

throw new Exception("No public static parameters method on class "
+ testClass.getName());
}

}

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/

_______________________________________________
Junit-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/junit-devel