@Test public void testSelectFilesToDelete2() { final Configuration config = new DefaultConfiguration(); config.initialize(); // creates the ScriptManager final List<PathWithAttributes> pathList = new ArrayList<>(); pathList.add(new PathWithAttributes(Paths.get("/path/1"), new DummyFileAttributes())); pathList.add(new PathWithAttributes(Paths.get("/path/2"), new DummyFileAttributes())); pathList.add(new PathWithAttributes(Paths.get("/path/3"), new DummyFileAttributes())); final String scriptText = "pathList.remove(1);" // + "pathList;"; final Script script = new Script("test", "javascript", scriptText); final ScriptCondition condition = new ScriptCondition(script, config); final Path base = Paths.get("baseDirectory"); final List<PathWithAttributes> result = condition.selectFilesToDelete(base, pathList); assertSame(result, pathList); assertEquals(2, result.size()); assertEquals(Paths.get("/path/1"), result.get(0).getPath()); assertEquals(Paths.get("/path/3"), result.get(1).getPath()); }
@Override public int compare(final PathWithAttributes path1, final PathWithAttributes path2) { final long lastModified1 = path1.getAttributes().lastModifiedTime().toMillis(); final long lastModified2 = path2.getAttributes().lastModifiedTime().toMillis(); int result = Long.signum(lastModified2 - lastModified1); if (result == 0) { // if same time compare paths lexicographically try { // assuming paths contain counters and dates, use reverse lexicographical order: // 20151129 before 20151128, path-2.log before path-1.log result = path2.getPath().compareTo(path1.getPath()); } catch (final ClassCastException ex) { result = path2.getPath().toString().compareTo(path1.getPath().toString()); } } return multiplier * result; } }
@Test public void testRecentFirst() throws Exception { final SortingVisitor visitor = new SortingVisitor(new PathSortByModificationTime(true)); final Set<FileVisitOption> options = Collections.emptySet(); Files.walkFileTree(base, options, 1, visitor); final List<PathWithAttributes> found = visitor.getSortedPaths(); assertNotNull(found); assertEquals("file count", 3, found.size()); assertEquals("1st: most recent; sorted=" + found, ccc, found.get(0).getPath()); assertEquals("2nd; sorted=" + found, bbb, found.get(1).getPath()); assertEquals("3rd: oldest; sorted=" + found, aaa, found.get(2).getPath()); }
private PathWithAttributes path(final Path path, final DummyFileAttributes attributes) { return new PathWithAttributes(path, attributes); }
@Override public boolean execute(final FileVisitor<Path> visitor) throws IOException { final List<PathWithAttributes> sortedPaths = getSortedPaths(); trace("Sorted paths:", sortedPaths); for (final PathWithAttributes element : sortedPaths) { try { visitor.visitFile(element.getPath(), element.getAttributes()); } catch (final IOException ioex) { LOGGER.error("Error in post-rollover Delete when visiting {}", element.getPath(), ioex); visitor.visitFileFailed(element.getPath(), ioex); } } // TODO return (visitor.success || ignoreProcessingFailure) return true; // do not abort rollover even if processing failed }
@Test public void testRecentLast() throws Exception { final SortingVisitor visitor = new SortingVisitor(new PathSortByModificationTime(false)); final Set<FileVisitOption> options = Collections.emptySet(); Files.walkFileTree(base, options, 1, visitor); final List<PathWithAttributes> found = visitor.getSortedPaths(); assertNotNull(found); assertEquals("file count", 3, found.size()); assertEquals("1st: oldest first; sorted=" + found, aaa, found.get(0).getPath()); assertEquals("2nd; sorted=" + found, bbb, found.get(1).getPath()); assertEquals("3rd: most recent sorted; list=" + found, ccc, found.get(2).getPath()); } }
@Override public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs) throws IOException { collected.add(new PathWithAttributes(path, attrs)); return FileVisitResult.CONTINUE; }
pathList.add(new PathWithAttributes(Paths.get("/path/1/abc/a.txt"), new DummyFileAttributes())); pathList.add(new PathWithAttributes(Paths.get("/path/2/abc/bbb.txt"), new DummyFileAttributes())); pathList.add(new PathWithAttributes(Paths.get("/path/3/abc/c.txt"), new DummyFileAttributes())); final List<PathWithAttributes> result = condition.selectFilesToDelete(base, pathList); assertEquals(1, result.size()); assertEquals(Paths.get("/path/2/abc/bbb.txt"), result.get(0).getPath());
private void deleteSelectedFiles(final List<PathWithAttributes> selectedForDeletion) throws IOException { trace("Paths the script selected for deletion:", selectedForDeletion); for (final PathWithAttributes pathWithAttributes : selectedForDeletion) { final Path path = pathWithAttributes == null ? null : pathWithAttributes.getPath(); if (isTestMode()) { LOGGER.info("Deleting {} (TEST MODE: file not actually deleted)", path); } else { delete(path); } } }