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