1717 */
1818package mustache .specs ;
1919
20- import java .lang .reflect .Method ;
2120import java .util .ArrayList ;
2221import java .util .Collection ;
2322import java .util .List ;
2423
2524import org .junit .runner .Description ;
2625import org .junit .runners .Parameterized ;
26+ import org .junit .runners .model .FrameworkMethod ;
27+ import org .junit .runners .model .TestClass ;
2728
2829public class SpecRunner extends Parameterized {
29-
3030 private List <String > labels ;
31-
3231 private Description labelledDescription ;
3332
3433 public SpecRunner (final Class <?> cl ) throws Throwable {
@@ -37,120 +36,28 @@ public SpecRunner(final Class<?> cl) throws Throwable {
3736 generateLabelledDescription ();
3837 }
3938
40- private void initialiseLabels () throws Exception {
41- Collection <Object []> parameterArrays = getParameterArrays ();
39+ private void initialiseLabels () throws Throwable {
40+ Collection <Object []> parameterArrays = getParameterArrays4_4 ();
4241 labels = new ArrayList <String >();
4342 for (Object [] parameterArray : parameterArrays ) {
4443 Spec spec = (Spec ) parameterArray [0 ];
4544 labels .add (spec .name ());
4645 }
47-
48- }
49-
50- private Collection <Object []> getParameterArrays () throws Exception {
51- Method testClassMethod = getDeclaredMethod (this .getClass (),
52- "getTestClass" );
53- Class <?> returnType = testClassMethod .getReturnType ();
54- if (returnType == Class .class ) {
55- return getParameterArrays4_3 ();
56- } else {
57- return getParameterArrays4_4 ();
58- }
59- }
60-
61- private Collection <Object []> getParameterArrays4_3 () throws Exception {
62- Object [][] methodCalls = new Object [][] {new Object [] {"getTestClass" } };
63- Class <?> cl = invokeMethodChain (this , methodCalls );
64- Method [] methods = cl .getMethods ();
65-
66- Method parametersMethod = null ;
67- for (Method method : methods ) {
68- boolean providesParameters = method
69- .isAnnotationPresent (Parameters .class );
70- if (!providesParameters ) {
71- continue ;
72- }
73-
74- if (parametersMethod != null ) {
75- throw new Exception (
76- "Only one method should be annotated with @Labels" );
77- }
78-
79- parametersMethod = method ;
80- }
81-
82- if (parametersMethod == null ) {
83- throw new Exception ("No @Parameters method found" );
84- }
85-
86- @ SuppressWarnings ("unchecked" )
87- Collection <Object []> parameterArrays =
88- (Collection <Object []>) parametersMethod
89- .invoke (null );
90- return parameterArrays ;
91-
92- }
93-
94- private Collection <Object []> getParameterArrays4_4 () throws Exception {
95- Object [][] methodCalls = new Object [][] {
96- new Object [] {"getTestClass" },
97- new Object [] {"getAnnotatedMethods" , Class .class ,
98- Parameters .class },
99- new Object [] {"get" , int .class , 0 },
100- // use array type for varargs (equivalent (almost))
101- new Object [] {"invokeExplosively" , Object .class , null ,
102- Object [].class , new Object [] {} } };
103- Collection <Object []> parameterArrays = invokeMethodChain (this ,
104- methodCalls );
105- return parameterArrays ;
10646 }
10747
10848 @ SuppressWarnings ("unchecked" )
109- private <T > T invokeMethodChain (Object object , final Object [][] methodCalls )
110- throws Exception {
111- for (Object [] methodCall : methodCalls ) {
112- String methodName = (String ) methodCall [0 ];
113- int parameterCount = (methodCall .length - 1 ) / 2 ;
114- Class <?>[] classes = new Class <?>[parameterCount ];
115- Object [] arguments = new Object [parameterCount ];
116- for (int i = 1 ; i < methodCall .length ; i += 2 ) {
117- Class <?> cl = (Class <?>) methodCall [i ];
118- Object argument = methodCall [i + 1 ];
119- int index = (i - 1 ) / 2 ; // messy!
120- classes [index ] = cl ;
121- arguments [index ] = argument ;
122- }
123- Method method = getDeclaredMethod (object .getClass (), methodName ,
124- classes );
125- method .setAccessible (true );
126- object = method .invoke (object , arguments );
127- }
128- return (T ) object ;
129- }
130-
131- // iterates through super-classes until found. Throws NoSuchMethodException
132- // if not
133- private Method getDeclaredMethod (Class <?> cl , final String methodName ,
134- final Class <?>... parameterTypes ) throws NoSuchMethodException {
135- do {
136- try {
137- Method method = cl
138- .getDeclaredMethod (methodName , parameterTypes );
139- return method ;
140- } catch (NoSuchMethodException e ) {
141- // do nothing - just fall through to the below
142- }
143- cl = cl .getSuperclass ();
144- } while (cl != null );
145- throw new NoSuchMethodException ("Method " + methodName
146- + "() not found in hierarchy" );
49+ private Collection <Object []> getParameterArrays4_4 () throws Throwable {
50+ TestClass testClass = this .getTestClass ();
51+ List <FrameworkMethod > annotatedMethods = testClass .getAnnotatedMethods (Parameters .class );
52+ FrameworkMethod frameworkMethod = annotatedMethods .get (0 );
53+ return (Collection <Object []>)frameworkMethod .invokeExplosively (testClass );
14754 }
14855
14956 private void generateLabelledDescription () throws Exception {
15057 Description originalDescription = super .getDescription ();
15158 labelledDescription = Description
15259 .createSuiteDescription (originalDescription .getDisplayName ());
153- ArrayList <Description > childDescriptions = originalDescription
60+ List <Description > childDescriptions = originalDescription
15461 .getChildren ();
15562 int childCount = childDescriptions .size ();
15663 if (childCount != labels .size ()) {
@@ -163,7 +70,7 @@ private void generateLabelledDescription() throws Exception {
16370 String label = labels .get (i );
16471 Description newDescription = Description
16572 .createSuiteDescription (label );
166- ArrayList <Description > grandChildren = childDescription
73+ List <Description > grandChildren = childDescription
16774 .getChildren ();
16875 for (Description grandChild : grandChildren ) {
16976 newDescription .addChild (grandChild );
@@ -176,5 +83,4 @@ private void generateLabelledDescription() throws Exception {
17683 public Description getDescription () {
17784 return labelledDescription ;
17885 }
179-
18086}
0 commit comments