Improve test robustness

Signed-off-by: Matt Sicker <boards@gmail.com>
This commit is contained in:
Matt Sicker committed 2018-12-18 11:35:07 -06:00
1 parent d5c9340041
commit dfb6d1d41c
1 file changed
+33 -9
@@ -34,7 +34,14 @@ import org.junit.rules.Timeout;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -78,18 +85,35 @@ public class PathRemoverTest {
@Test
public void testForceRemoveFile_ReadOnly() throws IOException {
File dir = tmp.newFolder();
File file = new File(dir, "file.tmp");
touchWithFileName(file);
file.setWritable(false);
dir.setWritable(false);
assertFalse("Unable to make file read-only: " + file, Files.isWritable(file.toPath()));
assertFalse("Unable to make directory read-only: " + dir, Files.isWritable(dir.toPath()));
Path dir = Files.createTempDirectory("remover");
Path file = dir.resolve("file.tmp");
touchWithFileName(file.toFile());
makeReadOnly(file);
makeReadOnly(dir);
assertFalse("Unable to make file read-only: " + file, Files.isWritable(file));
assertFalse("Unable to make directory read-only: " + dir, Files.isWritable(dir));
PathRemover remover = PathRemover.newSimpleRemover();
remover.forceRemoveFile(file.toPath());
remover.forceRemoveFile(file);
assertFalse("Unable to delete file: " + file, file.exists());
assertTrue("Unable to delete file: " + file, Files.notExists(file));
}
private static void makeReadOnly(Path path) {
try {
PosixFileAttributes attrs = Files.readAttributes(path, PosixFileAttributes.class);
Set<PosixFilePermission> permissions = attrs.permissions();
permissions.removeAll(Arrays.asList(PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_WRITE));
Files.setPosixFilePermissions(path, permissions);
} catch (NoSuchFileException ignored) {
return;
} catch (UnsupportedOperationException ignored) {
// retry with setWritable
} catch (IOException e) {
throw new UncheckedIOException(e);
}
// fallback method for filesystems that don't support POSIX attributes
path.toFile().setWritable(false);
}
@Test