1 package org.junit.rules;
2 
3 import java.io.File;
4 import java.io.IOException;
5 
6 import org.junit.Rule;
7 
8 /**
9  * The TemporaryFolder Rule allows creation of files and folders that are
10  * guaranteed to be deleted when the test method finishes (whether it passes or
11  * fails):
12  *
13  * <pre>
14  * public static class HasTempFolder {
15  * 	&#064;Rule
16  * 	public TemporaryFolder folder= new TemporaryFolder();
17  *
18  * 	&#064;Test
19  * 	public void testUsingTempFolder() throws IOException {
20  * 		File createdFile= folder.newFile(&quot;myfile.txt&quot;);
21  * 		File createdFolder= folder.newFolder(&quot;subfolder&quot;);
22  * 		// ...
23  * 	}
24  * }
25  * </pre>
26  */
27 public class TemporaryFolder extends ExternalResource {
28 	private File folder;
29 
30 	@Override
before()31 	protected void before() throws Throwable {
32 		create();
33 	}
34 
35 	@Override
after()36 	protected void after() {
37 		delete();
38 	}
39 
40 	// testing purposes only
41 	/**
42 	 * for testing purposes only.  Do not use.
43 	 */
create()44 	public void create() throws IOException {
45 		folder= newFolder();
46 	}
47 
48 	/**
49 	 * Returns a new fresh file with the given name under the temporary folder.
50 	 */
newFile(String fileName)51 	public File newFile(String fileName) throws IOException {
52 		File file= new File(getRoot(), fileName);
53 		file.createNewFile();
54 		return file;
55 	}
56 
57 	/**
58 	 * Returns a new fresh file with a random name under the temporary folder.
59 	 */
newFile()60 	public File newFile() throws IOException {
61 		return File.createTempFile("junit", null, folder);
62 	}
63 
64 	/**
65 	 * Returns a new fresh folder with the given name under the temporary folder.
66 	 */
newFolder(String... folderNames)67 	public File newFolder(String... folderNames) {
68 		File file = getRoot();
69 		for (String folderName : folderNames) {
70 			file = new File(file, folderName);
71 			file.mkdir();
72 		}
73 		return file;
74 	}
75 
76 	/**
77 	 * Returns a new fresh folder with a random name under the temporary
78 	 * folder.
79 	 */
newFolder()80 	public File newFolder() throws IOException {
81 		File createdFolder= File.createTempFile("junit", "", folder);
82 		createdFolder.delete();
83 		createdFolder.mkdir();
84 		return createdFolder;
85 	}
86 
87 	/**
88 	 * @return the location of this temporary folder.
89 	 */
getRoot()90 	public File getRoot() {
91 		if (folder == null) {
92 			throw new IllegalStateException("the temporary folder has not yet been created");
93 		}
94 		return folder;
95 	}
96 
97 	/**
98 	 * Delete all files and folders under the temporary folder.
99 	 * Usually not called directly, since it is automatically applied
100 	 * by the {@link Rule}
101 	 */
delete()102 	public void delete() {
103 		recursiveDelete(folder);
104 	}
105 
recursiveDelete(File file)106 	private void recursiveDelete(File file) {
107 		File[] files= file.listFiles();
108 		if (files != null)
109 			for (File each : files)
110 				recursiveDelete(each);
111 		file.delete();
112 	}
113 }
114