< prev index next >
test/java/lang/SecurityManager/CheckPackageAccess.java
Print this page
@@ -31,18 +31,20 @@
* @run main/othervm CheckPackageAccess
*/
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
+import java.lang.reflect.Layer;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class CheckPackageAccess {
private static final SecurityManager sm = new SecurityManager();
private static final ModuleFinder mf = ModuleFinder.ofSystem();
+ private static final String MODULE_TEXT = "Testing module: %1$s. Module is%2$s present.";
/*
* The expected list of restricted packages of the package.access property.
*
* This array should be updated whenever new packages are added to the
@@ -78,11 +80,14 @@
this.qualOpens = Optional.ofNullable(qualOpens);
this.qualOpensAndExports = Optional.ofNullable(qualOpensAndExports);
}
void test() {
- System.out.println("Testing module " + moduleName);
+ final boolean isModulePresent = Layer.boot().findModule(moduleName).isPresent();
+ System.out.println(String.format(MODULE_TEXT, moduleName, isModulePresent ? "" : " NOT"));
+
+ if (isModulePresent) {
// access to exported pkg should pass
testNonRestricted(exports);
// access to opened pkg should pass
@@ -97,10 +102,13 @@
// access to qualified open pkg should fail
qualOpens.ifPresent(Test::testRestricted);
// access to qualified opened pkg that is also exported should pass
qualOpensAndExports.ifPresent(Test::testNonRestricted);
+ } else {
+ System.out.println("Skipping tests for module.");
+ }
}
private static void testRestricted(String pkg) {
try {
sm.checkPackageAccess(pkg);
< prev index next >