Skip to content

Commit a969e3c

Browse files
committed
Switch commons module to use JUnit Pioneer
1 parent c45325a commit a969e3c

File tree

5 files changed

+50
-57
lines changed

5 files changed

+50
-57
lines changed

powertools-common/pom.xml

+11-7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
<artifactId>junit-jupiter-engine</artifactId>
5757
<scope>test</scope>
5858
</dependency>
59+
<dependency>
60+
<groupId>org.junit-pioneer</groupId>
61+
<artifactId>junit-pioneer</artifactId>
62+
<scope>test</scope>
63+
</dependency>
5964
<dependency>
6065
<groupId>org.apache.commons</groupId>
6166
<artifactId>commons-lang3</artifactId>
@@ -90,7 +95,10 @@
9095
<artifactId>maven-surefire-plugin</artifactId>
9196
<version>3.2.3</version>
9297
<configuration>
93-
<argLine>-Dmockito.mock.maker=subclass -Dorg.graalvm.nativeimage.imagecode=agent -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image/software.amazon.lambda/powertools-common,experimental-class-define-support</argLine>
98+
<argLine>-Dmockito.mock.maker=subclass -Dorg.graalvm.nativeimage.imagecode=agent -agentlib:native-image-agent=config-output-dir=src/main/resources/META-INF/native-image/software.amazon.lambda/powertools-common,experimental-class-define-support
99+
--add-opens java.base/java.util=ALL-UNNAMED
100+
--add-opens java.base/java.lang=ALL-UNNAMED
101+
</argLine>
94102
</configuration>
95103
</plugin>
96104
</plugins>
@@ -123,20 +131,16 @@
123131
</execution>
124132
</executions>
125133
<configuration>
126-
<agent>
127-
<enabled>true</enabled>
128-
<defaultMode>Standard</defaultMode>
129-
</agent>
130134
<imageName>powertools-common</imageName>
131135
<buildArgs>
136+
<buildArg>--add-opens java.base/java.util=ALL-UNNAMED</buildArg>
137+
<buildArg>--add-opens java.base/java.lang=ALL-UNNAMED</buildArg>
132138
<buildArg>--no-fallback</buildArg>
133139
<buildArg>-Dorg.graalvm.nativeimage.imagecode=agent</buildArg>
134140
<buildArg>-H:ClassInitialization=net.bytebuddy.ClassFileVersion:rerun</buildArg>
135141
<buildArg>-H:ClassInitialization=net.bytebuddy.utility.dispatcher.JavaDispatcher:rerun</buildArg>
136142
<buildArg>-H:ClassInitialization=net.bytebuddy.utility.Invoker$Dispatcher:rerun</buildArg>
137143
<buildArg>-H:ClassInitialization=net.bytebuddy.utility.GraalImageCode:rerun</buildArg>
138-
<buildArg>-H:IncludeResources=version.properties</buildArg>
139-
<buildArg>-H:IncludeResources=unreadable.properties</buildArg>
140144
<buildArg>--initialize-at-build-time=org.slf4j.simple.SimpleLogger</buildArg>
141145
<buildArg>--initialize-at-build-time=org.slf4j.LoggerFactory</buildArg>
142146
<buildArg>--initialize-at-build-time=org.junit.Ignore</buildArg>

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616

1717
import static software.amazon.lambda.powertools.common.internal.SystemWrapper.getenv;
1818

19-
import java.io.FileInputStream;
2019
import java.io.IOException;
21-
import java.net.URL;
20+
import java.io.InputStream;
2221
import java.util.Properties;
2322
import org.slf4j.Logger;
2423
import org.slf4j.LoggerFactory;
@@ -66,11 +65,12 @@ static String getProjectVersion() {
6665
*/
6766
static String getVersionFromProperties(String propertyFileName, String versionKey) {
6867

69-
URL propertiesFileURI = Thread.currentThread().getContextClassLoader().getResource(propertyFileName);
70-
if (propertiesFileURI != null) {
71-
try (FileInputStream fis = new FileInputStream(propertiesFileURI.getPath())) {
68+
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertyFileName);
69+
70+
if (is != null) {
71+
try {
7272
Properties properties = new Properties();
73-
properties.load(fis);
73+
properties.load(is);
7474
String version = properties.getProperty(versionKey);
7575
if (version != null && !version.isEmpty()) {
7676
return version;

powertools-common/src/main/resources/META-INF/native-image/software.amazon.lambda/powertools-common/reflect-config.json

+8
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@
5959
"queryAllDeclaredConstructors":true,
6060
"methods":[{"name":"<init>","parameterTypes":[] }, {"name":"clone","parameterTypes":[] }, {"name":"getClass","parameterTypes":[] }, {"name":"toString","parameterTypes":[] }]
6161
},
62+
{
63+
"name":"java.lang.ProcessEnvironment",
64+
"fields":[{"name":"theCaseInsensitiveEnvironment"}, {"name":"theEnvironment"}]
65+
},
6266
{
6367
"name":"java.lang.ProcessHandle",
6468
"methods":[{"name":"current","parameterTypes":[] }, {"name":"pid","parameterTypes":[] }]
@@ -132,6 +136,10 @@
132136
"name":"java.security.AccessController",
133137
"methods":[{"name":"doPrivileged","parameterTypes":["java.security.PrivilegedAction"] }, {"name":"doPrivileged","parameterTypes":["java.security.PrivilegedExceptionAction"] }]
134138
},
139+
{
140+
"name":"java.util.Collections$UnmodifiableMap",
141+
"fields":[{"name":"m"}]
142+
},
135143
{
136144
"name":"java.util.concurrent.ForkJoinTask",
137145
"fields":[{"name":"aux"}, {"name":"status"}]

powertools-common/src/test/java/software/amazon/lambda/powertools/common/internal/LambdaHandlerProcessorTest.java

+18-32
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
import static org.assertj.core.api.Assertions.assertThat;
1818
import static org.junit.jupiter.api.Assertions.assertNotNull;
1919
import static org.mockito.Mockito.mock;
20-
import static org.mockito.Mockito.mockStatic;
2120
import static org.mockito.Mockito.when;
22-
import static software.amazon.lambda.powertools.common.internal.SystemWrapper.getenv;
2321

2422
import com.amazonaws.services.lambda.runtime.Context;
2523
import com.amazonaws.services.lambda.runtime.RequestHandler;
@@ -30,7 +28,8 @@
3028
import org.aspectj.lang.ProceedingJoinPoint;
3129
import org.aspectj.lang.Signature;
3230
import org.junit.jupiter.api.Test;
33-
import org.mockito.MockedStatic;
31+
import org.junitpioneer.jupiter.ClearEnvironmentVariable;
32+
import org.junitpioneer.jupiter.SetEnvironmentVariable;
3433

3534
class LambdaHandlerProcessorTest {
3635

@@ -139,27 +138,23 @@ void placedOnStreamHandler_shouldInvalidateOnTypeOfArgs_invalidContextArg() {
139138
}
140139

141140
@Test
141+
@SetEnvironmentVariable(key = LambdaConstants.X_AMZN_TRACE_ID, value = "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\"")
142142
void getXrayTraceId_present() {
143143
String traceID = "Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\"";
144-
try (MockedStatic<SystemWrapper> mockedSystemWrapper = mockStatic(SystemWrapper.class)) {
145-
mockedSystemWrapper.when(() -> getenv(LambdaConstants.X_AMZN_TRACE_ID)).thenReturn(traceID);
146144

147-
Optional xRayTraceId = LambdaHandlerProcessor.getXrayTraceId();
145+
Optional xRayTraceId = LambdaHandlerProcessor.getXrayTraceId();
148146

149-
assertThat(xRayTraceId.isPresent()).isTrue();
150-
assertThat(traceID.split(";")[0].replace(LambdaConstants.ROOT_EQUALS, "")).isEqualTo(xRayTraceId.get());
151-
}
147+
assertThat(xRayTraceId.isPresent()).isTrue();
148+
assertThat(traceID.split(";")[0].replace(LambdaConstants.ROOT_EQUALS, "")).isEqualTo(xRayTraceId.get());
152149
}
153150

154151
@Test
152+
@ClearEnvironmentVariable(key = LambdaConstants.X_AMZN_TRACE_ID)
155153
void getXrayTraceId_notPresent() {
156-
try (MockedStatic<SystemWrapper> mockedSystemWrapper = mockStatic(SystemWrapper.class)) {
157-
mockedSystemWrapper.when(() -> getenv(LambdaConstants.X_AMZN_TRACE_ID)).thenReturn(null);
158154

159-
boolean isXRayTraceIdPresent = LambdaHandlerProcessor.getXrayTraceId().isPresent();
155+
boolean isXRayTraceIdPresent = LambdaHandlerProcessor.getXrayTraceId().isPresent();
160156

161-
assertThat(isXRayTraceIdPresent).isFalse();
162-
}
157+
assertThat(isXRayTraceIdPresent).isFalse();
163158
}
164159

165160
@Test
@@ -209,37 +204,28 @@ void isColdStart_coldStartDone() {
209204
}
210205

211206
@Test
207+
@SetEnvironmentVariable(key = LambdaConstants.AWS_SAM_LOCAL, value = "true")
212208
void isSamLocal() {
213-
try (MockedStatic<SystemWrapper> mockedSystemWrapper = mockStatic(SystemWrapper.class)) {
214-
mockedSystemWrapper.when(() -> getenv(LambdaConstants.AWS_SAM_LOCAL)).thenReturn("true");
215209

216-
boolean isSamLocal = LambdaHandlerProcessor.isSamLocal();
210+
boolean isSamLocal = LambdaHandlerProcessor.isSamLocal();
217211

218-
assertThat(isSamLocal).isTrue();
219-
}
212+
assertThat(isSamLocal).isTrue();
220213
}
221214

222215
@Test
216+
@SetEnvironmentVariable(key = LambdaConstants.POWERTOOLS_SERVICE_NAME, value = "MyService")
223217
void serviceName() {
224-
try (MockedStatic<SystemWrapper> mockedSystemWrapper = mockStatic(SystemWrapper.class)) {
225-
String expectedServiceName = "MyService";
226-
mockedSystemWrapper.when(() -> getenv(LambdaConstants.POWERTOOLS_SERVICE_NAME))
227-
.thenReturn(expectedServiceName);
218+
String expectedServiceName = "MyService";
219+
String actualServiceName = LambdaHandlerProcessor.serviceName();
228220

229-
String actualServiceName = LambdaHandlerProcessor.serviceName();
230-
231-
assertThat(actualServiceName).isEqualTo(expectedServiceName);
232-
}
221+
assertThat(actualServiceName).isEqualTo(expectedServiceName);
233222
}
234223

235224
@Test
225+
@ClearEnvironmentVariable(key = LambdaConstants.POWERTOOLS_SERVICE_NAME)
236226
void serviceName_Undefined() {
237227
LambdaHandlerProcessor.resetServiceName();
238-
try (MockedStatic<SystemWrapper> mockedSystemWrapper = mockStatic(SystemWrapper.class)) {
239-
mockedSystemWrapper.when(() -> getenv(LambdaConstants.POWERTOOLS_SERVICE_NAME)).thenReturn(null);
240-
241-
assertThat(LambdaHandlerProcessor.serviceName()).isEqualTo(LambdaConstants.SERVICE_UNDEFINED);
242-
}
228+
assertThat(LambdaHandlerProcessor.serviceName()).isEqualTo(LambdaConstants.SERVICE_UNDEFINED);
243229
}
244230

245231
private ProceedingJoinPoint mockRequestHandlerPjp(Class handlerClass, Object[] handlerArgs) {

powertools-common/src/test/java/software/amazon/lambda/powertools/common/internal/UserAgentConfiguratorTest.java

+7-12
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@
1515
package software.amazon.lambda.powertools.common.internal;
1616

1717
import static org.assertj.core.api.Assertions.assertThat;
18-
import static org.mockito.Mockito.mockStatic;
1918
import static software.amazon.lambda.powertools.common.internal.UserAgentConfigurator.AWS_EXECUTION_ENV;
2019
import static software.amazon.lambda.powertools.common.internal.UserAgentConfigurator.VERSION_KEY;
2120
import static software.amazon.lambda.powertools.common.internal.UserAgentConfigurator.VERSION_PROPERTIES_FILENAME;
2221
import static software.amazon.lambda.powertools.common.internal.UserAgentConfigurator.getVersionFromProperties;
23-
import static software.amazon.lambda.powertools.common.internal.SystemWrapper.getenv;
24-
2522

2623
import java.io.File;
2724
import java.util.Objects;
2825
import java.util.regex.Pattern;
2926
import org.junit.jupiter.api.Test;
30-
import org.mockito.MockedStatic;
27+
import org.junitpioneer.jupiter.SetEnvironmentVariable;
3128

3229
class UserAgentConfiguratorTest {
3330

@@ -110,15 +107,13 @@ void testGetUserAgent_NullFeature() {
110107
}
111108

112109
@Test
110+
@SetEnvironmentVariable(key = AWS_EXECUTION_ENV, value = "AWS_Lambda_java8")
113111
void testGetUserAgent_SetAWSExecutionEnv() {
114-
try (MockedStatic<SystemWrapper> mockedSystemWrapper = mockStatic(SystemWrapper.class)) {
115-
mockedSystemWrapper.when(() -> getenv(AWS_EXECUTION_ENV)).thenReturn("AWS_Lambda_java8");
116-
String userAgent = UserAgentConfigurator.getUserAgent("test-feature");
117-
118-
assertThat(userAgent)
119-
.isNotNull()
120-
.isEqualTo("PT/test-feature/" + VERSION + " PTEnv/AWS_Lambda_java8");
121-
}
112+
String userAgent = UserAgentConfigurator.getUserAgent("test-feature");
113+
114+
assertThat(userAgent)
115+
.isNotNull()
116+
.isEqualTo("PT/test-feature/" + VERSION + " PTEnv/AWS_Lambda_java8");
122117
}
123118

124119
}

0 commit comments

Comments
 (0)