*
+ * To format the placeholder you can use patterns like {@link MessageFormat}.
+ * For Example if you:
+ *
+ *
want specified leading zero for the {index} placeholder, you can use:
+ * {index,number,0000}
+ *
want format a parameter (placeholder with index 2) of type date, you can
+ * use: {2,date,full}
+ *
*
* 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