29
29
import java .util .Map ;
30
30
import java .util .Map .Entry ;
31
31
import java .util .Set ;
32
- import java .util .function .Function ;
32
+ import java .util .function .Predicate ;
33
33
import java .util .stream .Collectors ;
34
- import java .util .stream .Stream ;
35
34
36
35
import static java .util .Map .entry ;
37
36
import static org .elasticsearch .entitlement .qa .test .EntitlementTest .ExpectedAccess .ALWAYS_ALLOWED ;
@@ -49,29 +48,36 @@ record CheckAction(
49
48
Integer fromJavaVersion
50
49
) {}
51
50
52
- private static final Map <String , CheckAction > checkActions = Stream .of (
53
- getTestEntries (FileCheckActions .class ),
54
- getTestEntries (FileStoreActions .class ),
55
- getTestEntries (JvmActions .class ),
56
- getTestEntries (LoadNativeLibrariesCheckActions .class ),
57
- getTestEntries (ManageThreadsActions .class ),
58
- getTestEntries (NativeActions .class ),
59
- getTestEntries (NetworkAccessCheckActions .class ),
60
- getTestEntries (NioChannelsActions .class ),
61
- getTestEntries (NioFilesActions .class ),
62
- getTestEntries (NioFileSystemActions .class ),
63
- getTestEntries (OperatingSystemActions .class ),
64
- getTestEntries (PathActions .class ),
65
- getTestEntries (SpiActions .class ),
66
- getTestEntries (SystemActions .class ),
67
- getTestEntries (URLConnectionFileActions .class ),
68
- getTestEntries (URLConnectionNetworkActions .class ),
69
- getTestEntries (VersionSpecificManageThreadsActions .class ),
70
- getTestEntries (VersionSpecificNioFileSystemActions .class )
71
- )
72
- .flatMap (Function .identity ())
73
- .filter (entry -> entry .getValue ().fromJavaVersion () == null || Runtime .version ().feature () >= entry .getValue ().fromJavaVersion ())
74
- .collect (Collectors .toUnmodifiableMap (Entry ::getKey , Entry ::getValue ));
51
+ private static final Map <String , CheckAction > checkActions = collectTests (
52
+ FileCheckActions .class ,
53
+ FileStoreActions .class ,
54
+ JvmActions .class ,
55
+ LoadNativeLibrariesCheckActions .class ,
56
+ ManageThreadsActions .class ,
57
+ NativeActions .class ,
58
+ NetworkAccessCheckActions .class ,
59
+ NioChannelsActions .class ,
60
+ NioFilesActions .class ,
61
+ NioFileSystemActions .class ,
62
+ OperatingSystemActions .class ,
63
+ PathActions .class ,
64
+ SpiActions .class ,
65
+ SystemActions .class ,
66
+ URLConnectionFileActions .class ,
67
+ URLConnectionNetworkActions .class ,
68
+ VersionSpecificManageThreadsActions .class ,
69
+ VersionSpecificNioFileSystemActions .class
70
+ );
71
+
72
+ private static Map <String , CheckAction > collectTests (Class <?>... testClasses ) {
73
+ List <Entry <String , CheckAction >> entries = new ArrayList <>();
74
+ for (Class <?> testClass : testClasses ) {
75
+ getTestEntries (entries , testClass , a -> a .fromJavaVersion () == null || Runtime .version ().feature () >= a .fromJavaVersion ());
76
+ }
77
+ @ SuppressWarnings ({ "unchecked" , "rawtypes" })
78
+ Entry <String , CheckAction >[] entriesArray = entries .toArray (new Entry [0 ]);
79
+ return Map .ofEntries (entriesArray );
80
+ }
75
81
76
82
private final Environment environment ;
77
83
@@ -84,8 +90,7 @@ private static Method[] getDeclaredMethods(Class<?> clazz) {
84
90
return clazz .getDeclaredMethods ();
85
91
}
86
92
87
- private static Stream <Entry <String , CheckAction >> getTestEntries (Class <?> actionsClass ) {
88
- List <Entry <String , CheckAction >> entries = new ArrayList <>();
93
+ private static void getTestEntries (List <Entry <String , CheckAction >> entries , Class <?> actionsClass , Predicate <CheckAction > filter ) {
89
94
for (var method : getDeclaredMethods (actionsClass )) {
90
95
var testAnnotation = method .getAnnotation (EntitlementTest .class );
91
96
if (testAnnotation == null ) {
@@ -94,6 +99,9 @@ private static Stream<Entry<String, CheckAction>> getTestEntries(Class<?> action
94
99
if (Modifier .isStatic (method .getModifiers ()) == false ) {
95
100
throw new AssertionError ("Entitlement test method [" + method + "] must be static" );
96
101
}
102
+ if (Modifier .isPrivate (method .getModifiers ())) {
103
+ throw new AssertionError ("Entitlement test method [" + method + "] must not be private" );
104
+ }
97
105
final CheckedConsumer <Environment , Exception > call = createConsumerForMethod (method );
98
106
CheckedConsumer <Environment , Exception > runnable = env -> {
99
107
try {
@@ -109,14 +117,16 @@ private static Stream<Entry<String, CheckAction>> getTestEntries(Class<?> action
109
117
}
110
118
};
111
119
Integer fromJavaVersion = testAnnotation .fromJavaVersion () == -1 ? null : testAnnotation .fromJavaVersion ();
112
- entries . add (
113
- entry (
114
- method . getName (),
115
- new CheckAction ( runnable , testAnnotation .expectedAccess (), testAnnotation . expectedExceptionIfDenied (), fromJavaVersion )
116
- )
120
+ var checkAction = new CheckAction (
121
+ runnable ,
122
+ testAnnotation . expectedAccess (),
123
+ testAnnotation .expectedExceptionIfDenied (),
124
+ fromJavaVersion
117
125
);
126
+ if (filter .test (checkAction )) {
127
+ entries .add (entry (method .getName (), checkAction ));
128
+ }
118
129
}
119
- return entries .stream ();
120
130
}
121
131
122
132
private static CheckedConsumer <Environment , Exception > createConsumerForMethod (Method method ) {
0 commit comments