66import org .hamcrest .Matcher ;
77
88import java .io .IOException ;
9+ import java .util .ArrayList ;
910import java .util .Collection ;
11+ import java .util .Collections ;
1012import java .util .Comparator ;
1113import java .util .Iterator ;
1214import java .util .List ;
@@ -31,20 +33,18 @@ public static Matcher<String> secondMethod() {
3133 }
3234
3335 public void testIteratesOverFactoryMethods () {
34- Iterable <FactoryMethod > reader = new ReflectiveFactoryReader (SimpleSetOfMatchers .class );
35- Iterator <FactoryMethod > methods = reader .iterator ();
36-
37- assertTrue ("Expected first method" , methods .hasNext ());
38- FactoryMethod firstMethod = methods .next ();
39- assertEquals ("firstMethod" , firstMethod .getName ());
40- assertEquals (SimpleSetOfMatchers .class .getName ().replace ('$' , '.' ), firstMethod .getMatcherClass ());
41-
42- assertTrue ("Expected second method" , methods .hasNext ());
43- FactoryMethod secondMethod = methods .next ();
44- assertEquals ("secondMethod" , secondMethod .getName ());
45- assertEquals (SimpleSetOfMatchers .class .getName ().replace ('$' , '.' ), secondMethod .getMatcherClass ());
46-
47- assertFalse ("Expected no more methods" , methods .hasNext ());
36+ final ReflectiveFactoryReader reader = new ReflectiveFactoryReader (SimpleSetOfMatchers .class );
37+ final List <FactoryMethod > methods = methodsReadBy (reader );
38+
39+ assertEquals (2 , methods .size ());
40+
41+ final String expectedClass = SimpleSetOfMatchers .class .getName ().replace ('$' , '.' );
42+
43+ assertEquals ("firstMethod" , methods .get (0 ).getName ());
44+ assertEquals (expectedClass , methods .get (0 ).getMatcherClass ());
45+
46+ assertEquals ("secondMethod" , methods .get (1 ).getName ());
47+ assertEquals (expectedClass , methods .get (1 ).getMatcherClass ());
4848 }
4949
5050 public static class MatchersWithDodgySignatures {
@@ -80,16 +80,12 @@ public static void wrongReturnType() {
8080 }
8181
8282 public void testOnlyReadsPublicStaticAnnotatedMethodsThatReturnNonVoid () {
83- Iterable < FactoryMethod > reader = new ReflectiveFactoryReader (MatchersWithDodgySignatures .class );
84- Iterator <FactoryMethod > methods = reader . iterator ( );
83+ final ReflectiveFactoryReader reader = new ReflectiveFactoryReader (MatchersWithDodgySignatures .class );
84+ final List <FactoryMethod > methods = methodsReadBy ( reader );
8585
86- assertTrue ("Expected first method" , methods .hasNext ());
87- assertEquals ("goodMethod" , methods .next ().getName ());
88-
89- assertTrue ("Expected second method" , methods .hasNext ());
90- assertEquals ("anotherGoodMethod" , methods .next ().getName ());
91-
92- assertFalse ("Expected no more methods" , methods .hasNext ());
86+ assertEquals (2 , methods .size ());
87+ assertEquals ("anotherGoodMethod" , methods .get (0 ).getName ());
88+ assertEquals ("goodMethod" , methods .get (1 ).getName ());
9389 }
9490
9591 public static class GenerifiedMatchers {
@@ -173,10 +169,15 @@ public void testReadsArrayAndVarArgParameterTypes() {
173169
174170 public void testReadsGenerifiedParameterTypes () {
175171 FactoryMethod method = readMethod (ParameterizedMatchers .class , "withGenerifiedParam" );
172+
176173 assertEquals ("java.util.Collection<? extends java.lang.Comparable<java.lang.String>>" ,
177- method .getParameters ().get (0 ).getType ());
178- assertEquals ("java.util.Set<java.lang.String[]>[]" ,
179- method .getParameters ().get (1 ).getType ());
174+ method .getParameters ().get (0 ).getType ());
175+
176+ String expected = System .getProperty ("java.version" ).startsWith ("1.7." )
177+ ? "java.util.Set<[Ljava.lang.String;>[]"
178+ : "java.util.Set<java.lang.String[]>[]" ;
179+
180+ assertEquals (expected , method .getParameters ().get (1 ).getType ());
180181 }
181182
182183 public void testCannotReadParameterNamesSoMakesThemUpInstead () {
@@ -257,6 +258,19 @@ public void testCatchesSubclasses() {
257258 assertNotNull (readMethod (SubclassOfMatcher .class , "subclassMethod" ));
258259 }
259260
261+ private static List <FactoryMethod > methodsReadBy (final ReflectiveFactoryReader reader ) {
262+ final List <FactoryMethod > extractedMethods = new ArrayList <FactoryMethod >();
263+ for (FactoryMethod factoryMethod : reader ) {
264+ extractedMethods .add (factoryMethod );
265+ }
266+ Collections .sort (extractedMethods , new Comparator <FactoryMethod >() {
267+ @ Override public int compare (FactoryMethod o1 , FactoryMethod o2 ) {
268+ return o1 .getName ().compareTo (o2 .getName ());
269+ }
270+ });
271+ return extractedMethods ;
272+ }
273+
260274 private static FactoryMethod readMethod (Class <?> cls , String methodName ) {
261275 for (FactoryMethod method : new ReflectiveFactoryReader (cls )) {
262276 if (method .getName ().equals (methodName )) {
@@ -265,5 +279,4 @@ private static FactoryMethod readMethod(Class<?> cls, String methodName) {
265279 }
266280 return null ;
267281 }
268-
269282}
0 commit comments