Skip to content

Commit 1a5f2bc

Browse files
author
Davide Melfi
committed
chore: unsafeutil
1 parent 869d3a4 commit 1a5f2bc

File tree

1 file changed

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

1 file changed

+19
-32
lines changed

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

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
package com.amazonaws.services.lambda.runtime.api.client.util;
77

88
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.condition.EnabledForJreRange;
10+
import org.junit.jupiter.api.condition.JRE;
911
import java.lang.reflect.Field;
1012
import static org.junit.jupiter.api.Assertions.*;
11-
import static org.junit.jupiter.api.Assumptions.assumeTrue;
1213

1314
public class UnsafeUtilTest {
1415

@@ -30,38 +31,24 @@ void testThrowException() {
3031
}
3132
}
3233

34+
// IllegalAccessLogger only exists on JDK 9-16; skipped on JDK 8 (no module
35+
// system) and JDK 17+ (class removed).
3336
@Test
34-
void testDisableIllegalAccessWarning() {
35-
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-
46-
try {
47-
48-
// We disable the warning log for "jdk.internal.module.IllegalAccessLogger"
49-
UnsafeUtil.disableIllegalAccessWarning();
50-
51-
Class<?> illegalAccessLoggerClass = Class.forName("jdk.internal.module.IllegalAccessLogger");
52-
Field loggerField = illegalAccessLoggerClass.getDeclaredField("logger");
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));
61-
assertNull(loggerValue);
62-
} catch (ClassNotFoundException | NoSuchFieldException e) {
63-
fail("IllegalAccessLogger should exist on JDK " + version + ": " + e.getMessage());
64-
}
37+
@EnabledForJreRange(min = JRE.JAVA_9, max = JRE.JAVA_16)
38+
void testDisableIllegalAccessWarning() throws Exception {
39+
// We disable the warning log for "jdk.internal.module.IllegalAccessLogger"
40+
UnsafeUtil.disableIllegalAccessWarning();
41+
42+
Class<?> illegalAccessLoggerClass = Class.forName("jdk.internal.module.IllegalAccessLogger");
43+
Field loggerField = illegalAccessLoggerClass.getDeclaredField("logger");
44+
45+
// Now we are getting it back with getObjectVolatile and verify that the logger
46+
// is null. We are not using default reflection because that will throw because
47+
// that field is private, defeating the point of the test.
48+
Object loggerValue = UnsafeUtil.TheUnsafe.getObjectVolatile(
49+
illegalAccessLoggerClass,
50+
UnsafeUtil.TheUnsafe.staticFieldOffset(loggerField));
51+
assertNull(loggerValue);
6552
}
6653

6754
@Test

0 commit comments

Comments
 (0)