Created
August 11, 2010 00:24
-
-
Save ddimtirov/518274 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- C:\sandbox\junit4.8.2\src\org\junit\runners\Parameterized.java 2010-02-22 21:52:14.000000000 +-0900 | |
+++ C:\sandbox\junit4.8.2\src\org\junit\runners\Parameterized.new 2010-08-10 19:30:46.000000000 +-0900 | |
@@ -2,12 +2,13 @@ | |
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.text.MessageFormat; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import org.junit.runner.Runner; | |
import org.junit.runner.notification.RunNotifier; | |
@@ -57,18 +58,59 @@ | |
* two-argument constructor and the data values in the | |
* <code>@Parameters</code> method. | |
* </p> | |
*/ | |
public class Parameterized extends Suite { | |
/** | |
+ * If parentheses are present in a fixture description, it is not reported | |
+ * correctly, so we should strip them for now. | |
+ */ | |
+ private static final String PROBLEMATIC_FIXTURE_DESCRIPTION_CHARS = "\\(|\\)"; | |
+ | |
+ /** | |
* 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 { | |
+ /** | |
+ * <p>Optional pattern to derive the fixture name from the parameters. | |
+ * Use numbers in braces to refer to the parameters or the additional | |
+ * data as follows:</p> | |
+ * <pre> | |
+ * {0} - the original fixture name | |
+ * {1} - the current parameter index | |
+ * {2} - the first parameter value | |
+ * {3} - the second parameter value | |
+ * etc... | |
+ * </pre> | |
+ * <p>Default value is "[{0}]" for compatibility with previous JUnit versions.</p> | |
+ * | |
+ * @return {@link MessageFormat} pattern string | |
+ * @see MessageFormat | |
+ */ | |
+ String fixtureDescription() default "[{0}]"; | |
+ | |
+ /** | |
+ * <p>Optional pattern to derive the test name from the parameters. | |
+ * Use numbers in braces to refer to the parameters or the additional | |
+ * data as follows:</p> | |
+ * <pre> | |
+ * {0} - the original fixture name | |
+ * {1} - the current parameter index | |
+ * {2} - the first parameter value | |
+ * {3} - the second parameter value | |
+ * etc... | |
+ * </pre> | |
+ * <p>Default value is "{0}[{1}]" for compatibility with previous JUnit versions.</p> | |
+ * | |
+ * @return {@link MessageFormat} pattern string | |
+ * @see MessageFormat | |
+ */ | |
+ String testDescription() default "{0}[{1}]"; | |
} | |
private class TestClassRunnerForParameters extends | |
BlockJUnit4ClassRunner { | |
private final int fParameterSetNumber; | |
@@ -97,19 +139,32 @@ | |
getTestClass()).getName())); | |
} | |
} | |
@Override | |
protected String getName() { | |
- return String.format("[%s]", fParameterSetNumber); | |
+ String str = calculateDescription(fixtureDescriptionFmt, super.getName()); | |
+ return str.replaceAll(PROBLEMATIC_FIXTURE_DESCRIPTION_CHARS, ""); | |
} | |
@Override | |
protected String testName(final FrameworkMethod method) { | |
- return String.format("%s[%s]", method.getName(), | |
- fParameterSetNumber); | |
+ return calculateDescription(testDescriptionFmt, method.getName()); | |
+ } | |
+ | |
+ private String calculateDescription(MessageFormat descriptionFmt, String name) { | |
+ try { | |
+ Object[] parameters = computeParams(); | |
+ Object[] formatArgs = new Object[parameters.length + 2]; | |
+ formatArgs[0] = name; | |
+ formatArgs[1] = fParameterSetNumber; | |
+ System.arraycopy(parameters, 0, formatArgs, 2, parameters.length); | |
+ return descriptionFmt.format(formatArgs, new StringBuffer(), null).toString(); | |
+ } catch (Exception e) { | |
+ return String.format("[%s] ERR: %s", fParameterSetNumber, e.getMessage()); | |
+ } | |
} | |
@Override | |
protected void validateConstructor(List<Throwable> errors) { | |
validateOnlyOneConstructor(errors); | |
} | |
@@ -118,22 +173,29 @@ | |
protected Statement classBlock(RunNotifier notifier) { | |
return childrenInvoker(notifier); | |
} | |
} | |
private final ArrayList<Runner> runners= new ArrayList<Runner>(); | |
+ protected final MessageFormat fixtureDescriptionFmt; | |
+ protected final MessageFormat testDescriptionFmt; | |
/** | |
* Only called reflectively. Do not use programmatically. | |
*/ | |
public Parameterized(Class<?> klass) throws Throwable { | |
super(klass, Collections.<Runner>emptyList()); | |
List<Object[]> parametersList= getParametersList(getTestClass()); | |
for (int i= 0; i < parametersList.size(); i++) | |
runners.add(new TestClassRunnerForParameters(getTestClass().getJavaClass(), | |
parametersList, i)); | |
+ | |
+ FrameworkMethod parametersMethod = getParametersMethod(getTestClass()); | |
+ Parameters parameters = parametersMethod.getAnnotation(Parameters.class); | |
+ fixtureDescriptionFmt = new MessageFormat(parameters.fixtureDescription()); | |
+ testDescriptionFmt = new MessageFormat(parameters.testDescription()); | |
} | |
@Override | |
protected List<Runner> getChildren() { | |
return runners; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.junit.tests.running.methods; | |
import org.junit.Test; | |
import org.junit.runner.Description; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.Parameterized; | |
import org.junit.runners.Parameterized.Parameters; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import static org.junit.Assert.*; | |
public class NamedParameterizedTest { | |
@RunWith(Parameterized.class) | |
static public class NamedFoobar { | |
@Parameters(fixtureDescription = "{0}[{1}] - ({3}, {2})", | |
testDescription = "{0}") | |
public static List<Object[]> params() { | |
return Arrays.asList(new Object[][]{ | |
{"foo", 11}, {"bar", 22}, {"baz", 33} | |
}); | |
} | |
public NamedFoobar(String s, int i) { } | |
@Test public void test1() { } | |
@Test public void test2() { } | |
} | |
@Test public void testFoobar() throws Throwable { | |
Parameterized foobar = new Parameterized(NamedFoobar.class); | |
String className = NamedFoobar.class.getName(); | |
Description fixture = foobar.getDescription(); | |
assertEquals(className, fixture.getDisplayName()); | |
ArrayList<Description> parameterizedFixtures = fixture.getChildren(); | |
assertEquals(parameterizedFixtures.size(), 3); | |
assertEquals(className + "[0] - 11, foo", parameterizedFixtures.get(0).getDisplayName()); | |
assertEquals(className + "[1] - 22, bar", parameterizedFixtures.get(1).getDisplayName()); | |
assertEquals(className + "[2] - 33, baz", parameterizedFixtures.get(2).getDisplayName()); | |
for (Description child : parameterizedFixtures) { | |
ArrayList<Description> tests = child.getChildren(); | |
assertEquals(2, tests.size()); | |
assertEquals("test1(" + className + ")", tests.get(0).getDisplayName()); | |
assertEquals("test2(" + className + ")", tests.get(1).getDisplayName()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment