Repository: logging-log4j2
Updated Branches:
  refs/heads/master b12577e41 -> 9f3ce619f


Fix failing tests on Windows and provide a more orderly clean up
mechanism: The file clean up must happen after Log4j releases it's file
resources. Do this through a new JUnit Rule: CleanFolders.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/9f3ce619
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/9f3ce619
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/9f3ce619

Branch: refs/heads/master
Commit: 9f3ce619fa3dffba6d6d21755cc9e3bce6cd1154
Parents: b12577e
Author: Gary Gregory <[email protected]>
Authored: Thu Jun 23 19:54:57 2016 -0700
Committer: Gary Gregory <[email protected]>
Committed: Thu Jun 23 19:54:57 2016 -0700

----------------------------------------------------------------------
 .../rolling/RollingAppenderSizeTest.java        | 17 ++--
 .../logging/log4j/junit/CleanFolders.java       | 83 ++++++++++++++++++++
 2 files changed, 92 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9f3ce619/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
index 92b44de..2cc8c9f 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderSizeTest.java
@@ -43,6 +43,7 @@ import 
org.apache.commons.compress.compressors.CompressorStreamFactory;
 import org.apache.commons.compress.utils.IOUtils;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.util.Closer;
+import org.apache.logging.log4j.junit.CleanFolders;
 import org.apache.logging.log4j.junit.LoggerContextRule;
 import org.junit.After;
 import org.junit.AfterClass;
@@ -50,6 +51,7 @@ import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.RuleChain;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized;
 
@@ -59,6 +61,9 @@ import org.junit.runners.Parameterized;
 @RunWith(Parameterized.class)
 public class RollingAppenderSizeTest {
 
+    @Rule
+    public RuleChain chain;
+
     private static final String DIR = "target/rolling1";
 
     private final String fileExtension;
@@ -75,16 +80,17 @@ public class RollingAppenderSizeTest {
                 {"log4j-rolling-bzip2.xml", ".bz2"}, //
                 {"log4j-rolling-deflate.xml", ".deflate"}, //
                 {"log4j-rolling-pack200.xml", ".pack200"}, //
-                {"log4j-rolling-xz.xml", ".xz"},});
+                {"log4j-rolling-xz.xml", ".xz"}, //
+                });
                 // @formatter:on
     }
 
-    @Rule
-    public LoggerContextRule loggerContextRule;
+    private LoggerContextRule loggerContextRule;
 
     public RollingAppenderSizeTest(final String configFile, final String 
fileExtension) {
         this.fileExtension = fileExtension;
         this.loggerContextRule = new LoggerContextRule(configFile);
+        this.chain = RuleChain.outerRule(new CleanFolders(new 
File(DIR))).around(loggerContextRule);
     }
 
     @BeforeClass
@@ -102,11 +108,6 @@ public class RollingAppenderSizeTest {
         this.logger = 
this.loggerContextRule.getLogger(RollingAppenderSizeTest.class.getName());
     }
 
-    @After
-    public void tearDown() {
-        deleteDir();
-    }
-    
     @Test
     public void testAppender() throws Exception {
         for (int i = 0; i < 100; ++i) {

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9f3ce619/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFolders.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFolders.java 
b/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFolders.java
new file mode 100644
index 0000000..074ba33
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFolders.java
@@ -0,0 +1,83 @@
+/*
+ * 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.junit;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.rules.ExternalResource;
+
+/**
+ * A JUnit test rule to automatically delete folders after a test is run.
+ */
+public class CleanFolders extends ExternalResource {
+    private static final int MAX_TRIES = 10;
+    private final List<File> folders;
+
+    public CleanFolders(final File... files) {
+        this.folders = Arrays.asList(files);
+    }
+
+    public CleanFolders(final String... fileNames) {
+        this.folders = new ArrayList<>(fileNames.length);
+        for (final String fileName : fileNames) {
+            this.folders.add(new File(fileName));
+        }
+    }
+
+    @Override
+    protected void after() {
+        this.clean();
+    }
+
+    private void clean() {
+        for (final File folder : folders) {
+            for (int i = 0; i < MAX_TRIES; i++) {
+                final Path targetPath = folder.toPath();
+                if (Files.exists(targetPath)) {
+                    String fileName = null;
+                    try (DirectoryStream<Path> directoryStream = 
Files.newDirectoryStream(targetPath)) {
+                        for (final Path path : directoryStream) {
+                            fileName = path.toFile().getName();
+                            if (Files.exists(path)) {
+                                Files.delete(path);
+                            }
+                        }
+                        Files.delete(targetPath);
+                    } catch (final IOException e) {
+                        throw new IllegalStateException(fileName, e);
+                    }
+                }
+            }
+        }
+    }
+
+    @Override
+    public String toString() {
+        final StringBuilder builder = new StringBuilder();
+        builder.append("CleanFolders [");
+        builder.append(folders);
+        builder.append("]");
+        return builder.toString();
+    }
+}

Reply via email to