Skip to content
Permalink
Browse files
JDK18 test fixes - setting security manager is an UnsupportedOperatio…
…nException from 18 onwards as part of JEP-411
  • Loading branch information
paulk-asert committed Feb 4, 2022
1 parent 923f680 commit f150b33d3b3388de3b43ca4d30c94aad2139689e
Showing 1 changed file with 13 additions and 3 deletions.
@@ -138,19 +138,22 @@ private CachedField createCachedField(String name) throws Exception {
}

public void testInvokesPublicMethodsWithoutChecks() throws Exception {
if (isAtLeastJdk("18.0")) return;
cachedMethodUnderTest = createCachedMethod("publicMethod");
System.setSecurityManager(restrictiveSecurityManager);
assertTrue(invokesCachedMethod());
}

public void testReturnsAccesiblePublicMethodsWithoutChecks() throws Exception {
if (isAtLeastJdk("18.0")) return;
cachedMethodUnderTest = createCachedMethod("publicMethod");
System.setSecurityManager(restrictiveSecurityManager);
assertEquals("publicMethod", cachedMethodUnderTest.setAccessible().getName());
assertEquals("publicMethod", cachedMethodUnderTest.getName());
}

public void testAccessesPublicFieldsWithoutChecks() throws Exception {
if (isAtLeastJdk("18.0")) return;
cachedFieldUnderTest = createCachedField("publicField");
System.setSecurityManager(restrictiveSecurityManager);
TestClass object = new TestClass();
@@ -164,6 +167,7 @@ public void testInvokesPrivateMethodsWithoutSecurityManager() throws Exception{
}

public void testAccessesPrivateFieldsWithoutSecurityManager() throws Exception {
if (isAtLeastJdk("18.0")) return;
cachedFieldUnderTest = createCachedField("privateField");
System.setSecurityManager(null);
TestClass object = new TestClass();
@@ -172,13 +176,15 @@ public void testAccessesPrivateFieldsWithoutSecurityManager() throws Exception {
}

public void testReturnsAccesiblePrivateMethodsWithoutSecurityManager() throws Exception {
if (isAtLeastJdk("18.0")) return;
cachedMethodUnderTest = createCachedMethod("privateMethod");
System.setSecurityManager(null);
assertEquals("privateMethod", cachedMethodUnderTest.setAccessible().getName());
assertEquals("privateMethod", cachedMethodUnderTest.getName());
}

public void testChecksReflectPermissionForInvokeOnPrivateMethods() throws Exception {
if (isAtLeastJdk("18.0")) return;
cachedMethodUnderTest = createCachedMethod("privateMethod");
System.setSecurityManager(restrictiveSecurityManager);
try {
@@ -191,6 +197,7 @@ public void testChecksReflectPermissionForInvokeOnPrivateMethods() throws Except
}

public void testChecksReflectPermissionForFieldAccessOnPrivateFields() throws Exception {
if (isAtLeastJdk("18.0")) return;
cachedFieldUnderTest = createCachedField("privateField");
System.setSecurityManager(restrictiveSecurityManager);
TestClass object = new TestClass();
@@ -210,6 +217,7 @@ public void testChecksReflectPermissionForFieldAccessOnPrivateFields() throws Ex
}

public void testChecksReflectPermissionForMethodAccessOnPrivateMethods() throws Exception {
if (isAtLeastJdk("18.0")) return;
cachedMethodUnderTest = createCachedMethod("privateMethod");
System.setSecurityManager(restrictiveSecurityManager);
try {
@@ -228,16 +236,15 @@ public void testChecksReflectPermissionForMethodAccessOnPrivateMethods() throws
}

public void testInvokesPackagePrivateMethodsWithoutChecksInNonRestrictedPackages() throws Exception {
if (isAtLeastJdk("18.0")) return;
cachedMethodUnderTest = createCachedMethod("packagePrivateMethod");
System.setSecurityManager(restrictiveSecurityManager);
assertTrue(invokesCachedMethod());
}

public void testChecksReflectPermissionForInvokeOnPackagePrivateMethodsInRestrictedJavaPackages() throws Exception {
// FIX_JDK9 remove this exemption for JDK9
if (isAtLeastJdk("9.0")) {
return;
}
if (isAtLeastJdk("9.0")) return;
cachedMethodUnderTest = createCachedMethod(ClassLoader.class, "getBootstrapClassPath");
System.setSecurityManager(restrictiveSecurityManager);

@@ -251,6 +258,7 @@ public void testChecksReflectPermissionForInvokeOnPackagePrivateMethodsInRestric
}

public void testInvokesProtectedMethodsWithoutChecks() throws Exception {
if (isAtLeastJdk("18.0")) return;
cachedMethodUnderTest = createCachedMethod("protectedMethod");
System.setSecurityManager(restrictiveSecurityManager);
assertTrue(invokesCachedMethod());
@@ -278,6 +286,7 @@ public void testChecksCreateClassLoaderPermissionForClassLoaderProtectedMethodAc
}

public void testInvokesPrivateMethodsInGroovyObjectsWithoutChecks() throws Exception {
if (isAtLeastJdk("18.0")) return;
cachedMethodUnderTest = createCachedMethod(TestGroovyClass.class, "privateMethod");
TestGroovyClass object = new TestGroovyClass();
System.setSecurityManager(restrictiveSecurityManager);
@@ -286,6 +295,7 @@ public void testInvokesPrivateMethodsInGroovyObjectsWithoutChecks() throws Excep
}

public void testAccessesPrivateFieldsInGroovyObjectsWithoutChecks() throws Exception {
if (isAtLeastJdk("18.0")) return;
Field field = TestGroovyClass.class.getDeclaredField("privateField");
field.setAccessible(true);
cachedFieldUnderTest = new CachedField(field);

0 comments on commit f150b33

Please sign in to comment.