From d3ff2922fd5ecfacc37bff9efaf18b9ab72cd97d Mon Sep 17 00:00:00 2001 From: Jeremy Wood Date: Fri, 23 Aug 2019 15:27:45 -0400 Subject: [PATCH] World cloning should work once again! Resolves #1978. --- .../MultiverseCore/utils/FileUtils.java | 52 +++++-- .../MultiverseCore/utils/FileUtilsTest.java | 136 +++++++++++------- 2 files changed, 125 insertions(+), 63 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java b/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java index 18a39d57..ce793306 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/FileUtils.java @@ -7,16 +7,22 @@ package com.onarandombox.MultiverseCore.utils; -import com.dumptruckman.minecraft.util.Logging; +import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES; import java.io.File; import java.io.IOException; +import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.Comparator; +import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Stream; +import com.dumptruckman.minecraft.util.Logging; + /** * File-utilities. */ @@ -69,20 +75,42 @@ public class FileUtils { * @return if it had success */ public static boolean copyFolder(File source, File target, Logger log) { - 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()); - } - }); + Path sourceDir = source.toPath(); + Path targetDir = target.toPath(); + + try { + Files.walkFileTree(sourceDir, new CopyDirFileVisitor(sourceDir, targetDir)); return true; } catch (IOException e) { - log.warning(e.getMessage()); + log.log(Level.WARNING, "Unable to copy directory", e); return false; } } + + private static class CopyDirFileVisitor extends SimpleFileVisitor { + + private final Path sourceDir; + private final Path targetDir; + + private CopyDirFileVisitor(Path sourceDir, Path targetDir) { + this.sourceDir = sourceDir; + this.targetDir = targetDir; + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + Path newDir = targetDir.resolve(sourceDir.relativize(dir)); + if (!Files.isDirectory(newDir)) { + Files.createDirectory(newDir); + } + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Path targetFile = targetDir.resolve(sourceDir.relativize(file)); + Files.copy(file, targetFile, COPY_ATTRIBUTES); + return FileVisitResult.CONTINUE; + } + } } diff --git a/src/test/java/com/onarandombox/MultiverseCore/utils/FileUtilsTest.java b/src/test/java/com/onarandombox/MultiverseCore/utils/FileUtilsTest.java index b32de655..0b5da6f0 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/utils/FileUtilsTest.java +++ b/src/test/java/com/onarandombox/MultiverseCore/utils/FileUtilsTest.java @@ -1,54 +1,48 @@ package com.onarandombox.MultiverseCore.utils; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + 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; + private Path tempDir; + private Path parentDir; + private Path parentDirFile; + private Path childDir; + private Path childDirFile; + @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(); + tempDir = Files.createTempDirectory("testingTempDir"); - assertTrue(parentDir.exists()); - assertTrue(parentDirFile.exists()); - assertTrue(childDir.exists()); - assertTrue(childDirFile.exists()); - assertTrue(dest.exists()); + parentDir = Files.createDirectory(tempDir.resolve("parentDir")); + parentDirFile = Files.createFile(parentDir.resolve("parentDirFile.txt")); + + childDir = Files.createDirectory(parentDir.resolve("childDir")); + childDirFile = Files.createFile(childDir.resolve("childDirFile.txt")); + + assertTrue(Files.isDirectory(parentDir)); + assertTrue(Files.isRegularFile(parentDirFile)); + assertTrue(Files.isDirectory(childDir)); + assertTrue(Files.isRegularFile(childDirFile)); } @After public void tearDown() throws Exception { try { - org.apache.commons.io.FileUtils.deleteDirectory(parentDir); + org.apache.commons.io.FileUtils.deleteDirectory(tempDir.toFile()); } catch (IOException e) { - if (parentDir.exists()) { - throw e; - } - } - try { - org.apache.commons.io.FileUtils.deleteDirectory(dest); - } catch (IOException e) { - if (parentDir.exists()) { + if (Files.exists(tempDir)) { throw e; } } @@ -56,30 +50,70 @@ public class FileUtilsTest { @Test public void deleteFolder() { - FileUtils.deleteFolder(parentDir); - assertFalse(parentDir.exists()); - assertFalse(parentDirFile.exists()); - assertFalse(childDir.exists()); - assertFalse(childDirFile.exists()); + FileUtils.deleteFolder(parentDir.toFile()); + assertFalse(Files.isDirectory(parentDir)); + assertFalse(Files.isRegularFile(parentDirFile)); + assertFalse(Files.isDirectory(childDir)); + assertFalse(Files.isRegularFile(childDirFile)); } @Test public void deleteFolderContents() { - FileUtils.deleteFolderContents(parentDir); - assertTrue(parentDir.exists()); - assertFalse(parentDirFile.exists()); - assertFalse(childDir.exists()); - assertFalse(childDirFile.exists()); + FileUtils.deleteFolderContents(parentDir.toFile()); + assertTrue(Files.isDirectory(parentDir)); + assertFalse(Files.isRegularFile(parentDirFile)); + assertFalse(Files.isDirectory(childDir)); + assertFalse(Files.isRegularFile(childDirFile)); } @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()); + public void copyFolder() throws Exception { + Path targetDir = tempDir.resolve("target"); + Path targetFile = targetDir.resolve("parentDirFile.txt"); + Path targetChildDir = targetDir.resolve("childDir"); + Path targetChildDirFile = targetChildDir.resolve("childDirFile.txt"); + + assertFalse(Files.isDirectory(targetDir)); + assertFalse(Files.isRegularFile(targetFile)); + assertFalse(Files.isDirectory(targetChildDir)); + assertFalse(Files.isRegularFile(targetChildDirFile)); + + assertTrue(FileUtils.copyFolder(parentDir.toFile(), targetDir.toFile(), Logging.getLogger())); + + assertTrue(Files.isDirectory(targetDir)); + assertTrue(Files.isRegularFile(targetFile)); + assertTrue(Files.isDirectory(targetChildDir)); + assertTrue(Files.isRegularFile(targetChildDirFile)); } -} \ No newline at end of file + + @Test + public void copyFolder_intoExistingFolder() throws Exception { + Path targetDir = Files.createDirectory(tempDir.resolve("target")); + Path targetFile = targetDir.resolve("parentDirFile.txt"); + Path targetChildDir = targetDir.resolve("childDir"); + Path targetChildDirFile = targetChildDir.resolve("childDirFile.txt"); + + assertTrue(Files.isDirectory(targetDir)); + assertFalse(Files.isRegularFile(targetFile)); + assertFalse(Files.isDirectory(targetChildDir)); + assertFalse(Files.isRegularFile(targetChildDirFile)); + + assertTrue(FileUtils.copyFolder(parentDir.toFile(), targetDir.toFile(), Logging.getLogger())); + + assertTrue(Files.isDirectory(targetDir)); + assertTrue(Files.isRegularFile(targetFile)); + assertTrue(Files.isDirectory(targetChildDir)); + assertTrue(Files.isRegularFile(targetChildDirFile)); + } + + @Test() + public void copyFolder_intoExistingFolder_whereFileExists() throws Exception { + Path targetDir = Files.createDirectory(tempDir.resolve("target")); + Path targetFile = Files.createFile(targetDir.resolve("parentDirFile.txt")); + + assertTrue(Files.isDirectory(targetDir)); + assertTrue(Files.isRegularFile(targetFile)); + + assertFalse(FileUtils.copyFolder(parentDir.toFile(), targetDir.toFile(), Logging.getLogger())); + } +}