| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8e5797e commit 8906f46
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,148 +11,171 @@ | |||
| 11 | 11 | public class FileClassDemoUnitTest { | |
| 12 | 12 | ||
| 13 | 13 | @Test | |
| 14 | - public void givenDirectoryCreated_whenMkdirIsInvoked_thenDirectoryIsDeleted() { | ||
| 15 | - File directory = new File("testDirectory"); | ||
| 16 | - if (!directory.isDirectory() || !directory.exists()) { | ||
| 17 | - directory.mkdir(); | ||
| 18 | - } | ||
| 19 | - | ||
| 14 | + public void givenDir_whenMkdir_thenDirIsDeleted() { | ||
| 15 | + File directory = new File("dir"); | ||
| 16 | + assertTrue(directory.mkdir()); | ||
| 20 | 17 | assertTrue(directory.delete()); | |
| 21 | 18 | } | |
| 22 | 19 | ||
| 23 | 20 | @Test | |
| 24 | - public void givenFileCreated_whenCreateNewFileIsInvoked_thenFileIsDeleted() throws IOException { | ||
| 25 | - File file = new File("testFile.txt"); | ||
| 26 | - if (!file.isFile() || !file.exists()) { | ||
| 27 | - file.createNewFile(); | ||
| 21 | + public void givenFile_whenCreateNewFile_thenFileIsDeleted() { | ||
| 22 | + File file = new File("file.txt"); | ||
| 23 | + try { | ||
| 24 | + assertTrue(file.createNewFile()); | ||
| 25 | + } catch (IOException e) { | ||
| 26 | + fail("Could not create " + "file.txt"); | ||
| 28 | 27 | } | |
| 29 | - | ||
| 30 | 28 | assertTrue(file.delete()); | |
| 31 | 29 | } | |
| 32 | 30 | ||
| 33 | - | ||
| 34 | 31 | @Test | |
| 35 | - public void givenFileCreated_whenCreateNewFileInvoked_thenMetadataIsAsExpected() throws IOException { | ||
| 32 | + public void givenFile_whenCreateNewFile_thenMetadataIsCorrect() { | ||
| 36 | 33 | ||
| 37 | - // different Operating systems have different separator characters | ||
| 38 | - String separatorCharacter = System.getProperty("file.separator"); | ||
| 34 | + String sep = File.separator; | ||
| 39 | 35 | ||
| 40 | - File parentDirectory = makeDirectory("filesDirectory"); | ||
| 36 | + File parentDir = makeDir("filesDir"); | ||
| 41 | 37 | ||
| 42 | - File childFile = new File(parentDirectory, "file1.txt"); | ||
| 43 | - childFile.createNewFile(); | ||
| 38 | + File child = new File(parentDir, "file.txt"); | ||
| 39 | + try { | ||
| 40 | + child.createNewFile(); | ||
| 41 | + } catch (IOException e) { | ||
| 42 | + fail("Could not create " + "file.txt"); | ||
| 43 | + } | ||
| 44 | 44 | ||
| 45 | - assertTrue(childFile.getName().equals("file1.txt")); | ||
| 46 | - assertTrue(childFile.getParentFile().getName().equals(parentDirectory.getName())); | ||
| 47 | - assertTrue(childFile.getPath().equals(parentDirectory.getPath() + separatorCharacter + "file1.txt")); | ||
| 45 | + assertEquals("file.txt", child.getName()); | ||
| 46 | + assertEquals(parentDir.getName(), child.getParentFile().getName()); | ||
| 47 | + assertEquals(parentDir.getPath() + sep + "file.txt", child.getPath()); | ||
| 48 | 48 | ||
| 49 | - removeDirectory(parentDirectory); | ||
| 49 | + removeDir(parentDir); | ||
| 50 | 50 | } | |
| 51 | 51 | ||
| 52 | 52 | ||
| 53 | - @Test(expected = FileNotFoundException.class) | ||
| 54 | - public void givenReadOnlyFileCreated_whenCreateNewFileInvoked_thenFileCannotBeWrittenTo() throws IOException { | ||
| 55 | - File parentDirectory = makeDirectory("filesDirectory"); | ||
| 56 | - | ||
| 57 | - File childFile = new File(parentDirectory, "file1.txt"); | ||
| 58 | - childFile.createNewFile(); | ||
| 59 | - | ||
| 60 | - childFile.setWritable(false); | ||
| 61 | - | ||
| 62 | - FileOutputStream fos = new FileOutputStream(childFile); | ||
| 63 | - fos.write("Hello World".getBytes()); // write operation | ||
| 64 | - fos.flush(); | ||
| 65 | - fos.close(); | ||
| 66 | - | ||
| 67 | - removeDirectory(parentDirectory); | ||
| 53 | + @Test | ||
| 54 | + public void givenReadOnlyFile_whenCreateNewFile_thenCantModFile() { | ||
| 55 | + File parentDir = makeDir("readDir"); | ||
| 56 | + | ||
| 57 | + File child = new File(parentDir, "file.txt"); | ||
| 58 | + try { | ||
| 59 | + child.createNewFile(); | ||
| 60 | + } catch (IOException e) { | ||
| 61 | + fail("Could not create " + "file.txt"); | ||
| 62 | + } | ||
| 63 | + child.setWritable(false); | ||
| 64 | + boolean writable = true; | ||
| 65 | + try (FileOutputStream fos = new FileOutputStream(child)) { | ||
| 66 | + fos.write("Hello World".getBytes()); // write operation | ||
| 67 | + fos.flush(); | ||
| 68 | + } catch (IOException e) { | ||
| 69 | + writable = false; | ||
| 70 | + } finally { | ||
| 71 | + removeDir(parentDir); | ||
| 72 | + } | ||
| 73 | + assertFalse(writable); | ||
| 68 | 74 | } | |
| 69 | 75 | ||
| 70 | - @Test(expected = FileNotFoundException.class) | ||
| 71 | - public void givenWriteOnlyFileCreated_whenCreateNewFileInvoked_thenFileCannotBeReadFrom() throws IOException { | ||
| 72 | - File parentDirectory = makeDirectory("filesDirectory"); | ||
| 73 | - | ||
| 74 | - File childFile = new File(parentDirectory, "file1.txt"); | ||
| 75 | - childFile.createNewFile(); | ||
| 76 | - | ||
| 77 | - childFile.setReadable(false); | ||
| 78 | - | ||
| 79 | - FileInputStream fis = new FileInputStream(childFile); | ||
| 80 | - fis.read(); // read operation | ||
| 81 | - fis.close(); | ||
| 82 | - | ||
| 83 | - removeDirectory(parentDirectory); | ||
| 76 | + @Test | ||
| 77 | + public void givenWriteOnlyFile_whenCreateNewFile_thenCantReadFile() { | ||
| 78 | + File parentDir = makeDir("writeDir"); | ||
| 79 | + | ||
| 80 | + File child = new File(parentDir, "file.txt"); | ||
| 81 | + try { | ||
| 82 | + child.createNewFile(); | ||
| 83 | + } catch (IOException e) { | ||
| 84 | + fail("Could not create " + "file.txt"); | ||
| 85 | + } | ||
| 86 | + child.setReadable(false); | ||
| 87 | + boolean readable = true; | ||
| 88 | + try (FileInputStream fis = new FileInputStream(child)) { | ||
| 89 | + fis.read(); // read operation | ||
| 90 | + } catch (IOException e) { | ||
| 91 | + readable = false; | ||
| 92 | + } finally { | ||
| 93 | + removeDir(parentDir); | ||
| 94 | + } | ||
| 95 | + assertFalse(readable); | ||
| 84 | 96 | } | |
| 85 | 97 | ||
| 86 | 98 | @Test | |
| 87 | - public void givenFilesCreatedInDirectory_whenCreateNewFileInvoked_thenTheyCanBeListedAsExpected() throws IOException { | ||
| 88 | - File directory = makeDirectory("filtersDirectory"); | ||
| 89 | - | ||
| 90 | - File csvFile = new File(directory, "csvFile.csv"); | ||
| 91 | - csvFile.createNewFile(); | ||
| 92 | - | ||
| 93 | - File txtFile = new File(directory, "txtFile.txt"); | ||
| 94 | - txtFile.createNewFile(); | ||
| 99 | + public void givenFilesInDir_whenCreateNewFile_thenCanListFiles() { | ||
| 100 | + File parentDir = makeDir("filtersDir"); | ||
| 101 | + | ||
| 102 | + String[] files = {"file1.csv", "file2.txt"}; | ||
| 103 | + for (String file : files) { | ||
| 104 | + try { | ||
| 105 | + new File(parentDir, file).createNewFile(); | ||
| 106 | + } catch (IOException e) { | ||
| 107 | + fail("Could not create " + file); | ||
| 108 | + } | ||
| 109 | + } | ||
| 95 | 110 | ||
| 96 | 111 | //normal listing | |
| 97 | - assertEquals(2, directory.list().length); | ||
| 112 | + assertEquals(2, parentDir.list().length); | ||
| 98 | 113 | ||
| 99 | 114 | //filtered listing | |
| 100 | 115 | FilenameFilter csvFilter = (dir, ext) -> ext.endsWith(".csv"); | |
| 101 | - assertEquals(1, directory.list(csvFilter).length); | ||
| 116 | + assertEquals(1, parentDir.list(csvFilter).length); | ||
| 102 | 117 | ||
| 103 | - removeDirectory(directory); | ||
| 118 | + removeDir(parentDir); | ||
| 104 | 119 | } | |
| 105 | 120 | ||
| 106 | 121 | @Test | |
| 107 | - public void givenDirectoryIsCreated_whenMkdirInvoked_thenDirectoryCanBeRenamed() { | ||
| 122 | + public void givenDir_whenMkdir_thenCanRenameDir() { | ||
| 108 | 123 | ||
| 109 | - File source = makeDirectory("source"); | ||
| 110 | - File destination = makeDirectory("destination"); | ||
| 111 | - source.renameTo(destination); | ||
| 124 | + File source = makeDir("source"); | ||
| 125 | + File destination = makeDir("destination"); | ||
| 126 | + boolean renamed = source.renameTo(destination); | ||
| 112 | 127 | ||
| 113 | - assertFalse(source.isDirectory()); | ||
| 114 | - assertTrue(destination.isDirectory()); | ||
| 128 | + if (renamed) { | ||
| 129 | + assertFalse(source.isDirectory()); | ||
| 130 | + assertTrue(destination.isDirectory()); | ||
| 115 | 131 | ||
| 116 | - removeDirectory(destination); | ||
| 132 | + removeDir(destination); | ||
| 133 | + } | ||
| 117 | 134 | } | |
| 118 | 135 | ||
| 119 | 136 | @Test | |
| 120 | - public void givenDataIsWrittenToFile_whenWriteIsInvoked_thenFreeSpaceOnSystemDecreases() throws IOException { | ||
| 137 | + public void givenDataWritten_whenWrite_thenFreeSpaceReduces() { | ||
| 121 | 138 | ||
| 122 | - String name = System.getProperty("user.home") + System.getProperty("file.separator") + "test"; | ||
| 123 | - File testDir = makeDirectory(name); | ||
| 139 | + String home = System.getProperty("user.home"); | ||
| 140 | + String sep = File.separator; | ||
| 141 | + File testDir = makeDir(home + sep + "test"); | ||
| 124 | 142 | File sample = new File(testDir, "sample.txt"); | |
| 125 | 143 | ||
| 126 | - long freeSpaceBeforeWrite = testDir.getFreeSpace(); | ||
| 127 | - writeSampleDataToFile(sample); | ||
| 144 | + long freeSpaceBefore = testDir.getFreeSpace(); | ||
| 145 | + try { | ||
| 146 | + writeSampleDataToFile(sample); | ||
| 147 | + } catch (IOException e) { | ||
| 148 | + fail("Could not write to " + "sample.txt"); | ||
| 149 | + } | ||
| 128 | 150 | ||
| 129 | - long freeSpaceAfterWrite = testDir.getFreeSpace(); | ||
| 130 | - assertTrue(freeSpaceAfterWrite < freeSpaceBeforeWrite); | ||
| 151 | + long freeSpaceAfter = testDir.getFreeSpace(); | ||
| 152 | + assertTrue(freeSpaceAfter < freeSpaceBefore); | ||
| 131 | 153 | ||
| 132 | - removeDirectory(testDir); | ||
| 154 | + removeDir(testDir); | ||
| 133 | 155 | } | |
| 134 | 156 | ||
| 135 | - private static File makeDirectory(String directoryName) { | ||
| 136 | - File directory = new File(directoryName); | ||
| 157 | + private static File makeDir(String name) { | ||
| 158 | + File directory = new File(name); | ||
| 137 | 159 | directory.mkdir(); | |
| 138 | 160 | if (directory.isDirectory()) { | |
| 139 | 161 | return directory; | |
| 140 | 162 | } | |
| 141 | - throw new RuntimeException("Directory not created for " + directoryName); | ||
| 163 | + throw new RuntimeException("'" + name + "' not made!"); | ||
| 142 | 164 | } | |
| 143 | 165 | ||
| 144 | - private static void removeDirectory(File directory) { | ||
| 166 | + private static void removeDir(File directory) { | ||
| 145 | 167 | // make sure you don't delete your home directory here | |
| 146 | - if (directory.getPath().equals(System.getProperty("user.home"))) { | ||
| 168 | + String home = System.getProperty("user.home"); | ||
| 169 | + if (directory.getPath().equals(home)) { | ||
| 147 | 170 | return; | |
| 148 | 171 | } | |
| 149 | 172 | ||
| 150 | 173 | // remove directory and its files from system | |
| 151 | - if (directory != null && directory.exists()) { | ||
| 174 | + if (directory.exists()) { | ||
| 152 | 175 | // delete all files inside the directory | |
| 153 | - File[] filesInDirectory = directory.listFiles(); | ||
| 154 | - if (filesInDirectory != null) { | ||
| 155 | - List<File> files = Arrays.asList(filesInDirectory); | ||
| 176 | + File[] dirFiles = directory.listFiles(); | ||
| 177 | + if (dirFiles != null) { | ||
| 178 | + List<File> files = Arrays.asList(dirFiles); | ||
| 156 | 179 | files.forEach(f -> deleteFile(f)); | |
| 157 | 180 | } | |
| 158 | 181 | ||
@@ -171,8 +194,8 @@ private static void writeSampleDataToFile(File sample) throws IOException { | |||
| 171 | 194 | //write sample text to file | |
| 172 | 195 | try (FileOutputStream out = new FileOutputStream(sample)) { | |
| 173 | 196 | for (int i = 1; i <= 100000; i++) { | |
| 174 | - String sampleText = "Sample line number " + i + "\n"; | ||
| 175 | - out.write(sampleText.getBytes()); | ||
| 197 | + String text = "Sample line number " + i + "\n"; | ||
| 198 | + out.write(text.getBytes()); | ||
| 176 | 199 | } | |
| 177 | 200 | } | |
| 178 | 201 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments