44
55import java .io .File ;
66import java .lang .reflect .Field ;
7+ import java .lang .reflect .InvocationTargetException ;
8+ import java .lang .reflect .Method ;
9+ import java .util .ArrayList ;
710import java .util .Arrays ;
11+ import java .util .List ;
812import java .util .Objects ;
13+ import java .util .stream .Collectors ;
914
15+ import static org .assertj .core .api .Assertions .assertThat ;
1016import static org .junit .Assert .assertEquals ;
1117import static org .junit .Assert .assertFalse ;
1218
@@ -15,35 +21,59 @@ public class EnumsTest {
1521 static String ARRAY_VALUE = "idToEnum" ;
1622 static String ID_VALUE = "id" ;
1723
24+
25+ static List <Class <?>> getBWAPIEnums () throws ClassNotFoundException {
26+ List <Class <?>> enums = new ArrayList <>();
27+ for (File file : Objects .requireNonNull (new File (CLASSES_LOCATION ).listFiles ())) {
28+ if (file .isFile ()) {
29+ Class <?> cls = Class .forName ("bwapi." + file .getName ().replace (".java" , "" ));
30+ if (cls .isEnum ()) {
31+ enums .add (cls );
32+ }
33+ }
34+ }
35+ return enums ;
36+ }
1837 /*
1938 * Check, via reflection, that all Enums that have an "idToEnum" array to be of correct lenght
2039 */
2140 @ Test
22- public void checkAllEnums () throws Exception {
23- for (File file : Objects .requireNonNull (new File (CLASSES_LOCATION ).listFiles ())) {
24- if (file .isFile ()) {
25- Class <?> cls = Class .forName ("bwapi." + file .getName ().replace (".java" , "" ));
26- if (cls .isEnum () && Arrays .stream (cls .getDeclaredFields ()).anyMatch (f -> f .getName ().equals (ARRAY_VALUE ))) {
27- Field field = cls .getDeclaredField (ARRAY_VALUE );
28- assertFalse (field .isAccessible ());
29- field .setAccessible (true );
30-
31- int arrayLength = ((Object [])field .get (null )).length ;
32- int maxEnumVal = Arrays .stream ((Object []) cls .getDeclaredMethod ("values" ).invoke (null ))
33- .mapToInt (obj -> {
34- try {
35- Field f = obj .getClass ().getDeclaredField (ID_VALUE );
36- assertFalse (f .isAccessible ());
37- f .setAccessible (true );
38- return f .getInt (obj );
39- } catch (Exception e ) {
40- e .printStackTrace ();
41- return -1 ;
42- }
43- })
44- .max ()
45- .orElse (-1 );
46- assertEquals (arrayLength , maxEnumVal + 1 );
41+ public void checkAllidToEnumsArrayLenghts () throws Exception {
42+ for (Class <?> cls : getBWAPIEnums ()) {
43+ if (Arrays .stream (cls .getDeclaredFields ()).anyMatch (f -> f .getName ().equals (ARRAY_VALUE ))) {
44+ Field field = cls .getDeclaredField (ARRAY_VALUE );
45+ assertFalse (field .isAccessible ());
46+ field .setAccessible (true );
47+
48+ int arrayLength = ((Object [])field .get (null )).length ;
49+ int maxEnumVal = Integer .MIN_VALUE ;
50+ for (Object obj : (Object []) cls .getDeclaredMethod ("values" ).invoke (null )) {
51+ Field f = obj .getClass ().getDeclaredField (ID_VALUE );
52+ assertFalse (f .isAccessible ());
53+ f .setAccessible (true );
54+ int val = f .getInt (obj );
55+ if (val > maxEnumVal ) {
56+ maxEnumVal = val ;
57+ }
58+ }
59+ assertEquals (arrayLength , maxEnumVal + 1 );
60+ }
61+ }
62+ }
63+
64+ @ Test
65+ public void ensureSimpleGettersReturnNonNullAndDontFail () throws ClassNotFoundException , InvocationTargetException , IllegalAccessException {
66+ for (Class <?> cls : getBWAPIEnums ()) {
67+ List <Method > simpleGetters = Arrays .stream (cls .getMethods ())
68+ .filter (it -> it .getParameterCount () == 0 && it .getReturnType () != Void .TYPE )
69+ .collect (Collectors .toList ());
70+ for (Object type : cls .getEnumConstants ()) {
71+ for (Method getter : simpleGetters ) {
72+ // WHEN
73+ Object result = getter .invoke (type );
74+
75+ // THEN
76+ assertThat (result ).describedAs ("When calling " + getter .getName ()).isNotNull ();
4777 }
4878 }
4979 }
0 commit comments