diff --git a/pom.xml b/pom.xml index e534ff19..6a1ed181 100644 --- a/pom.xml +++ b/pom.xml @@ -189,9 +189,6 @@ shade - - commons-io:commons-io - me.main__.util @@ -228,27 +225,6 @@ - - apache-commons-io - package - - shade - - - - - commons-io:commons-io - - - - - org.apache.commons - com.onarandombox.commons - - - true - - @@ -268,11 +244,6 @@ - - commons-io - commons-io - 2.4 - org.bukkit bukkit @@ -372,6 +343,12 @@ 4.0.2 test + + commons-io + commons-io + 2.4 + test + diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java b/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java index 4bc4a18a..18a39d57 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java @@ -11,7 +11,11 @@ import com.dumptruckman.minecraft.util.Logging; import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Comparator; import java.util.logging.Logger; +import java.util.stream.Stream; /** * File-utilities. @@ -28,8 +32,8 @@ public class FileUtils { * @return true if the folder was successfully deleted. */ public static boolean deleteFolder(File file) { - try { - org.apache.commons.io.FileUtils.deleteDirectory(file); + try (Stream files = Files.walk(file.toPath())) { + files.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); return true; } catch (IOException e) { Logging.warning(e.getMessage()); @@ -44,8 +48,11 @@ public class FileUtils { * @return true if the contents were successfully deleted */ public static boolean deleteFolderContents(File file) { - try { - org.apache.commons.io.FileUtils.cleanDirectory(file); + try (Stream files = Files.walk(file.toPath())){ + files.sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .filter(f -> !f.equals(file)) + .forEach(File::delete); return true; } catch (IOException e) { Logging.warning(e.getMessage()); @@ -53,8 +60,6 @@ public class FileUtils { } } - private static final int COPY_BLOCK_SIZE = 1024; - /** * Helper method to copy the world-folder. * @param source Source-File @@ -64,8 +69,16 @@ public class FileUtils { * @return if it had success */ public static boolean copyFolder(File source, File target, Logger log) { - try { - org.apache.commons.io.FileUtils.copyDirectory(source, target); + Path sourcePath = source.toPath(); + Path destPath = target.toPath(); + try (Stream files = Files.walk(source.toPath())) { + files.forEachOrdered(src -> { + try { + Files.copy(src, sourcePath.resolve(destPath.relativize(src))); + } catch (IOException e) { + log.warning(e.getMessage()); + } + }); return true; } catch (IOException e) { log.warning(e.getMessage()); diff --git a/src/test/java/com/onarandombox/MultiverseCore/utils/FileUtilsTest.java b/src/test/java/com/onarandombox/MultiverseCore/utils/FileUtilsTest.java new file mode 100644 index 00000000..b32de655 --- /dev/null +++ b/src/test/java/com/onarandombox/MultiverseCore/utils/FileUtilsTest.java @@ -0,0 +1,85 @@ +package com.onarandombox.MultiverseCore.utils; + +import com.dumptruckman.minecraft.util.Logging; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +import static org.junit.Assert.*; + +public class FileUtilsTest { + + private File parentDir; + private File parentDirFile; + private File childDir; + private File childDirFile; + private File dest; + + @Before + public void setUp() throws Exception { + parentDir = Files.createTempDirectory("parentDir").toFile(); + parentDirFile = new File(parentDir, "parentDirFile.txt"); + parentDirFile.createNewFile(); + childDir = Files.createTempDirectory(parentDir.toPath(), "childDir").toFile(); + childDirFile = new File(childDir, "childDirFile.txt"); + childDirFile.createNewFile(); + dest = Files.createTempDirectory("dest").toFile(); + + assertTrue(parentDir.exists()); + assertTrue(parentDirFile.exists()); + assertTrue(childDir.exists()); + assertTrue(childDirFile.exists()); + assertTrue(dest.exists()); + } + + @After + public void tearDown() throws Exception { + try { + org.apache.commons.io.FileUtils.deleteDirectory(parentDir); + } catch (IOException e) { + if (parentDir.exists()) { + throw e; + } + } + try { + org.apache.commons.io.FileUtils.deleteDirectory(dest); + } catch (IOException e) { + if (parentDir.exists()) { + throw e; + } + } + } + + @Test + public void deleteFolder() { + FileUtils.deleteFolder(parentDir); + assertFalse(parentDir.exists()); + assertFalse(parentDirFile.exists()); + assertFalse(childDir.exists()); + assertFalse(childDirFile.exists()); + } + + @Test + public void deleteFolderContents() { + FileUtils.deleteFolderContents(parentDir); + assertTrue(parentDir.exists()); + assertFalse(parentDirFile.exists()); + assertFalse(childDir.exists()); + assertFalse(childDirFile.exists()); + } + + @Test + public void copyFolder() { + File destFile = new File(dest, "parentDirFile.txt"); + File destChildDir = new File(dest, "childDir"); + File destChildDirFile = new File(destChildDir, "childDirFile.txt"); + assertFalse(destFile.exists()); + assertFalse(destChildDir.exists()); + assertFalse(destChildDirFile.exists()); + FileUtils.copyFolder(parentDir, dest, Logging.getLogger()); + } +} \ No newline at end of file