Skip to content

Commit b00ab2c

Browse files
authored
Merge branch 'main' into main
2 parents 188f73b + 338d006 commit b00ab2c

File tree

3 files changed

+241
-1
lines changed

3 files changed

+241
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package com.amazonaws.services.lambda.runtime.api.client;
7+
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.io.TempDir;
10+
import java.io.File;
11+
import java.io.FileNotFoundException;
12+
import java.io.FileOutputStream;
13+
import java.io.IOException;
14+
import java.nio.file.Path;
15+
import java.util.Collections;
16+
import java.util.Enumeration;
17+
import java.util.jar.JarEntry;
18+
import java.util.jar.JarFile;
19+
import java.util.jar.JarOutputStream;
20+
21+
import static org.junit.jupiter.api.Assertions.*;
22+
23+
class ClasspathLoaderTest {
24+
25+
@Test
26+
void testLoadAllClassesWithNoClasspath() throws IOException {
27+
String originalClasspath = System.getProperty("java.class.path");
28+
try {
29+
System.clearProperty("java.class.path");
30+
ClasspathLoader.main(new String[]{});
31+
} finally {
32+
if (originalClasspath != null) {
33+
System.setProperty("java.class.path", originalClasspath);
34+
}
35+
}
36+
}
37+
38+
@Test
39+
void testLoadAllClassesWithEmptyClasspath() {
40+
String originalClasspath = System.getProperty("java.class.path");
41+
try {
42+
System.setProperty("java.class.path", "");
43+
assertThrows(FileNotFoundException.class, () ->
44+
ClasspathLoader.main(new String[]{}));
45+
} finally {
46+
if (originalClasspath != null) {
47+
System.setProperty("java.class.path", originalClasspath);
48+
}
49+
}
50+
}
51+
52+
@Test
53+
void testLoadAllClassesWithInvalidPath() {
54+
String originalClasspath = System.getProperty("java.class.path");
55+
try {
56+
System.setProperty("java.class.path", "nonexistent/path");
57+
assertThrows(FileNotFoundException.class, () ->
58+
ClasspathLoader.main(new String[]{}));
59+
} finally {
60+
if (originalClasspath != null) {
61+
System.setProperty("java.class.path", originalClasspath);
62+
}
63+
}
64+
}
65+
66+
@Test
67+
void testLoadAllClassesWithValidJar(@TempDir Path tempDir) throws IOException {
68+
File jarFile = createSimpleJar(tempDir, "test.jar", "TestClass");
69+
String originalClasspath = System.getProperty("java.class.path");
70+
try {
71+
System.setProperty("java.class.path", jarFile.getAbsolutePath());
72+
ClasspathLoader.main(new String[]{});
73+
} finally {
74+
if (originalClasspath != null) {
75+
System.setProperty("java.class.path", originalClasspath);
76+
}
77+
}
78+
}
79+
80+
@Test
81+
void testLoadAllClassesWithDirectory(@TempDir Path tempDir) throws IOException {
82+
String originalClasspath = System.getProperty("java.class.path");
83+
try {
84+
System.setProperty("java.class.path", tempDir.toString());
85+
ClasspathLoader.main(new String[]{});
86+
} finally {
87+
if (originalClasspath != null) {
88+
System.setProperty("java.class.path", originalClasspath);
89+
}
90+
}
91+
}
92+
93+
@Test
94+
void testLoadAllClassesWithMultipleEntries(@TempDir Path tempDir) throws IOException {
95+
File jarFile1 = createSimpleJar(tempDir, "test1.jar", "TestClass1");
96+
File jarFile2 = createSimpleJar(tempDir, "test2.jar", "TestClass2");
97+
98+
String originalClasspath = System.getProperty("java.class.path");
99+
try {
100+
String newClasspath = jarFile1.getAbsolutePath() +
101+
File.pathSeparator +
102+
jarFile2.getAbsolutePath();
103+
System.setProperty("java.class.path", newClasspath);
104+
ClasspathLoader.main(new String[]{});
105+
} finally {
106+
if (originalClasspath != null) {
107+
System.setProperty("java.class.path", originalClasspath);
108+
}
109+
}
110+
}
111+
112+
private File createSimpleJar(Path tempDir, String jarName, String className) throws IOException {
113+
File jarFile = tempDir.resolve(jarName).toFile();
114+
115+
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile))) {
116+
// Add a simple non-class file to make it a valid jar
117+
JarEntry entry = new JarEntry("com/test/" + className + ".txt");
118+
jos.putNextEntry(entry);
119+
jos.write("test content".getBytes());
120+
jos.closeEntry();
121+
}
122+
123+
return jarFile;
124+
}
125+
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
/* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
1+
/*
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
25

36
package com.amazonaws.services.lambda.runtime.api.client;
47

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package com.amazonaws.services.lambda.runtime.api.client.runtimeapi.converters;
6+
7+
import com.amazonaws.services.lambda.runtime.api.client.UserFault;
8+
import com.amazonaws.services.lambda.runtime.api.client.runtimeapi.dto.ErrorRequest;
9+
import org.junit.jupiter.api.Test;
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
class LambdaErrorConverterTest {
13+
14+
@Test
15+
void testFromUserFaultWithMessageAndException() {
16+
UserFault userFault = new UserFault("Test error message", "TestException", "Test stack trace");
17+
ErrorRequest errorRequest = LambdaErrorConverter.fromUserFault(userFault);
18+
19+
assertNotNull(errorRequest);
20+
assertEquals("Test error message", errorRequest.errorMessage);
21+
assertEquals("TestException", errorRequest.errorType);
22+
assertNull(errorRequest.stackTrace);
23+
}
24+
25+
@Test
26+
void testFromUserFaultWithNullValues() {
27+
UserFault userFault = new UserFault(null, null, null);
28+
ErrorRequest errorRequest = LambdaErrorConverter.fromUserFault(userFault);
29+
30+
assertNotNull(errorRequest);
31+
assertNull(errorRequest.errorMessage);
32+
assertNull(errorRequest.errorType);
33+
assertNull(errorRequest.stackTrace);
34+
}
35+
36+
@Test
37+
void testFromUserFaultWithFatalError() {
38+
UserFault userFault = new UserFault("Fatal error", "FatalException", "Test stack trace", true);
39+
ErrorRequest errorRequest = LambdaErrorConverter.fromUserFault(userFault);
40+
41+
assertNotNull(errorRequest);
42+
assertEquals("Fatal error", errorRequest.errorMessage);
43+
assertEquals("FatalException", errorRequest.errorType);
44+
assertNull(errorRequest.stackTrace);
45+
}
46+
47+
@Test
48+
void testFromUserFaultCreatedFromException() {
49+
Exception exception = new RuntimeException("Test exception message");
50+
UserFault userFault = UserFault.makeUserFault(exception);
51+
ErrorRequest errorRequest = LambdaErrorConverter.fromUserFault(userFault);
52+
53+
assertNotNull(errorRequest);
54+
assertEquals("Test exception message", errorRequest.errorMessage);
55+
assertEquals("java.lang.RuntimeException", errorRequest.errorType);
56+
assertNull(errorRequest.stackTrace);
57+
}
58+
59+
@Test
60+
void testFromUserFaultCreatedFromMessage() {
61+
UserFault userFault = UserFault.makeUserFault("Simple message");
62+
ErrorRequest errorRequest = LambdaErrorConverter.fromUserFault(userFault);
63+
64+
assertNotNull(errorRequest);
65+
assertEquals("Simple message", errorRequest.errorMessage);
66+
assertNull(errorRequest.errorType);
67+
assertNull(errorRequest.stackTrace);
68+
}
69+
70+
@Test
71+
void testFromThrowableWithMessage() {
72+
Exception exception = new RuntimeException("Test exception message");
73+
ErrorRequest errorRequest = LambdaErrorConverter.fromThrowable(exception);
74+
75+
assertNotNull(errorRequest);
76+
assertEquals("Test exception message", errorRequest.errorMessage);
77+
assertEquals("java.lang.RuntimeException", errorRequest.errorType);
78+
assertNotNull(errorRequest.stackTrace);
79+
assertTrue(errorRequest.stackTrace.length > 0);
80+
}
81+
82+
@Test
83+
void testFromThrowableWithNullMessage() {
84+
Exception exception = new RuntimeException();
85+
ErrorRequest errorRequest = LambdaErrorConverter.fromThrowable(exception);
86+
87+
assertNotNull(errorRequest);
88+
assertEquals("java.lang.RuntimeException", errorRequest.errorMessage);
89+
assertEquals("java.lang.RuntimeException", errorRequest.errorType);
90+
assertNotNull(errorRequest.stackTrace);
91+
assertTrue(errorRequest.stackTrace.length > 0);
92+
}
93+
94+
@Test
95+
void testFromThrowableStackTraceContent() {
96+
Exception exception = new RuntimeException("Test message");
97+
ErrorRequest errorRequest = LambdaErrorConverter.fromThrowable(exception);
98+
99+
String[] stackTrace = errorRequest.stackTrace;
100+
assertNotNull(stackTrace);
101+
assertTrue(stackTrace.length > 0);
102+
103+
boolean foundTestClass = false;
104+
for (String traceLine : stackTrace) {
105+
if (traceLine.contains(LambdaErrorConverterTest.class.getSimpleName())) {
106+
foundTestClass = true;
107+
break;
108+
}
109+
}
110+
assertTrue(foundTestClass);
111+
}
112+
}

0 commit comments

Comments
 (0)