Try improving RollingDirectTimeNewDirectoryTest to alleviate test report failures.
*/
package org.apache.logging.log4j.core.appender.rolling;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.junit.LoggerContextRule;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import java.io.File;
+import java.util.Arrays;
+import java.util.Iterator;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-/**
- * Tests
- */
public class RollingDirectTimeNewDirectoryTest {
+
private static final String CONFIG = "log4j-rolling-folder-direct.xml";
+ // Note that the path is hardcoded in the configuration!
private static final String DIR = "target/rolling-folder-direct";
- public static LoggerContextRule loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG);
+ public static LoggerContextRule loggerContextRule =
+ LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG);
@Rule
public RuleChain chain = loggerContextRule.withCleanFoldersRule(DIR);
- private Logger logger;
-
- @Before
- public void setUp() throws Exception {
- this.logger = loggerContextRule.getLogger(RollingDirectTimeNewDirectoryTest.class.getName());
- }
-
-
@Test
public void streamClosedError() throws Exception {
+
+ final Logger logger =
+ loggerContextRule.getLogger(
+ RollingDirectTimeNewDirectoryTest.class.getName());
+
for (int i = 0; i < 1000; i++) {
logger.info("nHq6p9kgfvWfjzDRYbZp");
}
@@ -60,14 +59,42 @@ public class RollingDirectTimeNewDirectoryTest {
logger.info("nHq6p9kgfvWfjzDRYbZp");
}
- File tempDirectoryAsFile = new File(DIR);
- File[] loggingFolders = tempDirectoryAsFile.listFiles();
- assertNotNull(loggingFolders);
- // Check if two folders were created
- assertTrue("Not enough directories created", loggingFolders.length >= 2);
- for (File dir : loggingFolders) {
- File[] files = dir.listFiles();
- assertTrue("No files in directory " + dir.toString(), files != null && files.length > 0);
+ File logDir = new File(DIR);
+ File[] logFolders = logDir.listFiles();
+ assertNotNull(logFolders);
+ Arrays.sort(logFolders);
+
+ try {
+
+ final int minExpectedLogFolderCount = 2;
+ assertTrue(
+ "was expecting at least " + minExpectedLogFolderCount + " folders, " +
+ "found " + logFolders.length,
+ logFolders.length >= minExpectedLogFolderCount);
+
+ for (File logFolder : logFolders) {
+ File[] logFiles = logFolder.listFiles();
+ if (logFiles != null) {
+ Arrays.sort(logFiles);
+ }
+ assertTrue("empty folder: " + logFolder, logFiles != null && logFiles.length > 0);
+ }
+
+ } catch (AssertionError error) {
+ System.out.format("log directory (%s) contents:%n", DIR);
+ final Iterator<File> fileIterator =
+ FileUtils.iterateFilesAndDirs(
+ logDir, TrueFileFilter.TRUE, TrueFileFilter.TRUE);
+ int totalFileCount = 0;
+ while (fileIterator.hasNext()) {
+ totalFileCount++;
+ final File file = fileIterator.next();
+ System.out.format("-> %s (%d)%n", file, file.length());
+ }
+ System.out.format("total file count: %d%n", totalFileCount);
+ throw new AssertionError("check failure", error);
}
+
}
+
}
