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,27 +48,34 @@ 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- )
70- .flatMap (Function .identity ())
71- .filter (entry -> entry .getValue ().fromJavaVersion () == null || Runtime .version ().feature () >= entry .getValue ().fromJavaVersion ())
72- .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+ );
69+
70+ private static Map <String , CheckAction > collectTests (Class <?>... testClasses ) {
71+ List <Entry <String , CheckAction >> entries = new ArrayList <>();
72+ for (Class <?> testClass : testClasses ) {
73+ getTestEntries (entries , testClass , a -> a .fromJavaVersion () == null || Runtime .version ().feature () >= a .fromJavaVersion ());
74+ }
75+ @ SuppressWarnings ({ "unchecked" , "rawtypes" })
76+ Entry <String , CheckAction >[] entriesArray = entries .toArray (new Entry [0 ]);
77+ return Map .ofEntries (entriesArray );
78+ }
7379
7480 private final Environment environment ;
7581
@@ -82,8 +88,7 @@ private static Method[] getDeclaredMethods(Class<?> clazz) {
8288 return clazz .getDeclaredMethods ();
8389 }
8490
85- private static Stream <Entry <String , CheckAction >> getTestEntries (Class <?> actionsClass ) {
86- List <Entry <String , CheckAction >> entries = new ArrayList <>();
91+ private static void getTestEntries (List <Entry <String , CheckAction >> entries , Class <?> actionsClass , Predicate <CheckAction > filter ) {
8792 for (var method : getDeclaredMethods (actionsClass )) {
8893 var testAnnotation = method .getAnnotation (EntitlementTest .class );
8994 if (testAnnotation == null ) {
@@ -92,6 +97,9 @@ private static Stream<Entry<String, CheckAction>> getTestEntries(Class<?> action
9297 if (Modifier .isStatic (method .getModifiers ()) == false ) {
9398 throw new AssertionError ("Entitlement test method [" + method + "] must be static" );
9499 }
100+ if (Modifier .isPrivate (method .getModifiers ())) {
101+ throw new AssertionError ("Entitlement test method [" + method + "] must not be private" );
102+ }
95103 final CheckedConsumer <Environment , Exception > call = createConsumerForMethod (method );
96104 CheckedConsumer <Environment , Exception > runnable = env -> {
97105 try {
@@ -107,14 +115,16 @@ private static Stream<Entry<String, CheckAction>> getTestEntries(Class<?> action
107115 }
108116 };
109117 Integer fromJavaVersion = testAnnotation .fromJavaVersion () == -1 ? null : testAnnotation .fromJavaVersion ();
110- entries . add (
111- entry (
112- method . getName (),
113- new CheckAction ( runnable , testAnnotation .expectedAccess (), testAnnotation . expectedExceptionIfDenied (), fromJavaVersion )
114- )
118+ var checkAction = new CheckAction (
119+ runnable ,
120+ testAnnotation . expectedAccess (),
121+ testAnnotation .expectedExceptionIfDenied (),
122+ fromJavaVersion
115123 );
124+ if (filter .test (checkAction )) {
125+ entries .add (entry (method .getName (), checkAction ));
126+ }
116127 }
117- return entries .stream ();
118128 }
119129
120130 private static CheckedConsumer <Environment , Exception > createConsumerForMethod (Method method ) {
0 commit comments