-
-
Notifications
You must be signed in to change notification settings - Fork 955
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deleting temporary directories created in tests on shutdown
* Deleting using a shutdown hook (other approaches didn't work)
- Loading branch information
1 parent
f081e7d
commit 56b0612
Showing
3 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
main/commons-test/src/main/java/org/cryptomator/common/test/TempFilesRemovedOnShutdown.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.cryptomator.common.test; | ||
|
||
import static java.nio.file.Files.walkFileTree; | ||
import static java.util.Collections.synchronizedSet; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.FileVisitResult; | ||
import java.nio.file.FileVisitor; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class TempFilesRemovedOnShutdown { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(TempFilesRemovedOnShutdown.class); | ||
|
||
private static final Set<Path> PATHS_TO_REMOVE_ON_SHUTDOWN = synchronizedSet(new HashSet<>()); | ||
private static final Thread ON_SHUTDOWN_DELETER = new Thread(TempFilesRemovedOnShutdown::removeAll); | ||
|
||
static { | ||
Runtime.getRuntime().addShutdownHook(ON_SHUTDOWN_DELETER); | ||
} | ||
|
||
public static Path createTempDirectory(String prefix) throws IOException { | ||
Path path = Files.createTempDirectory(prefix); | ||
PATHS_TO_REMOVE_ON_SHUTDOWN.add(path); | ||
return path; | ||
} | ||
|
||
private static void removeAll() { | ||
PATHS_TO_REMOVE_ON_SHUTDOWN.forEach(TempFilesRemovedOnShutdown::remove); | ||
} | ||
|
||
private static void remove(Path path) { | ||
try { | ||
tryRemove(path); | ||
} catch (Throwable e) { | ||
LOG.debug("Failed to remove " + path, e); | ||
} | ||
} | ||
|
||
private static void tryRemove(Path path) throws IOException { | ||
walkFileTree(path, new FileVisitor<Path>() { | ||
@Override | ||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { | ||
Files.delete(file); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { | ||
return FileVisitResult.CONTINUE; | ||
} | ||
|
||
@Override | ||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { | ||
Files.delete(dir); | ||
return FileVisitResult.CONTINUE; | ||
} | ||
}); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...riants-tests/src/test/java/org/cryptomator/filesystem/invariants/FileSystemFactories.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters