World cloning should work once again! Resolves #1978.

This commit is contained in:
Jeremy Wood 2019-08-23 15:27:45 -04:00
parent 5e2824abeb
commit d3ff2922fd
2 changed files with 125 additions and 63 deletions

View File

@ -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<Path> 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<Path> {
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;
}
}
}

View File

@ -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));
}
}
@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()));
}
}