|
17 | 17 | package org.apache.logging.log4j.core.appender.rolling;
|
18 | 18 |
|
19 | 19 | import java.io.BufferedReader;
|
20 |
| -import java.io.File; |
21 |
| -import java.io.FileInputStream; |
22 |
| -import java.io.FileReader; |
| 20 | +import java.io.InputStream; |
23 | 21 | import java.io.InputStreamReader;
|
| 22 | +import java.nio.file.DirectoryStream; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import java.util.regex.Pattern; |
24 | 27 | import java.util.zip.GZIPInputStream;
|
25 | 28 |
|
26 | 29 | import org.apache.logging.log4j.Logger;
|
27 |
| -import org.apache.logging.log4j.core.test.junit.LoggerContextRule; |
28 |
| -import org.junit.Before; |
29 |
| -import org.junit.Rule; |
30 |
| -import org.junit.Test; |
31 |
| -import org.junit.rules.RuleChain; |
| 30 | +import org.apache.logging.log4j.core.LoggerContext; |
| 31 | +import org.apache.logging.log4j.core.test.junit.LoggerContextSource; |
| 32 | +import org.apache.logging.log4j.test.junit.TempLoggingDir; |
| 33 | +import org.apache.logging.log4j.test.junit.UsingStatusListener; |
| 34 | +import org.junit.jupiter.api.Test; |
32 | 35 |
|
33 |
| -import static org.apache.logging.log4j.core.test.hamcrest.Descriptors.that; |
34 |
| -import static org.apache.logging.log4j.core.test.hamcrest.FileMatchers.hasName; |
35 |
| -import static org.hamcrest.Matchers.endsWith; |
36 |
| -import static org.hamcrest.Matchers.hasItemInArray; |
37 |
| -import static org.junit.Assert.*; |
| 36 | +import static java.nio.charset.StandardCharsets.UTF_8; |
38 | 37 |
|
39 |
| -/** |
40 |
| - * |
41 |
| - */ |
42 |
| -public class RollingAppenderDirectWriteTest { |
43 |
| - |
44 |
| - private static final String CONFIG = "log4j-rolling-direct.xml"; |
45 |
| - |
46 |
| - private static final String DIR = "target/rolling-direct"; |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
47 | 39 |
|
48 |
| - public static LoggerContextRule loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG); |
49 |
| - |
50 |
| - @Rule |
51 |
| - public RuleChain chain = loggerContextRule.withCleanFoldersRule(DIR); |
| 40 | +@UsingStatusListener |
| 41 | +public class RollingAppenderDirectWriteTest { |
52 | 42 |
|
53 |
| - private Logger logger; |
| 43 | + private final Pattern FILE_PATTERN = Pattern.compile("test-\\d{4}-\\d{2}-\\d{2}T\\d{2}-\\d{2}-\\d+\\.log(\\.gz)?"); |
| 44 | + private final Pattern LINE_PATTERN = Pattern.compile("This is test message number \\d+\\."); |
54 | 45 |
|
55 |
| - @Before |
56 |
| - public void setUp() throws Exception { |
57 |
| - this.logger = loggerContextRule.getLogger(RollingAppenderDirectWriteTest.class.getName()); |
58 |
| - } |
| 46 | + @TempLoggingDir |
| 47 | + private Path loggingPath; |
59 | 48 |
|
60 | 49 | @Test
|
61 |
| - public void testAppender() throws Exception { |
| 50 | + @LoggerContextSource |
| 51 | + public void testAppender(final LoggerContext ctx) throws Exception { |
| 52 | + final Logger logger = ctx.getLogger(getClass()); |
62 | 53 | final int count = 100;
|
63 | 54 | for (int i=0; i < count; ++i) {
|
64 |
| - logger.debug("This is test message number " + i); |
| 55 | + logger.debug("This is test message number {}.", i); |
65 | 56 | }
|
66 |
| - Thread.sleep(50); |
67 |
| - final File dir = new File(DIR); |
68 |
| - assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0); |
69 |
| - final File[] files = dir.listFiles(); |
70 |
| - assertNotNull(files); |
71 |
| - assertThat(files, hasItemInArray(that(hasName(that(endsWith(".gz")))))); |
| 57 | + ctx.stop(500, TimeUnit.MILLISECONDS); |
72 | 58 | int found = 0;
|
73 |
| - for (final File file: files) { |
74 |
| - final String actual = file.getName(); |
75 |
| - BufferedReader reader; |
76 |
| - if (file.getName().endsWith(".gz")) { |
77 |
| - reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file)))); |
78 |
| - } else { |
79 |
| - reader = new BufferedReader(new FileReader(file)); |
| 59 | + try (final DirectoryStream<Path> stream = Files.newDirectoryStream(loggingPath)) { |
| 60 | + for (final Path file: stream) { |
| 61 | + final String fileName = file.getFileName().toString(); |
| 62 | + assertThat(fileName).matches(FILE_PATTERN); |
| 63 | + try (final InputStream is = Files.newInputStream(file); |
| 64 | + final InputStream uncompressed = fileName.endsWith(".gz") ? new GZIPInputStream(is) : is; |
| 65 | + final BufferedReader reader = new BufferedReader(new InputStreamReader(uncompressed, UTF_8))) { |
| 66 | + String line; |
| 67 | + while ((line = reader.readLine()) != null) { |
| 68 | + assertThat(line).matches(LINE_PATTERN); |
| 69 | + ++found; |
| 70 | + } |
| 71 | + } |
80 | 72 | }
|
81 |
| - String line; |
82 |
| - while ((line = reader.readLine()) != null) { |
83 |
| - assertNotNull("No log event in file " + actual, line); |
84 |
| - final String[] parts = line.split((" ")); |
85 |
| - final String expected = "test1-" + parts[0]; |
86 |
| - assertTrue("Incorrect file name. Expected file prefix: " + expected + " Actual: " + actual, |
87 |
| - actual.startsWith(expected)); |
88 |
| - ++found; |
89 |
| - } |
90 |
| - reader.close(); |
91 | 73 | }
|
92 |
| - assertEquals("Incorrect number of events read. Expected " + count + ", Actual " + found, count, found); |
| 74 | + |
| 75 | + assertThat(found).as("Number of events.").isEqualTo(count); |
93 | 76 | }
|
94 | 77 | }
|
0 commit comments