Skip to content

Commit 3e7e70e

Browse files
author
Grzegorz Piwowarek
committed
Refactor FileTest
1 parent cd23acd commit 3e7e70e

1 file changed

Lines changed: 20 additions & 33 deletions

File tree

‎core-java/src/test/java/com/baeldung/java/nio2/FileTest.java‎

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
package com.baeldung.java.nio2;
22

3-
import static org.junit.Assert.assertFalse;
4-
import static org.junit.Assert.assertTrue;
3+
import org.junit.Test;
54

65
import java.io.IOException;
7-
import java.nio.file.CopyOption;
8-
import java.nio.file.DirectoryNotEmptyException;
9-
import java.nio.file.FileAlreadyExistsException;
10-
import java.nio.file.Files;
11-
import java.nio.file.NoSuchFileException;
12-
import java.nio.file.Path;
13-
import java.nio.file.Paths;
14-
import java.nio.file.StandardCopyOption;
15-
import java.util.Date;
6+
import java.nio.file.*;
7+
import java.util.UUID;
168

17-
import org.junit.Test;
9+
import static org.junit.Assert.assertFalse;
10+
import static org.junit.Assert.assertTrue;
1811

1912
public class FileTest {
2013
private static final String HOME = System.getProperty("user.home");
@@ -67,7 +60,7 @@ public void givenSameFilePaths_whenConfirmsIsSame_thenCorrect() throws IOExcepti
6760
// creating file
6861
@Test
6962
public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException {
70-
String fileName = "myfile_" + new Date().getTime() + ".txt";
63+
String fileName = "myfile_" + UUID.randomUUID().toString() + ".txt";
7164
Path p = Paths.get(HOME + "/" + fileName);
7265
assertFalse(Files.exists(p));
7366
Files.createFile(p);
@@ -77,7 +70,7 @@ public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException {
7770

7871
@Test
7972
public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException {
80-
String dirName = "myDir_" + new Date().getTime();
73+
String dirName = "myDir_" + UUID.randomUUID().toString();
8174
Path p = Paths.get(HOME + "/" + dirName);
8275
assertFalse(Files.exists(p));
8376
Files.createDirectory(p);
@@ -89,16 +82,15 @@ public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException {
8982

9083
@Test(expected = NoSuchFileException.class)
9184
public void givenDirPath_whenFailsToCreateRecursively_thenCorrect() throws IOException {
92-
String dirName = "myDir_" + new Date().getTime() + "/subdir";
85+
String dirName = "myDir_" + UUID.randomUUID().toString() + "/subdir";
9386
Path p = Paths.get(HOME + "/" + dirName);
9487
assertFalse(Files.exists(p));
9588
Files.createDirectory(p);
96-
9789
}
9890

9991
@Test
10092
public void givenDirPath_whenCreatesRecursively_thenCorrect() throws IOException {
101-
Path dir = Paths.get(HOME + "/myDir_" + new Date().getTime());
93+
Path dir = Paths.get(HOME + "/myDir_" + UUID.randomUUID().toString());
10294
Path subdir = dir.resolve("subdir");
10395
assertFalse(Files.exists(dir));
10496
assertFalse(Files.exists(subdir));
@@ -148,7 +140,7 @@ public void givenPath_whenDeletes_thenCorrect() throws IOException {
148140

149141
@Test(expected = DirectoryNotEmptyException.class)
150142
public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOException {
151-
Path dir = Paths.get(HOME + "/emptyDir" + new Date().getTime());
143+
Path dir = Paths.get(HOME + "/emptyDir" + UUID.randomUUID().toString());
152144
Files.createDirectory(dir);
153145
assertTrue(Files.exists(dir));
154146
Path file = dir.resolve("file.txt");
@@ -159,16 +151,11 @@ public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOExcept
159151

160152
}
161153

162-
@Test
154+
@Test(expected = NoSuchFileException.class)
163155
public void givenInexistentFile_whenDeleteFails_thenCorrect() throws IOException {
164156
Path p = Paths.get(HOME + "/inexistentFile.txt");
165157
assertFalse(Files.exists(p));
166-
try {
167-
Files.delete(p);
168-
} catch (IOException e) {
169-
assertTrue(e instanceof NoSuchFileException);
170-
}
171-
158+
Files.delete(p);
172159
}
173160

174161
@Test
@@ -182,8 +169,8 @@ public void givenInexistentFile_whenDeleteIfExistsWorks_thenCorrect() throws IOE
182169
// copy file
183170
@Test
184171
public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOException {
185-
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
186-
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
172+
Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
173+
Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
187174
Files.createDirectory(dir1);
188175
Files.createDirectory(dir2);
189176
Path file1 = dir1.resolve("filetocopy.txt");
@@ -198,8 +185,8 @@ public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOExcepti
198185

199186
@Test(expected = FileAlreadyExistsException.class)
200187
public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOException {
201-
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
202-
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
188+
Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
189+
Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
203190
Files.createDirectory(dir1);
204191
Files.createDirectory(dir2);
205192
Path file1 = dir1.resolve("filetocopy.txt");
@@ -215,8 +202,8 @@ public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOExce
215202
// moving files
216203
@Test
217204
public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOException {
218-
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
219-
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
205+
Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
206+
Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
220207
Files.createDirectory(dir1);
221208
Files.createDirectory(dir2);
222209
Path file1 = dir1.resolve("filetocopy.txt");
@@ -232,8 +219,8 @@ public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOExceptio
232219

233220
@Test(expected = FileAlreadyExistsException.class)
234221
public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect() throws IOException {
235-
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
236-
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
222+
Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
223+
Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
237224
Files.createDirectory(dir1);
238225
Files.createDirectory(dir2);
239226
Path file1 = dir1.resolve("filetocopy.txt");

0 commit comments

Comments
 (0)