Skip to content

Commit 85513ea

Browse files
committed
Fixed Sonar issues
1 parent 8b2dba4 commit 85513ea

File tree

5 files changed

+10
-43
lines changed

5 files changed

+10
-43
lines changed

powertools-common/src/main/java/software/amazon/lambda/powertools/common/internal/ClassPreLoader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
public class ClassPreLoader {
2929
public static final String CLASSES_FILE = "classesloaded.txt";
3030

31+
private ClassPreLoader() {
32+
// Hide default constructor
33+
}
3134
/**
3235
* Initializes the classes listed in the classesloaded resource
3336
*/
Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,14 @@
11
package software.amazon.lambda.powertools.common.internal;
22

3-
import static org.mockito.Mockito.*;
4-
5-
import java.io.ByteArrayInputStream;
6-
import java.io.InputStream;
7-
import java.net.URL;
8-
import java.net.URLConnection;
9-
import java.util.Collections;
3+
import static org.junit.jupiter.api.Assertions.*;
104

115
import org.junit.jupiter.api.Test;
126

137
class ClassPreLoaderTest {
148

159
@Test
16-
void preloadClasses_shouldIgnoreInvalidClassesAndLoadValidClasses() throws Exception {
17-
// Mock the class loader with no resources
18-
ClassLoader classLoader = mock(ClassLoader.class);
19-
URL mockUrl = mock(URL.class);
20-
URLConnection mockConnection = mock(URLConnection.class);
21-
InputStream mockInputStream = new ByteArrayInputStream("java.lang.String\nInvalid.Class".getBytes());
22-
23-
when(mockUrl.openConnection()).thenReturn(mockConnection);
24-
when(mockConnection.getInputStream()).thenReturn(mockInputStream);
25-
when(classLoader.getResources(ClassPreLoader.CLASSES_FILE))
26-
.thenReturn(Collections.enumeration(Collections.singletonList(mockUrl)));
27-
28-
// Inject the mocked class loader
29-
Thread.currentThread().setContextClassLoader(classLoader);
30-
// Call the method under test
31-
ClassPreLoader.preloadClasses();
32-
33-
// Verify that only the valid class was loaded
34-
Class.forName("java.lang.String", true, ClassPreLoader.class.getClassLoader());
35-
}
36-
37-
@Test
38-
void preloadClasses_shouldHandleEmptyResources() throws Exception {
39-
// Mock the class loader with no resources
40-
ClassLoader classLoader = mock(ClassLoader.class);
41-
when(classLoader.getResources(ClassPreLoader.CLASSES_FILE))
42-
.thenReturn(Collections.emptyEnumeration());
43-
44-
// Inject the mocked class loader
45-
Thread.currentThread().setContextClassLoader(classLoader);
46-
47-
// Call the method under test
48-
ClassPreLoader.preloadClasses();
49-
50-
// Verify no interactions with the class loader
51-
verifyNoInteractions(classLoader);
10+
void preloadClasses_shouldIgnoreInvalidClassesAndLoadValidClasses() {
11+
// Verify that the missing class did not throw any exception
12+
assertDoesNotThrow(ClassPreLoader::preloadClasses);
5213
}
5314
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
software.amazon.lambda.powertools.common.internal.NonExistingClass

powertools-idempotency/powertools-idempotency-core/src/main/java/software/amazon/lambda/powertools/idempotency/internal/IdempotencyHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public void beforeCheckpoint(org.crac.Context<? extends Resource> context) throw
8383

8484
@Override
8585
public void afterRestore(org.crac.Context<? extends Resource> context) throws Exception {
86+
// This is a no-op, as we don't need to do anything after restore
8687
}
8788

8889
/**

powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/internal/LambdaMetricsAspect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,6 @@ public void beforeCheckpoint(org.crac.Context<? extends Resource> context) throw
155155

156156
@Override
157157
public void afterRestore(org.crac.Context<? extends Resource> context) throws Exception {
158+
// No action needed after restore
158159
}
159160
}

0 commit comments

Comments
 (0)