Shortening methods

This commit is contained in:
Thabo Ntsoko 2019-11-03 08:09:58 +02:00
parent f7d3b00ab3
commit a611831b37
1 changed files with 117 additions and 94 deletions

View File

@ -11,148 +11,171 @@ import static org.junit.Assert.*;
public class FileClassDemoUnitTest { public class FileClassDemoUnitTest {
@Test @Test
public void givenDirectoryCreated_whenMkdirIsInvoked_thenDirectoryIsDeleted() { public void givenDir_whenMkdir_thenDirIsDeleted() {
File directory = new File("testDirectory"); File directory = new File("dir");
if (!directory.isDirectory() || !directory.exists()) { assertTrue(directory.mkdir());
directory.mkdir();
}
assertTrue(directory.delete()); assertTrue(directory.delete());
} }
@Test @Test
public void givenFileCreated_whenCreateNewFileIsInvoked_thenFileIsDeleted() throws IOException { public void givenFile_whenCreateNewFile_thenFileIsDeleted() {
File file = new File("testFile.txt"); File file = new File("file.txt");
if (!file.isFile() || !file.exists()) { try {
file.createNewFile(); assertTrue(file.createNewFile());
} catch (IOException e) {
fail("Could not create " + "file.txt");
} }
assertTrue(file.delete()); assertTrue(file.delete());
} }
@Test
public void givenFile_whenCreateNewFile_thenMetadataIsCorrect() {
String sep = File.separator;
File parentDir = makeDir("filesDir");
File child = new File(parentDir, "file.txt");
try {
child.createNewFile();
} catch (IOException e) {
fail("Could not create " + "file.txt");
}
assertEquals("file.txt", child.getName());
assertEquals(parentDir.getName(), child.getParentFile().getName());
assertEquals(parentDir.getPath() + sep + "file.txt", child.getPath());
removeDir(parentDir);
}
@Test @Test
public void givenFileCreated_whenCreateNewFileInvoked_thenMetadataIsAsExpected() throws IOException { public void givenReadOnlyFile_whenCreateNewFile_thenCantModFile() {
File parentDir = makeDir("readDir");
// different Operating systems have different separator characters File child = new File(parentDir, "file.txt");
String separatorCharacter = System.getProperty("file.separator"); try {
child.createNewFile();
File parentDirectory = makeDirectory("filesDirectory"); } catch (IOException e) {
fail("Could not create " + "file.txt");
File childFile = new File(parentDirectory, "file1.txt"); }
childFile.createNewFile(); child.setWritable(false);
boolean writable = true;
assertTrue(childFile.getName().equals("file1.txt")); try (FileOutputStream fos = new FileOutputStream(child)) {
assertTrue(childFile.getParentFile().getName().equals(parentDirectory.getName())); fos.write("Hello World".getBytes()); // write operation
assertTrue(childFile.getPath().equals(parentDirectory.getPath() + separatorCharacter + "file1.txt")); fos.flush();
} catch (IOException e) {
removeDirectory(parentDirectory); writable = false;
} } finally {
removeDir(parentDir);
}
@Test(expected = FileNotFoundException.class) assertFalse(writable);
public void givenReadOnlyFileCreated_whenCreateNewFileInvoked_thenFileCannotBeWrittenTo() throws IOException {
File parentDirectory = makeDirectory("filesDirectory");
File childFile = new File(parentDirectory, "file1.txt");
childFile.createNewFile();
childFile.setWritable(false);
FileOutputStream fos = new FileOutputStream(childFile);
fos.write("Hello World".getBytes()); // write operation
fos.flush();
fos.close();
removeDirectory(parentDirectory);
}
@Test(expected = FileNotFoundException.class)
public void givenWriteOnlyFileCreated_whenCreateNewFileInvoked_thenFileCannotBeReadFrom() throws IOException {
File parentDirectory = makeDirectory("filesDirectory");
File childFile = new File(parentDirectory, "file1.txt");
childFile.createNewFile();
childFile.setReadable(false);
FileInputStream fis = new FileInputStream(childFile);
fis.read(); // read operation
fis.close();
removeDirectory(parentDirectory);
} }
@Test @Test
public void givenFilesCreatedInDirectory_whenCreateNewFileInvoked_thenTheyCanBeListedAsExpected() throws IOException { public void givenWriteOnlyFile_whenCreateNewFile_thenCantReadFile() {
File directory = makeDirectory("filtersDirectory"); File parentDir = makeDir("writeDir");
File csvFile = new File(directory, "csvFile.csv"); File child = new File(parentDir, "file.txt");
csvFile.createNewFile(); try {
child.createNewFile();
} catch (IOException e) {
fail("Could not create " + "file.txt");
}
child.setReadable(false);
boolean readable = true;
try (FileInputStream fis = new FileInputStream(child)) {
fis.read(); // read operation
} catch (IOException e) {
readable = false;
} finally {
removeDir(parentDir);
}
assertFalse(readable);
}
File txtFile = new File(directory, "txtFile.txt"); @Test
txtFile.createNewFile(); public void givenFilesInDir_whenCreateNewFile_thenCanListFiles() {
File parentDir = makeDir("filtersDir");
String[] files = {"file1.csv", "file2.txt"};
for (String file : files) {
try {
new File(parentDir, file).createNewFile();
} catch (IOException e) {
fail("Could not create " + file);
}
}
//normal listing //normal listing
assertEquals(2, directory.list().length); assertEquals(2, parentDir.list().length);
//filtered listing //filtered listing
FilenameFilter csvFilter = (dir, ext) -> ext.endsWith(".csv"); FilenameFilter csvFilter = (dir, ext) -> ext.endsWith(".csv");
assertEquals(1, directory.list(csvFilter).length); assertEquals(1, parentDir.list(csvFilter).length);
removeDirectory(directory); removeDir(parentDir);
} }
@Test @Test
public void givenDirectoryIsCreated_whenMkdirInvoked_thenDirectoryCanBeRenamed() { public void givenDir_whenMkdir_thenCanRenameDir() {
File source = makeDirectory("source"); File source = makeDir("source");
File destination = makeDirectory("destination"); File destination = makeDir("destination");
source.renameTo(destination); boolean renamed = source.renameTo(destination);
assertFalse(source.isDirectory()); if (renamed) {
assertTrue(destination.isDirectory()); assertFalse(source.isDirectory());
assertTrue(destination.isDirectory());
removeDirectory(destination); removeDir(destination);
}
} }
@Test @Test
public void givenDataIsWrittenToFile_whenWriteIsInvoked_thenFreeSpaceOnSystemDecreases() throws IOException { public void givenDataWritten_whenWrite_thenFreeSpaceReduces() {
String name = System.getProperty("user.home") + System.getProperty("file.separator") + "test"; String home = System.getProperty("user.home");
File testDir = makeDirectory(name); String sep = File.separator;
File testDir = makeDir(home + sep + "test");
File sample = new File(testDir, "sample.txt"); File sample = new File(testDir, "sample.txt");
long freeSpaceBeforeWrite = testDir.getFreeSpace(); long freeSpaceBefore = testDir.getFreeSpace();
writeSampleDataToFile(sample); try {
writeSampleDataToFile(sample);
} catch (IOException e) {
fail("Could not write to " + "sample.txt");
}
long freeSpaceAfterWrite = testDir.getFreeSpace(); long freeSpaceAfter = testDir.getFreeSpace();
assertTrue(freeSpaceAfterWrite < freeSpaceBeforeWrite); assertTrue(freeSpaceAfter < freeSpaceBefore);
removeDirectory(testDir); removeDir(testDir);
} }
private static File makeDirectory(String directoryName) { private static File makeDir(String name) {
File directory = new File(directoryName); File directory = new File(name);
directory.mkdir(); directory.mkdir();
if (directory.isDirectory()) { if (directory.isDirectory()) {
return directory; return directory;
} }
throw new RuntimeException("Directory not created for " + directoryName); throw new RuntimeException("'" + name + "' not made!");
} }
private static void removeDirectory(File directory) { private static void removeDir(File directory) {
// make sure you don't delete your home directory here // make sure you don't delete your home directory here
if (directory.getPath().equals(System.getProperty("user.home"))) { String home = System.getProperty("user.home");
if (directory.getPath().equals(home)) {
return; return;
} }
// remove directory and its files from system // remove directory and its files from system
if (directory != null && directory.exists()) { if (directory.exists()) {
// delete all files inside the directory // delete all files inside the directory
File[] filesInDirectory = directory.listFiles(); File[] dirFiles = directory.listFiles();
if (filesInDirectory != null) { if (dirFiles != null) {
List<File> files = Arrays.asList(filesInDirectory); List<File> files = Arrays.asList(dirFiles);
files.forEach(f -> deleteFile(f)); files.forEach(f -> deleteFile(f));
} }
@ -171,8 +194,8 @@ public class FileClassDemoUnitTest {
//write sample text to file //write sample text to file
try (FileOutputStream out = new FileOutputStream(sample)) { try (FileOutputStream out = new FileOutputStream(sample)) {
for (int i = 1; i <= 100000; i++) { for (int i = 1; i <= 100000; i++) {
String sampleText = "Sample line number " + i + "\n"; String text = "Sample line number " + i + "\n";
out.write(sampleText.getBytes()); out.write(text.getBytes());
} }
} }
} }