forked from junit-team/junit4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TemporaryFolderUsageTest.java
144 lines (115 loc) · 3.86 KB
/
TemporaryFolderUsageTest.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
package org.junit.tests.experimental.rules;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import java.io.File;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
/**
* <tt>TemporaryFolderUsageTest</tt> provides tests for API usage correctness
* and ensure implementation symmetry of public methods against a root folder.
*/
public class TemporaryFolderUsageTest {
private TemporaryFolder tempFolder;
@Before
public void setUp() {
tempFolder= new TemporaryFolder();
}
@After
public void tearDown() {
tempFolder.delete();
}
@Test(expected= IllegalStateException.class)
public void getRootShouldThrowIllegalStateExceptionIfCreateWasNotInvoked() {
new TemporaryFolder().getRoot();
}
@Test(expected= IllegalStateException.class)
public void newFileThrowsIllegalStateExceptionIfCreateWasNotInvoked()
throws IOException {
new TemporaryFolder().newFile();
}
@Test(expected= IllegalStateException.class)
public void newFileWithGivenNameThrowsIllegalStateExceptionIfCreateWasNotInvoked()
throws IOException {
new TemporaryFolder().newFile("MyFile.txt");
}
@Test(expected= IllegalStateException.class)
public void newFolderThrowsIllegalStateExceptionIfCreateWasNotInvoked()
throws IOException {
new TemporaryFolder().newFolder();
}
@Test(expected= IllegalStateException.class)
public void newFolderWithGivenPathThrowsIllegalStateExceptionIfCreateWasNotInvoked() {
new TemporaryFolder().newFolder("level1", "leve2", "leve3");
}
@Test
public void createInitializesRootFolder() throws IOException {
tempFolder.create();
assertFileExists(tempFolder.getRoot());
}
@Test
public void deleteShouldDoNothingIfRootFolderWasNotInitialized() {
tempFolder.delete();
}
@Test
public void deleteRemovesRootFolder() throws IOException {
tempFolder.create();
tempFolder.delete();
assertFileDoesNotExist(tempFolder.getRoot());
}
@Test
public void newRandomFileIsCreatedUnderRootFolder() throws IOException {
tempFolder.create();
File f= tempFolder.newFile();
assertFileExists(f);
assertFileCreatedUnderRootFolder("Random file", f);
}
@Test
public void newNamedFileIsCreatedUnderRootFolder() throws IOException {
final String fileName= "SampleFile.txt";
tempFolder.create();
File f= tempFolder.newFile(fileName);
assertFileExists(f);
assertFileCreatedUnderRootFolder("Named file", f);
assertThat("file name", f.getName(), equalTo(fileName));
}
@Test
public void newRandomFolderIsCreatedUnderRootFolder() throws IOException {
tempFolder.create();
File f= tempFolder.newFolder();
assertFileExists(f);
assertFileCreatedUnderRootFolder("Random folder", f);
}
@Test
public void newNestedFoldersCreatedUnderRootFolder() throws IOException {
tempFolder.create();
File f= tempFolder.newFolder("top", "middle", "bottom");
assertFileExists(f);
assertParentFolderForFileIs(f, new File(tempFolder.getRoot(),
"top/middle"));
assertParentFolderForFileIs(f.getParentFile(),
new File(tempFolder.getRoot(), "top"));
assertFileCreatedUnderRootFolder("top", f.getParentFile()
.getParentFile());
}
private void assertFileDoesNotExist(File file) {
checkFileExists("exists", file, false);
}
private void checkFileExists(String msg, File file, boolean exists) {
assertThat("File is null", file, is(notNullValue()));
assertThat("File '" + file.getAbsolutePath() + "' " + msg,
file.exists(), is(exists));
}
private void assertFileExists(File file) {
checkFileExists("does not exist", file, true);
}
private void assertFileCreatedUnderRootFolder(String msg, File f) {
assertParentFolderForFileIs(f, tempFolder.getRoot());
}
private void assertParentFolderForFileIs(File f, File parentFolder) {
assertThat("'" + f.getAbsolutePath() + "': not under root",
f.getParentFile(), is(parentFolder));
}
}