4
4
5
5
import java .io .File ;
6
6
import java .lang .reflect .Field ;
7
+ import java .lang .reflect .InvocationTargetException ;
8
+ import java .lang .reflect .Method ;
9
+ import java .util .ArrayList ;
7
10
import java .util .Arrays ;
11
+ import java .util .List ;
8
12
import java .util .Objects ;
13
+ import java .util .stream .Collectors ;
9
14
15
+ import static org .assertj .core .api .Assertions .assertThat ;
10
16
import static org .junit .Assert .assertEquals ;
11
17
import static org .junit .Assert .assertFalse ;
12
18
@@ -15,35 +21,59 @@ public class EnumsTest {
15
21
static String ARRAY_VALUE = "idToEnum" ;
16
22
static String ID_VALUE = "id" ;
17
23
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
+ }
18
37
/*
19
38
* Check, via reflection, that all Enums that have an "idToEnum" array to be of correct lenght
20
39
*/
21
40
@ 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 ();
47
77
}
48
78
}
49
79
}
0 commit comments