Refactor clean files and clean folder rules.

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

Branch: 
refs/heads/LOG4J2-1010&LOG4J2-1447-injectable-contextdata&better-datastructure
Commit: e52b01618474ee5888b3698171ee381d6ae2add5
Parents: e81c6a1
Author: Gary Gregory <ggreg...@apache.org>
Authored: Sun Aug 7 11:43:50 2016 -0700
Committer: Gary Gregory <ggreg...@apache.org>
Committed: Sun Aug 7 11:43:50 2016 -0700

----------------------------------------------------------------------
 .../junit/AbstractExternalFileCleaner.java      | 69 +++++++++++++++++---
 .../apache/logging/log4j/junit/CleanFiles.java  | 25 +------
 .../logging/log4j/junit/CleanFolders.java       | 48 ++------------
 3 files changed, 69 insertions(+), 73 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e52b0161/log4j-core/src/test/java/org/apache/logging/log4j/junit/AbstractExternalFileCleaner.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/junit/AbstractExternalFileCleaner.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/junit/AbstractExternalFileCleaner.java
index 7860917..fa15c67 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/junit/AbstractExternalFileCleaner.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/junit/AbstractExternalFileCleaner.java
@@ -17,30 +17,38 @@
 package org.apache.logging.log4j.junit;
 
 import java.io.File;
-import java.util.ArrayList;
+import java.io.IOException;
+import java.nio.file.Path;
 import java.util.Arrays;
-import java.util.List;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
 
+import org.junit.Assert;
 import org.junit.rules.ExternalResource;
 
 public abstract class AbstractExternalFileCleaner extends ExternalResource {
 
+    private static final int SLEEP_RETRY_MILLIS = 200;
     private final boolean cleanAfter;
     private final boolean cleanBefore;
-    private final List<File> files;
+    private final Set<File> files;
     private final int maxTries;
 
-    public AbstractExternalFileCleaner(final boolean before, final boolean 
after, final int maxTries, final File... files) {
+    public AbstractExternalFileCleaner(final boolean before, final boolean 
after, final int maxTries,
+            final File... files) {
         this.cleanBefore = before;
         this.cleanAfter = after;
-        this.files = Arrays.asList(files);
+        this.files = new HashSet<>(Arrays.asList(files));
         this.maxTries = maxTries;
     }
 
-    public AbstractExternalFileCleaner(final boolean before, final boolean 
after, final int maxTries, final String... fileNames) {
+    public AbstractExternalFileCleaner(final boolean before, final boolean 
after, final int maxTries,
+            final String... fileNames) {
         this.cleanBefore = before;
         this.cleanAfter = after;
-        this.files = new ArrayList<>(fileNames.length);
+        this.files = new HashSet<>(fileNames.length);
         for (final String fileName : fileNames) {
             this.files.add(new File(fileName));
         }
@@ -61,7 +69,50 @@ public abstract class AbstractExternalFileCleaner extends 
ExternalResource {
         }
     }
 
-    abstract protected void clean();
+    protected void clean() {
+        Map<Path, IOException> failures = new HashMap<>();
+        // Clean and gather failures
+        for (final File file : getFiles()) {
+            if (file.exists()) {
+                final Path path = file.toPath();
+                for (int i = 0; i < getMaxTries(); i++) {
+                    try {
+                        if (clean(path, i)) {
+                            if (failures.containsKey(path)) {
+                                failures.remove(path);
+                            }
+                            break;
+                        }
+                    } catch (final IOException e) {
+                        // We will try again.
+                        failures.put(path, e);
+                    }
+                    try {
+                        Thread.sleep(SLEEP_RETRY_MILLIS);
+                    } catch (final InterruptedException ignored) {
+                        // ignore
+                    }
+                }
+            }
+        }
+        // Fail on failures
+        if (failures.size() > 0) {
+            StringBuilder sb = new StringBuilder();
+            boolean first = true;
+            for (Map.Entry<Path, IOException> failure : failures.entrySet()) {
+                failure.getValue().printStackTrace();
+                if (!first) {
+                    sb.append(", ");
+                }
+                sb.append(failure.getKey()).append(" failed with 
").append(failure.getValue());
+                first = false;
+            }
+            Assert.fail(sb.toString());
+        }
+
+    }
+
+    protected abstract boolean clean(Path path, int tryIndex) throws 
IOException;
 
     public boolean cleanAfter() {
         return cleanAfter;
@@ -71,7 +122,7 @@ public abstract class AbstractExternalFileCleaner extends 
ExternalResource {
         return cleanBefore;
     }
 
-    public List<File> getFiles() {
+    public Set<File> getFiles() {
         return files;
     }
 

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e52b0161/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java 
b/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java
index d38ca85..fdd4cdd 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java
@@ -19,8 +19,7 @@ package org.apache.logging.log4j.junit;
 import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
-
-import org.junit.Assert;
+import java.nio.file.Path;
 
 /**
  * A JUnit test rule to automatically delete files after a test is run.
@@ -45,26 +44,8 @@ public class CleanFiles extends AbstractExternalFileCleaner {
     }
 
     @Override
-    protected void clean() {
-        for (final File file : getFiles()) {
-            if (file.exists()) {
-                for (int i = 0; i < getMaxTries(); i++) {
-                    try {
-                        if (Files.deleteIfExists(file.toPath())) {
-                            // Break from MAX_TRIES and move on to the next 
file.
-                            break;
-                        }
-                    } catch (final IOException e) {
-                        Assert.fail(file + ": " + e.toString());
-                    }
-                    try {
-                        Thread.sleep(200);
-                    } catch (final InterruptedException ignored) {
-                        // ignore
-                    }
-                }
-            }
-        }
+    protected boolean clean(Path path, int tryIndex) throws IOException {
+        return Files.deleteIfExists(path);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/e52b0161/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
index ea75a55..52ade89 100644
--- 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
@@ -23,10 +23,6 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.SimpleFileVisitor;
 import java.nio.file.attribute.BasicFileAttributes;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.junit.Assert;
 
 /**
  * A JUnit test rule to automatically delete folders recursively before 
(optional) and after (optional) a test is run.
@@ -50,44 +46,6 @@ public class CleanFolders extends 
AbstractExternalFileCleaner {
         super(true, true, MAX_TRIES, folderNames);
     }
 
-    @Override
-    protected void clean() {
-        Map<Path, IOException> failures = new HashMap<>();
-        // Clean and gather failures
-        for (final File folder : getFiles()) {
-            if (folder.exists()) {
-                final Path path = folder.toPath();
-                for (int i = 0; i < getMaxTries(); i++) {
-                    try {
-                        cleanFolder(path);
-                        if (failures.containsKey(path)) {
-                            failures.remove(path);
-                        }
-                        // break from MAX_TRIES and goes to the next folder
-                        break;
-                    } catch (final IOException e) {
-                        // We will try again.
-                        failures.put(path, e);
-                    }
-                }
-            }
-        }
-        // Fail on failures
-        if (failures.size() > 0) {
-            StringBuilder sb = new StringBuilder();
-            boolean first = true;
-            for (Map.Entry<Path, IOException> failure : failures.entrySet()) {
-                failure.getValue().printStackTrace();
-                if (!first) {
-                    sb.append(", ");
-                }
-                sb.append(failure.getKey()).append(" failed with 
").append(failure.getValue());
-                first = false;
-            }
-            Assert.fail(sb.toString());
-        }
-    }
-
     private void cleanFolder(final Path folder) throws IOException {
         if (Files.exists(folder) && Files.isDirectory(folder)) {
             Files.walkFileTree(folder, new SimpleFileVisitor<Path>() {
@@ -105,4 +63,10 @@ public class CleanFolders extends 
AbstractExternalFileCleaner {
             });
         }
     }
+
+    @Override
+    protected boolean clean(Path path, int tryIndex) throws IOException {
+        cleanFolder(path);
+        return true;
+    }
 }

Reply via email to