/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */
package org.apache.logging.log4j.core.appender.rolling;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.Arrays;

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.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;

public class RollingDirectSizeTimeNewDirectoryTest {

    private static final String CONFIG = "log4j-rolling-size-time-new-directory.xml";

    // Note that the path is hardcoded in the configuration!
    private static final String DIR = "target/rolling-size-time-new-directory";

    public static LoggerContextRule loggerContextRule =
            LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG);

    @Rule
    public RuleChain chain = loggerContextRule.withCleanFoldersRule(DIR);

    @Test
    public void streamClosedError() throws Exception {

        final Logger logger =
                loggerContextRule.getLogger(RollingDirectSizeTimeNewDirectoryTest.class);

        for (int i = 0; i < 1000; i++) {
            logger.info("nHq6p9kgfvWfjzDRYbZp");
        }
        Thread.sleep(1500);
        for (int i = 0; i < 1000; i++) {
            logger.info("nHq6p9kgfvWfjzDRYbZp");
        }

        final File logDir = new File(DIR);
        final 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 (int logFolderIndex = 0; logFolderIndex < logFolders.length; ++logFolderIndex) {

                File logFolder = logFolders[logFolderIndex];
                File[] logFiles = logFolder.listFiles();
                Arrays.sort(logFiles);
                assertTrue(
                        "no files found in folder: " + logFolder,
                        logFiles != null && logFiles.length > 0);

                final int minExpectedLogFileCount = 2;
                if (logFolderIndex > 0
                        && logFolderIndex < logFolders.length - 1) {
                    assertTrue(
                            "was expecting at least " + minExpectedLogFileCount + " files, " +
                                    "found " + logFiles.length + ": " + Arrays.toString(logFiles),
                            logFiles.length >= minExpectedLogFileCount);
                }
            }

        } catch (AssertionError error) {
            System.out.format("log directory (%s) contents:%n", DIR);
            int totalFileCount = 0;
            for (final File file : FileUtils.listFilesAndDirs(logDir, TrueFileFilter.TRUE, TrueFileFilter.TRUE)) {
                totalFileCount++;
                System.out.format("-> %s (%d)%n", file, file.length());
            }
            System.out.format("total file count: %d%n", totalFileCount);
            throw new AssertionError("check failure", error);
        }

    }

}

@@ -1080,24 +1080,68 @@
 FileFilter;%0A
+import org.apache.commons.lang3.ArrayUtils;%0A
 import org.a
@@ -1904,32 +1904,16 @@
 logger =
-%0A               
  loggerC
@@ -2328,16 +2328,43 @@
 NotNull(
+%22Not a folder: %22 + logDir, 
 logFolde
@@ -2416,17 +2416,16 @@
   try %7B%0A
-%0A
         
@@ -2587,36 +2587,8 @@
  %22 +
-%0A                           
  %22fo
@@ -2920,191 +2920,614 @@
     
-Arrays.sort(logFiles);%0A                assertTrue(%0A                        %22no files found in folder: %22 + logFolder,%0A                        logFiles != null && logFiles.length %3E 0);%0A
+assertNotNull(%22Not a folder: %22 + logFolder, logFiles);%0A                Arrays.sort(logFiles);%0A                if (logFolderIndex == 0 && logFolders.length %3E 0 && logFiles.length == 0) %7B%0A                    // In a slow execution period, it is possible for the initial directory to be%0A                    // empty because it has not received events yet. If this is the case, then the%0A                    // next directory MUST contain at least one log file.%0A                %7D else %7B%0A                    assertTrue(%22no files found in folder: %22 + logFolder, ArrayUtils.isNotEmpty(logFiles));%0A                %7D
 %0A   
@@ -3620,32 +3620,8 @@
  %3E 0
-%0A                       
  && 
@@ -3788,16 +3788,25 @@
 les, %22 +
+ %22found %22
 %0A       
@@ -3833,25 +3833,16 @@
         
- %22found %22
  + logFi

Fix test on slow machines.