FileTuple { public void delete() { if (original.isFile() && !original.delete()) { InternalLogger.log(Level.WARN, "Failed to delete log file '" + original + "'"); } if (!backup.equals(original) && backup.isFile() && !backup.delete()) { InternalLogger.log(Level.WARN, "Failed to delete backup file '" + backup + "'"); } } FileTuple(final File original, final File backup); File getOriginal(); File getBackup(); long getLastModified(); void delete();  }
@Test public void deleteOriginalFile() throws IOException { File file = new File(FileSystem.createTemporaryFile()); new FileTuple(file, new File(file.getPath() + ".backup")).delete(); assertThat(file).doesNotExist(); }
@Test public void deleteBackupFile() throws IOException { File file = new File(FileSystem.createTemporaryFile()); new FileTuple(new File(file.getPath() + ".log"), file).delete(); assertThat(file).doesNotExist(); }
@Test public void deleteBothFiles() throws IOException { File originalFile = new File(FileSystem.createTemporaryFile()); File backupFile = new File(FileSystem.createTemporaryFile()); new FileTuple(originalFile, backupFile).delete(); assertThat(originalFile).doesNotExist(); assertThat(backupFile).doesNotExist(); }
@Test public void warnIfDeletingOriginalFileFailedOnWindows() throws IOException { assumeTrue(System.getProperty("os.name").startsWith("Windows")); File originalFile = new File(FileSystem.createTemporaryFile()); File backupFile = new File(FileSystem.createTemporaryFile()); try (FileInputStream stream = new FileInputStream(originalFile)) { new FileTuple(originalFile, backupFile).delete(); } assertThat(systemStream.consumeErrorOutput()) .containsOnlyOnce("WARN") .containsOnlyOnce(originalFile.getPath()); assertThat(originalFile).exists(); assertThat(backupFile).doesNotExist(); }
@Test public void warnIfDeletingBackupFileFailedOnWindows() throws IOException { assumeTrue(System.getProperty("os.name").startsWith("Windows")); File originalFile = new File(FileSystem.createTemporaryFile()); File backupFile = new File(FileSystem.createTemporaryFile()); try (FileInputStream stream = new FileInputStream(backupFile)) { new FileTuple(originalFile, backupFile).delete(); } assertThat(systemStream.consumeErrorOutput()) .containsOnlyOnce("WARN") .containsOnlyOnce(backupFile.getPath()); assertThat(originalFile).doesNotExist(); assertThat(backupFile).exists(); }
@Test public void warnIfDeletingOriginalFileFailedOnPosix() throws IOException { assumeTrue(FileSystems.getDefault().supportedFileAttributeViews().contains("posix")); Path originalFolder = Files.createTempDirectory("original"); originalFolder.toFile().deleteOnExit(); File originalFile = Files.createFile(originalFolder.resolve("log.txt")).toFile(); Path backupFolder = Files.createTempDirectory("backup"); backupFolder.toFile().deleteOnExit(); File backupFile = Files.createFile(backupFolder.resolve("log.backup")).toFile(); try { originalFolder.toFile().setWritable(false); new FileTuple(originalFile, backupFile).delete(); } finally { originalFolder.toFile().setWritable(true); } assertThat(systemStream.consumeErrorOutput()) .containsOnlyOnce("WARN") .containsOnlyOnce(originalFile.getPath()); assertThat(originalFile).exists(); assertThat(backupFile).doesNotExist(); }
@Test public void warnIfDeletingBackupFileFailedOnPosix() throws IOException { assumeTrue(FileSystems.getDefault().supportedFileAttributeViews().contains("posix")); Path originalFolder = Files.createTempDirectory("original"); originalFolder.toFile().deleteOnExit(); File originalFile = Files.createFile(originalFolder.resolve("log.txt")).toFile(); Path backupFolder = Files.createTempDirectory("backup"); backupFolder.toFile().deleteOnExit(); File backupFile = Files.createFile(backupFolder.resolve("log.backup")).toFile(); try { backupFolder.toFile().setWritable(false); new FileTuple(originalFile, backupFile).delete(); } finally { backupFolder.toFile().setWritable(true); } assertThat(systemStream.consumeErrorOutput()) .containsOnlyOnce("WARN") .containsOnlyOnce(backupFile.getPath()); assertThat(originalFile).doesNotExist(); assertThat(backupFile).exists(); }