Skip to content

Commit 869d3a4

Browse files
author
Davide Melfi
committed
chore: fixing the UnsafeUtilTest
1 parent ecb0406 commit 869d3a4

File tree

1 file changed

+26
-9
lines changed
  • aws-lambda-java-runtime-interface-client/src/test/java/com/amazonaws/services/lambda/runtime/api/client/util

1 file changed

+26
-9
lines changed

aws-lambda-java-runtime-interface-client/src/test/java/com/amazonaws/services/lambda/runtime/api/client/util/UnsafeUtilTest.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.junit.jupiter.api.Test;
99
import java.lang.reflect.Field;
1010
import static org.junit.jupiter.api.Assertions.*;
11+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
1112

1213
public class UnsafeUtilTest {
1314

@@ -19,7 +20,7 @@ void testTheUnsafeIsInitialized() {
1920
@Test
2021
void testThrowException() {
2122
Exception testException = new Exception("Test exception");
22-
23+
2324
try {
2425
UnsafeUtil.throwException(testException);
2526
fail("Should have thrown an exception");
@@ -32,18 +33,34 @@ void testThrowException() {
3233
@Test
3334
void testDisableIllegalAccessWarning() {
3435
assertDoesNotThrow(() -> UnsafeUtil.disableIllegalAccessWarning());
36+
37+
String specVersion = System.getProperty("java.specification.version");
38+
int version = specVersion.startsWith("1.")
39+
? Integer.parseInt(specVersion.substring(2))
40+
: Integer.parseInt(specVersion);
41+
// we are using assume to skip the test in java8 and java 17+ because the
42+
// IllegalAccessorLogger does not exist there.
43+
assumeTrue(version >= 9 && version < 17,
44+
"IllegalAccessLogger only exists on JDK 9-16");
45+
3546
try {
47+
48+
// We disable the warning log for "jdk.internal.module.IllegalAccessLogger"
49+
UnsafeUtil.disableIllegalAccessWarning();
50+
3651
Class<?> illegalAccessLoggerClass = Class.forName("jdk.internal.module.IllegalAccessLogger");
3752
Field loggerField = illegalAccessLoggerClass.getDeclaredField("logger");
38-
loggerField.setAccessible(true);
39-
Object loggerValue = loggerField.get(null);
53+
54+
// Now we are getting it back with getObjectVolatile and verirfy that the logger
55+
// is null. We are not using default reflection because that will throw because
56+
// that field is private
57+
// defeating the point of the tests.
58+
Object loggerValue = UnsafeUtil.TheUnsafe.getObjectVolatile(
59+
illegalAccessLoggerClass,
60+
UnsafeUtil.TheUnsafe.staticFieldOffset(loggerField));
4061
assertNull(loggerValue);
41-
} catch (ClassNotFoundException e) {
42-
assertTrue(true);
43-
} catch (NoSuchFieldException e) {
44-
assertTrue(true);
45-
} catch (Exception e) {
46-
fail("Unexpected exception: " + e.getMessage());
62+
} catch (ClassNotFoundException | NoSuchFieldException e) {
63+
fail("IllegalAccessLogger should exist on JDK " + version + ": " + e.getMessage());
4764
}
4865
}
4966

0 commit comments

Comments
 (0)