Refactor FileTest

This commit is contained in:
Grzegorz Piwowarek 2016-11-01 07:26:04 +00:00
parent cd23acde59
commit 3e7e70eb33

View File

@ -1,21 +1,14 @@
package com.baeldung.java.nio2; package com.baeldung.java.nio2;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.*;
import java.util.UUID;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import org.junit.Test;
public class FileTest { public class FileTest {
private static final String HOME = System.getProperty("user.home"); private static final String HOME = System.getProperty("user.home");
@ -67,7 +60,7 @@ public class FileTest {
// creating file // creating file
@Test @Test
public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException { public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException {
String fileName = "myfile_" + new Date().getTime() + ".txt"; String fileName = "myfile_" + UUID.randomUUID().toString() + ".txt";
Path p = Paths.get(HOME + "/" + fileName); Path p = Paths.get(HOME + "/" + fileName);
assertFalse(Files.exists(p)); assertFalse(Files.exists(p));
Files.createFile(p); Files.createFile(p);
@ -77,7 +70,7 @@ public class FileTest {
@Test @Test
public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException { public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException {
String dirName = "myDir_" + new Date().getTime(); String dirName = "myDir_" + UUID.randomUUID().toString();
Path p = Paths.get(HOME + "/" + dirName); Path p = Paths.get(HOME + "/" + dirName);
assertFalse(Files.exists(p)); assertFalse(Files.exists(p));
Files.createDirectory(p); Files.createDirectory(p);
@ -89,16 +82,15 @@ public class FileTest {
@Test(expected = NoSuchFileException.class) @Test(expected = NoSuchFileException.class)
public void givenDirPath_whenFailsToCreateRecursively_thenCorrect() throws IOException { public void givenDirPath_whenFailsToCreateRecursively_thenCorrect() throws IOException {
String dirName = "myDir_" + new Date().getTime() + "/subdir"; String dirName = "myDir_" + UUID.randomUUID().toString() + "/subdir";
Path p = Paths.get(HOME + "/" + dirName); Path p = Paths.get(HOME + "/" + dirName);
assertFalse(Files.exists(p)); assertFalse(Files.exists(p));
Files.createDirectory(p); Files.createDirectory(p);
} }
@Test @Test
public void givenDirPath_whenCreatesRecursively_thenCorrect() throws IOException { public void givenDirPath_whenCreatesRecursively_thenCorrect() throws IOException {
Path dir = Paths.get(HOME + "/myDir_" + new Date().getTime()); Path dir = Paths.get(HOME + "/myDir_" + UUID.randomUUID().toString());
Path subdir = dir.resolve("subdir"); Path subdir = dir.resolve("subdir");
assertFalse(Files.exists(dir)); assertFalse(Files.exists(dir));
assertFalse(Files.exists(subdir)); assertFalse(Files.exists(subdir));
@ -148,7 +140,7 @@ public class FileTest {
@Test(expected = DirectoryNotEmptyException.class) @Test(expected = DirectoryNotEmptyException.class)
public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOException { public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOException {
Path dir = Paths.get(HOME + "/emptyDir" + new Date().getTime()); Path dir = Paths.get(HOME + "/emptyDir" + UUID.randomUUID().toString());
Files.createDirectory(dir); Files.createDirectory(dir);
assertTrue(Files.exists(dir)); assertTrue(Files.exists(dir));
Path file = dir.resolve("file.txt"); Path file = dir.resolve("file.txt");
@ -159,16 +151,11 @@ public class FileTest {
} }
@Test @Test(expected = NoSuchFileException.class)
public void givenInexistentFile_whenDeleteFails_thenCorrect() throws IOException { public void givenInexistentFile_whenDeleteFails_thenCorrect() throws IOException {
Path p = Paths.get(HOME + "/inexistentFile.txt"); Path p = Paths.get(HOME + "/inexistentFile.txt");
assertFalse(Files.exists(p)); assertFalse(Files.exists(p));
try { Files.delete(p);
Files.delete(p);
} catch (IOException e) {
assertTrue(e instanceof NoSuchFileException);
}
} }
@Test @Test
@ -182,8 +169,8 @@ public class FileTest {
// copy file // copy file
@Test @Test
public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOException { public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOException {
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
Files.createDirectory(dir1); Files.createDirectory(dir1);
Files.createDirectory(dir2); Files.createDirectory(dir2);
Path file1 = dir1.resolve("filetocopy.txt"); Path file1 = dir1.resolve("filetocopy.txt");
@ -198,8 +185,8 @@ public class FileTest {
@Test(expected = FileAlreadyExistsException.class) @Test(expected = FileAlreadyExistsException.class)
public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOException { public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOException {
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
Files.createDirectory(dir1); Files.createDirectory(dir1);
Files.createDirectory(dir2); Files.createDirectory(dir2);
Path file1 = dir1.resolve("filetocopy.txt"); Path file1 = dir1.resolve("filetocopy.txt");
@ -215,8 +202,8 @@ public class FileTest {
// moving files // moving files
@Test @Test
public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOException { public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOException {
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
Files.createDirectory(dir1); Files.createDirectory(dir1);
Files.createDirectory(dir2); Files.createDirectory(dir2);
Path file1 = dir1.resolve("filetocopy.txt"); Path file1 = dir1.resolve("filetocopy.txt");
@ -232,8 +219,8 @@ public class FileTest {
@Test(expected = FileAlreadyExistsException.class) @Test(expected = FileAlreadyExistsException.class)
public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect() throws IOException { public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect() throws IOException {
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
Files.createDirectory(dir1); Files.createDirectory(dir1);
Files.createDirectory(dir2); Files.createDirectory(dir2);
Path file1 = dir1.resolve("filetocopy.txt"); Path file1 = dir1.resolve("filetocopy.txt");