|
| 1 | +package com.baeldung.java.nio2; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.nio.file.CopyOption; |
| 8 | +import java.nio.file.DirectoryNotEmptyException; |
| 9 | +import java.nio.file.FileAlreadyExistsException; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.NoSuchFileException; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.nio.file.StandardCopyOption; |
| 15 | +import java.util.Date; |
| 16 | + |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +public class FileTest { |
| 20 | + private static final String HOME = System.getProperty("user.home"); |
| 21 | + |
| 22 | + // checking file or dir |
| 23 | + @Test |
| 24 | + public void givenExistentPath_whenConfirmsFileExists_thenCorrect() { |
| 25 | + Path p = Paths.get(HOME); |
| 26 | + assertTrue(Files.exists(p)); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void givenNonexistentPath_whenConfirmsFileNotExists_thenCorrect() { |
| 31 | + Path p = Paths.get(HOME + "/inexistent_file.txt"); |
| 32 | + assertTrue(Files.notExists(p)); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void givenExistentDirPath_whenConfirmsNotRegularFile_thenCorrect() { |
| 37 | + Path p = Paths.get(HOME); |
| 38 | + assertFalse(Files.isRegularFile(p)); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void givenExistentDirPath_whenConfirmsReadable_thenCorrect() { |
| 43 | + Path p = Paths.get(HOME); |
| 44 | + assertTrue(Files.isReadable(p)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void givenExistentDirPath_whenConfirmsWritable_thenCorrect() { |
| 49 | + Path p = Paths.get(HOME); |
| 50 | + assertTrue(Files.isWritable(p)); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void givenExistentDirPath_whenConfirmsExecutable_thenCorrect() { |
| 55 | + Path p = Paths.get(HOME); |
| 56 | + assertTrue(Files.isExecutable(p)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void givenSameFilePaths_whenConfirmsIsSame_thenCorrect() throws IOException { |
| 61 | + Path p1 = Paths.get(HOME); |
| 62 | + Path p2 = Paths.get(HOME); |
| 63 | + assertTrue(Files.isSameFile(p1, p2)); |
| 64 | + } |
| 65 | + |
| 66 | + // reading, writing and creating files |
| 67 | + // creating file |
| 68 | + @Test |
| 69 | + public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException { |
| 70 | + String fileName = "myfile_" + new Date().getTime() + ".txt"; |
| 71 | + Path p = Paths.get(HOME + "/" + fileName); |
| 72 | + assertFalse(Files.exists(p)); |
| 73 | + Files.createFile(p); |
| 74 | + assertTrue(Files.exists(p)); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException { |
| 80 | + String dirName = "myDir_" + new Date().getTime(); |
| 81 | + Path p = Paths.get(HOME + "/" + dirName); |
| 82 | + assertFalse(Files.exists(p)); |
| 83 | + Files.createDirectory(p); |
| 84 | + assertTrue(Files.exists(p)); |
| 85 | + assertFalse(Files.isRegularFile(p)); |
| 86 | + assertTrue(Files.isDirectory(p)); |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + @Test(expected = NoSuchFileException.class) |
| 91 | + public void givenDirPath_whenFailsToCreateRecursively_thenCorrect() throws IOException { |
| 92 | + String dirName = "myDir_" + new Date().getTime() + "/subdir"; |
| 93 | + Path p = Paths.get(HOME + "/" + dirName); |
| 94 | + assertFalse(Files.exists(p)); |
| 95 | + Files.createDirectory(p); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void givenDirPath_whenCreatesRecursively_thenCorrect() throws IOException { |
| 101 | + Path dir = Paths.get(HOME + "/myDir_" + new Date().getTime()); |
| 102 | + Path subdir = dir.resolve("subdir"); |
| 103 | + assertFalse(Files.exists(dir)); |
| 104 | + assertFalse(Files.exists(subdir)); |
| 105 | + Files.createDirectories(subdir); |
| 106 | + assertTrue(Files.exists(dir)); |
| 107 | + assertTrue(Files.exists(subdir)); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void givenFilePath_whenCreatesTempFile_thenCorrect() throws IOException { |
| 112 | + String prefix = "log_"; |
| 113 | + String suffix = ".txt"; |
| 114 | + Path p = Paths.get(HOME + "/"); |
| 115 | + p = Files.createTempFile(p, prefix, suffix); |
| 116 | + // like log_8821081429012075286.txt |
| 117 | + assertTrue(Files.exists(p)); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void givenFilePath_whenCreatesTempFileWithDefaultsNaming_thenCorrect() throws IOException { |
| 123 | + Path p = Paths.get(HOME + "/"); |
| 124 | + p = Files.createTempFile(p, null, null); |
| 125 | + // like 8600179353689423985.tmp |
| 126 | + assertTrue(Files.exists(p)); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void givenNoFilePath_whenCreatesTempFileInTempDir_thenCorrect() throws IOException { |
| 131 | + Path p = Files.createTempFile(null, null); |
| 132 | + // like C:\Users\new\AppData\Local\Temp\6100927974988978748.tmp |
| 133 | + assertTrue(Files.exists(p)); |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + // delete file |
| 138 | + @Test |
| 139 | + public void givenPath_whenDeletes_thenCorrect() throws IOException { |
| 140 | + Path p = Paths.get(HOME + "/fileToDelete.txt"); |
| 141 | + assertFalse(Files.exists(p)); |
| 142 | + Files.createFile(p); |
| 143 | + assertTrue(Files.exists(p)); |
| 144 | + Files.delete(p); |
| 145 | + assertFalse(Files.exists(p)); |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + @Test(expected = DirectoryNotEmptyException.class) |
| 150 | + public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOException { |
| 151 | + Path dir = Paths.get(HOME + "/emptyDir" + new Date().getTime()); |
| 152 | + Files.createDirectory(dir); |
| 153 | + assertTrue(Files.exists(dir)); |
| 154 | + Path file = dir.resolve("file.txt"); |
| 155 | + Files.createFile(file); |
| 156 | + Files.delete(dir); |
| 157 | + |
| 158 | + assertTrue(Files.exists(dir)); |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void givenInexistentFile_whenDeleteFails_thenCorrect() throws IOException { |
| 164 | + Path p = Paths.get(HOME + "/inexistentFile.txt"); |
| 165 | + assertFalse(Files.exists(p)); |
| 166 | + try { |
| 167 | + Files.delete(p); |
| 168 | + } catch (IOException e) { |
| 169 | + assertTrue(e instanceof NoSuchFileException); |
| 170 | + } |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void givenInexistentFile_whenDeleteIfExistsWorks_thenCorrect() throws IOException { |
| 176 | + Path p = Paths.get(HOME + "/inexistentFile.txt"); |
| 177 | + assertFalse(Files.exists(p)); |
| 178 | + Files.deleteIfExists(p); |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + // copy file |
| 183 | + @Test |
| 184 | + public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOException { |
| 185 | + Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); |
| 186 | + Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); |
| 187 | + Files.createDirectory(dir1); |
| 188 | + Files.createDirectory(dir2); |
| 189 | + Path file1 = dir1.resolve("filetocopy.txt"); |
| 190 | + Path file2 = dir2.resolve("filetocopy.txt"); |
| 191 | + Files.createFile(file1); |
| 192 | + assertTrue(Files.exists(file1)); |
| 193 | + assertFalse(Files.exists(file2)); |
| 194 | + Files.copy(file1, file2); |
| 195 | + assertTrue(Files.exists(file2)); |
| 196 | + |
| 197 | + } |
| 198 | + |
| 199 | + @Test(expected = FileAlreadyExistsException.class) |
| 200 | + public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOException { |
| 201 | + Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); |
| 202 | + Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); |
| 203 | + Files.createDirectory(dir1); |
| 204 | + Files.createDirectory(dir2); |
| 205 | + Path file1 = dir1.resolve("filetocopy.txt"); |
| 206 | + Path file2 = dir2.resolve("filetocopy.txt"); |
| 207 | + Files.createFile(file1); |
| 208 | + Files.createFile(file2); |
| 209 | + assertTrue(Files.exists(file1)); |
| 210 | + assertTrue(Files.exists(file2)); |
| 211 | + Files.copy(file1, file2); |
| 212 | + Files.copy(file1, file2, StandardCopyOption.REPLACE_EXISTING); |
| 213 | + } |
| 214 | + |
| 215 | + // moving files |
| 216 | + @Test |
| 217 | + public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOException { |
| 218 | + Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); |
| 219 | + Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); |
| 220 | + Files.createDirectory(dir1); |
| 221 | + Files.createDirectory(dir2); |
| 222 | + Path file1 = dir1.resolve("filetocopy.txt"); |
| 223 | + Path file2 = dir2.resolve("filetocopy.txt"); |
| 224 | + Files.createFile(file1); |
| 225 | + assertTrue(Files.exists(file1)); |
| 226 | + assertFalse(Files.exists(file2)); |
| 227 | + Files.move(file1, file2); |
| 228 | + assertTrue(Files.exists(file2)); |
| 229 | + assertFalse(Files.exists(file1)); |
| 230 | + |
| 231 | + } |
| 232 | + |
| 233 | + @Test(expected = FileAlreadyExistsException.class) |
| 234 | + public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect() throws IOException { |
| 235 | + Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); |
| 236 | + Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); |
| 237 | + Files.createDirectory(dir1); |
| 238 | + Files.createDirectory(dir2); |
| 239 | + Path file1 = dir1.resolve("filetocopy.txt"); |
| 240 | + Path file2 = dir2.resolve("filetocopy.txt"); |
| 241 | + Files.createFile(file1); |
| 242 | + Files.createFile(file2); |
| 243 | + assertTrue(Files.exists(file1)); |
| 244 | + assertTrue(Files.exists(file2)); |
| 245 | + Files.move(file1, file2); |
| 246 | + Files.move(file1, file2, StandardCopyOption.REPLACE_EXISTING); |
| 247 | + assertTrue(Files.exists(file2)); |
| 248 | + assertFalse(Files.exists(file1)); |
| 249 | + } |
| 250 | +} |
0 commit comments