Skip to content

test: add test for classpath #524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

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

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;

import static org.junit.jupiter.api.Assertions.*;

class ClasspathLoaderTest {

@Test
void testLoadAllClassesWithNoClasspath() throws IOException {
String originalClasspath = System.getProperty("java.class.path");
try {
System.clearProperty("java.class.path");
ClasspathLoader.main(new String[]{});
} finally {
if (originalClasspath != null) {
System.setProperty("java.class.path", originalClasspath);
}
}
}

@Test
void testLoadAllClassesWithEmptyClasspath() {
String originalClasspath = System.getProperty("java.class.path");
try {
System.setProperty("java.class.path", "");
assertThrows(FileNotFoundException.class, () ->
ClasspathLoader.main(new String[]{}));
} finally {
if (originalClasspath != null) {
System.setProperty("java.class.path", originalClasspath);
}
}
}

@Test
void testLoadAllClassesWithInvalidPath() {
String originalClasspath = System.getProperty("java.class.path");
try {
System.setProperty("java.class.path", "nonexistent/path");
assertThrows(FileNotFoundException.class, () ->
ClasspathLoader.main(new String[]{}));
} finally {
if (originalClasspath != null) {
System.setProperty("java.class.path", originalClasspath);
}
}
}

@Test
void testLoadAllClassesWithValidJar(@TempDir Path tempDir) throws IOException {
File jarFile = createSimpleJar(tempDir, "test.jar", "TestClass");
String originalClasspath = System.getProperty("java.class.path");
try {
System.setProperty("java.class.path", jarFile.getAbsolutePath());
ClasspathLoader.main(new String[]{});
} finally {
if (originalClasspath != null) {
System.setProperty("java.class.path", originalClasspath);
}
}
}

@Test
void testLoadAllClassesWithDirectory(@TempDir Path tempDir) throws IOException {
String originalClasspath = System.getProperty("java.class.path");
try {
System.setProperty("java.class.path", tempDir.toString());
ClasspathLoader.main(new String[]{});
} finally {
if (originalClasspath != null) {
System.setProperty("java.class.path", originalClasspath);
}
}
}

@Test
void testLoadAllClassesWithMultipleEntries(@TempDir Path tempDir) throws IOException {
File jarFile1 = createSimpleJar(tempDir, "test1.jar", "TestClass1");
File jarFile2 = createSimpleJar(tempDir, "test2.jar", "TestClass2");

String originalClasspath = System.getProperty("java.class.path");
try {
String newClasspath = jarFile1.getAbsolutePath() +
File.pathSeparator +
jarFile2.getAbsolutePath();
System.setProperty("java.class.path", newClasspath);
ClasspathLoader.main(new String[]{});
} finally {
if (originalClasspath != null) {
System.setProperty("java.class.path", originalClasspath);
}
}
}

private File createSimpleJar(Path tempDir, String jarName, String className) throws IOException {
File jarFile = tempDir.resolve(jarName).toFile();

try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile))) {
// Add a simple non-class file to make it a valid jar
JarEntry entry = new JarEntry("com/test/" + className + ".txt");
jos.putNextEntry(entry);
jos.write("test content".getBytes());
jos.closeEntry();
}

return jarFile;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

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

Expand Down