|
| 1 | +package org.baeldung.java.io; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.FileVisitResult; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.nio.file.SimpleFileVisitor; |
| 12 | +import java.nio.file.attribute.BasicFileAttributes; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Comparator; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import org.apache.commons.io.FileUtils; |
| 18 | +import org.junit.After; |
| 19 | +import org.junit.AfterClass; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.BeforeClass; |
| 22 | +import org.junit.Test; |
| 23 | +import org.springframework.util.FileSystemUtils; |
| 24 | + |
| 25 | +public class JavaDirectoryDeleteUnitTest { |
| 26 | + private static Path TEMP_DIRECTORY; |
| 27 | + private static final String DIRECTORY_NAME = "toBeDeleted"; |
| 28 | + |
| 29 | + public static final List<String> ALL_LINES = Arrays.asList(new String[] { "This is line 1", "This is line 2", "This is line 3", "This is line 4", "This is line 5", "This is line 6" }); |
| 30 | + |
| 31 | + @BeforeClass |
| 32 | + public static void initializeTempDirectory() throws IOException { |
| 33 | + TEMP_DIRECTORY = Files.createTempDirectory("tmpForJUnit"); |
| 34 | + } |
| 35 | + |
| 36 | + @AfterClass |
| 37 | + public static void cleanTempDirectory() throws IOException { |
| 38 | + FileUtils.deleteDirectory(TEMP_DIRECTORY.toFile()); |
| 39 | + } |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setupDirectory() throws IOException { |
| 43 | + Path tempPathForEachTest = Files.createDirectory(TEMP_DIRECTORY.resolve(DIRECTORY_NAME)); |
| 44 | + |
| 45 | + // Create a directory structure |
| 46 | + Files.write(tempPathForEachTest.resolve("file1.txt"), ALL_LINES.subList(0, 2)); |
| 47 | + Files.write(tempPathForEachTest.resolve("file2.txt"), ALL_LINES.subList(2, 4)); |
| 48 | + |
| 49 | + Files.createDirectories(tempPathForEachTest.resolve("Empty")); |
| 50 | + |
| 51 | + Path aSubDir = Files.createDirectories(tempPathForEachTest.resolve("notEmpty")); |
| 52 | + Files.write(aSubDir.resolve("file3.txt"), ALL_LINES.subList(3, 5)); |
| 53 | + Files.write(aSubDir.resolve("file4.txt"), ALL_LINES.subList(0, 3)); |
| 54 | + |
| 55 | + aSubDir = Files.createDirectories(aSubDir.resolve("anotherSubDirectory")); |
| 56 | + Files.write(aSubDir.resolve("file5.txt"), ALL_LINES.subList(4, 5)); |
| 57 | + Files.write(aSubDir.resolve("file6.txt"), ALL_LINES.subList(0, 2)); |
| 58 | + } |
| 59 | + |
| 60 | + @After |
| 61 | + public void checkAndCleanupIfRequired() throws IOException { |
| 62 | + Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); |
| 63 | + if (Files.exists(pathToBeDeleted)) { |
| 64 | + FileUtils.deleteDirectory(pathToBeDeleted.toFile()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + boolean deleteDirectory(File directoryToBeDeleted) { |
| 69 | + File[] allContents = directoryToBeDeleted.listFiles(); |
| 70 | + |
| 71 | + for (File file : allContents) { |
| 72 | + if (file.isDirectory()) { |
| 73 | + deleteDirectory(file); |
| 74 | + } else { |
| 75 | + file.delete(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return directoryToBeDeleted.delete(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void givenDirectory_whenDeletedWithRecursion_thenIsGone() throws IOException { |
| 84 | + Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); |
| 85 | + |
| 86 | + boolean result = deleteDirectory(pathToBeDeleted.toFile()); |
| 87 | + |
| 88 | + assertTrue("Could not delete directory", result); |
| 89 | + assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void givenDirectory_whenDeletedWithCommonsIOFileUtils_thenIsGone() throws IOException { |
| 94 | + Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); |
| 95 | + |
| 96 | + FileUtils.deleteDirectory(pathToBeDeleted.toFile()); |
| 97 | + |
| 98 | + assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void givenDirectory_whenDeletedWithSpringFileSystemUtils_thenIsGone() throws IOException { |
| 103 | + Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); |
| 104 | + |
| 105 | + boolean result = FileSystemUtils.deleteRecursively(pathToBeDeleted.toFile()); |
| 106 | + |
| 107 | + assertTrue("Could not delete directory", result); |
| 108 | + assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void givenDirectory_whenDeletedWithFilesWalk_thenIsGone() throws IOException { |
| 113 | + Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); |
| 114 | + |
| 115 | + Files.walk(pathToBeDeleted) |
| 116 | + .sorted(Comparator.reverseOrder()) |
| 117 | + .map(Path::toFile) |
| 118 | + .forEach(File::delete); |
| 119 | + |
| 120 | + assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void givenDirectory_whenDeletedWithNIO2WalkFileTree_thenIsGone() throws IOException { |
| 125 | + Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); |
| 126 | + |
| 127 | + Files.walkFileTree(pathToBeDeleted, new SimpleFileVisitor<Path>() { |
| 128 | + @Override |
| 129 | + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 130 | + Files.delete(dir); |
| 131 | + return FileVisitResult.CONTINUE; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 136 | + Files.delete(file); |
| 137 | + return FileVisitResult.CONTINUE; |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + assertFalse("Directory still exists", Files.exists(pathToBeDeleted)); |
| 142 | + } |
| 143 | +} |
0 commit comments