Repository: tomee Updated Branches: refs/heads/master 6823b155b -> ca3f6a239
File delete and test using win gc Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/ca3f6a23 Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/ca3f6a23 Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/ca3f6a23 Branch: refs/heads/master Commit: ca3f6a23938b5aa0998f002db69646a1a9d9c50d Parents: 6823b15 Author: [email protected] <[email protected]> Authored: Mon Mar 16 19:35:28 2015 +0100 Committer: [email protected] <[email protected]> Committed: Mon Mar 16 19:35:41 2015 +0100 ---------------------------------------------------------------------- .../java/org/apache/openejb/loader/Files.java | 47 ++++++++------- .../org/apache/openejb/loader/FilesTest.java | 60 ++++++++++++++++++++ 2 files changed, 87 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/ca3f6a23/container/openejb-loader/src/main/java/org/apache/openejb/loader/Files.java ---------------------------------------------------------------------- diff --git a/container/openejb-loader/src/main/java/org/apache/openejb/loader/Files.java b/container/openejb-loader/src/main/java/org/apache/openejb/loader/Files.java index 04793f4..b131a49 100644 --- a/container/openejb-loader/src/main/java/org/apache/openejb/loader/Files.java +++ b/container/openejb-loader/src/main/java/org/apache/openejb/loader/Files.java @@ -121,7 +121,6 @@ public class Files { throw new FileRuntimeException("Not a directory: " + file.getAbsolutePath()); } - System.gc(); return file; } @@ -299,9 +298,15 @@ public class Files { } } + /** + * Delete a file and all contents if specified file is a directory. + * If the delete fails then the file/s are flagged for delete on exit. + * + * @param file File + */ public static void delete(final File file) { - if (file.exists()) { + if (null != file && file.exists()) { if (file.isDirectory()) { final File[] files = file.listFiles(); if (null != files) { @@ -326,31 +331,33 @@ public class Files { } } + /** + * Delete a file and all contents if specified file is a directory + * + * @param file File + * @Throws IllegalStateException on failure at any point + */ public static void remove(final File file) { - if (file == null) { - return; - } - if (!file.exists()) { - return; - } + if (null != file && file.exists()) { - if (file.isDirectory()) { - final File[] files = file.listFiles(); - if (files != null) { - for (final File child : files) { - remove(child); + if (file.isDirectory()) { + final File[] files = file.listFiles(); + if (files != null) { + for (final File child : files) { + remove(child); + } } } - } - if (isWindows) { - //Known Windows bug JDK-4715154 and as of JDK8 still not fixable due to OS - System.gc(); - } + if (isWindows) { + //Known Windows bug JDK-4715154 and as of JDK8 still not fixable due to OS + System.gc(); + } - if (!file.delete()) { - throw new IllegalStateException("Could not delete file: " + file.getAbsolutePath()); + if (!file.delete()) { + throw new IllegalStateException("Could not delete file: " + file.getAbsolutePath()); + } } } http://git-wip-us.apache.org/repos/asf/tomee/blob/ca3f6a23/container/openejb-loader/src/test/java/org/apache/openejb/loader/FilesTest.java ---------------------------------------------------------------------- diff --git a/container/openejb-loader/src/test/java/org/apache/openejb/loader/FilesTest.java b/container/openejb-loader/src/test/java/org/apache/openejb/loader/FilesTest.java new file mode 100644 index 0000000..9467b9b --- /dev/null +++ b/container/openejb-loader/src/test/java/org/apache/openejb/loader/FilesTest.java @@ -0,0 +1,60 @@ +package org.apache.openejb.loader; + +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.logging.LogManager; +import java.util.logging.Logger; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class FilesTest { + + private final static File file = new File("target/test/foo.jar"); + + @Test + public void testDelete() throws Exception { + doDelete(false); + } + + @Test + public void testRemove() throws Exception { + doDelete(true); + } + + private void doDelete(final boolean remove) throws IOException { + + final long start = System.nanoTime(); + + for (int i = 0; i < 20; i++) { + + if (remove) { + Files.remove(file); + } else { + Files.delete(file); + } + + Files.mkdirs(file.getParentFile()); + assertTrue(file.createNewFile()); + assertTrue(file.exists()); + } + + assertTrue(file.getParentFile().exists()); + + if (remove) { + Files.remove(file.getParentFile()); + } else { + Files.delete(file.getParentFile()); + } + + assertFalse(file.exists()); + assertFalse(file.getParentFile().exists()); + + final long time = TimeUnit.MILLISECONDS.convert(System.nanoTime() - start, TimeUnit.NANOSECONDS); + Logger.getLogger(this.getClass().getName()).log(Level.INFO, String.format("Completed File.%1$s in %2$sms" , remove ? "remove" : "delete", String.valueOf(time))); + } +} \ No newline at end of file
