5151import  java .util .Set ;
5252import  java .util .TreeSet ;
5353import  java .util .concurrent .ConcurrentHashMap ;
54+ import  java .util .concurrent .atomic .AtomicBoolean ;
5455import  java .util .function .BiConsumer ;
5556import  java .util .function .BiFunction ;
5657import  java .util .function .Consumer ;
@@ -68,6 +69,7 @@ public class TestEntitlementsRule implements TestRule {
6869
6970    private  static  final  Map <BaseDir , Collection <Path >> BASE_DIR_PATHS  = new  ConcurrentHashMap <>();
7071    private  static  final  TestPolicyManager  POLICY_MANAGER ;
72+     private  static  final  AtomicBoolean  active  = new  AtomicBoolean (false );
7173
7274    static  {
7375        PathLookup  pathLookup  = new  TestPathLookup (BASE_DIR_PATHS );
@@ -103,25 +105,30 @@ public Statement apply(Statement base, Description description) {
103105            return  new  Statement () {
104106                @ Override 
105107                public  void  evaluate () throws  Throwable  {
106-                     try  {
107-                         POLICY_MANAGER .setActive (false  == withoutEntitlements );
108-                         POLICY_MANAGER .setTriviallyAllowingTestCode (false  == withEntitlementsOnTestCode );
109-                         if  (entitledPackages  != null ) {
110-                             assert  entitledPackages .value ().length  > 0  : "No test packages specified in @EntitledTestPackages" ;
111-                             POLICY_MANAGER .setEntitledTestPackages (entitledPackages .value ());
112-                         } else  {
108+                     if  (active .compareAndSet (false , true )) {
109+                         try  {
110+                             POLICY_MANAGER .setActive (false  == withoutEntitlements );
111+                             POLICY_MANAGER .setTriviallyAllowingTestCode (false  == withEntitlementsOnTestCode );
112+                             if  (entitledPackages  != null ) {
113+                                 assert  entitledPackages .value ().length  > 0  : "No test packages specified in @EntitledTestPackages" ;
114+                                 POLICY_MANAGER .setEntitledTestPackages (entitledPackages .value ());
115+                             } else  {
116+                                 POLICY_MANAGER .setEntitledTestPackages ();
117+                             }
118+                             BASE_DIR_PATHS .keySet ().retainAll (List .of (TEMP ));
119+                             POLICY_MANAGER .clearModuleEntitlementsCache ();
120+                             // evaluate the suite 
121+                             base .evaluate ();
122+                         } finally  {
123+                             POLICY_MANAGER .setActive (false );
124+                             POLICY_MANAGER .setTriviallyAllowingTestCode (true );
113125                            POLICY_MANAGER .setEntitledTestPackages ();
126+                             BASE_DIR_PATHS .keySet ().retainAll (List .of (TEMP ));
127+                             POLICY_MANAGER .clearModuleEntitlementsCache ();
128+                             active .set (false );
114129                        }
115-                         BASE_DIR_PATHS .keySet ().retainAll (List .of (TEMP ));
116-                         POLICY_MANAGER .clearModuleEntitlementsCache ();
117-                         // evaluate the suite 
118-                         base .evaluate ();
119-                     } finally  {
120-                         POLICY_MANAGER .setActive (false );
121-                         POLICY_MANAGER .setTriviallyAllowingTestCode (true );
122-                         POLICY_MANAGER .setEntitledTestPackages ();
123-                         BASE_DIR_PATHS .keySet ().retainAll (List .of (TEMP ));
124-                         POLICY_MANAGER .clearModuleEntitlementsCache ();
130+                     } else  {
131+                         throw  new  AssertionError ("TestPolicyManager doesn't support test isolation, test suits cannot be run in parallel" );
125132                    }
126133                }
127134            };
0 commit comments