Skip to content

Commit

Permalink
Make RollingAppenderOnstartupTest do something reasonable
Browse files Browse the repository at this point in the history
  • Loading branch information
rgoers committed Feb 19, 2020
1 parent 8b526a0 commit a0137e1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ protected synchronized boolean closeOutputStream() {
}
try {
stream.close();
LOGGER.debug("OutputStream closed");
} catch (final IOException ex) {
logError("Unable to close stream", ex);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@
*/
package org.apache.logging.log4j.core.appender.rolling;

import java.io.File;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
Expand All @@ -40,27 +48,20 @@
/**
*
*/
@RunWith(Parameterized.class)
public class RollingAppenderOnStartupTest {

private static final String SOURCE = "src/test/resources/__files";
private static final String DIR = "target/onStartup";
private static final String CONFIG = "log4j-test4.xml";
private static final String FILENAME = "onStartup.log";

private Logger logger;

@Parameterized.Parameters(name = "{0} \u2192 {1}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { //
// @formatter:off
{"log4j-test4.xml"},
{"log4j-test4.xml"},});
// @formatter:on
}

@Rule
public LoggerContextRule loggerContextRule;

public RollingAppenderOnStartupTest(final String configFile) {
this.loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(configFile);
public RollingAppenderOnStartupTest() {
this.loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG);
}

@Before
Expand All @@ -78,18 +79,28 @@ public static void beforeClass() throws Exception {
Files.delete(Paths.get(DIR));
}
}
Files.createDirectory(new File(DIR).toPath());
Path target = Paths.get(DIR, FILENAME);
Files.copy(Paths.get(SOURCE, FILENAME), target, StandardCopyOption.COPY_ATTRIBUTES);
FileTime newTime = FileTime.from(Instant.now().minus(1, ChronoUnit.DAYS));
Files.getFileAttributeView(target, BasicFileAttributeView.class).setTimes(newTime, newTime, newTime);
}

@AfterClass
public static void afterClass() throws Exception {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(DIR))) {
boolean rolled = false;
for (final Path path : directoryStream) {
if (!path.toFile().getName().endsWith(FILENAME)) {
rolled = true;
}
List<String> lines = Files.lines(path).collect(Collectors.toList());
assertTrue("No header present for " + path.toFile().getName(), lines.get(0).startsWith("<!DOCTYPE HTML"));
Files.delete(path);
}
Files.delete(Paths.get("target/onStartup"));
assertTrue("File did not roll", rolled);
}
Files.delete(Paths.get("target/onStartup"));
}

@Test
Expand Down
16 changes: 16 additions & 0 deletions log4j-core/src/test/resources/__files/onStartup.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8"/>
<title>Log4j Log Messages</title>
<style type="text/css">
<!--
body, table {font-family:arial,sans-serif; font-size: medium
;}th {background: #336699; color: #FFFFFF; text-align: left;}
-->
</style>
</head>
<body bgcolor="#FFFFFF" topmargin="6" leftmargin="6">

<br>
</body></html>

0 comments on commit a0137e1

Please sign in to comment.