diff --git a/core-java/src/test/java/com/baeldung/java/nio2/FileTest.java b/core-java/src/test/java/com/baeldung/java/nio2/FileTest.java index 4a1ad1da60..e8b10fe767 100644 --- a/core-java/src/test/java/com/baeldung/java/nio2/FileTest.java +++ b/core-java/src/test/java/com/baeldung/java/nio2/FileTest.java @@ -1,21 +1,14 @@ 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.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 { private static final String HOME = System.getProperty("user.home"); @@ -67,7 +60,7 @@ public class FileTest { // creating file @Test 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); assertFalse(Files.exists(p)); Files.createFile(p); @@ -77,7 +70,7 @@ public class FileTest { @Test 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); assertFalse(Files.exists(p)); Files.createDirectory(p); @@ -89,16 +82,15 @@ public class FileTest { @Test(expected = NoSuchFileException.class) 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); assertFalse(Files.exists(p)); Files.createDirectory(p); - } @Test 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"); assertFalse(Files.exists(dir)); assertFalse(Files.exists(subdir)); @@ -148,7 +140,7 @@ public class FileTest { @Test(expected = DirectoryNotEmptyException.class) 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); assertTrue(Files.exists(dir)); 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 { Path p = Paths.get(HOME + "/inexistentFile.txt"); assertFalse(Files.exists(p)); - try { - Files.delete(p); - } catch (IOException e) { - assertTrue(e instanceof NoSuchFileException); - } - + Files.delete(p); } @Test @@ -182,8 +169,8 @@ public class FileTest { // copy file @Test public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOException { - Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); - Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); + Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString()); + Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString()); Files.createDirectory(dir1); Files.createDirectory(dir2); Path file1 = dir1.resolve("filetocopy.txt"); @@ -198,8 +185,8 @@ public class FileTest { @Test(expected = FileAlreadyExistsException.class) public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOException { - Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); - Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); + Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString()); + Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString()); Files.createDirectory(dir1); Files.createDirectory(dir2); Path file1 = dir1.resolve("filetocopy.txt"); @@ -215,8 +202,8 @@ public class FileTest { // moving files @Test public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOException { - Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); - Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); + Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString()); + Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString()); Files.createDirectory(dir1); Files.createDirectory(dir2); Path file1 = dir1.resolve("filetocopy.txt"); @@ -232,8 +219,8 @@ public class FileTest { @Test(expected = FileAlreadyExistsException.class) public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect() throws IOException { - Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime()); - Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime()); + Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString()); + Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString()); Files.createDirectory(dir1); Files.createDirectory(dir2); Path file1 = dir1.resolve("filetocopy.txt");