From a4faaca346729386c88484059af8b4ebd355bc5a Mon Sep 17 00:00:00 2001 From: valerijf Date: Wed, 8 Mar 2017 14:16:42 +0100 Subject: Rewritted DeleteOldAppData to use java.nio --- .../hosted/node/maintainer/CoredumpHandler.java | 8 +- .../hosted/node/maintainer/DeleteOldAppData.java | 158 +++++++++++---------- .../node/maintainer/DeleteOldAppDataTest.java | 114 ++++++++------- 3 files changed, 153 insertions(+), 127 deletions(-) diff --git a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/CoredumpHandler.java b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/CoredumpHandler.java index 35f2885a857..54ff7db997f 100644 --- a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/CoredumpHandler.java +++ b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/CoredumpHandler.java @@ -13,8 +13,10 @@ import java.io.IOException; import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Path; +import java.time.Duration; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; @@ -41,14 +43,14 @@ public class CoredumpHandler { this.coreCollector = coreCollector; } - void processAndReportCoredumps(Path coredumpsPath, Path doneCoredumpPath, Map nodeAttributes) throws IOException { + public void processAndReportCoredumps(Path coredumpsPath, Path doneCoredumpPath, Map nodeAttributes) throws IOException { Path processingCoredumps = processCoredumps(coredumpsPath, nodeAttributes); reportCoredumps(processingCoredumps, doneCoredumpPath); } - void removeJavaCoredumps(Path javaCoredumpsPath) { + public void removeJavaCoredumps(Path javaCoredumpsPath) throws IOException { if (! javaCoredumpsPath.toFile().isDirectory()) return; - DeleteOldAppData.deleteFiles(javaCoredumpsPath.toString(), 0, "^java_pid.*\\.hprof$", false); + DeleteOldAppData.deleteFiles(javaCoredumpsPath, Duration.ZERO, Optional.of("^java_pid.*\\.hprof$"), false); } Path processCoredumps(Path coredumpsPath, Map nodeAttributes) throws IOException { diff --git a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/DeleteOldAppData.java b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/DeleteOldAppData.java index 9bb5df1eeb8..ea8481b6bcb 100644 --- a/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/DeleteOldAppData.java +++ b/node-maintainer/src/main/java/com/yahoo/vespa/hosted/node/maintainer/DeleteOldAppData.java @@ -1,12 +1,17 @@ // Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.node.maintainer; -import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.nio.file.attribute.FileTime; import java.time.Duration; -import java.util.Arrays; +import java.time.Instant; +import java.util.Collections; +import java.util.Comparator; import java.util.List; +import java.util.Optional; import java.util.logging.Logger; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -22,26 +27,25 @@ public class DeleteOldAppData { * (Recursively) deletes files if they match all the criteria, also deletes empty directories. * * @param basePath Base path from where to start the search - * @param maxAgeSeconds Delete files older (last modified date) than maxAgeSeconds + * @param maxAge Delete files older (last modified date) than maxAge * @param fileNameRegex Delete files where filename matches fileNameRegex * @param recursive Delete files in sub-directories (with the same criteria) */ - static void deleteFiles(String basePath, long maxAgeSeconds, String fileNameRegex, boolean recursive) { - Pattern fileNamePattern = fileNameRegex != null ? Pattern.compile(fileNameRegex) : null; - File[] filesInDeleteDirectory = getContentsOfDirectory(basePath); + public static void deleteFiles(Path basePath, Duration maxAge, Optional fileNameRegex, boolean recursive) throws IOException { + Pattern fileNamePattern = fileNameRegex.map(Pattern::compile).orElse(null); - for (File file : filesInDeleteDirectory) { - if (file.isDirectory()) { + for (Path path : listContentsOfDirectory(basePath)) { + if (Files.isDirectory(path)) { if (recursive) { - deleteFiles(file.getAbsolutePath(), maxAgeSeconds, fileNameRegex, true); - if (file.list().length == 0 && !file.delete()) { - logger.warning("Could not delete directory: " + file.getAbsolutePath()); + deleteFiles(path, maxAge, fileNameRegex, true); + if (listContentsOfDirectory(path).isEmpty() && !Files.deleteIfExists(path)) { + logger.warning("Could not delete directory: " + path.toAbsolutePath()); } } - } else if (isPatternMatchingFilename(fileNamePattern, file) && - isTimeSinceLastModifiedMoreThan(file, Duration.ofSeconds(maxAgeSeconds))) { - if (!file.delete()) { - logger.warning("Could not delete file: " + file.getAbsolutePath()); + } else if (isPatternMatchingFilename(fileNamePattern, path) && + isTimeSinceLastModifiedMoreThan(path, maxAge)) { + if (! Files.deleteIfExists(path)) { + logger.warning("Could not delete file: " + path.toAbsolutePath()); } } } @@ -53,35 +57,31 @@ public class DeleteOldAppData { * @param basePath Base path to delete from * @param nMostRecentToKeep Number of most recent files to keep */ - static void deleteFilesExceptNMostRecent(String basePath, int nMostRecentToKeep) { - File[] deleteDirContents = getContentsOfDirectory(basePath); - + static void deleteFilesExceptNMostRecent(Path basePath, int nMostRecentToKeep) throws IOException { if (nMostRecentToKeep < 1) { throw new IllegalArgumentException("Number of files to keep must be a positive number"); } - List filesInDeleteDir = Arrays.stream(deleteDirContents) - .filter(File::isFile) - .sorted((f1, f2) -> Long.signum(f1.lastModified() - f2.lastModified())) + List pathsInDeleteDir = Files.list(basePath) + .filter(Files::isRegularFile) + .sorted(Comparator.comparing(DeleteOldAppData::getLastModifiedTime)) + .skip(nMostRecentToKeep) .collect(Collectors.toList()); - if (filesInDeleteDir.size() <= nMostRecentToKeep) return; - for (int i = nMostRecentToKeep; i < filesInDeleteDir.size(); i++) { - if (!filesInDeleteDir.get(i).delete()) { - logger.warning("Could not delete file: " + filesInDeleteDir.get(i).getAbsolutePath()); + for (Path path : pathsInDeleteDir) { + if (!Files.deleteIfExists(path)) { + logger.warning("Could not delete file: " + path.toAbsolutePath()); } } } - static void deleteFilesLargerThan(File baseDirectory, long sizeInBytes) { - File[] filesInBaseDirectory = getContentsOfDirectory(baseDirectory.getAbsolutePath()); - - for (File file : filesInBaseDirectory) { - if (file.isDirectory()) { - deleteFilesLargerThan(file, sizeInBytes); + static void deleteFilesLargerThan(Path basePath, long sizeInBytes) throws IOException { + for (Path path : listContentsOfDirectory(basePath)) { + if (Files.isDirectory(path)) { + deleteFilesLargerThan(path, sizeInBytes); } else { - if (file.length() > sizeInBytes && !file.delete()) { - logger.warning("Could not delete file: " + file.getAbsolutePath()); + if (Files.size(path) > sizeInBytes && !Files.deleteIfExists(path)) { + logger.warning("Could not delete file: " + path.toAbsolutePath()); } } } @@ -91,20 +91,23 @@ public class DeleteOldAppData { * Deletes directories and their contents if they match all the criteria * * @param basePath Base path to delete the directories from - * @param maxAgeSeconds Delete directories older (last modified date) than maxAgeSeconds + * @param maxAge Delete directories older (last modified date) than maxAge * @param dirNameRegex Delete directories where directory name matches dirNameRegex */ - static void deleteDirectories(String basePath, long maxAgeSeconds, String dirNameRegex) { - Pattern dirNamePattern = dirNameRegex != null ? Pattern.compile(dirNameRegex) : null; - File[] filesInDeleteDirectory = getContentsOfDirectory(basePath); - - for (File file : filesInDeleteDirectory) { - if (file.isDirectory() && - isPatternMatchingFilename(dirNamePattern, file) && - isTimeSinceLastModifiedMoreThan(getMostRecentlyModifiedFileIn(file), Duration.ofSeconds(maxAgeSeconds))) { - deleteFiles(file.getPath(), 0, null, true); - if (file.list().length == 0 && !file.delete()) { - logger.warning("Could not delete directory: " + file.getAbsolutePath()); + public static void deleteDirectories(Path basePath, Duration maxAge, Optional dirNameRegex) throws IOException { + Pattern dirNamePattern = dirNameRegex.map(Pattern::compile).orElse(null); + + for (Path path : listContentsOfDirectory(basePath)) { + if (Files.isDirectory(path) && isPatternMatchingFilename(dirNamePattern, path)) { + boolean mostRecentFileModifiedBeforeMaxAge = getMostRecentlyModifiedFileIn(path) + .map(mostRecentlyModified -> isTimeSinceLastModifiedMoreThan(mostRecentlyModified, maxAge)) + .orElse(true); + + if (mostRecentFileModifiedBeforeMaxAge) { + deleteFiles(path, Duration.ZERO, Optional.empty(), true); + if (listContentsOfDirectory(path).isEmpty() && !Files.deleteIfExists(path)) { + logger.warning("Could not delete directory: " + path.toAbsolutePath()); + } } } } @@ -116,47 +119,56 @@ public class DeleteOldAppData { * - If file is a directory, it and all content is removed * - For symlinks: Only the symlink is removed, not what the symlink points to */ - static void recursiveDelete(String path) { - File file = new File(path); - if (file.isDirectory()) { - for (File childFile : file.listFiles()) { - recursiveDelete(childFile.getAbsolutePath()); + public static void recursiveDelete(Path basePath) throws IOException { + if (Files.isDirectory(basePath)) { + for (Path path : listContentsOfDirectory(basePath)) { + recursiveDelete(path); } } - try { - Files.deleteIfExists(file.toPath()); - } catch (IOException ignored) { } + Files.deleteIfExists(basePath); } - static File[] getContentsOfDirectory(String directoryPath) { - File directory = new File(directoryPath); - File[] directoryContents = directory.listFiles(); + public static void moveIfExists(Path from, Path to) throws IOException { + if (Files.exists(from)) { + Files.move(from, to); + } + } - return directoryContents == null ? new File[0] : directoryContents; + private static Optional getMostRecentlyModifiedFileIn(Path basePath) { + return listContentsOfDirectory(basePath).stream() + .filter(Files::isRegularFile) + .max(Comparator.comparing(DeleteOldAppData::getLastModifiedTime)); } - private static File getMostRecentlyModifiedFileIn(File baseFile) { - File mostRecent = baseFile; - File[] filesInDirectory = getContentsOfDirectory(baseFile.getAbsolutePath()); + private static boolean isTimeSinceLastModifiedMoreThan(Path path, Duration duration) { + Instant nowMinusDuration = Instant.now().minus(duration); + Instant lastModified = getLastModifiedTime(path).toInstant(); - for (File file : filesInDirectory) { - if (file.isDirectory()) { - file = getMostRecentlyModifiedFileIn(file); - } + // Return true also if they are equal for test stability + // (lastModified <= nowMinusDuration) is the same as !(lastModified > nowMinusDuration) + return !lastModified.isAfter(nowMinusDuration); + } - if (file.lastModified() > mostRecent.lastModified()) { - mostRecent = file; - } - } - return mostRecent; + private static boolean isPatternMatchingFilename(Pattern pattern, Path path) { + return pattern == null || pattern.matcher(path.getFileName().toString()).find(); } - private static boolean isTimeSinceLastModifiedMoreThan(File file, Duration duration) { - return System.currentTimeMillis() - file.lastModified() > duration.toMillis(); + static List listContentsOfDirectory(Path basePath) { + try { + return Files.list(basePath).collect(Collectors.toList()); + } catch (NoSuchFileException ignored) { + return Collections.emptyList(); + } catch (IOException e) { + throw new RuntimeException("Failed to list contents of directory " + basePath.toAbsolutePath(), e); + } } - private static boolean isPatternMatchingFilename(Pattern pattern, File file) { - return pattern == null || pattern.matcher(file.getName()).find(); + private static FileTime getLastModifiedTime(Path path) { + try { + return Files.getLastModifiedTime(path); + } catch (IOException e) { + throw new RuntimeException("Failed to get last modified time of " + path.toAbsolutePath(), e); + } } } diff --git a/node-maintainer/src/test/java/com/yahoo/vespa/hosted/node/maintainer/DeleteOldAppDataTest.java b/node-maintainer/src/test/java/com/yahoo/vespa/hosted/node/maintainer/DeleteOldAppDataTest.java index 6406731c2b3..0092c77e477 100644 --- a/node-maintainer/src/test/java/com/yahoo/vespa/hosted/node/maintainer/DeleteOldAppDataTest.java +++ b/node-maintainer/src/test/java/com/yahoo/vespa/hosted/node/maintainer/DeleteOldAppDataTest.java @@ -10,12 +10,15 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.time.Duration; import java.util.Arrays; +import java.util.Collections; +import java.util.Optional; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; /** @@ -47,108 +50,111 @@ public class DeleteOldAppDataTest { } @Test - public void testDeleteAll() { - DeleteOldAppData.deleteFiles(folder.getRoot().getAbsolutePath(), 0, null, false); + public void testDeleteAll() throws IOException { + DeleteOldAppData.deleteFiles(folder.getRoot().toPath(), Duration.ZERO, Optional.empty(), false); - assertThat(folder.getRoot().listFiles().length, is(0)); + assertEquals(0, getContentsOfDirectory(folder.getRoot()).length); } @Test - public void testDeletePrefix() { - DeleteOldAppData.deleteFiles(folder.getRoot().getAbsolutePath(), 0, "^test_", false); + public void testDeletePrefix() throws IOException { + DeleteOldAppData.deleteFiles(folder.getRoot().toPath(), Duration.ZERO, Optional.of("^test_"), false); - assertThat(folder.getRoot().listFiles().length, is(6)); // 5 abc files + 1 week_old_file + assertEquals(6, getContentsOfDirectory(folder.getRoot()).length); // 5 abc files + 1 week_old_file } @Test - public void testDeleteSuffix() { - DeleteOldAppData.deleteFiles(folder.getRoot().getAbsolutePath(), 0, ".json$", false); + public void testDeleteSuffix() throws IOException { + DeleteOldAppData.deleteFiles(folder.getRoot().toPath(), Duration.ZERO, Optional.of(".json$"), false); - assertThat(folder.getRoot().listFiles().length, is(7)); + assertEquals(7, getContentsOfDirectory(folder.getRoot()).length); } @Test - public void testDeletePrefixAndSuffix() { - DeleteOldAppData.deleteFiles(folder.getRoot().getAbsolutePath(), 0, "^test_.*\\.json$", false); + public void testDeletePrefixAndSuffix() throws IOException { + DeleteOldAppData.deleteFiles(folder.getRoot().toPath(), Duration.ZERO, Optional.of("^test_.*\\.json$"), false); - assertThat(folder.getRoot().listFiles().length, is(13)); // 5 abc files + 7 test_*_file.test files + week_old_file + assertEquals(13, getContentsOfDirectory(folder.getRoot()).length); // 5 abc files + 7 test_*_file.test files + week_old_file } @Test - public void testDeleteOld() { - DeleteOldAppData.deleteFiles(folder.getRoot().getAbsolutePath(), 600, null, false); + public void testDeleteOld() throws IOException { + DeleteOldAppData.deleteFiles(folder.getRoot().toPath(), Duration.ofSeconds(600), Optional.empty(), false); - assertThat(folder.getRoot().listFiles().length, is(13)); // All 23 - 6 (from test_*_.json) - 3 (from test_*_file.test) - 1 week old file + assertEquals(13, getContentsOfDirectory(folder.getRoot()).length); // All 23 - 6 (from test_*_.json) - 3 (from test_*_file.test) - 1 week old file } @Test - public void testDeleteWithAllParameters() { - DeleteOldAppData.deleteFiles(folder.getRoot().getAbsolutePath(), 200, "^test_.*\\.json$", false); + public void testDeleteWithAllParameters() throws IOException { + DeleteOldAppData.deleteFiles(folder.getRoot().toPath(), Duration.ofSeconds(200), Optional.of("^test_.*\\.json$"), false); - assertThat(folder.getRoot().listFiles().length, is(15)); // All 23 - 8 (from test_*_.json) + assertEquals(15, getContentsOfDirectory(folder.getRoot()).length); // All 23 - 8 (from test_*_.json) } @Test public void testDeleteWithSubDirectoriesNoRecursive() throws IOException { initSubDirectories(); - DeleteOldAppData.deleteFiles(folder.getRoot().getAbsolutePath(), 0, "^test_.*\\.json$", false); + DeleteOldAppData.deleteFiles(folder.getRoot().toPath(), Duration.ZERO, Optional.of("^test_.*\\.json$"), false); // 6 test_*.json from test_folder1/ // + 9 test_*.json and 4 abc_*.json from test_folder2/ // + 13 test_*.json from test_folder2/subSubFolder2/ // + 7 test_*_file.test and 5 *-abc.json and 1 week_old_file from root // + test_folder1/ and test_folder2/ and test_folder2/subSubFolder2/ themselves - assertThat(getNumberOfFilesAndDirectoriesIn(folder.getRoot()), is(48)); + assertEquals(48, getNumberOfFilesAndDirectoriesIn(folder.getRoot())); } @Test public void testDeleteWithSubDirectoriesRecursive() throws IOException { initSubDirectories(); - DeleteOldAppData.deleteFiles(folder.getRoot().getAbsolutePath(), 0, "^test_.*\\.json$", true); + DeleteOldAppData.deleteFiles(folder.getRoot().toPath(), Duration.ZERO, Optional.of("^test_.*\\.json$"), true); // 4 abc_*.json from test_folder2/ // + 7 test_*_file.test and 5 *-abc.json and 1 week_old_file from root // + test_folder2/ itself - assertThat(getNumberOfFilesAndDirectoriesIn(folder.getRoot()), is(18)); + assertEquals(18, getNumberOfFilesAndDirectoriesIn(folder.getRoot())); } @Test public void testDeleteFilesWhereFilenameRegexAlsoMatchesDirectories() throws IOException { initSubDirectories(); - DeleteOldAppData.deleteFiles(folder.getRoot().getAbsolutePath(), 0, "^test_", false); + DeleteOldAppData.deleteFiles(folder.getRoot().toPath(), Duration.ZERO, Optional.of("^test_"), false); - assertThat(folder.getRoot().listFiles().length, is(8)); // 5 abc files + 1 week_old_file + 2 directories + assertEquals(8, getContentsOfDirectory(folder.getRoot()).length); // 5 abc files + 1 week_old_file + 2 directories } @Test public void testGetContentsOfNonExistingDirectory() throws IOException { - assertArrayEquals(new File[0], DeleteOldAppData.getContentsOfDirectory("/some/made/up/dir/")); + Path fakePath = Paths.get("/some/made/up/dir/"); + assertEquals(Collections.emptyList(), DeleteOldAppData.listContentsOfDirectory(fakePath)); } @Test(expected=IllegalArgumentException.class) - public void testDeleteFilesExceptNMostRecentWithNegativeN() { - DeleteOldAppData.deleteFilesExceptNMostRecent(folder.getRoot().getAbsolutePath(), -5); + public void testDeleteFilesExceptNMostRecentWithNegativeN() throws IOException { + DeleteOldAppData.deleteFilesExceptNMostRecent(folder.getRoot().toPath(), -5); } @Test - public void testDeleteFilesExceptFiveMostRecent() { - DeleteOldAppData.deleteFilesExceptNMostRecent(folder.getRoot().getAbsolutePath(), 5); + public void testDeleteFilesExceptFiveMostRecent() throws IOException { + DeleteOldAppData.deleteFilesExceptNMostRecent(folder.getRoot().toPath(), 5); - assertThat(folder.getRoot().listFiles().length, is(5)); + assertEquals(5, getContentsOfDirectory(folder.getRoot()).length); String[] oldestFiles = {"test_5_file.test", "test_6_file.test", "test_8.json", "test_9.json", "week_old_file.json"}; - String[] remainingFiles = folder.getRoot().list(); - Arrays.sort(remainingFiles); + String[] remainingFiles = Arrays.stream(getContentsOfDirectory(folder.getRoot())) + .map(File::getName) + .sorted() + .toArray(String[]::new); assertArrayEquals(oldestFiles, remainingFiles); } @Test - public void testDeleteFilesExceptNMostRecentWithLargeN() { + public void testDeleteFilesExceptNMostRecentWithLargeN() throws IOException { String[] filesPreDelete = folder.getRoot().list(); - DeleteOldAppData.deleteFilesExceptNMostRecent(folder.getRoot().getAbsolutePath(), 50); + DeleteOldAppData.deleteFilesExceptNMostRecent(folder.getRoot().toPath(), 50); assertArrayEquals(filesPreDelete, folder.getRoot().list()); } @@ -166,41 +172,41 @@ public class DeleteOldAppDataTest { File temp3 = new File(folder.getRoot(), "test_folder1/some_other_file"); writeNBytesToFile(temp3, 75); - DeleteOldAppData.deleteFilesLargerThan(folder.getRoot(), 10); + DeleteOldAppData.deleteFilesLargerThan(folder.getRoot().toPath(), 10); - assertThat(getNumberOfFilesAndDirectoriesIn(folder.getRoot()), is(58)); - assertThat(temp1.exists() || temp2.exists() || temp3.exists(), is(false)); + assertEquals(58, getNumberOfFilesAndDirectoriesIn(folder.getRoot())); + assertFalse(temp1.exists() || temp2.exists() || temp3.exists()); } @Test public void testDeleteDirectories() throws IOException { initSubDirectories(); - DeleteOldAppData.deleteDirectories(folder.getRoot().getAbsolutePath(), 0, ".*folder2"); + DeleteOldAppData.deleteDirectories(folder.getRoot().toPath(), Duration.ZERO, Optional.of(".*folder2")); //23 files in root // + 6 in test_folder1 + test_folder1 itself - assertThat(getNumberOfFilesAndDirectoriesIn(folder.getRoot()), is(30)); + assertEquals(30, getNumberOfFilesAndDirectoriesIn(folder.getRoot())); } @Test public void testDeleteDirectoriesBasedOnAge() throws IOException { initSubDirectories(); - DeleteOldAppData.deleteDirectories(folder.getRoot().getAbsolutePath(), 50, ".*folder.*"); + DeleteOldAppData.deleteDirectories(folder.getRoot().toPath(), Duration.ofSeconds(50), Optional.of(".*folder.*")); //23 files in root // + 13 in test_folder2 // + 13 in subSubFolder2 // + test_folder2 + subSubFolder2 itself - assertThat(getNumberOfFilesAndDirectoriesIn(folder.getRoot()), is(51)); + assertEquals(51, getNumberOfFilesAndDirectoriesIn(folder.getRoot())); } @Test public void testRecursivelyDeleteDirectory() throws IOException { initSubDirectories(); - DeleteOldAppData.recursiveDelete(folder.getRoot().toString()); - assertTrue(!folder.getRoot().exists()); + DeleteOldAppData.recursiveDelete(folder.getRoot().toPath()); + assertFalse(folder.getRoot().exists()); } @Test @@ -208,16 +214,16 @@ public class DeleteOldAppDataTest { File file = folder.newFile(); assertTrue(file.exists()); assertTrue(file.isFile()); - DeleteOldAppData.recursiveDelete(file.toString()); - assertTrue(!file.exists()); + DeleteOldAppData.recursiveDelete(file.toPath()); + assertFalse(file.exists()); } @Test public void testRecursivelyDeleteNonExistingFile() throws IOException { File file = folder.getRoot().toPath().resolve("non-existing-file.json").toFile(); - assertTrue(!file.exists()); - DeleteOldAppData.recursiveDelete(file.toString()); - assertTrue(!file.exists()); + assertFalse(file.exists()); + DeleteOldAppData.recursiveDelete(file.toPath()); + assertFalse(file.exists()); } @Test @@ -244,7 +250,6 @@ public class DeleteOldAppDataTest { File subFolder2 = folder.newFolder("test_folder2"); File subSubFolder2 = folder.newFolder("test_folder2/subSubFolder2"); - for (int j=0; j<6; j++) { File temp = File.createTempFile("test_", ".json", subFolder1); temp.setLastModified(System.currentTimeMillis() - (j+1)*Duration.ofSeconds(60).toMillis()); @@ -284,4 +289,11 @@ public class DeleteOldAppDataTest { private static void writeNBytesToFile(File file, int nBytes) throws IOException { Files.write(file.toPath(), new byte[nBytes]); } + + + static File[] getContentsOfDirectory(File directory) { + File[] directoryContents = directory.listFiles(); + + return directoryContents == null ? new File[0] : directoryContents; + } } -- cgit v1.2.3