diff --git a/src/main/java/org/junit/runners/Parameterized.java b/src/main/java/org/junit/runners/Parameterized.java index 829c8f0f15af..3f5551680619 100644 --- a/src/main/java/org/junit/runners/Parameterized.java +++ b/src/main/java/org/junit/runners/Parameterized.java @@ -10,6 +10,8 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.junit.runner.Runner; import org.junit.runners.model.FrameworkMethod; @@ -67,6 +69,14 @@ *
...
*
...
* + * To format the placeholder you can use patterns like {@link MessageFormat}.
+ * For Example if you: + * *

* In the example given above, the Parameterized runner creates * names like [1: fib(3)=2]. If you don't use the name parameter, @@ -163,6 +173,7 @@ * @since 4.0 */ public class Parameterized extends Suite { + /** * Annotation for a method which provides parameters to be injected into the * test class constructor by Parameterized. The method has to @@ -234,6 +245,8 @@ public class Parameterized extends Suite { private static final List NO_RUNNERS = Collections.emptyList(); + private static final Pattern INDEX_MATCHER_PATTERN = Pattern.compile("(\\{)index([^\\}]*\\})"); + private final List runners; /** @@ -342,8 +355,12 @@ private Exception parametersMethodReturnedWrongType() throws Exception { private static TestWithParameters createTestWithParameters( TestClass testClass, String pattern, int index, Object[] parameters) { - String finalPattern = pattern.replaceAll("\\{index\\}", - Integer.toString(index)); + String finalPattern = pattern; + Matcher matcher = INDEX_MATCHER_PATTERN.matcher(pattern); + while (matcher.find()) { + String idxPattern = matcher.group(1) + "0" + matcher.group(2); + finalPattern = finalPattern.replace(matcher.group(), MessageFormat.format(idxPattern, index)); + } String name = MessageFormat.format(finalPattern, parameters); return new TestWithParameters("[" + name + "]", testClass, Arrays.asList(parameters)); diff --git a/src/test/java/org/junit/tests/running/classes/ParameterizedTestTest.java b/src/test/java/org/junit/tests/running/classes/ParameterizedTestTest.java index 6ab94da863f3..5d5e8cc8c0d4 100644 --- a/src/test/java/org/junit/tests/running/classes/ParameterizedTestTest.java +++ b/src/test/java/org/junit/tests/running/classes/ParameterizedTestTest.java @@ -86,6 +86,62 @@ public void plansNamedCorrectly() throws Exception { assertEquals("[0: fib(0)=0]", description.getChildren().get(0) .getDisplayName()); } + + @RunWith(Parameterized.class) + static public class ParameterizedWithSpecialTestname { + @Parameters(name = "{index,number,0000}: param 1: {0} on test#: {index} with expected result: {1} - {index}") + public static Iterable data() { + return Arrays.asList(new Object[][]{{0, 0}, {1, 1}, {2, 1}, + {3, 2}, {4, 3}, {5, 5}, {6, 8}}); + } + + private final int fInput; + + private final int fExpected; + + public ParameterizedWithSpecialTestname(int input, int expected) { + fInput = input; + fExpected = expected; + } + + @Test + public void test() { + assertEquals(fExpected, fib(fInput)); + } + + private int fib(int x) { + return 0; + } + } + + @Test + public void countWithSpecialTestname() { + Result result = JUnitCore.runClasses(ParameterizedWithSpecialTestname.class); + assertEquals(7, result.getRunCount()); + assertEquals(6, result.getFailureCount()); + } + + @Test + public void failuresNamedCorrectlyWithSpecialTestname() { + Result result = JUnitCore.runClasses(ParameterizedWithSpecialTestname.class); + assertEquals( + "test[0001: param 1: 1 on test#: 1 with expected result: 1 - 1](" + ParameterizedWithSpecialTestname.class.getName() + ")", + result.getFailures().get(0).getTestHeader()); + } + + @Test + public void countBeforeRunWithSpecialTestname() throws Exception { + Runner runner = Request.aClass(ParameterizedWithSpecialTestname.class).getRunner(); + assertEquals(7, runner.testCount()); + } + + @Test + public void plansNamedCorrectlyWithSpecialTestname() throws Exception { + Runner runner = Request.aClass(ParameterizedWithSpecialTestname.class).getRunner(); + Description description = runner.getDescription(); + assertEquals("[0000: param 1: 0 on test#: 0 with expected result: 0 - 0]", description.getChildren().get(0) + .getDisplayName()); + } @RunWith(Parameterized.class) public static class ParameterizedWithoutSpecialTestname { @@ -385,7 +441,7 @@ private int fib(int x) { @Test public void runsEveryTestOfArray() { - Result result= JUnitCore.runClasses(FibonacciTestWithArray.class); + Result result = JUnitCore.runClasses(FibonacciTestWithArray.class); assertEquals(7, result.getRunCount()); }