|
10 | 10 | import org.junit.runners.model.TestClass;
|
11 | 11 |
|
12 | 12 | /**
|
13 |
| - * A collection of useful methods extracted and duplicated from {@link Parameterized}. |
| 13 | + * A collection of useful methods extracted and duplicated from |
| 14 | + * {@link Parameterized}. |
14 | 15 | * <p>
|
15 | 16 | * Code is under the license of JUnit: http://junit.org/junit4/license.html
|
16 | 17 | * <p>
|
17 | 18 | *
|
18 |
| - * Please see the package info of {@link com.github.peterwippermann.junit4.parameterizedsuite.util} for more details. |
| 19 | + * Please see the package info of |
| 20 | + * {@link com.github.peterwippermann.junit4.parameterizedsuite.util} for more |
| 21 | + * details. |
19 | 22 | */
|
20 | 23 | public class ParameterizedUtil {
|
21 | 24 |
|
22 |
| - /** |
23 |
| - * @param testClass |
24 |
| - * @return the parameters from the method annotated with {@link Parameters} |
25 |
| - * @throws Throwable |
26 |
| - * |
27 |
| - * @see Parameterized#allParameters() |
28 |
| - */ |
29 |
| - @SuppressWarnings("unchecked") |
30 |
| - public static Iterable<Object> getParameters(TestClass testClass) throws Throwable { |
31 |
| - Object parameters = getParametersMethod(testClass).invokeExplosively(null); |
32 |
| - if (parameters instanceof Iterable) { |
33 |
| - return (Iterable<Object>) parameters; |
34 |
| - } else if (parameters instanceof Object[]) { |
35 |
| - return Arrays.asList((Object[]) parameters); |
36 |
| - } else { |
37 |
| - throw parametersMethodReturnedWrongType(testClass); |
38 |
| - } |
39 |
| - } |
| 25 | + /** |
| 26 | + * @param testClass |
| 27 | + * @return the parameters from the method annotated with {@link Parameters} |
| 28 | + * @throws Throwable |
| 29 | + * |
| 30 | + * @see Parameterized#allParameters() |
| 31 | + */ |
| 32 | + @SuppressWarnings("unchecked") |
| 33 | + public static Iterable<Object> getParameters(TestClass testClass) throws Throwable { |
| 34 | + Object parameters = getParametersMethod(testClass).invokeExplosively(null); |
| 35 | + if (parameters instanceof Iterable) { |
| 36 | + return (Iterable<Object>) parameters; |
| 37 | + } else if (parameters instanceof Object[]) { |
| 38 | + return Arrays.asList((Object[]) parameters); |
| 39 | + } else { |
| 40 | + throw parametersMethodReturnedWrongType(testClass); |
| 41 | + } |
| 42 | + } |
40 | 43 |
|
41 |
| - /** |
42 |
| - * @param testClass |
43 |
| - * @return the method annotated with {@link Parameters} |
44 |
| - * @throws Exception |
45 |
| - * |
46 |
| - * @see Parameterized#allParameters() |
47 |
| - */ |
48 |
| - private static FrameworkMethod getParametersMethod(TestClass testClass) throws Exception { |
49 |
| - List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Parameters.class); |
50 |
| - for (FrameworkMethod each : methods) { |
51 |
| - if (each.isStatic() && each.isPublic()) { |
52 |
| - return each; |
53 |
| - } |
54 |
| - } |
| 44 | + /** |
| 45 | + * @param testClass |
| 46 | + * @return the method annotated with {@link Parameters} |
| 47 | + * @throws Exception |
| 48 | + * |
| 49 | + * @see Parameterized#allParameters() |
| 50 | + */ |
| 51 | + private static FrameworkMethod getParametersMethod(TestClass testClass) throws Exception { |
| 52 | + List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Parameters.class); |
| 53 | + for (FrameworkMethod each : methods) { |
| 54 | + if (each.isStatic() && each.isPublic()) { |
| 55 | + return each; |
| 56 | + } |
| 57 | + } |
55 | 58 |
|
56 |
| - throw new Exception("No public static parameters method on class " + testClass.getName()); |
57 |
| - } |
| 59 | + throw new Exception("No public static parameters method on class " + testClass.getName()); |
| 60 | + } |
58 | 61 |
|
59 |
| - /** |
60 |
| - * @see Parameterized#parametersMethodReturnedWrongType() |
61 |
| - */ |
62 |
| - private static Exception parametersMethodReturnedWrongType(TestClass testClass) throws Exception { |
63 |
| - String className = testClass.getName(); |
64 |
| - String methodName = getParametersMethod(testClass).getName(); |
65 |
| - String message = MessageFormat.format("{0}.{1}() must return an Iterable of arrays.", className, methodName); |
66 |
| - return new Exception(message); |
67 |
| - } |
| 62 | + /** |
| 63 | + * @see Parameterized#parametersMethodReturnedWrongType() |
| 64 | + */ |
| 65 | + private static Exception parametersMethodReturnedWrongType(TestClass testClass) throws Exception { |
| 66 | + String className = testClass.getName(); |
| 67 | + String methodName = getParametersMethod(testClass).getName(); |
| 68 | + String message = MessageFormat.format("{0}.{1}() must return an Iterable of arrays.", className, methodName); |
| 69 | + return new Exception(message); |
| 70 | + } |
68 | 71 |
|
69 |
| - /** |
70 |
| - * Parameters of a test can either be 1.) a set of {@link Object}s or 2.) a set of Arrays of |
71 |
| - * Objects. This method normalizes both variants to Object[]. Single Objects are therefore |
72 |
| - * stored in a new Array. |
73 |
| - * |
74 |
| - * @param singleParameterAsArrayOrObject |
75 |
| - * |
76 |
| - * @see {@link Parameterized#createTestWithNotNormalizedParameters(String, int, Object)} |
77 |
| - */ |
78 |
| - public static Object[] normalizeParameter(Object singleParameterAsArrayOrObject) { |
79 |
| - return (singleParameterAsArrayOrObject instanceof Object[]) ? (Object[]) singleParameterAsArrayOrObject : new Object[] {singleParameterAsArrayOrObject}; |
80 |
| - } |
| 72 | + /** |
| 73 | + * Parameters of a test can either be 1.) a set of {@link Object}s or 2.) a |
| 74 | + * set of Arrays of Objects. This method normalizes both variants to |
| 75 | + * Object[]. Single Objects are therefore stored in a new Array. |
| 76 | + * |
| 77 | + * @param singleParameterAsArrayOrObject |
| 78 | + * |
| 79 | + * @see Parameterized#createTestWithNotNormalizedParameters(String, int, |
| 80 | + * Object) |
| 81 | + */ |
| 82 | + public static Object[] normalizeParameter(Object singleParameterAsArrayOrObject) { |
| 83 | + return (singleParameterAsArrayOrObject instanceof Object[]) ? (Object[]) singleParameterAsArrayOrObject |
| 84 | + : new Object[] { singleParameterAsArrayOrObject }; |
| 85 | + } |
81 | 86 |
|
82 |
| - /** |
83 |
| - * Builds a name for a test from a given name pattern by inserting the current parameter and its index. |
84 |
| - * |
85 |
| - * @param pattern |
86 |
| - * @param index |
87 |
| - * @param parameters |
88 |
| - * |
89 |
| - * @see {@link Parameterized#createTestWithParameters(TestClass, String, int, Object[])} |
90 |
| - */ |
91 |
| - public static String buildTestName(String pattern, int index, Object[] parameters) { |
92 |
| - String finalPattern = pattern.replaceAll("\\{index\\}", Integer.toString(index)); |
93 |
| - return "[" + MessageFormat.format(finalPattern, parameters) + "]"; |
94 |
| - } |
| 87 | + /** |
| 88 | + * Builds a name for a test from a given name pattern by inserting the |
| 89 | + * current parameter and its index. |
| 90 | + * |
| 91 | + * @param pattern |
| 92 | + * @param index |
| 93 | + * @param parameters |
| 94 | + * |
| 95 | + * @see Parameterized#createTestWithParameters(TestClass, String, int, |
| 96 | + * Object[]) |
| 97 | + */ |
| 98 | + public static String buildTestName(String pattern, int index, Object[] parameters) { |
| 99 | + String finalPattern = pattern.replaceAll("\\{index\\}", Integer.toString(index)); |
| 100 | + return "[" + MessageFormat.format(finalPattern, parameters) + "]"; |
| 101 | + } |
95 | 102 |
|
96 |
| - /** |
97 |
| - * @param testClass |
98 |
| - * @return The name pattern for tests as defined in the {@link Parameters} annotation. |
99 |
| - * @throws Exception |
100 |
| - * |
101 |
| - * @see {@link Parameterized#Parameterized(Class)} |
102 |
| - */ |
103 |
| - public static String getNamePatternForParameters(TestClass testClass) throws Exception { |
104 |
| - Parameters parametersAnnotation = getParametersMethod(testClass).getAnnotation(Parameters.class); |
105 |
| - return parametersAnnotation.name(); |
106 |
| - } |
| 103 | + /** |
| 104 | + * @param testClass |
| 105 | + * @return The name pattern for tests as defined in the {@link Parameters} |
| 106 | + * annotation. |
| 107 | + * @throws Exception |
| 108 | + * |
| 109 | + * @see Parameterized#Parameterized(Class) |
| 110 | + */ |
| 111 | + public static String getNamePatternForParameters(TestClass testClass) throws Exception { |
| 112 | + Parameters parametersAnnotation = getParametersMethod(testClass).getAnnotation(Parameters.class); |
| 113 | + return parametersAnnotation.name(); |
| 114 | + } |
107 | 115 |
|
108 | 116 | }
|
0 commit comments