Skip to content

Commit

Permalink
Deleting temporary directories created in tests on shutdown
Browse files Browse the repository at this point in the history
* Deleting using a shutdown hook (other approaches didn't work)
  • Loading branch information
markuskreusch committed Jan 24, 2016
1 parent f081e7d commit 56b0612
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
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;
}
});
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.cryptomator.filesystem.invariants;

import static java.nio.file.Files.createTempDirectory;
import static org.cryptomator.common.test.TempFilesRemovedOnShutdown.createTempDirectory;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cryptomator.filesystem.nio.integrationtests;

import static org.cryptomator.common.test.TempFilesRemovedOnShutdown.createTempDirectory;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
Expand All @@ -15,15 +17,15 @@ class FilesystemSetupUtils {

public static Path emptyFilesystem() {
try {
return Files.createTempDirectory("test-filesystem");
return createTempDirectory("test-filesystem");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static Path testFilesystem(Entry firstEntry, Entry... entries) {
try {
Path root = Files.createTempDirectory("test-filesystem");
Path root = createTempDirectory("test-filesystem");
firstEntry.create(root);
for (Entry entry : entries) {
entry.create(root);
Expand Down

0 comments on commit 56b0612

Please sign in to comment.