clean-up
This commit is contained in:
parent
2a58dcf29d
commit
784ea2b061
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.copyfolder;
|
package com.baeldung.copydirectory;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -8,8 +8,8 @@ import org.apache.commons.io.FileUtils;
|
|||||||
public class ApacheCommons {
|
public class ApacheCommons {
|
||||||
|
|
||||||
public static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) throws IOException {
|
public static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) throws IOException {
|
||||||
File sourceFolder = new File(sourceDirectoryLocation);
|
File sourceDirectory = new File(sourceDirectoryLocation);
|
||||||
File destinationFolder = new File(destinationDirectoryLocation);
|
File destinationDirectory = new File(destinationDirectoryLocation);
|
||||||
FileUtils.copyDirectory(sourceFolder, destinationFolder);
|
FileUtils.copyDirectory(sourceDirectory, destinationDirectory);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.copyfolder;
|
package com.baeldung.copydirectory;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
@ -17,12 +17,12 @@ public class CoreOld {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void copyDirectory(File sourceFolder, File destinationFolder) throws IOException {
|
private static void copyDirectory(File sourceDirectory, File destinationDirectory) throws IOException {
|
||||||
if (!destinationFolder.exists()) {
|
if (!destinationDirectory.exists()) {
|
||||||
destinationFolder.mkdir();
|
destinationDirectory.mkdir();
|
||||||
}
|
}
|
||||||
for (String f : sourceFolder.list()) {
|
for (String f : sourceDirectory.list()) {
|
||||||
copyDirectoryJavaUnder7(new File(sourceFolder, f), new File(destinationFolder, f));
|
copyDirectoryJavaUnder7(new File(sourceDirectory, f), new File(destinationDirectory, f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package com.baeldung.copyfolder;
|
package com.baeldung.copydirectory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.baeldung.copydirectory;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class ApacheCommonsUnitTest {
|
||||||
|
|
||||||
|
private final String sourceDirectoryLocation = "src/test/resources/sourceDirectory";
|
||||||
|
private final String subDirectoryName = "/childDirectory";
|
||||||
|
private final String fileName = "/file.txt";
|
||||||
|
private final String destinationDirectoryLocation = "src/test/resources/destinationDirectory";
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void createDirectoryWithSubdirectoryAndFile() throws IOException {
|
||||||
|
Files.createDirectories(Paths.get(sourceDirectoryLocation));
|
||||||
|
Files.createDirectories(Paths.get(sourceDirectoryLocation + subDirectoryName));
|
||||||
|
Files.createFile(Paths.get(sourceDirectoryLocation + subDirectoryName + fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSourceDirectoryExists_thenDirectoryIsFullyCopied() throws IOException {
|
||||||
|
ApacheCommons.copyDirectory(sourceDirectoryLocation, destinationDirectoryLocation);
|
||||||
|
|
||||||
|
assertTrue(new File(destinationDirectoryLocation).exists());
|
||||||
|
assertTrue(new File(destinationDirectoryLocation + subDirectoryName).exists());
|
||||||
|
assertTrue(new File(destinationDirectoryLocation + subDirectoryName + fileName).exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSourceDirectoryDoesNotExist_thenExceptionIsThrown() {
|
||||||
|
assertThrows(Exception.class, () -> ApacheCommons.copyDirectory("nonExistingDirectory", destinationDirectoryLocation));
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void cleanUp() throws IOException {
|
||||||
|
Files.walk(Paths.get(sourceDirectoryLocation))
|
||||||
|
.sorted(Comparator.reverseOrder())
|
||||||
|
.map(Path::toFile)
|
||||||
|
.forEach(File::delete);
|
||||||
|
if (new File(destinationDirectoryLocation).exists()) {
|
||||||
|
Files.walk(Paths.get(destinationDirectoryLocation))
|
||||||
|
.sorted(Comparator.reverseOrder())
|
||||||
|
.map(Path::toFile)
|
||||||
|
.forEach(File::delete);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
package com.baeldung.copydirectory;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class CoreOldUnitTest {
|
||||||
|
|
||||||
|
private final String sourceDirectoryLocation = "src/test/resources/sourceDirectory";
|
||||||
|
private final String subDirectoryName = "/childDirectory";
|
||||||
|
private final String fileName = "/file.txt";
|
||||||
|
private final String destinationDirectoryLocation = "src/test/resources/destinationDirectory";
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void createDirectoryWithSubdirectoryAndFile() throws IOException {
|
||||||
|
Files.createDirectories(Paths.get(sourceDirectoryLocation));
|
||||||
|
Files.createDirectories(Paths.get(sourceDirectoryLocation + subDirectoryName));
|
||||||
|
Files.createFile(Paths.get(sourceDirectoryLocation + subDirectoryName + fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSourceDirectoryExists_thenDirectoryIsFullyCopied() throws IOException {
|
||||||
|
File sourceDirectory = new File(sourceDirectoryLocation);
|
||||||
|
File destinationDirectory = new File(destinationDirectoryLocation);
|
||||||
|
CoreOld.copyDirectoryJavaUnder7(sourceDirectory, destinationDirectory);
|
||||||
|
|
||||||
|
assertTrue(new File(destinationDirectoryLocation).exists());
|
||||||
|
assertTrue(new File(destinationDirectoryLocation + subDirectoryName).exists());
|
||||||
|
assertTrue(new File(destinationDirectoryLocation + subDirectoryName + fileName).exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSourceDirectoryDoesNotExist_thenExceptionIsThrown() throws IOException {
|
||||||
|
File sourceDirectory = new File("nonExistingDirectory");
|
||||||
|
File destinationDirectory = new File(destinationDirectoryLocation);
|
||||||
|
assertThrows(IOException.class, () -> CoreOld.copyDirectoryJavaUnder7(sourceDirectory, destinationDirectory));
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void cleanUp() throws IOException {
|
||||||
|
Files.walk(Paths.get(sourceDirectoryLocation))
|
||||||
|
.sorted(Comparator.reverseOrder())
|
||||||
|
.map(Path::toFile)
|
||||||
|
.forEach(File::delete);
|
||||||
|
if (new File(destinationDirectoryLocation).exists()) {
|
||||||
|
Files.walk(Paths.get(destinationDirectoryLocation))
|
||||||
|
.sorted(Comparator.reverseOrder())
|
||||||
|
.map(Path::toFile)
|
||||||
|
.forEach(File::delete);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.baeldung.copydirectory;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class JavaNioUnitTest {
|
||||||
|
|
||||||
|
private final String sourceDirectoryLocation = "src/test/resources/sourceDirectory";
|
||||||
|
private final String subDirectoryName = "/childDirectory";
|
||||||
|
private final String fileName = "/file.txt";
|
||||||
|
private final String destinationDirectoryLocation = "src/test/resources/destinationDirectory";
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void createDirectoryWithSubdirectoryAndFile() throws IOException {
|
||||||
|
Files.createDirectories(Paths.get(sourceDirectoryLocation));
|
||||||
|
Files.createDirectories(Paths.get(sourceDirectoryLocation + subDirectoryName));
|
||||||
|
Files.createFile(Paths.get(sourceDirectoryLocation + subDirectoryName + fileName));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSourceDirectoryExists_thenDirectoryIsFullyCopied() throws IOException {
|
||||||
|
JavaNio.copyDirectory(sourceDirectoryLocation, destinationDirectoryLocation);
|
||||||
|
|
||||||
|
assertTrue(new File(destinationDirectoryLocation).exists());
|
||||||
|
assertTrue(new File(destinationDirectoryLocation + subDirectoryName).exists());
|
||||||
|
assertTrue(new File(destinationDirectoryLocation + subDirectoryName + fileName).exists());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenSourceDirectoryDoesNotExist_thenExceptionIsThrown() {
|
||||||
|
assertThrows(IOException.class, () -> JavaNio.copyDirectory("nonExistingDirectory", destinationDirectoryLocation));
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void cleanUp() throws IOException {
|
||||||
|
Files.walk(Paths.get(sourceDirectoryLocation))
|
||||||
|
.sorted(Comparator.reverseOrder())
|
||||||
|
.map(Path::toFile)
|
||||||
|
.forEach(File::delete);
|
||||||
|
if (new File(destinationDirectoryLocation).exists()) {
|
||||||
|
Files.walk(Paths.get(destinationDirectoryLocation))
|
||||||
|
.sorted(Comparator.reverseOrder())
|
||||||
|
.map(Path::toFile)
|
||||||
|
.forEach(File::delete);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,59 +0,0 @@
|
|||||||
package com.baeldung.copyfolder;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.Comparator;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
public class ApacheCommonsUnitTest {
|
|
||||||
|
|
||||||
private final String sourceFolderLocation = "src/test/resources/sourceFolder";
|
|
||||||
private final String subFolderName = "/childFolder";
|
|
||||||
private final String fileName = "/file.txt";
|
|
||||||
private final String destinationFolderLocation = "src/test/resources/destinationFolder";
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void createFolderWithSubfolderAndFile() throws IOException {
|
|
||||||
Files.createDirectories(Paths.get(sourceFolderLocation));
|
|
||||||
Files.createDirectories(Paths.get(sourceFolderLocation + subFolderName));
|
|
||||||
Files.createFile(Paths.get(sourceFolderLocation + subFolderName + fileName));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSourceFolderExists_thenFolderIsFullyCopied() throws IOException {
|
|
||||||
ApacheCommons.copyDirectory(sourceFolderLocation, destinationFolderLocation);
|
|
||||||
|
|
||||||
assertTrue(new File(destinationFolderLocation).exists());
|
|
||||||
assertTrue(new File(destinationFolderLocation + subFolderName).exists());
|
|
||||||
assertTrue(new File(destinationFolderLocation + subFolderName + fileName).exists());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSourceFolderDoesNotExist_thenExceptionIsThrown() {
|
|
||||||
assertThrows(Exception.class, () -> ApacheCommons.copyDirectory("nonExistingFolder", destinationFolderLocation));
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterEach
|
|
||||||
public void cleanUp() throws IOException {
|
|
||||||
Files.walk(Paths.get(sourceFolderLocation))
|
|
||||||
.sorted(Comparator.reverseOrder())
|
|
||||||
.map(Path::toFile)
|
|
||||||
.forEach(File::delete);
|
|
||||||
if (new File(destinationFolderLocation).exists()) {
|
|
||||||
Files.walk(Paths.get(destinationFolderLocation))
|
|
||||||
.sorted(Comparator.reverseOrder())
|
|
||||||
.map(Path::toFile)
|
|
||||||
.forEach(File::delete);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,63 +0,0 @@
|
|||||||
package com.baeldung.copyfolder;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.Comparator;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
public class CoreOldUnitTest {
|
|
||||||
|
|
||||||
private final String sourceFolderLocation = "src/test/resources/sourceFolder";
|
|
||||||
private final String subFolderName = "/childFolder";
|
|
||||||
private final String fileName = "/file.txt";
|
|
||||||
private final String destinationFolderLocation = "src/test/resources/destinationFolder";
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void createFolderWithSubfolderAndFile() throws IOException {
|
|
||||||
Files.createDirectories(Paths.get(sourceFolderLocation));
|
|
||||||
Files.createDirectories(Paths.get(sourceFolderLocation + subFolderName));
|
|
||||||
Files.createFile(Paths.get(sourceFolderLocation + subFolderName + fileName));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSourceFolderExists_thenFolderIsFullyCopied() throws IOException {
|
|
||||||
File sourceFolder = new File(sourceFolderLocation);
|
|
||||||
File destinationFolder = new File(destinationFolderLocation);
|
|
||||||
CoreOld.copyDirectoryJavaUnder7(sourceFolder, destinationFolder);
|
|
||||||
|
|
||||||
assertTrue(new File(destinationFolderLocation).exists());
|
|
||||||
assertTrue(new File(destinationFolderLocation + subFolderName).exists());
|
|
||||||
assertTrue(new File(destinationFolderLocation + subFolderName + fileName).exists());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSourceFolderDoesNotExist_thenExceptionIsThrown() throws IOException {
|
|
||||||
File sourceFolder = new File("nonExistingFolder");
|
|
||||||
File destinationFolder = new File(destinationFolderLocation);
|
|
||||||
assertThrows(IOException.class, () -> CoreOld.copyDirectoryJavaUnder7(sourceFolder, destinationFolder));
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterEach
|
|
||||||
public void cleanUp() throws IOException {
|
|
||||||
Files.walk(Paths.get(sourceFolderLocation))
|
|
||||||
.sorted(Comparator.reverseOrder())
|
|
||||||
.map(Path::toFile)
|
|
||||||
.forEach(File::delete);
|
|
||||||
if (new File(destinationFolderLocation).exists()) {
|
|
||||||
Files.walk(Paths.get(destinationFolderLocation))
|
|
||||||
.sorted(Comparator.reverseOrder())
|
|
||||||
.map(Path::toFile)
|
|
||||||
.forEach(File::delete);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
package com.baeldung.copyfolder;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.Comparator;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
public class JavaNioUnitTest {
|
|
||||||
|
|
||||||
private final String sourceFolderLocation = "src/test/resources/sourceFolder";
|
|
||||||
private final String subFolderName = "/childFolder";
|
|
||||||
private final String fileName = "/file.txt";
|
|
||||||
private final String destinationFolderLocation = "src/test/resources/destinationFolder";
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void createFolderWithSubfolderAndFile() throws IOException {
|
|
||||||
Files.createDirectories(Paths.get(sourceFolderLocation));
|
|
||||||
Files.createDirectories(Paths.get(sourceFolderLocation + subFolderName));
|
|
||||||
Files.createFile(Paths.get(sourceFolderLocation + subFolderName + fileName));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSourceFolderExists_thenFolderIsFullyCopied() throws IOException {
|
|
||||||
JavaNio.copyDirectory(sourceFolderLocation, destinationFolderLocation);
|
|
||||||
|
|
||||||
assertTrue(new File(destinationFolderLocation).exists());
|
|
||||||
assertTrue(new File(destinationFolderLocation + subFolderName).exists());
|
|
||||||
assertTrue(new File(destinationFolderLocation + subFolderName + fileName).exists());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSourceFolderDoesNotExist_thenExceptionIsThrown() {
|
|
||||||
assertThrows(IOException.class, () -> JavaNio.copyDirectory("nonExistingFolder", destinationFolderLocation));
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterEach
|
|
||||||
public void cleanUp() throws IOException {
|
|
||||||
Files.walk(Paths.get(sourceFolderLocation))
|
|
||||||
.sorted(Comparator.reverseOrder())
|
|
||||||
.map(Path::toFile)
|
|
||||||
.forEach(File::delete);
|
|
||||||
if (new File(destinationFolderLocation).exists()) {
|
|
||||||
Files.walk(Paths.get(destinationFolderLocation))
|
|
||||||
.sorted(Comparator.reverseOrder())
|
|
||||||
.map(Path::toFile)
|
|
||||||
.forEach(File::delete);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user