fixed testDeleteFail - using chmod on unixes

This commit is contained in:
Egor.Ushakov
2015-08-27 13:39:08 +03:00
parent cd4564cc85
commit 6f78ade9cb
@@ -181,22 +181,33 @@ public class FileUtilHeavyTest {
}
@Test
public void testDeleteFail() throws IOException {
public void testDeleteFail() throws Exception {
File targetDir = IoTestUtil.createTestDir(myTempDirectory, "failed_delete");
File file = IoTestUtil.createTestFile(targetDir, "file");
// lock file
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
RandomAccessFile rw = new RandomAccessFile(file, "rw");
FileLock lock = null;
try {
lock = rw.getChannel().tryLock();
assertFalse(FileUtil.delete(file));
if (SystemInfo.isWindows) {
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
RandomAccessFile rw = new RandomAccessFile(file, "rw");
FileLock lock = null;
try {
lock = rw.getChannel().tryLock();
assertFalse(FileUtil.delete(file));
}
finally {
if (lock != null) {
lock.release();
}
rw.close();
}
}
finally {
if (lock != null) {
lock.release();
else { // on unix use chmod
assertEquals(0, new ProcessBuilder("chmod", "a-w", file.getParentFile().getPath()).start().waitFor());
try {
assertFalse(FileUtil.delete(file));
}
finally {
assertEquals(0, new ProcessBuilder("chmod", "a+w", file.getParentFile().getPath()).start().waitFor());
}
rw.close();
}
}