Skip to content

Commit 8c0563b

Browse files
authored
Merge branch 'main' into maxday/add-test-converter
2 parents e93414d + 1514151 commit 8c0563b

File tree

5 files changed

+272
-1
lines changed

5 files changed

+272
-1
lines changed

aws-lambda-java-runtime-interface-client/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@
8787
<version>4.11.0</version>
8888
<scope>test</scope>
8989
</dependency>
90+
<dependency>
91+
<groupId>org.mockito</groupId>
92+
<artifactId>mockito-junit-jupiter</artifactId>
93+
<version>4.11.0</version>
94+
<scope>test</scope>
95+
</dependency>
9096
<dependency>
9197
<groupId>com.squareup.okhttp3</groupId>
9298
<artifactId>mockwebserver</artifactId>
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

Lines changed: 4 additions & 1 deletion
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,81 @@
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.util;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
import org.mockito.Mock;
12+
import org.mockito.junit.jupiter.MockitoExtension;
13+
14+
import java.io.IOException;
15+
import java.io.OutputStream;
16+
17+
import static org.mockito.Mockito.*;
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
@ExtendWith(MockitoExtension.class)
21+
public class LambdaOutputStreamTest {
22+
23+
@Mock
24+
private OutputStream mockInnerStream;
25+
26+
private LambdaOutputStream lambdaOutputStream;
27+
28+
@BeforeEach
29+
void setUp() {
30+
lambdaOutputStream = new LambdaOutputStream(mockInnerStream);
31+
}
32+
33+
@Test
34+
void writeSingleByte() throws IOException {
35+
int testByte = 65; // 'A'
36+
lambdaOutputStream.write(testByte);
37+
verify(mockInnerStream).write(new byte[]{(byte) testByte}, 0, 1);
38+
}
39+
40+
@Test
41+
void writeByteArray() throws IOException {
42+
byte[] testBytes = "test".getBytes();
43+
lambdaOutputStream.write(testBytes);
44+
verify(mockInnerStream).write(testBytes, 0, testBytes.length);
45+
}
46+
47+
@Test
48+
void writeOffsetLength() throws IOException {
49+
byte[] testBytes = "test".getBytes();
50+
int offset = 1;
51+
int length = 2;
52+
lambdaOutputStream.write(testBytes, offset, length);
53+
verify(mockInnerStream).write(testBytes, offset, length);
54+
}
55+
56+
@Test
57+
void throwWriteSingleByte() throws IOException {
58+
doThrow(new IOException("Test exception"))
59+
.when(mockInnerStream)
60+
.write(any(byte[].class), anyInt(), anyInt());
61+
assertThrows(IOException.class, () -> lambdaOutputStream.write(65));
62+
}
63+
64+
@Test
65+
void throwWriteByteArray() throws IOException {
66+
byte[] testBytes = "test".getBytes();
67+
doThrow(new IOException("Test exception"))
68+
.when(mockInnerStream)
69+
.write(any(byte[].class), anyInt(), anyInt());
70+
assertThrows(IOException.class, () -> lambdaOutputStream.write(testBytes));
71+
}
72+
73+
@Test
74+
void throwWriteOffsetLength() throws IOException {
75+
byte[] testBytes = "test".getBytes();
76+
doThrow(new IOException("Test exception"))
77+
.when(mockInnerStream)
78+
.write(any(byte[].class), anyInt(), anyInt());
79+
assertThrows(IOException.class, () -> lambdaOutputStream.write(testBytes, 1, 2));
80+
}
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.util;
7+
8+
import org.junit.jupiter.api.Test;
9+
import java.lang.reflect.Field;
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
public class UnsafeUtilTest {
13+
14+
@Test
15+
void testTheUnsafeIsInitialized() {
16+
assertNotNull(UnsafeUtil.TheUnsafe);
17+
}
18+
19+
@Test
20+
void testThrowException() {
21+
Exception testException = new Exception("Test exception");
22+
23+
try {
24+
UnsafeUtil.throwException(testException);
25+
fail("Should have thrown an exception");
26+
} catch (Throwable e) {
27+
assertEquals("Test exception", e.getMessage());
28+
assertSame(testException, e);
29+
}
30+
}
31+
32+
@Test
33+
void testDisableIllegalAccessWarning() {
34+
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+
}
48+
}
49+
50+
@Test
51+
void testPrivateConstructor() {
52+
assertThrows(IllegalAccessException.class, () -> {
53+
UnsafeUtil.class.getDeclaredConstructor().newInstance();
54+
});
55+
}
56+
}

0 commit comments

Comments
 (0)