11package com .baeldung .java .nio2 ;
22
3- import static org .junit .Assert .assertFalse ;
4- import static org .junit .Assert .assertTrue ;
3+ import org .apache .commons .io .FileUtils ;
4+ import org .junit .AfterClass ;
5+ import org .junit .BeforeClass ;
6+ import org .junit .Test ;
57
8+ import java .io .File ;
69import java .io .IOException ;
7- import java .nio .file .DirectoryNotEmptyException ;
8- import java .nio .file .FileAlreadyExistsException ;
9- import java .nio .file .Files ;
10- import java .nio .file .NoSuchFileException ;
11- import java .nio .file .Path ;
12- import java .nio .file .Paths ;
13- import java .nio .file .StandardCopyOption ;
10+ import java .nio .file .*;
1411import java .util .UUID ;
1512
16- import org .junit .Test ;
13+ import static org .junit .Assert .assertFalse ;
14+ import static org .junit .Assert .assertTrue ;
1715
1816public class FileTest {
19- private static final String HOME = System .getProperty ("user.home" );
17+ private static final String TEMP_DIR = String .format ("%s/temp%s" , System .getProperty ("user.home" ), UUID .randomUUID ().toString ());
18+
19+ @ BeforeClass
20+ public static void setup () throws IOException {
21+ Files .createDirectory (Paths .get (TEMP_DIR ));
22+ }
23+
24+ @ AfterClass
25+ public static void cleanup () throws IOException {
26+ FileUtils .deleteDirectory (new File (TEMP_DIR ));
27+ }
2028
2129 // checking file or dir
2230 @ Test
2331 public void givenExistentPath_whenConfirmsFileExists_thenCorrect () {
24- Path p = Paths .get (HOME );
32+ Path p = Paths .get (TEMP_DIR );
2533 assertTrue (Files .exists (p ));
2634 }
2735
2836 @ Test
2937 public void givenNonexistentPath_whenConfirmsFileNotExists_thenCorrect () {
30- Path p = Paths .get (HOME + "/inexistent_file.txt" );
38+ Path p = Paths .get (TEMP_DIR + "/inexistent_file.txt" );
3139 assertTrue (Files .notExists (p ));
3240 }
3341
3442 @ Test
3543 public void givenDirPath_whenConfirmsNotRegularFile_thenCorrect () {
36- Path p = Paths .get (HOME );
44+ Path p = Paths .get (TEMP_DIR );
3745 assertFalse (Files .isRegularFile (p ));
3846 }
3947
4048 @ Test
4149 public void givenExistentDirPath_whenConfirmsReadable_thenCorrect () {
42- Path p = Paths .get (HOME );
50+ Path p = Paths .get (TEMP_DIR );
4351 assertTrue (Files .isReadable (p ));
4452 }
4553
4654 @ Test
4755 public void givenExistentDirPath_whenConfirmsWritable_thenCorrect () {
48- Path p = Paths .get (HOME );
56+ Path p = Paths .get (System . getProperty ( "user.home" ) );
4957 assertTrue (Files .isWritable (p ));
5058 }
5159
5260 @ Test
5361 public void givenExistentDirPath_whenConfirmsExecutable_thenCorrect () {
54- Path p = Paths .get (HOME );
62+ Path p = Paths .get (System . getProperty ( "user.home" ) );
5563 assertTrue (Files .isExecutable (p ));
5664 }
5765
5866 @ Test
5967 public void givenSameFilePaths_whenConfirmsIsSame_thenCorrect () throws IOException {
60- Path p1 = Paths .get (HOME );
61- Path p2 = Paths .get (HOME );
68+ Path p1 = Paths .get (TEMP_DIR );
69+ Path p2 = Paths .get (TEMP_DIR );
6270 assertTrue (Files .isSameFile (p1 , p2 ));
6371 }
6472
@@ -67,7 +75,7 @@ public void givenSameFilePaths_whenConfirmsIsSame_thenCorrect() throws IOExcepti
6775 @ Test
6876 public void givenFilePath_whenCreatesNewFile_thenCorrect () throws IOException {
6977 String fileName = "myfile_" + UUID .randomUUID ().toString () + ".txt" ;
70- Path p = Paths .get (HOME + "/" + fileName );
78+ Path p = Paths .get (TEMP_DIR + "/" + fileName );
7179 assertFalse (Files .exists (p ));
7280 Files .createFile (p );
7381 assertTrue (Files .exists (p ));
@@ -77,7 +85,7 @@ public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException {
7785 @ Test
7886 public void givenDirPath_whenCreatesNewDir_thenCorrect () throws IOException {
7987 String dirName = "myDir_" + UUID .randomUUID ().toString ();
80- Path p = Paths .get (HOME + "/" + dirName );
88+ Path p = Paths .get (TEMP_DIR + "/" + dirName );
8189 assertFalse (Files .exists (p ));
8290 Files .createDirectory (p );
8391 assertTrue (Files .exists (p ));
@@ -89,15 +97,15 @@ public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException {
8997 @ Test (expected = NoSuchFileException .class )
9098 public void givenDirPath_whenFailsToCreateRecursively_thenCorrect () throws IOException {
9199 String dirName = "myDir_" + UUID .randomUUID ().toString () + "/subdir" ;
92- Path p = Paths .get (HOME + "/" + dirName );
100+ Path p = Paths .get (TEMP_DIR + "/" + dirName );
93101 assertFalse (Files .exists (p ));
94102 Files .createDirectory (p );
95103
96104 }
97105
98106 @ Test
99107 public void givenDirPath_whenCreatesRecursively_thenCorrect () throws IOException {
100- Path dir = Paths .get (HOME + "/myDir_" + UUID .randomUUID ().toString ());
108+ Path dir = Paths .get (TEMP_DIR + "/myDir_" + UUID .randomUUID ().toString ());
101109 Path subdir = dir .resolve ("subdir" );
102110 assertFalse (Files .exists (dir ));
103111 assertFalse (Files .exists (subdir ));
@@ -110,7 +118,7 @@ public void givenDirPath_whenCreatesRecursively_thenCorrect() throws IOException
110118 public void givenFilePath_whenCreatesTempFile_thenCorrect () throws IOException {
111119 String prefix = "log_" ;
112120 String suffix = ".txt" ;
113- Path p = Paths .get (HOME + "/" );
121+ Path p = Paths .get (TEMP_DIR + "/" );
114122 p = Files .createTempFile (p , prefix , suffix );
115123 // like log_8821081429012075286.txt
116124 assertTrue (Files .exists (p ));
@@ -119,7 +127,7 @@ public void givenFilePath_whenCreatesTempFile_thenCorrect() throws IOException {
119127
120128 @ Test
121129 public void givenPath_whenCreatesTempFileWithDefaults_thenCorrect () throws IOException {
122- Path p = Paths .get (HOME + "/" );
130+ Path p = Paths .get (TEMP_DIR + "/" );
123131 p = Files .createTempFile (p , null , null );
124132 // like 8600179353689423985.tmp
125133 assertTrue (Files .exists (p ));
@@ -136,7 +144,7 @@ public void givenNoFilePath_whenCreatesTempFileInTempDir_thenCorrect() throws IO
136144 // delete file
137145 @ Test
138146 public void givenPath_whenDeletes_thenCorrect () throws IOException {
139- Path p = Paths .get (HOME + "/fileToDelete.txt" );
147+ Path p = Paths .get (TEMP_DIR + "/fileToDelete.txt" );
140148 assertFalse (Files .exists (p ));
141149 Files .createFile (p );
142150 assertTrue (Files .exists (p ));
@@ -147,7 +155,7 @@ public void givenPath_whenDeletes_thenCorrect() throws IOException {
147155
148156 @ Test (expected = DirectoryNotEmptyException .class )
149157 public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect () throws IOException {
150- Path dir = Paths .get (HOME + "/emptyDir" + UUID .randomUUID ().toString ());
158+ Path dir = Paths .get (TEMP_DIR + "/emptyDir" + UUID .randomUUID ().toString ());
151159 Files .createDirectory (dir );
152160 assertTrue (Files .exists (dir ));
153161 Path file = dir .resolve ("file.txt" );
@@ -160,15 +168,15 @@ public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOExcept
160168
161169 @ Test (expected = NoSuchFileException .class )
162170 public void givenInexistentFile_whenDeleteFails_thenCorrect () throws IOException {
163- Path p = Paths .get (HOME + "/inexistentFile.txt" );
171+ Path p = Paths .get (TEMP_DIR + "/inexistentFile.txt" );
164172 assertFalse (Files .exists (p ));
165173 Files .delete (p );
166174
167175 }
168176
169177 @ Test
170178 public void givenInexistentFile_whenDeleteIfExistsWorks_thenCorrect () throws IOException {
171- Path p = Paths .get (HOME + "/inexistentFile.txt" );
179+ Path p = Paths .get (TEMP_DIR + "/inexistentFile.txt" );
172180 assertFalse (Files .exists (p ));
173181 Files .deleteIfExists (p );
174182
@@ -177,8 +185,8 @@ public void givenInexistentFile_whenDeleteIfExistsWorks_thenCorrect() throws IOE
177185 // copy file
178186 @ Test
179187 public void givenFilePath_whenCopiesToNewLocation_thenCorrect () throws IOException {
180- Path dir1 = Paths .get (HOME + "/firstdir_" + UUID .randomUUID ().toString ());
181- Path dir2 = Paths .get (HOME + "/otherdir_" + UUID .randomUUID ().toString ());
188+ Path dir1 = Paths .get (TEMP_DIR + "/firstdir_" + UUID .randomUUID ().toString ());
189+ Path dir2 = Paths .get (TEMP_DIR + "/otherdir_" + UUID .randomUUID ().toString ());
182190 Files .createDirectory (dir1 );
183191 Files .createDirectory (dir2 );
184192 Path file1 = dir1 .resolve ("filetocopy.txt" );
@@ -193,8 +201,8 @@ public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOExcepti
193201
194202 @ Test (expected = FileAlreadyExistsException .class )
195203 public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect () throws IOException {
196- Path dir1 = Paths .get (HOME + "/firstdir_" + UUID .randomUUID ().toString ());
197- Path dir2 = Paths .get (HOME + "/otherdir_" + UUID .randomUUID ().toString ());
204+ Path dir1 = Paths .get (TEMP_DIR + "/firstdir_" + UUID .randomUUID ().toString ());
205+ Path dir2 = Paths .get (TEMP_DIR + "/otherdir_" + UUID .randomUUID ().toString ());
198206 Files .createDirectory (dir1 );
199207 Files .createDirectory (dir2 );
200208 Path file1 = dir1 .resolve ("filetocopy.txt" );
@@ -210,8 +218,8 @@ public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOExce
210218 // moving files
211219 @ Test
212220 public void givenFilePath_whenMovesToNewLocation_thenCorrect () throws IOException {
213- Path dir1 = Paths .get (HOME + "/firstdir_" + UUID .randomUUID ().toString ());
214- Path dir2 = Paths .get (HOME + "/otherdir_" + UUID .randomUUID ().toString ());
221+ Path dir1 = Paths .get (TEMP_DIR + "/firstdir_" + UUID .randomUUID ().toString ());
222+ Path dir2 = Paths .get (TEMP_DIR + "/otherdir_" + UUID .randomUUID ().toString ());
215223 Files .createDirectory (dir1 );
216224 Files .createDirectory (dir2 );
217225 Path file1 = dir1 .resolve ("filetocopy.txt" );
@@ -227,8 +235,8 @@ public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOExceptio
227235
228236 @ Test (expected = FileAlreadyExistsException .class )
229237 public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect () throws IOException {
230- Path dir1 = Paths .get (HOME + "/firstdir_" + UUID .randomUUID ().toString ());
231- Path dir2 = Paths .get (HOME + "/otherdir_" + UUID .randomUUID ().toString ());
238+ Path dir1 = Paths .get (TEMP_DIR + "/firstdir_" + UUID .randomUUID ().toString ());
239+ Path dir2 = Paths .get (TEMP_DIR + "/otherdir_" + UUID .randomUUID ().toString ());
232240 Files .createDirectory (dir1 );
233241 Files .createDirectory (dir2 );
234242 Path file1 = dir1 .resolve ("filetocopy.txt" );
0 commit comments