Skip to content

Commit ce9f1fd

Browse files
committed
test: add test for classpath
1 parent cfb3ece commit ce9f1fd

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-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

0 commit comments

Comments
 (0)