|
1 | 1 | package org.baeldung.java.io; |
2 | 2 |
|
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; |
5 | 7 |
|
6 | 8 | import java.io.File; |
7 | 9 | import java.io.IOException; |
8 | 10 | import java.nio.file.FileSystemException; |
9 | 11 | import java.nio.file.Files; |
10 | 12 | import java.nio.file.Path; |
11 | 13 | import java.nio.file.Paths; |
| 14 | +import java.util.UUID; |
12 | 15 |
|
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; |
15 | 18 |
|
16 | 19 | public class JavaFileUnitTest { |
17 | 20 |
|
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 | + } |
19 | 33 |
|
20 | 34 | @Test |
21 | 35 | 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"); |
23 | 37 | final boolean success = newFile.createNewFile(); |
24 | 38 |
|
25 | 39 | assertTrue(success); |
26 | 40 | } |
27 | 41 |
|
28 | 42 | @Test |
29 | 43 | 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"); |
31 | 45 | Files.createFile(newFilePath); |
32 | 46 | } |
33 | 47 |
|
34 | 48 | @Test |
35 | 49 | 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")); |
37 | 51 | } |
38 | 52 |
|
39 | 53 | @Test |
40 | 54 | 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")); |
42 | 56 | } |
43 | 57 |
|
44 | 58 | // move a file |
45 | 59 |
|
46 | 60 | @Test |
47 | 61 | 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"); |
49 | 63 | fileToMove.createNewFile();// .exists(); |
50 | | - final File destDir = new File("src/test/resources/"); |
| 64 | + final File destDir = new File(TEMP_DIR + "/"); |
51 | 65 | destDir.mkdir(); |
52 | 66 |
|
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")); |
54 | 68 | if (!isMoved) { |
55 | | - throw new FileSystemException("src/test/resources/movedFile_jdk6.txt"); |
| 69 | + throw new FileSystemException(TEMP_DIR + "/movedFile_jdk6.txt"); |
56 | 70 | } |
57 | 71 | } |
58 | 72 |
|
59 | 73 | @Test |
60 | 74 | 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 + "/"); |
63 | 77 |
|
64 | 78 | Files.move(fileToMovePath, targetPath.resolve(fileToMovePath.getFileName())); |
65 | 79 | } |
66 | 80 |
|
67 | 81 | @Test |
68 | 82 | 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"); |
70 | 84 | fileToMove.createNewFile(); |
71 | | - final File destDir = new File("src/main/resources/"); |
| 85 | + final File destDir = new File(TEMP_DIR + "/temp"); |
72 | 86 | final File targetFile = new File(destDir, fileToMove.getName()); |
73 | 87 | com.google.common.io.Files.createParentDirs(targetFile); |
74 | 88 | com.google.common.io.Files.move(fileToMove, targetFile); |
75 | 89 | } |
76 | 90 |
|
77 | 91 | @Test |
78 | 92 | 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")); |
81 | 95 | } |
82 | 96 |
|
83 | 97 | @Test |
84 | 98 | 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); |
87 | 102 | } |
88 | 103 |
|
89 | 104 | // delete a file |
90 | 105 |
|
91 | 106 | @Test |
92 | 107 | 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(); |
94 | 109 |
|
95 | | - final File fileToDelete = new File("src/test/resources/fileToDelete_jdk6.txt"); |
| 110 | + final File fileToDelete = new File(TEMP_DIR + "/fileToDelete_jdk6.txt"); |
96 | 111 | final boolean success = fileToDelete.delete(); |
97 | 112 |
|
98 | 113 | assertTrue(success); |
99 | 114 | } |
100 | 115 |
|
101 | 116 | @Test |
102 | 117 | 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")); |
104 | 119 |
|
105 | | - final Path fileToDeletePath = Paths.get("src/test/resources/fileToDelete_jdk7.txt"); |
| 120 | + final Path fileToDeletePath = Paths.get(TEMP_DIR + "/fileToDelete_jdk7.txt"); |
106 | 121 | Files.delete(fileToDeletePath); |
107 | 122 | } |
108 | 123 |
|
109 | 124 | @Test |
110 | 125 | 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")); |
112 | 127 |
|
113 | | - final File fileToDelete = FileUtils.getFile("src/test/resources/fileToDelete_commonsIo.txt"); |
| 128 | + final File fileToDelete = FileUtils.getFile(TEMP_DIR + "/fileToDelete_commonsIo.txt"); |
114 | 129 | final boolean success = FileUtils.deleteQuietly(fileToDelete); |
115 | 130 |
|
116 | 131 | assertTrue(success); |
117 | 132 | } |
118 | 133 |
|
119 | 134 | @Test |
120 | 135 | 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")); |
122 | 137 |
|
123 | | - FileUtils.forceDelete(FileUtils.getFile("src/test/resources/fileToDelete.txt")); |
| 138 | + FileUtils.forceDelete(FileUtils.getFile(TEMP_DIR + "/fileToDelete.txt")); |
124 | 139 | } |
125 | 140 |
|
126 | 141 | } |
0 commit comments