Skip to content

Commit a976e66

Browse files
committed
Disentangle tests writing to AsyncLoggerConfig4Test.xml
1 parent b13abd6 commit a976e66

File tree

4 files changed

+81
-37
lines changed

4 files changed

+81
-37
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfig4Test.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,36 @@
1919
import java.io.BufferedReader;
2020
import java.io.File;
2121
import java.io.FileReader;
22+
import java.nio.file.Path;
23+
import java.util.concurrent.TimeUnit;
2224

23-
import org.apache.logging.log4j.LogManager;
2425
import org.apache.logging.log4j.Logger;
25-
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
26-
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
26+
import org.apache.logging.log4j.core.LoggerContext;
27+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
28+
import org.apache.logging.log4j.test.junit.TempLoggingDir;
29+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
2730
import org.junit.jupiter.api.Tag;
2831
import org.junit.jupiter.api.Test;
29-
import org.junitpioneer.jupiter.SetSystemProperty;
3032

3133
import static org.hamcrest.MatcherAssert.assertThat;
32-
import static org.hamcrest.Matchers.containsString;
34+
import static org.hamcrest.Matchers.equalTo;
3335
import static org.junit.jupiter.api.Assertions.assertNull;
34-
import static org.junit.jupiter.api.Assertions.assertTrue;
3536

3637
@Tag("async")
37-
@SetSystemProperty(key = Log4jPropertyKey.Constant.CONFIG_LOCATION, value = "AsyncLoggerConfigTest4.xml")
38+
@UsingStatusListener
3839
public class AsyncLoggerConfig4Test {
3940

41+
@TempLoggingDir
42+
private static Path loggingPath;
43+
4044
@Test
41-
public void testParameters() throws Exception {
42-
final File file = new File("target", "AsyncLoggerConfigTest4.log");
43-
assertTrue(!file.exists() || file.delete(), "Deleted old file before test");
45+
@LoggerContextSource
46+
public void testParameters(final LoggerContext ctx) throws Exception {
47+
final File file = loggingPath.resolve("AsyncLoggerConfigTest4.log").toFile();
4448

45-
final Logger log = LogManager.getLogger("com.foo.Bar");
49+
final Logger log = ctx.getLogger("com.foo.Bar");
4650
log.info("Additive logging: {} for the price of {}!", 2, 1);
47-
CoreLoggerContexts.stopLoggerContext(file); // stop async thread
51+
ctx.stop(500, TimeUnit.MILLISECONDS);
4852

4953
final BufferedReader reader = new BufferedReader(new FileReader(file));
5054
final String line1 = reader.readLine();
@@ -56,13 +60,13 @@ public void testParameters() throws Exception {
5660
file.delete();
5761

5862
assertThat(line1,
59-
containsString("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!"));
63+
equalTo("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!"));
6064
assertThat(line2,
61-
containsString("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!"));
65+
equalTo("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!"));
6266
assertThat(line3,
63-
containsString("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!"));
67+
equalTo("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!"));
6468
assertThat(line4,
65-
containsString("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!"));
66-
assertNull(line5, "Expected only two lines to be logged");
69+
equalTo("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!"));
70+
assertNull(line5, "Expected only four lines to be logged");
6771
}
6872
}

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigWithAsyncEnabledTest.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,36 @@
2020
import java.io.File;
2121
import java.io.FileReader;
2222
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.util.concurrent.TimeUnit;
2325

24-
import org.apache.logging.log4j.LogManager;
2526
import org.apache.logging.log4j.Logger;
26-
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
27-
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
28-
import org.apache.logging.log4j.core.test.junit.ContextSelectorType;
27+
import org.apache.logging.log4j.core.LoggerContext;
28+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
29+
import org.apache.logging.log4j.test.junit.TempLoggingDir;
30+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
2931
import org.junit.jupiter.api.Tag;
3032
import org.junit.jupiter.api.Test;
31-
import org.junitpioneer.jupiter.SetSystemProperty;
3233

3334
import static org.hamcrest.MatcherAssert.assertThat;
34-
import static org.hamcrest.Matchers.containsString;
35-
import static org.junit.jupiter.api.Assertions.assertTrue;
35+
import static org.hamcrest.Matchers.equalTo;
3636

3737
@Tag("async")
38-
@SetSystemProperty(key = Log4jPropertyKey.Constant.CONFIG_LOCATION, value = "AsyncLoggerConfigTest4.xml")
39-
@ContextSelectorType(AsyncLoggerContextSelector.class)
38+
@UsingStatusListener
4039
public class AsyncLoggerConfigWithAsyncEnabledTest {
4140

41+
@TempLoggingDir
42+
private static Path loggingPath;
43+
4244
@Test
43-
public void testParametersAreAvailableToLayout() throws Exception {
44-
final File file = new File("target", "AsyncLoggerConfigTest4.log");
45-
assertTrue(!file.exists() || file.delete(), "Deleted old file before test");
45+
@LoggerContextSource(selector = AsyncLoggerContextSelector.class)
46+
public void testParametersAreAvailableToLayout(final LoggerContext ctx) throws Exception {
47+
final File file = loggingPath.resolve("AsyncLoggerConfigTest4.log").toFile();
4648

47-
final Logger log = LogManager.getLogger("com.foo.Bar");
49+
final Logger log = ctx.getLogger("com.foo.Bar");
4850
final String format = "Additive logging: {} for the price of {}!";
4951
log.info(format, 2, 1);
50-
CoreLoggerContexts.stopLoggerContext(file); // stop async thread
52+
ctx.stop(500, TimeUnit.MILLISECONDS);
5153

5254
final String line1, line2;
5355
try (final BufferedReader reader = new BufferedReader(new FileReader(file))) {
@@ -57,7 +59,7 @@ public void testParametersAreAvailableToLayout() throws Exception {
5759
Files.delete(file.toPath());
5860

5961
final String expected = "Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!";
60-
assertThat(line1, containsString(expected));
61-
assertThat(line2, containsString(expected));
62+
assertThat(line1, equalTo(expected));
63+
assertThat(line2, equalTo(expected));
6264
}
6365
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration status="off">
19+
<Appenders>
20+
<RandomAccessFile name="File"
21+
fileName="${test:logging.path}/AsyncLoggerConfigTest4.log"
22+
append="false">
23+
<PatternLayout pattern="%testformat %testparameters %m%n"/>
24+
</RandomAccessFile>
25+
</Appenders>
26+
<Loggers>
27+
<Logger name="com.foo.Bar" level="trace">
28+
<AppenderRef ref="File"/>
29+
</Logger>
30+
<AsyncLogger name="com.foo" level="trace">
31+
<AppenderRef ref="File"/>
32+
</AsyncLogger>
33+
<Logger name="com" level="trace">
34+
<AppenderRef ref="File"/>
35+
</Logger>
36+
<AsyncRoot level="info">
37+
<AppenderRef ref="File"/>
38+
</AsyncRoot>
39+
</Loggers>
40+
</Configuration>

log4j-core-test/src/test/resources/AsyncLoggerConfigTest4.xml renamed to log4j-core-test/src/test/resources/org/apache/logging/log4j/core/async/AsyncLoggerConfigWithAsyncEnabledTest.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
<Configuration status="off">
1919
<Appenders>
2020
<RandomAccessFile name="File"
21-
fileName="target/AsyncLoggerConfigTest4.log"
21+
fileName="${test:logging.path}/AsyncLoggerConfigTest4.log"
2222
bufferedIO="false"
2323
immediateFlush="true"
2424
append="false">
25-
<PatternLayout>
26-
<Pattern>%d %p %c{1.} [%t] %testformat %testparameters %m %ex%n</Pattern>
27-
</PatternLayout>
25+
<PatternLayout pattern="%testformat %testparameters %m%n"/>
2826
</RandomAccessFile>
2927
</Appenders>
3028
<Loggers>

0 commit comments

Comments
 (0)