Skip to content

Commit 8906f46

Browse files
author
Thabo Ntsoko
committed
Shortening methods
1 parent 8e5797e commit 8906f46

1 file changed

Lines changed: 111 additions & 88 deletions

File tree

core-java-modules/core-java-io-2/src/test/java/com/baeldung/file/FileClassDemoUnitTest.java

Lines changed: 111 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -11,148 +11,171 @@
1111
public class FileClassDemoUnitTest {
1212

1313
@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());
2017
assertTrue(directory.delete());
2118
}
2219

2320
@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");
2827
}
29-
3028
assertTrue(file.delete());
3129
}
3230

33-
3431
@Test
35-
public void givenFileCreated_whenCreateNewFileInvoked_thenMetadataIsAsExpected() throws IOException {
32+
public void givenFile_whenCreateNewFile_thenMetadataIsCorrect() {
3633

37-
// different Operating systems have different separator characters
38-
String separatorCharacter = System.getProperty("file.separator");
34+
String sep = File.separator;
3935

40-
File parentDirectory = makeDirectory("filesDirectory");
36+
File parentDir = makeDir("filesDir");
4137

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+
}
4444

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());
4848

49-
removeDirectory(parentDirectory);
49+
removeDir(parentDir);
5050
}
5151

5252

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);
6874
}
6975

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);
8496
}
8597

8698
@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+
}
95110

96111
//normal listing
97-
assertEquals(2, directory.list().length);
112+
assertEquals(2, parentDir.list().length);
98113

99114
//filtered listing
100115
FilenameFilter csvFilter = (dir, ext) -> ext.endsWith(".csv");
101-
assertEquals(1, directory.list(csvFilter).length);
116+
assertEquals(1, parentDir.list(csvFilter).length);
102117

103-
removeDirectory(directory);
118+
removeDir(parentDir);
104119
}
105120

106121
@Test
107-
public void givenDirectoryIsCreated_whenMkdirInvoked_thenDirectoryCanBeRenamed() {
122+
public void givenDir_whenMkdir_thenCanRenameDir() {
108123

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);
112127

113-
assertFalse(source.isDirectory());
114-
assertTrue(destination.isDirectory());
128+
if (renamed) {
129+
assertFalse(source.isDirectory());
130+
assertTrue(destination.isDirectory());
115131

116-
removeDirectory(destination);
132+
removeDir(destination);
133+
}
117134
}
118135

119136
@Test
120-
public void givenDataIsWrittenToFile_whenWriteIsInvoked_thenFreeSpaceOnSystemDecreases() throws IOException {
137+
public void givenDataWritten_whenWrite_thenFreeSpaceReduces() {
121138

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");
124142
File sample = new File(testDir, "sample.txt");
125143

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+
}
128150

129-
long freeSpaceAfterWrite = testDir.getFreeSpace();
130-
assertTrue(freeSpaceAfterWrite < freeSpaceBeforeWrite);
151+
long freeSpaceAfter = testDir.getFreeSpace();
152+
assertTrue(freeSpaceAfter < freeSpaceBefore);
131153

132-
removeDirectory(testDir);
154+
removeDir(testDir);
133155
}
134156

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);
137159
directory.mkdir();
138160
if (directory.isDirectory()) {
139161
return directory;
140162
}
141-
throw new RuntimeException("Directory not created for " + directoryName);
163+
throw new RuntimeException("'" + name + "' not made!");
142164
}
143165

144-
private static void removeDirectory(File directory) {
166+
private static void removeDir(File directory) {
145167
// 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)) {
147170
return;
148171
}
149172

150173
// remove directory and its files from system
151-
if (directory != null && directory.exists()) {
174+
if (directory.exists()) {
152175
// 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);
156179
files.forEach(f -> deleteFile(f));
157180
}
158181

@@ -171,8 +194,8 @@ private static void writeSampleDataToFile(File sample) throws IOException {
171194
//write sample text to file
172195
try (FileOutputStream out = new FileOutputStream(sample)) {
173196
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());
176199
}
177200
}
178201
}

0 commit comments

Comments
 (0)