2929import java .util .Map ;
3030import java .util .Map .Entry ;
3131import java .util .Set ;
32- import java .util .function .Function ;
32+ import java .util .function .Predicate ;
3333import java .util .stream .Collectors ;
34- import java .util .stream .Stream ;
3534
3635import static java .util .Map .entry ;
3736import static org .elasticsearch .entitlement .qa .test .EntitlementTest .ExpectedAccess .ALWAYS_ALLOWED ;
@@ -49,29 +48,36 @@ record CheckAction(
4948 Integer fromJavaVersion
5049 ) {}
5150
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+ }
7581
7682 private final Environment environment ;
7783
@@ -84,8 +90,7 @@ private static Method[] getDeclaredMethods(Class<?> clazz) {
8490 return clazz .getDeclaredMethods ();
8591 }
8692
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 ) {
8994 for (var method : getDeclaredMethods (actionsClass )) {
9095 var testAnnotation = method .getAnnotation (EntitlementTest .class );
9196 if (testAnnotation == null ) {
@@ -94,6 +99,9 @@ private static Stream<Entry<String, CheckAction>> getTestEntries(Class<?> action
9499 if (Modifier .isStatic (method .getModifiers ()) == false ) {
95100 throw new AssertionError ("Entitlement test method [" + method + "] must be static" );
96101 }
102+ if (Modifier .isPrivate (method .getModifiers ())) {
103+ throw new AssertionError ("Entitlement test method [" + method + "] must not be private" );
104+ }
97105 final CheckedConsumer <Environment , Exception > call = createConsumerForMethod (method );
98106 CheckedConsumer <Environment , Exception > runnable = env -> {
99107 try {
@@ -109,14 +117,16 @@ private static Stream<Entry<String, CheckAction>> getTestEntries(Class<?> action
109117 }
110118 };
111119 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
117125 );
126+ if (filter .test (checkAction )) {
127+ entries .add (entry (method .getName (), checkAction ));
128+ }
118129 }
119- return entries .stream ();
120130 }
121131
122132 private static CheckedConsumer <Environment , Exception > createConsumerForMethod (Method method ) {
0 commit comments