Skip to content

Commit 4eb70d7

Browse files
committed
test: add coverage for utils
1 parent cfb3ece commit 4eb70d7

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
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>5.10.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,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,60 @@
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 static org.junit.jupiter.api.Assertions.*;
10+
11+
public class UnsafeUtilTest {
12+
13+
@Test
14+
void testTheUnsafeIsInitialized() {
15+
assertNotNull(UnsafeUtil.TheUnsafe);
16+
}
17+
18+
@Test
19+
void testDisableIllegalAccessWarning() {
20+
assertDoesNotThrow(() -> UnsafeUtil.disableIllegalAccessWarning());
21+
}
22+
23+
@Test
24+
void testThrowException() {
25+
Exception testException = new Exception("Test exception");
26+
27+
try {
28+
UnsafeUtil.throwException(testException);
29+
fail("Should have thrown an exception");
30+
} catch (Throwable e) {
31+
assertEquals("Test exception", e.getMessage());
32+
assertSame(testException, e);
33+
}
34+
}
35+
36+
@Test
37+
void testDisableIllegalAccessWarning() {
38+
assertDoesNotThrow(() -> UnsafeUtil.disableIllegalAccessWarning());
39+
try {
40+
Class<?> illegalAccessLoggerClass = Class.forName("jdk.internal.module.IllegalAccessLogger");
41+
Field loggerField = illegalAccessLoggerClass.getDeclaredField("logger");
42+
loggerField.setAccessible(true);
43+
Object loggerValue = loggerField.get(null);
44+
assertNull(loggerValue);
45+
} catch (ClassNotFoundException e) {
46+
assertTrue(true);
47+
} catch (NoSuchFieldException e) {
48+
assertTrue(true);
49+
} catch (Exception e) {
50+
fail("Unexpected exception: " + e.getMessage());
51+
}
52+
}
53+
54+
@Test
55+
void testPrivateConstructor() {
56+
assertThrows(IllegalAccessException.class, () -> {
57+
UnsafeUtil.class.getDeclaredConstructor().newInstance();
58+
});
59+
}
60+
}

0 commit comments

Comments
 (0)