View Javadoc
1   package org.junit.tests.experimental.rules;
2   
3   import static org.hamcrest.CoreMatchers.equalTo;
4   import static org.hamcrest.CoreMatchers.is;
5   import static org.hamcrest.CoreMatchers.notNullValue;
6   import static org.junit.Assert.assertThat;
7   import static org.junit.Assert.assertTrue;
8   
9   import java.io.File;
10  import java.io.IOException;
11  
12  import org.junit.After;
13  import org.junit.Before;
14  import org.junit.Rule;
15  import org.junit.Test;
16  import org.junit.rules.ExpectedException;
17  import org.junit.rules.TemporaryFolder;
18  
19  /**
20   * <tt>TemporaryFolderUsageTest</tt> provides tests for API usage correctness
21   * and ensure implementation symmetry of public methods against a root folder.
22   */
23  public class TemporaryFolderUsageTest {
24  
25      private TemporaryFolder tempFolder;
26  
27      @Rule
28      public final ExpectedException thrown = ExpectedException.none();
29  
30      @Before
31      public void setUp() {
32          tempFolder = new TemporaryFolder();
33      }
34  
35      @After
36      public void tearDown() {
37          tempFolder.delete();
38      }
39  
40      @Test(expected = IllegalStateException.class)
41      public void getRootShouldThrowIllegalStateExceptionIfCreateWasNotInvoked() {
42          new TemporaryFolder().getRoot();
43      }
44  
45      @Test(expected = IllegalStateException.class)
46      public void newFileThrowsIllegalStateExceptionIfCreateWasNotInvoked()
47              throws IOException {
48          new TemporaryFolder().newFile();
49      }
50  
51      @Test(expected = IllegalStateException.class)
52      public void newFileWithGivenNameThrowsIllegalStateExceptionIfCreateWasNotInvoked()
53              throws IOException {
54          new TemporaryFolder().newFile("MyFile.txt");
55      }
56  
57      @Test
58      public void newFileWithGivenFilenameThrowsIllegalArgumentExceptionIfFileExists() throws IOException {
59          tempFolder.create();
60          tempFolder.newFile("MyFile.txt");
61  
62          thrown.expect(IOException.class);
63          thrown.expectMessage("a file with the name 'MyFile.txt' already exists in the test folder");
64          tempFolder.newFile("MyFile.txt");
65      }
66  
67      @Test(expected = IllegalStateException.class)
68      public void newFolderThrowsIllegalStateExceptionIfCreateWasNotInvoked()
69              throws IOException {
70          new TemporaryFolder().newFolder();
71      }
72  
73      @Test(expected = IllegalStateException.class)
74      public void newFolderWithGivenPathThrowsIllegalStateExceptionIfCreateWasNotInvoked() throws IOException {
75          new TemporaryFolder().newFolder("level1", "level2", "level3");
76      }
77  
78      @Test
79      public void newFolderWithGivenFolderThrowsIllegalArgumentExceptionIfFolderExists() throws IOException {
80          tempFolder.create();
81          tempFolder.newFolder("level1");
82  
83          thrown.expect(IOException.class);
84          thrown.expectMessage("a folder with the name 'level1' already exists");
85          tempFolder.newFolder("level1");
86      }
87      
88      @Test
89      public void newFolderWithGivenFolderThrowsIOExceptionIfFolderNameConsistsOfMultiplePathComponents()
90              throws IOException {
91          tempFolder.create();
92          thrown.expect(IOException.class);
93          thrown.expectMessage("name cannot consist of multiple path components");
94          tempFolder.newFolder("temp1/temp2");
95      }
96      
97      @Test
98      public void newFolderWithGivenPathThrowsIllegalArgumentExceptionIfPathExists() throws IOException {
99          tempFolder.create();
100         tempFolder.newFolder("level1", "level2", "level3");
101 
102         thrown.expect(IOException.class);
103         thrown.expectMessage("a folder with the name 'level3' already exists");
104         tempFolder.newFolder("level1", "level2", "level3");
105     }
106 
107     @Test
108     public void newFolderWithGivenPathThrowsIOExceptionIfFolderNamesConsistOfMultiplePathComponents()
109             throws IOException {
110         tempFolder.create();
111         thrown.expect(IOException.class);
112         thrown.expectMessage("name cannot consist of multiple path components");
113         tempFolder.newFolder("temp1", "temp2", "temp3/temp4");
114     }
115     
116     @Test
117     public void createInitializesRootFolder() throws IOException {
118         tempFolder.create();
119         assertFileExists(tempFolder.getRoot());
120     }
121 
122     @Test
123     public void deleteShouldDoNothingIfRootFolderWasNotInitialized() {
124         tempFolder.delete();
125     }
126 
127     @Test
128     public void deleteRemovesRootFolder() throws IOException {
129         tempFolder.create();
130         tempFolder.delete();
131         assertFileDoesNotExist(tempFolder.getRoot());
132     }
133 
134     @Test
135     public void newRandomFileIsCreatedUnderRootFolder() throws IOException {
136         tempFolder.create();
137 
138         File f = tempFolder.newFile();
139         assertFileExists(f);
140         assertFileCreatedUnderRootFolder("Random file", f);
141     }
142 
143     @Test
144     public void newNamedFileIsCreatedUnderRootFolder() throws IOException {
145         final String fileName = "SampleFile.txt";
146         tempFolder.create();
147 
148         File f = tempFolder.newFile(fileName);
149 
150         assertFileExists(f);
151         assertFileCreatedUnderRootFolder("Named file", f);
152         assertThat("file name", f.getName(), equalTo(fileName));
153     }
154 
155     @Test
156     public void newRandomFolderIsCreatedUnderRootFolder() throws IOException {
157         tempFolder.create();
158 
159         File f = tempFolder.newFolder();
160         assertFileExists(f);
161         assertFileCreatedUnderRootFolder("Random folder", f);
162     }
163 
164     @Test
165     public void newNestedFoldersCreatedUnderRootFolder() throws IOException {
166         tempFolder.create();
167 
168         File f = tempFolder.newFolder("top", "middle", "bottom");
169         assertFileExists(f);
170         assertParentFolderForFileIs(f, new File(tempFolder.getRoot(),
171                 "top/middle"));
172         assertParentFolderForFileIs(f.getParentFile(),
173                 new File(tempFolder.getRoot(), "top"));
174         assertFileCreatedUnderRootFolder("top", f.getParentFile()
175                 .getParentFile());
176     }
177 
178     @Test
179     public void canSetTheBaseFileForATemporaryFolder() throws IOException {
180         File tempDir = createTemporaryFolder();
181 
182         TemporaryFolder folder = new TemporaryFolder(tempDir);
183         folder.create();
184 
185         assertThat(tempDir, is(folder.getRoot().getParentFile()));
186     }
187 
188     private File createTemporaryFolder() throws IOException {
189         File tempDir = File.createTempFile("junit", "tempFolder");
190         assertTrue("Unable to delete temporary file", tempDir.delete());
191         assertTrue("Unable to create temp directory", tempDir.mkdir());
192         return tempDir;
193     }
194 
195     private void assertFileDoesNotExist(File file) {
196         checkFileExists("exists", file, false);
197     }
198 
199     private void checkFileExists(String msg, File file, boolean exists) {
200         assertThat("File is null", file, is(notNullValue()));
201         assertThat("File '" + file.getAbsolutePath() + "' " + msg,
202                 file.exists(), is(exists));
203     }
204 
205     private void assertFileExists(File file) {
206         checkFileExists("does not exist", file, true);
207     }
208 
209     private void assertFileCreatedUnderRootFolder(String msg, File f) {
210         assertParentFolderForFileIs(f, tempFolder.getRoot());
211     }
212 
213     private void assertParentFolderForFileIs(File f, File parentFolder) {
214         assertThat("'" + f.getAbsolutePath() + "': not under root",
215                 f.getParentFile(), is(parentFolder));
216     }
217 }