|
16 | 16 | */
|
17 | 17 | package org.apache.logging.log4j.core.appender.rolling;
|
18 | 18 |
|
19 |
| -import org.apache.logging.log4j.Logger; |
20 |
| -import org.apache.logging.log4j.core.LoggerContext; |
21 |
| -import org.apache.logging.log4j.core.test.junit.LoggerContextSource; |
22 |
| -import org.apache.logging.log4j.plugins.Named; |
23 |
| -import org.apache.logging.log4j.test.junit.CleanUpDirectories; |
24 |
| -import org.junit.jupiter.api.Test; |
25 |
| - |
26 | 19 | import java.io.File;
|
27 |
| -import java.util.concurrent.CountDownLatch; |
28 | 20 |
|
29 |
| -import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import org.apache.logging.log4j.Logger; |
| 22 | +import org.apache.logging.log4j.core.test.junit.LoggerContextRule; |
| 23 | +import org.hamcrest.Matcher; |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.rules.RuleChain; |
| 27 | + |
| 28 | +import static org.apache.logging.log4j.core.test.hamcrest.Descriptors.that; |
| 29 | +import static org.apache.logging.log4j.core.test.hamcrest.FileMatchers.hasName; |
| 30 | +import static org.hamcrest.Matchers.endsWith; |
| 31 | +import static org.hamcrest.Matchers.hasItemInArray; |
| 32 | +import static org.junit.Assert.assertTrue; |
| 33 | +import static org.junit.Assert.fail; |
30 | 34 |
|
31 | 35 | /**
|
32 | 36 | *
|
33 | 37 | */
|
34 |
| -public class RollingAppenderTimeTest extends AbstractRollingListenerTest { |
| 38 | +public class RollingAppenderTimeTest { |
35 | 39 |
|
36 | 40 | private static final String CONFIG = "log4j-rolling2.xml";
|
37 | 41 | private static final String DIR = "target/rolling2";
|
38 |
| - private final CountDownLatch rollover = new CountDownLatch(1); |
| 42 | + |
| 43 | + private final LoggerContextRule loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG); |
| 44 | + |
| 45 | + @Rule |
| 46 | + public RuleChain chain = loggerContextRule.withCleanFoldersRule(DIR); |
39 | 47 |
|
40 | 48 | @Test
|
41 |
| - @CleanUpDirectories(DIR) |
42 |
| - @LoggerContextSource(value = CONFIG, timeout = 10) |
43 |
| - public void testAppender(final LoggerContext context, @Named("RollingFile") final RollingFileManager manager) throws Exception { |
44 |
| - manager.addRolloverListener(this); |
45 |
| - final Logger logger = context.getLogger(getClass()); |
| 49 | + public void testAppender() throws Exception { |
| 50 | + final Logger logger = loggerContextRule.getLogger(); |
46 | 51 | logger.debug("This is test message number 1");
|
47 |
| - currentTimeMillis.addAndGet(1500); |
| 52 | + Thread.sleep(1500); |
48 | 53 | // Trigger the rollover
|
49 | 54 | for (int i = 0; i < 16; ++i) {
|
50 |
| - logger.debug("This is test message number {}", i + 1); |
51 |
| - currentTimeMillis.addAndGet(100); |
| 55 | + logger.debug("This is test message number " + i + 1); |
52 | 56 | }
|
53 |
| - rollover.await(); |
54 | 57 | final File dir = new File(DIR);
|
55 |
| - assertThat(dir).isNotEmptyDirectory(); |
56 |
| - assertThat(dir).isDirectoryContaining("glob:**.gz"); |
57 |
| - } |
| 58 | + assertTrue("Directory not created", dir.exists() && dir.listFiles().length > 0); |
58 | 59 |
|
59 |
| - @Override |
60 |
| - public void rolloverComplete(final String fileName) { |
61 |
| - rollover.countDown(); |
| 60 | + final int MAX_TRIES = 20; |
| 61 | + final Matcher<File[]> hasGzippedFile = hasItemInArray(that(hasName(that(endsWith(".gz"))))); |
| 62 | + for (int i = 0; i < MAX_TRIES; i++) { |
| 63 | + final File[] files = dir.listFiles(); |
| 64 | + if (hasGzippedFile.matches(files)) { |
| 65 | + return; // test succeeded |
| 66 | + } |
| 67 | + logger.debug("Adding additional event " + i); |
| 68 | + Thread.sleep(100); // Allow time for rollover to complete |
| 69 | + } |
| 70 | + fail("No compressed files found"); |
62 | 71 | }
|
63 | 72 | }
|
0 commit comments