forked from junit-team/junit4
/
TempFolderRuleTest.java
239 lines (197 loc) · 6.75 KB
/
TempFolderRuleTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package org.junit.tests.experimental.rules;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.failureCountIs;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
public class TempFolderRuleTest {
private static File[] createdFiles= new File[20];
public static class HasTempFolder {
@Rule
public TemporaryFolder folder= new TemporaryFolder();
@Test
public void testUsingTempFolder() throws IOException {
createdFiles[0]= folder.newFile("myfile.txt");
assertTrue(createdFiles[0].exists());
}
}
@Test
public void tempFolderIsDeleted() {
assertThat(testResult(HasTempFolder.class), isSuccessful());
assertFalse(createdFiles[0].exists());
}
public static class CreatesSubFolder {
@Rule
public TemporaryFolder folder= new TemporaryFolder();
@Test
public void testUsingTempFolderStringReflection() throws Exception {
String subfolder = "subfolder";
String filename = "a.txt";
// force usage of folder.newFolder(String),
// check is available and works, to avoid a potential NoSuchMethodError with non-recompiled code.
Method method = folder.getClass().getMethod("newFolder", new Class<?>[] {String.class});
createdFiles[0]= (File) method.invoke(folder, subfolder);
new File(createdFiles[0], filename).createNewFile();
File expectedFile = new File(folder.getRoot(), join(subfolder, filename));
assertTrue(expectedFile.exists());
}
@Test
public void testUsingTempFolderString() throws IOException {
String subfolder = "subfolder";
String filename = "a.txt";
// this uses newFolder(String), ensure that a single String works
createdFiles[0]= folder.newFolder(subfolder);
new File(createdFiles[0], filename).createNewFile();
File expectedFile = new File(folder.getRoot(), join(subfolder, filename));
assertTrue(expectedFile.exists());
}
@Test
public void testUsingTempTreeFolders() throws IOException {
String subfolder = "subfolder";
String anotherfolder = "anotherfolder";
String filename = "a.txt";
createdFiles[0] = folder.newFolder(subfolder, anotherfolder);
new File(createdFiles[0], filename).createNewFile();
File expectedFile = new File(folder.getRoot(), join(subfolder, anotherfolder, filename));
assertTrue(expectedFile.exists());
}
private String join(String... folderNames) {
StringBuilder path = new StringBuilder();
for (String folderName : folderNames) {
path.append(File.separator).append(folderName);
}
return path.toString();
}
}
@Test
public void subFolderIsDeleted() {
assertThat(testResult(CreatesSubFolder.class), isSuccessful());
assertFalse(createdFiles[0].exists());
}
public static class CreatesRandomSubFolders {
@Rule
public TemporaryFolder folder= new TemporaryFolder();
@Test
public void testUsingRandomTempFolders() throws IOException {
for (int i= 0; i < 20; i++) {
File newFolder= folder.newFolder();
assertThat(Arrays.asList(createdFiles), not(hasItem(newFolder)));
createdFiles[i]= newFolder;
new File(newFolder, "a.txt").createNewFile();
assertTrue(newFolder.exists());
}
}
}
@Test
public void randomSubFoldersAreDeleted() {
assertThat(testResult(CreatesRandomSubFolders.class), isSuccessful());
for (File f : createdFiles) {
assertFalse(f.exists());
}
}
public static class CreatesRandomFiles {
@Rule
public TemporaryFolder folder= new TemporaryFolder();
@Test
public void testUsingRandomTempFiles() throws IOException {
for (int i= 0; i < 20; i++) {
File newFile= folder.newFile();
assertThat(Arrays.asList(createdFiles), not(hasItem(newFile)));
createdFiles[i]= newFile;
assertTrue(newFile.exists());
}
}
}
@Test
public void randomFilesAreDeleted() {
assertThat(testResult(CreatesRandomFiles.class), isSuccessful());
for (File f : createdFiles) {
assertFalse(f.exists());
}
}
@Test
public void recursiveDeleteFolderWithOneElement() throws IOException {
TemporaryFolder folder= new TemporaryFolder();
folder.create();
File file= folder.newFile("a");
folder.delete();
assertFalse(file.exists());
assertFalse(folder.getRoot().exists());
}
@Test
public void recursiveDeleteFolderWithOneRandomElement() throws IOException {
TemporaryFolder folder= new TemporaryFolder();
folder.create();
File file= folder.newFile();
folder.delete();
assertFalse(file.exists());
assertFalse(folder.getRoot().exists());
}
@Test
public void recursiveDeleteFolderWithZeroElements() throws IOException {
TemporaryFolder folder= new TemporaryFolder();
folder.create();
folder.delete();
assertFalse(folder.getRoot().exists());
}
public static class NameClashes {
@Rule
public TemporaryFolder folder= new TemporaryFolder();
@Test
public void fileWithFileClash() throws IOException {
folder.newFile("something.txt");
folder.newFile("something.txt");
}
@Test
public void fileWithFolderTest() throws IOException {
folder.newFolder("dummy");
folder.newFile("dummy");
}
}
@Test
public void nameClashesResultInTestFailures() {
assertThat(testResult(NameClashes.class), failureCountIs(2));
}
private static final String GET_ROOT_DUMMY= "dummy-getRoot";
private static final String NEW_FILE_DUMMY= "dummy-newFile";
private static final String NEW_FOLDER_DUMMY= "dummy-newFolder";
public static class IncorrectUsage {
public TemporaryFolder folder= new TemporaryFolder();
@Test
public void testGetRoot() throws IOException {
new File(folder.getRoot(), GET_ROOT_DUMMY).createNewFile();
}
@Test
public void testNewFile() throws IOException {
folder.newFile(NEW_FILE_DUMMY);
}
@Test
public void testNewFolder() throws IOException {
folder.newFolder(NEW_FOLDER_DUMMY);
}
}
@Test
public void incorrectUsageWithoutApplyingTheRuleShouldNotPolluteTheCurrentWorkingDirectory() {
assertThat(testResult(IncorrectUsage.class), failureCountIs(3));
assertFalse("getRoot should have failed early", new File(GET_ROOT_DUMMY).exists());
assertFalse("newFile should have failed early", new File(NEW_FILE_DUMMY).exists());
assertFalse("newFolder should have failed early", new File(NEW_FOLDER_DUMMY).exists());
}
@After
public void cleanCurrentWorkingDirectory() {
new File(GET_ROOT_DUMMY).delete();
new File(NEW_FILE_DUMMY).delete();
new File(NEW_FOLDER_DUMMY).delete();
}
}