Unit test might create less than 3 files on first and last directory.
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingDirectSizeTimeNewDirectoryTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingDirectSizeTimeNewDirectoryTest.java
index a9d3ee5..9978aa8 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingDirectSizeTimeNewDirectoryTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingDirectSizeTimeNewDirectoryTest.java
@@ -24,6 +24,7 @@
 import org.junit.rules.RuleChain;
 
 import java.io.File;
+import java.util.Arrays;
 import java.util.List;
 
 import static java.util.Arrays.asList;
@@ -69,17 +70,20 @@
         assertNotNull(loggingFolders);
         // Check if two folders were created
         assertTrue("Not enough directories created", loggingFolders.length >= 2);
-        for (File dir : loggingFolders) {
+        Arrays.sort(loggingFolders);
+        for (int i = 0; i < loggingFolders.length; ++i) {
+            File dir = loggingFolders[i];
             File[] files = dir.listFiles();
             assertTrue("No files in directory " + dir.toString(), files != null && files.length > 0);
-            if (files.length < 3) {
-                System.out.println("Only " + files.length + " files created in " + dir.toString());
-                for (File logFile : files) {
-                    System.out.println("File name: " + logFile.getName() + " size: " + logFile.length());
+            if (i > 0 && i < loggingFolders.length - 1) {
+                if (files.length < 3) {
+                    System.out.println("Only " + files.length + " files created in " + dir.toString());
+                    for (File logFile : files) {
+                        System.out.println("File name: " + logFile.getName() + " size: " + logFile.length());
+                    }
+                    fail();
                 }
-                fail();
             }
-
         }
     }
 }