1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.io; 18 19 import static org.junit.jupiter.api.Assertions.assertEquals; 20 import static org.junit.jupiter.api.Assertions.assertFalse; 21 import static org.junit.jupiter.api.Assertions.assertThrows; 22 import static org.junit.jupiter.api.Assertions.assertTrue; 23 24 import java.io.BufferedOutputStream; 25 import java.io.File; 26 import java.io.IOException; 27 import java.nio.file.Files; 28 29 import org.apache.commons.io.test.TestUtils; 30 import org.junit.jupiter.api.Test; 31 import org.junit.jupiter.api.io.TempDir; 32 /** 33 * Tests {@link FileDeleteStrategy}. 34 */ 35 public class FileDeleteStrategyTest { 36 37 @TempDir 38 public File temporaryFolder; 39 40 @Test testDeleteForce()41 public void testDeleteForce() throws Exception { 42 final File baseDir = temporaryFolder; 43 final File subDir = new File(baseDir, "test"); 44 assertTrue(subDir.mkdir()); 45 final File subFile = new File(subDir, "a.txt"); 46 if (!subFile.getParentFile().exists()) { 47 throw new IOException("Cannot create file " + subFile 48 + " as the parent directory does not exist"); 49 } 50 try (BufferedOutputStream output = 51 new BufferedOutputStream(Files.newOutputStream(subFile.toPath()))) { 52 TestUtils.generateTestData(output, 16); 53 } 54 55 assertTrue(subDir.exists()); 56 assertTrue(subFile.exists()); 57 // delete dir 58 FileDeleteStrategy.FORCE.delete(subDir); 59 assertFalse(subDir.exists()); 60 assertFalse(subFile.exists()); 61 // delete dir 62 FileDeleteStrategy.FORCE.delete(subDir); // no error 63 assertFalse(subDir.exists()); 64 } 65 66 @Test testDeleteNormal()67 public void testDeleteNormal() throws Exception { 68 final File baseDir = temporaryFolder; 69 final File subDir = new File(baseDir, "test"); 70 assertTrue(subDir.mkdir()); 71 final File subFile = new File(subDir, "a.txt"); 72 if (!subFile.getParentFile().exists()) { 73 throw new IOException("Cannot create file " + subFile 74 + " as the parent directory does not exist"); 75 } 76 try (BufferedOutputStream output = 77 new BufferedOutputStream(Files.newOutputStream(subFile.toPath()))) { 78 TestUtils.generateTestData(output, 16); 79 } 80 81 assertTrue(subDir.exists()); 82 assertTrue(subFile.exists()); 83 // delete dir 84 assertThrows(IOException.class, () -> FileDeleteStrategy.NORMAL.delete(subDir)); 85 assertTrue(subDir.exists()); 86 assertTrue(subFile.exists()); 87 // delete file 88 FileDeleteStrategy.NORMAL.delete(subFile); 89 assertTrue(subDir.exists()); 90 assertFalse(subFile.exists()); 91 // delete dir 92 FileDeleteStrategy.NORMAL.delete(subDir); 93 assertFalse(subDir.exists()); 94 // delete dir 95 FileDeleteStrategy.NORMAL.delete(subDir); // no error 96 assertFalse(subDir.exists()); 97 } 98 99 @Test testDeleteNull()100 public void testDeleteNull() throws Exception { 101 assertThrows(NullPointerException.class, () -> FileDeleteStrategy.NORMAL.delete(null)); 102 assertTrue(FileDeleteStrategy.NORMAL.deleteQuietly(null)); 103 } 104 105 @Test testDeleteQuietlyNormal()106 public void testDeleteQuietlyNormal() throws Exception { 107 final File baseDir = temporaryFolder; 108 final File subDir = new File(baseDir, "test"); 109 assertTrue(subDir.mkdir()); 110 final File subFile = new File(subDir, "a.txt"); 111 if (!subFile.getParentFile().exists()) { 112 throw new IOException("Cannot create file " + subFile 113 + " as the parent directory does not exist"); 114 } 115 try (BufferedOutputStream output = 116 new BufferedOutputStream(Files.newOutputStream(subFile.toPath()))) { 117 TestUtils.generateTestData(output, 16); 118 } 119 120 assertTrue(subDir.exists()); 121 assertTrue(subFile.exists()); 122 // delete dir 123 assertFalse(FileDeleteStrategy.NORMAL.deleteQuietly(subDir)); 124 assertTrue(subDir.exists()); 125 assertTrue(subFile.exists()); 126 // delete file 127 assertTrue(FileDeleteStrategy.NORMAL.deleteQuietly(subFile)); 128 assertTrue(subDir.exists()); 129 assertFalse(subFile.exists()); 130 // delete dir 131 assertTrue(FileDeleteStrategy.NORMAL.deleteQuietly(subDir)); 132 assertFalse(subDir.exists()); 133 // delete dir 134 assertTrue(FileDeleteStrategy.NORMAL.deleteQuietly(subDir)); // no error 135 assertFalse(subDir.exists()); 136 } 137 138 @Test testToString()139 public void testToString() { 140 assertEquals("FileDeleteStrategy[Normal]", FileDeleteStrategy.NORMAL.toString()); 141 assertEquals("FileDeleteStrategy[Force]", FileDeleteStrategy.FORCE.toString()); 142 } 143 } 144