Skip to content

Commit ce65ed6

Browse files
author
Davide Melfi
committed
test: fix tests that are failing after introducint enum matrix
1 parent c093beb commit ce65ed6

3 files changed

Lines changed: 61 additions & 47 deletions

File tree

.github/workflows/aws-lambda-java-tests.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,14 @@ on:
99
branches: [ main ]
1010
paths:
1111
- 'aws-lambda-java-tests/**'
12-
<<<<<<< Updated upstream
13-
- 'aws-lambda-java-runtime-interface-client/**'
14-
- 'aws-lambda-java-serialization/**'
15-
- 'aws-lambda-java-events/**'
16-
=======
1712
- 'aws-lambda-java-events/**'
1813
- 'aws-lambda-java-serialization/**'
19-
>>>>>>> Stashed changes
2014
pull_request:
2115
branches: [ '*' ]
2216
paths:
2317
- 'aws-lambda-java-tests/**'
24-
<<<<<<< Updated upstream
25-
- 'aws-lambda-java-runtime-interface-client/**'
26-
- 'aws-lambda-java-serialization/**'
27-
- 'aws-lambda-java-events/**'
28-
=======
2918
- 'aws-lambda-java-events/**'
3019
- 'aws-lambda-java-serialization/**'
31-
>>>>>>> Stashed changes
3220
- '.github/workflows/aws-lambda-java-tests.yml'
3321

3422
permissions:

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

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
package com.amazonaws.services.lambda.runtime.api.client;
77

88
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.condition.DisabledForJreRange;
10+
import org.junit.jupiter.api.condition.EnabledForJreRange;
11+
import org.junit.jupiter.api.condition.JRE;
912
import org.junit.jupiter.api.io.TempDir;
1013
import java.io.File;
1114
import java.io.FileNotFoundException;
1215
import java.io.FileOutputStream;
1316
import java.io.IOException;
1417
import java.nio.file.Path;
15-
import java.util.Collections;
16-
import java.util.Enumeration;
1718
import java.util.jar.JarEntry;
18-
import java.util.jar.JarFile;
1919
import java.util.jar.JarOutputStream;
2020

2121
import static org.junit.jupiter.api.Assertions.*;
@@ -27,21 +27,38 @@ void testLoadAllClassesWithNoClasspath() throws IOException {
2727
String originalClasspath = System.getProperty("java.class.path");
2828
try {
2929
System.clearProperty("java.class.path");
30-
ClasspathLoader.main(new String[]{});
30+
ClasspathLoader.main(new String[] {});
3131
} finally {
3232
if (originalClasspath != null) {
3333
System.setProperty("java.class.path", originalClasspath);
3434
}
3535
}
3636
}
3737

38+
// On JDK 8-24, new File("").exists() returns false → FileNotFoundException.
3839
@Test
40+
@DisabledForJreRange(min = JRE.JAVA_25)
3941
void testLoadAllClassesWithEmptyClasspath() {
4042
String originalClasspath = System.getProperty("java.class.path");
4143
try {
4244
System.setProperty("java.class.path", "");
43-
assertThrows(FileNotFoundException.class, () ->
44-
ClasspathLoader.main(new String[]{}));
45+
assertThrows(FileNotFoundException.class, () -> ClasspathLoader.main(new String[] {}));
46+
} finally {
47+
if (originalClasspath != null) {
48+
System.setProperty("java.class.path", originalClasspath);
49+
}
50+
}
51+
}
52+
53+
// On JDK 25+, new File("") resolves to cwd (exists=true, isDirectory=true) →
54+
// skipped with warning, no exception.
55+
@Test
56+
@EnabledForJreRange(min = JRE.JAVA_25)
57+
void testLoadAllClassesWithEmptyClasspathJdk25Plus() throws IOException {
58+
String originalClasspath = System.getProperty("java.class.path");
59+
try {
60+
System.setProperty("java.class.path", "");
61+
ClasspathLoader.main(new String[] {});
4562
} finally {
4663
if (originalClasspath != null) {
4764
System.setProperty("java.class.path", originalClasspath);
@@ -54,8 +71,7 @@ void testLoadAllClassesWithInvalidPath() {
5471
String originalClasspath = System.getProperty("java.class.path");
5572
try {
5673
System.setProperty("java.class.path", "nonexistent/path");
57-
assertThrows(FileNotFoundException.class, () ->
58-
ClasspathLoader.main(new String[]{}));
74+
assertThrows(FileNotFoundException.class, () -> ClasspathLoader.main(new String[] {}));
5975
} finally {
6076
if (originalClasspath != null) {
6177
System.setProperty("java.class.path", originalClasspath);
@@ -69,7 +85,7 @@ void testLoadAllClassesWithValidJar(@TempDir Path tempDir) throws IOException {
6985
String originalClasspath = System.getProperty("java.class.path");
7086
try {
7187
System.setProperty("java.class.path", jarFile.getAbsolutePath());
72-
ClasspathLoader.main(new String[]{});
88+
ClasspathLoader.main(new String[] {});
7389
} finally {
7490
if (originalClasspath != null) {
7591
System.setProperty("java.class.path", originalClasspath);
@@ -82,7 +98,7 @@ void testLoadAllClassesWithDirectory(@TempDir Path tempDir) throws IOException {
8298
String originalClasspath = System.getProperty("java.class.path");
8399
try {
84100
System.setProperty("java.class.path", tempDir.toString());
85-
ClasspathLoader.main(new String[]{});
101+
ClasspathLoader.main(new String[] {});
86102
} finally {
87103
if (originalClasspath != null) {
88104
System.setProperty("java.class.path", originalClasspath);
@@ -94,14 +110,14 @@ void testLoadAllClassesWithDirectory(@TempDir Path tempDir) throws IOException {
94110
void testLoadAllClassesWithMultipleEntries(@TempDir Path tempDir) throws IOException {
95111
File jarFile1 = createSimpleJar(tempDir, "test1.jar", "TestClass1");
96112
File jarFile2 = createSimpleJar(tempDir, "test2.jar", "TestClass2");
97-
113+
98114
String originalClasspath = System.getProperty("java.class.path");
99115
try {
100-
String newClasspath = jarFile1.getAbsolutePath() +
101-
File.pathSeparator +
102-
jarFile2.getAbsolutePath();
116+
String newClasspath = jarFile1.getAbsolutePath() +
117+
File.pathSeparator +
118+
jarFile2.getAbsolutePath();
103119
System.setProperty("java.class.path", newClasspath);
104-
ClasspathLoader.main(new String[]{});
120+
ClasspathLoader.main(new String[] {});
105121
} finally {
106122
if (originalClasspath != null) {
107123
System.setProperty("java.class.path", originalClasspath);
@@ -112,7 +128,7 @@ void testLoadAllClassesWithMultipleEntries(@TempDir Path tempDir) throws IOExcep
112128
@Test
113129
void testLoadAllClassesWithBlocklistedClass(@TempDir Path tempDir) throws IOException {
114130
File jarFile = tempDir.resolve("blocklist-test.jar").toFile();
115-
131+
116132
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile))) {
117133
JarEntry blockedEntry = new JarEntry("META-INF/versions/9/module-info.class");
118134
jos.putNextEntry(blockedEntry);
@@ -128,8 +144,9 @@ void testLoadAllClassesWithBlocklistedClass(@TempDir Path tempDir) throws IOExce
128144
String originalClasspath = System.getProperty("java.class.path");
129145
try {
130146
System.setProperty("java.class.path", jarFile.getAbsolutePath());
131-
ClasspathLoader.main(new String[]{});
132-
// The test passes if no exception is thrown and the blocklisted class is skipped
147+
ClasspathLoader.main(new String[] {});
148+
// The test passes if no exception is thrown and the blocklisted class is
149+
// skipped
133150
} finally {
134151
if (originalClasspath != null) {
135152
System.setProperty("java.class.path", originalClasspath);
@@ -139,15 +156,15 @@ void testLoadAllClassesWithBlocklistedClass(@TempDir Path tempDir) throws IOExce
139156

140157
private File createSimpleJar(Path tempDir, String jarName, String className) throws IOException {
141158
File jarFile = tempDir.resolve(jarName).toFile();
142-
159+
143160
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile))) {
144161
// Add a simple non-class file to make it a valid jar
145162
JarEntry entry = new JarEntry("com/test/" + className + ".txt");
146163
jos.putNextEntry(entry);
147164
jos.write("test content".getBytes());
148165
jos.closeEntry();
149166
}
150-
167+
151168
return jarFile;
152169
}
153170
}

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
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.*;
1113

@@ -19,7 +21,7 @@ void testTheUnsafeIsInitialized() {
1921
@Test
2022
void testThrowException() {
2123
Exception testException = new Exception("Test exception");
22-
24+
2325
try {
2426
UnsafeUtil.throwException(testException);
2527
fail("Should have thrown an exception");
@@ -29,22 +31,29 @@ void testThrowException() {
2931
}
3032
}
3133

34+
// IllegalAccessLogger only exists on JDK 9-16; skipped on JDK 8 (no module
35+
// system) and JDK 17+ (class removed).
36+
@Test
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);
52+
}
53+
3254
@Test
33-
void testDisableIllegalAccessWarning() {
55+
void testDisableIllegalAccessWarningDoesNotThrow() {
3456
assertDoesNotThrow(() -> UnsafeUtil.disableIllegalAccessWarning());
35-
try {
36-
Class<?> illegalAccessLoggerClass = Class.forName("jdk.internal.module.IllegalAccessLogger");
37-
Field loggerField = illegalAccessLoggerClass.getDeclaredField("logger");
38-
loggerField.setAccessible(true);
39-
Object loggerValue = loggerField.get(null);
40-
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());
47-
}
4857
}
4958

5059
@Test

0 commit comments

Comments
 (0)