Skip to content

Commit 8de1d3c

Browse files
committed
Catch NoSuchFileException also on cleanup since order of test execution is not guaranteed.
1 parent 5187199 commit 8de1d3c

File tree

7 files changed

+39
-10
lines changed

7 files changed

+39
-10
lines changed

powertools-logging/powertools-logging-log4j/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/PowerToolsResolverFactoryTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,12 @@ void setUp() throws IllegalAccessException, IOException {
6262

6363
@AfterEach
6464
void cleanUp() throws IOException {
65-
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
66-
FileChannel.open(Paths.get("target/ecslogfile.json"), StandardOpenOption.WRITE).truncate(0).close();
65+
try {
66+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
67+
FileChannel.open(Paths.get("target/ecslogfile.json"), StandardOpenOption.WRITE).truncate(0).close();
68+
} catch (NoSuchFileException e) {
69+
// file may not exist on the first launch
70+
}
6771
}
6872

6973
@Test

powertools-logging/powertools-logging-log4j/src/test/java/org/apache/logging/log4j/layout/template/json/resolver/PowertoolsResolverArgumentsTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ void setUp() throws IOException {
5959

6060
@AfterEach
6161
void cleanUp() throws IOException {
62-
// Make sure file is cleaned up before running full stack logging regression
63-
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
64-
FileChannel.open(Paths.get("target/ecslogfile.json"), StandardOpenOption.WRITE).truncate(0).close();
62+
try {
63+
// Make sure file is cleaned up before running full stack logging regression
64+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
65+
FileChannel.open(Paths.get("target/ecslogfile.json"), StandardOpenOption.WRITE).truncate(0).close();
66+
} catch (NoSuchFileException e) {
67+
// may not be there in the first run
68+
}
6569
}
6670

6771
@Test

powertools-logging/powertools-logging-log4j/src/test/java/software/amazon/lambda/powertools/logging/log4j/BufferingAppenderTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ void setUp() throws IOException {
3636

3737
@AfterEach
3838
void cleanUp() throws IOException {
39-
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
39+
try {
40+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
41+
} catch (NoSuchFileException e) {
42+
// may not be there in the first run
43+
}
4044
}
4145

4246
@Test

powertools-logging/powertools-logging-logback/src/test/java/software/amazon/lambda/powertools/logging/internal/LambdaJsonEncoderTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ void setUp() throws IllegalAccessException, IOException {
8989

9090
@AfterEach
9191
void cleanUp() throws IOException {
92-
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
92+
93+
try {
94+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
95+
} catch (NoSuchFileException e) {
96+
// file may not exist on the first launch
97+
}
9398
}
9499

95100
@Test

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/PowertoolsLoggingTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ void setUp() throws IllegalAccessException, IOException {
8080
@AfterEach
8181
void cleanUp() throws IOException {
8282
// Make sure file is cleaned up
83-
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
83+
try {
84+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
85+
} catch (NoSuchFileException e) {
86+
// may not be there in the first run
87+
}
8488
}
8589

8690
@Test

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/internal/KeyBufferTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ void setUp() throws IOException {
5353
@AfterEach
5454
void cleanUp() throws IOException {
5555
// Make sure file is cleaned up after each test
56-
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
56+
try {
57+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
58+
} catch (NoSuchFileException e) {
59+
// may not be there in the first run
60+
}
5761
}
5862

5963
@Test

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/internal/LambdaLoggingAspectTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ void setUp() throws IllegalAccessException, IOException {
125125
@AfterEach
126126
void cleanUp() throws IOException {
127127
// Make sure file is cleaned up before running full stack logging regression
128-
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
128+
try {
129+
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
130+
} catch (NoSuchFileException e) {
131+
// may not be there in the first run
132+
}
129133

130134
// Reset log level to INFO to ensure test isolation
131135
LoggingManager loggingManager = LoggingManagerRegistry.getLoggingManager();

0 commit comments

Comments
 (0)