From 6c350cb919bf3b3cca76523357b0d02a98ea6a3e Mon Sep 17 00:00:00 2001 From: melchisedek Date: Wed, 30 Jul 2014 17:26:39 +0200 Subject: [PATCH 1/7] handle the {index} parameter like a native MessageFormat argument. For example that allows to specified leading zero by using {index,number,0000} --- src/main/java/org/junit/runners/Parameterized.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/junit/runners/Parameterized.java b/src/main/java/org/junit/runners/Parameterized.java index 829c8f0f15af..6c1ba2529aef 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; @@ -342,8 +344,13 @@ 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; + Pattern indexMatcherPattern = Pattern.compile("(\\{)index([^\\}]*\\})"); + Matcher matcher = indexMatcherPattern.matcher(pattern); + if (matcher.find()) { + String idxPattern = matcher.group(1) + 0 + matcher.group(2); + finalPattern = pattern.replace(matcher.group(), MessageFormat.format(idxPattern, index)); + } String name = MessageFormat.format(finalPattern, parameters); return new TestWithParameters("[" + name + "]", testClass, Arrays.asList(parameters)); From 111e8795c1b3e734b0f6edba0ffeac0bf0411a5d Mon Sep 17 00:00:00 2001 From: melchisedek Date: Thu, 31 Jul 2014 23:53:04 +0200 Subject: [PATCH 2/7] add test for ParameterizedTest with special test names --- .../classes/ParameterizedTestTest.java | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) 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..d35154068574 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}: fib({0})={1}") + 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: fib(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: fib(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()); } From fbddf8b130afc9721515326106b91e1c37a4fdec Mon Sep 17 00:00:00 2001 From: melchisedek Date: Fri, 1 Aug 2014 12:03:03 +0200 Subject: [PATCH 3/7] fix Test for ParameterizedTest --- .../junit/tests/running/classes/ParameterizedTestTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 d35154068574..5d5e8cc8c0d4 100644 --- a/src/test/java/org/junit/tests/running/classes/ParameterizedTestTest.java +++ b/src/test/java/org/junit/tests/running/classes/ParameterizedTestTest.java @@ -89,7 +89,7 @@ public void plansNamedCorrectly() throws Exception { @RunWith(Parameterized.class) static public class ParameterizedWithSpecialTestname { - @Parameters(name = "{index,number,0000}: fib({0})={1}") + @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}}); @@ -125,7 +125,7 @@ public void countWithSpecialTestname() { public void failuresNamedCorrectlyWithSpecialTestname() { Result result = JUnitCore.runClasses(ParameterizedWithSpecialTestname.class); assertEquals( - "test[0001: fib(1)=1](" + ParameterizedWithSpecialTestname.class.getName() + ")", + "test[0001: param 1: 1 on test#: 1 with expected result: 1 - 1](" + ParameterizedWithSpecialTestname.class.getName() + ")", result.getFailures().get(0).getTestHeader()); } @@ -139,7 +139,7 @@ public void countBeforeRunWithSpecialTestname() throws Exception { public void plansNamedCorrectlyWithSpecialTestname() throws Exception { Runner runner = Request.aClass(ParameterizedWithSpecialTestname.class).getRunner(); Description description = runner.getDescription(); - assertEquals("[0000: fib(0)=0]", description.getChildren().get(0) + assertEquals("[0000: param 1: 0 on test#: 0 with expected result: 0 - 0]", description.getChildren().get(0) .getDisplayName()); } From 367a3625745311703c40f31772d73c3f18d41726 Mon Sep 17 00:00:00 2001 From: melchisedek Date: Fri, 1 Aug 2014 12:06:27 +0200 Subject: [PATCH 4/7] Add feadback of #969 * extract a constant for this Pattern * use while loop (replace and not replaceall) * use "0" instead of 0 --- src/main/java/org/junit/runners/Parameterized.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/junit/runners/Parameterized.java b/src/main/java/org/junit/runners/Parameterized.java index 6c1ba2529aef..b857dd0282e5 100644 --- a/src/main/java/org/junit/runners/Parameterized.java +++ b/src/main/java/org/junit/runners/Parameterized.java @@ -165,6 +165,8 @@ * @since 4.0 */ public class Parameterized extends Suite { + private static final String INDEX_MATCHER_PATTERN = "(\\{)index([^\\}]*\\})"; + /** * Annotation for a method which provides parameters to be injected into the * test class constructor by Parameterized. The method has to @@ -345,11 +347,11 @@ private Exception parametersMethodReturnedWrongType() throws Exception { private static TestWithParameters createTestWithParameters( TestClass testClass, String pattern, int index, Object[] parameters) { String finalPattern = pattern; - Pattern indexMatcherPattern = Pattern.compile("(\\{)index([^\\}]*\\})"); + Pattern indexMatcherPattern = Pattern.compile(INDEX_MATCHER_PATTERN); Matcher matcher = indexMatcherPattern.matcher(pattern); - if (matcher.find()) { - String idxPattern = matcher.group(1) + 0 + matcher.group(2); - finalPattern = pattern.replace(matcher.group(), MessageFormat.format(idxPattern, index)); + 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, From e1708f247e0218ae1867c9cd4a7faa61489473de Mon Sep 17 00:00:00 2001 From: melchisedek Date: Sun, 10 Aug 2014 12:43:48 +0200 Subject: [PATCH 5/7] extract Pattern to a constant field --- src/main/java/org/junit/runners/Parameterized.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/junit/runners/Parameterized.java b/src/main/java/org/junit/runners/Parameterized.java index b857dd0282e5..6b427212d6d6 100644 --- a/src/main/java/org/junit/runners/Parameterized.java +++ b/src/main/java/org/junit/runners/Parameterized.java @@ -165,7 +165,6 @@ * @since 4.0 */ public class Parameterized extends Suite { - private static final String INDEX_MATCHER_PATTERN = "(\\{)index([^\\}]*\\})"; /** * Annotation for a method which provides parameters to be injected into the @@ -238,6 +237,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; /** @@ -347,8 +348,7 @@ private Exception parametersMethodReturnedWrongType() throws Exception { private static TestWithParameters createTestWithParameters( TestClass testClass, String pattern, int index, Object[] parameters) { String finalPattern = pattern; - Pattern indexMatcherPattern = Pattern.compile(INDEX_MATCHER_PATTERN); - Matcher matcher = indexMatcherPattern.matcher(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)); From de1e40a7246a1dfc41c4d424770f607baecd8f11 Mon Sep 17 00:00:00 2001 From: melchisedek Date: Sat, 16 Aug 2014 12:00:01 +0200 Subject: [PATCH 6/7] add short description on javaDoc --- .../java/org/junit/runners/Parameterized.java | 71 +++++++++++-------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/junit/runners/Parameterized.java b/src/main/java/org/junit/runners/Parameterized.java index 6b427212d6d6..97fbe5b7ecca 100644 --- a/src/main/java/org/junit/runners/Parameterized.java +++ b/src/main/java/org/junit/runners/Parameterized.java @@ -27,24 +27,25 @@ * 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(name= "{index}: fib[{0}]={1}")
+ *     @Parameters(name = "{index}: fib[{0}]={1}")
  *     public static Iterable<Object[]> 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;
+ *         fInput = input;
+ *         fExpected = expected;
  *     }
- *
+ * 
  *     @Test
  *     public void test() {
  *         assertEquals(fExpected, Fibonacci.compute(fInput));
@@ -69,37 +70,47 @@
  * 
...
*
...
* + * 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, * then the current parameter index is used as name. *

* You can also write: + * *

  * @RunWith(Parameterized.class)
  * public class FibonacciTest {
- *  @Parameters
- *  public static Iterable<Object[]> data() {
- *      return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
+ *     @Parameters
+ *     public static Iterable<Object[]> data() {
+ *         return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
  *                 { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
- *  }
- *  
- *  @Parameter(0)
- *  public int fInput;
- *
- *  @Parameter(1)
- *  public int fExpected;
- *
- *  @Test
- *  public void test() {
- *      assertEquals(fExpected, Fibonacci.compute(fInput));
- *  }
+ *     }
+ * 
+ *     @Parameter(0)
+ *     public int fInput;
+ * 
+ *     @Parameter(1)
+ *     public int fExpected;
+ * 
+ *     @Test
+ *     public void test() {
+ *         assertEquals(fExpected, Fibonacci.compute(fInput));
+ *     }
  * }
  * 
*

- * Each instance of FibonacciTest will be constructed with the default constructor - * and fields annotated by @Parameter will be initialized - * with the data values in the @Parameters method. + * Each instance of FibonacciTest will be constructed with the + * default constructor and fields annotated by @Parameter will + * be initialized with the data values in the @Parameters + * method. * *

* The parameters can be provided as an array, too: @@ -107,8 +118,8 @@ *

  * @Parameters
  * public static Object[][] data() {
- * 	return new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 },
- * 			{ 5, 5 }, { 6, 8 } };
+ *     return new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 },
+ *             { 5, 5 }, { 6, 8 } };
  * }
  * 
* @@ -117,18 +128,20 @@ * If your test needs a single parameter only, you don't have to wrap it with an * array. Instead you can provide an Iterable or an array of * objects. + * *
  * @Parameters
  * public static Iterable<? extends Object> data() {
- * 	return Arrays.asList("first test", "second test");
+ *     return Arrays.asList("first test", "second test");
  * }
  * 
*

* or + * *

  * @Parameters
  * public static Object[] data() {
- * 	return new Object[] { "first test", "second test" };
+ *     return new Object[] { "first test", "second test" };
  * }
  * 
* From e6d2a2608b6e03c1e7ceba921f9a35884647796f Mon Sep 17 00:00:00 2001 From: melchisedek Date: Sat, 20 Sep 2014 09:19:48 +0200 Subject: [PATCH 7/7] revert code formatting and add only additional javaDoc. --- .../java/org/junit/runners/Parameterized.java | 65 +++++++++---------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/junit/runners/Parameterized.java b/src/main/java/org/junit/runners/Parameterized.java index 97fbe5b7ecca..3f5551680619 100644 --- a/src/main/java/org/junit/runners/Parameterized.java +++ b/src/main/java/org/junit/runners/Parameterized.java @@ -27,25 +27,24 @@ * 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(name = "{index}: fib[{0}]={1}")
+ *     @Parameters(name= "{index}: fib[{0}]={1}")
  *     public static Iterable<Object[]> 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;
+ *         fInput= input;
+ *         fExpected= expected;
  *     }
- * 
+ *
  *     @Test
  *     public void test() {
  *         assertEquals(fExpected, Fibonacci.compute(fInput));
@@ -75,7 +74,7 @@
  * 
    *
  • 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 + *
  • want format a parameter (placeholder with index 2) of type date, you can * use: {2,date,full}
  • *
*

@@ -84,33 +83,31 @@ * then the current parameter index is used as name. *

* You can also write: - * *

  * @RunWith(Parameterized.class)
  * public class FibonacciTest {
- *     @Parameters
- *     public static Iterable<Object[]> data() {
- *         return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
+ *  @Parameters
+ *  public static Iterable<Object[]> data() {
+ *      return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
  *                 { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
- *     }
- * 
- *     @Parameter(0)
- *     public int fInput;
- * 
- *     @Parameter(1)
- *     public int fExpected;
- * 
- *     @Test
- *     public void test() {
- *         assertEquals(fExpected, Fibonacci.compute(fInput));
- *     }
+ *  }
+ *  
+ *  @Parameter(0)
+ *  public int fInput;
+ *
+ *  @Parameter(1)
+ *  public int fExpected;
+ *
+ *  @Test
+ *  public void test() {
+ *      assertEquals(fExpected, Fibonacci.compute(fInput));
+ *  }
  * }
  * 
*

- * Each instance of FibonacciTest will be constructed with the - * default constructor and fields annotated by @Parameter will - * be initialized with the data values in the @Parameters - * method. + * Each instance of FibonacciTest will be constructed with the default constructor + * and fields annotated by @Parameter will be initialized + * with the data values in the @Parameters method. * *

* The parameters can be provided as an array, too: @@ -118,8 +115,8 @@ *

  * @Parameters
  * public static Object[][] data() {
- *     return new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 },
- *             { 5, 5 }, { 6, 8 } };
+ * 	return new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 },
+ * 			{ 5, 5 }, { 6, 8 } };
  * }
  * 
* @@ -128,20 +125,18 @@ * If your test needs a single parameter only, you don't have to wrap it with an * array. Instead you can provide an Iterable or an array of * objects. - * *
  * @Parameters
  * public static Iterable<? extends Object> data() {
- *     return Arrays.asList("first test", "second test");
+ * 	return Arrays.asList("first test", "second test");
  * }
  * 
*

* or - * *

  * @Parameters
  * public static Object[] data() {
- *     return new Object[] { "first test", "second test" };
+ * 	return new Object[] { "first test", "second test" };
  * }
  * 
*