summaryrefslogtreecommitdiffstats
path: root/junit4/src/test/java/org/junit/tests/experimental/rules/TempFolderRuleTest.java
blob: 200d51eb1c848a2a9ca8da1fc6714458e1be1b1a (plain)
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
package org.junit.tests.experimental.rules;

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 static org.junit.internal.matchers.IsCollectionContaining.hasItem;

import java.io.File;
import java.io.IOException;
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 testUsingTempFolder() throws IOException {
			String subfolder = "subfolder";
			String filename = "a.txt";
			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());
	}

	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() {
			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();
	}
}