Skip to content

Commit 1f00652

Browse files
Merge branch 'master' into pr/850-yasser-generics
2 parents 6835625 + b846bcc commit 1f00652

9 files changed

Lines changed: 119 additions & 99 deletions

File tree

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

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,72 @@
11
package 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;
69
import 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.*;
1411
import java.util.UUID;
1512

16-
import org.junit.Test;
13+
import static org.junit.Assert.assertFalse;
14+
import static org.junit.Assert.assertTrue;
1715

1816
public 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");
Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,141 @@
11
package org.baeldung.java.io;
22

3-
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
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

68
import java.io.File;
79
import java.io.IOException;
810
import java.nio.file.FileSystemException;
911
import java.nio.file.Files;
1012
import java.nio.file.Path;
1113
import java.nio.file.Paths;
14+
import java.util.UUID;
1215

13-
import org.apache.commons.io.FileUtils;
14-
import org.junit.Test;
16+
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
17+
import static org.junit.Assert.assertTrue;
1518

1619
public class JavaFileUnitTest {
1720

18-
// create a file
21+
private static final String TEMP_DIR = "src/test/resources/temp" + UUID.randomUUID().toString();
22+
23+
24+
@BeforeClass
25+
public static void setup() throws IOException {
26+
Files.createDirectory(Paths.get(TEMP_DIR));
27+
}
28+
29+
@AfterClass
30+
public static void cleanup() throws IOException {
31+
FileUtils.deleteDirectory(new File(TEMP_DIR));
32+
}
1933

2034
@Test
2135
public final void givenUsingJDK6_whenCreatingFile_thenCorrect() throws IOException {
22-
final File newFile = new File("src/test/resources/newFile_jdk6.txt");
36+
final File newFile = new File(TEMP_DIR + "/newFile_jdk6.txt");
2337
final boolean success = newFile.createNewFile();
2438

2539
assertTrue(success);
2640
}
2741

2842
@Test
2943
public final void givenUsingJDK7nio2_whenCreatingFile_thenCorrect() throws IOException {
30-
final Path newFilePath = Paths.get("src/test/resources/newFile_jdk7.txt");
44+
final Path newFilePath = Paths.get(TEMP_DIR + "/newFile_jdk7.txt");
3145
Files.createFile(newFilePath);
3246
}
3347

3448
@Test
3549
public final void givenUsingCommonsIo_whenCreatingFile_thenCorrect() throws IOException {
36-
FileUtils.touch(new File("src/test/resources/newFile_commonsio.txt"));
50+
FileUtils.touch(new File(TEMP_DIR + "/newFile_commonsio.txt"));
3751
}
3852

3953
@Test
4054
public final void givenUsingGuava_whenCreatingFile_thenCorrect() throws IOException {
41-
com.google.common.io.Files.touch(new File("src/test/resources/newFile_guava.txt"));
55+
com.google.common.io.Files.touch(new File(TEMP_DIR + "/newFile_guava.txt"));
4256
}
4357

4458
// move a file
4559

4660
@Test
4761
public final void givenUsingJDK6_whenMovingFile_thenCorrect() throws IOException {
48-
final File fileToMove = new File("src/test/resources/toMoveFile_jdk6.txt");
62+
final File fileToMove = new File(TEMP_DIR + "/toMoveFile_jdk6.txt");
4963
fileToMove.createNewFile();// .exists();
50-
final File destDir = new File("src/test/resources/");
64+
final File destDir = new File(TEMP_DIR + "/");
5165
destDir.mkdir();
5266

53-
final boolean isMoved = fileToMove.renameTo(new File("src/test/resources/movedFile_jdk6.txt"));
67+
final boolean isMoved = fileToMove.renameTo(new File(TEMP_DIR + "/movedFile_jdk6.txt"));
5468
if (!isMoved) {
55-
throw new FileSystemException("src/test/resources/movedFile_jdk6.txt");
69+
throw new FileSystemException(TEMP_DIR + "/movedFile_jdk6.txt");
5670
}
5771
}
5872

5973
@Test
6074
public final void givenUsingJDK7Nio2_whenMovingFile_thenCorrect() throws IOException {
61-
final Path fileToMovePath = Files.createFile(Paths.get("src/test/resources/" + randomAlphabetic(5) + ".txt"));
62-
final Path targetPath = Paths.get("src/main/resources/");
75+
final Path fileToMovePath = Files.createFile(Paths.get(TEMP_DIR + "/" + randomAlphabetic(5) + ".txt"));
76+
final Path targetPath = Paths.get(TEMP_DIR + "/");
6377

6478
Files.move(fileToMovePath, targetPath.resolve(fileToMovePath.getFileName()));
6579
}
6680

6781
@Test
6882
public final void givenUsingGuava_whenMovingFile_thenCorrect() throws IOException {
69-
final File fileToMove = new File("src/test/resources/fileToMove.txt");
83+
final File fileToMove = new File(TEMP_DIR + "/fileToMove.txt");
7084
fileToMove.createNewFile();
71-
final File destDir = new File("src/main/resources/");
85+
final File destDir = new File(TEMP_DIR + "/temp");
7286
final File targetFile = new File(destDir, fileToMove.getName());
7387
com.google.common.io.Files.createParentDirs(targetFile);
7488
com.google.common.io.Files.move(fileToMove, targetFile);
7589
}
7690

7791
@Test
7892
public final void givenUsingApache_whenMovingFile_thenCorrect() throws IOException {
79-
FileUtils.touch(new File("src/test/resources/fileToMove_apache.txt"));
80-
FileUtils.moveFile(FileUtils.getFile("src/test/resources/fileToMove_apache.txt"), FileUtils.getFile("src/test/resources/fileMoved_apache2.txt"));
93+
FileUtils.touch(new File(TEMP_DIR + "/fileToMove_apache.txt"));
94+
FileUtils.moveFile(FileUtils.getFile(TEMP_DIR + "/fileToMove_apache.txt"), FileUtils.getFile(TEMP_DIR + "/fileMoved_apache2.txt"));
8195
}
8296

8397
@Test
8498
public final void givenUsingApache_whenMovingFileApproach2_thenCorrect() throws IOException {
85-
FileUtils.touch(new File("src/test/resources/fileToMove_apache.txt"));
86-
FileUtils.moveFileToDirectory(FileUtils.getFile("src/test/resources/fileToMove_apache.txt"), FileUtils.getFile("src/main/resources/"), true);
99+
FileUtils.touch(new File(TEMP_DIR + "/fileToMove_apache.txt"));
100+
Files.createDirectory(Paths.get(TEMP_DIR + "/temp"));
101+
FileUtils.moveFileToDirectory(FileUtils.getFile(TEMP_DIR + "/fileToMove_apache.txt"), FileUtils.getFile(TEMP_DIR + "/temp"), true);
87102
}
88103

89104
// delete a file
90105

91106
@Test
92107
public final void givenUsingJDK6_whenDeletingAFile_thenCorrect() throws IOException {
93-
new File("src/test/resources/fileToDelete_jdk6.txt").createNewFile();
108+
new File(TEMP_DIR + "/fileToDelete_jdk6.txt").createNewFile();
94109

95-
final File fileToDelete = new File("src/test/resources/fileToDelete_jdk6.txt");
110+
final File fileToDelete = new File(TEMP_DIR + "/fileToDelete_jdk6.txt");
96111
final boolean success = fileToDelete.delete();
97112

98113
assertTrue(success);
99114
}
100115

101116
@Test
102117
public final void givenUsingJDK7nio2_whenDeletingAFile_thenCorrect() throws IOException {
103-
Files.createFile(Paths.get("src/test/resources/fileToDelete_jdk7.txt"));
118+
Files.createFile(Paths.get(TEMP_DIR + "/fileToDelete_jdk7.txt"));
104119

105-
final Path fileToDeletePath = Paths.get("src/test/resources/fileToDelete_jdk7.txt");
120+
final Path fileToDeletePath = Paths.get(TEMP_DIR + "/fileToDelete_jdk7.txt");
106121
Files.delete(fileToDeletePath);
107122
}
108123

109124
@Test
110125
public final void givenUsingCommonsIo_whenDeletingAFileV1_thenCorrect() throws IOException {
111-
FileUtils.touch(new File("src/test/resources/fileToDelete_commonsIo.txt"));
126+
FileUtils.touch(new File(TEMP_DIR + "/fileToDelete_commonsIo.txt"));
112127

113-
final File fileToDelete = FileUtils.getFile("src/test/resources/fileToDelete_commonsIo.txt");
128+
final File fileToDelete = FileUtils.getFile(TEMP_DIR + "/fileToDelete_commonsIo.txt");
114129
final boolean success = FileUtils.deleteQuietly(fileToDelete);
115130

116131
assertTrue(success);
117132
}
118133

119134
@Test
120135
public void givenUsingCommonsIo_whenDeletingAFileV2_thenCorrect() throws IOException {
121-
FileUtils.touch(new File("src/test/resources/fileToDelete.txt"));
136+
FileUtils.touch(new File(TEMP_DIR + "/fileToDelete.txt"));
122137

123-
FileUtils.forceDelete(FileUtils.getFile("src/test/resources/fileToDelete.txt"));
138+
FileUtils.forceDelete(FileUtils.getFile(TEMP_DIR + "/fileToDelete.txt"));
124139
}
125140

126141
}

0 commit comments

Comments
 (0)