public void clean() {
try {
- Files.walkFileTree(tempDir.toPath(), DeleteRecursivelyFileVisitor.INSTANCE);
+ if (tempDir.exists()) {
+ Files.walkFileTree(tempDir.toPath(), DeleteRecursivelyFileVisitor.INSTANCE);
+ }
} catch (IOException e) {
LOG.error("Failed to delete temp folder", e);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- Files.delete(file);
+ Files.deleteIfExists(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
- Files.delete(dir);
+ Files.deleteIfExists(dir);
return FileVisitResult.CONTINUE;
}
}
*/
package org.sonar.api.utils.internal;
+import java.io.File;
+import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
-
-import java.io.File;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
import static org.assertj.core.api.Assertions.assertThat;
@Rule
public TemporaryFolder temp = new TemporaryFolder();
+ @Rule
+ public LogTester logTester = new LogTester();
+
@Test
public void createTempFolderAndFile() throws Exception {
File rootTempFolder = temp.newFolder();
throwable.expectMessage("Failed to create temp file");
tempFolder.newFile(tooLong, ".txt");
}
+
+ @Test
+ public void clean_deletes_non_empty_directory() throws Exception {
+ File dir = temp.newFolder();
+ FileUtils.touch(new File(dir, "foo.txt"));
+
+ DefaultTempFolder underTest = new DefaultTempFolder(dir);
+ underTest.clean();
+
+ assertThat(dir).doesNotExist();
+ }
+
+ @Test
+ public void clean_does_not_fail_if_directory_has_already_been_deleted() throws Exception {
+ File dir = temp.newFolder();
+
+ DefaultTempFolder underTest = new DefaultTempFolder(dir);
+ underTest.clean();
+ assertThat(dir).doesNotExist();
+
+ // second call does not fail, nor log ERROR logs
+ underTest.clean();
+
+ assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty();
+ }
}