@Test public void testCreateFileVisitorTestModeIsActionTestMode() { final DeleteAction delete = createAnyFilter("any", true, 0, false); assertFalse(delete.isTestMode()); final FileVisitor<Path> visitor = delete.createFileVisitor(delete.getBasePath(), delete.getPathConditions()); assertTrue(visitor instanceof DeletingVisitor); assertFalse(((DeletingVisitor) visitor).isTestMode()); final DeleteAction deleteTestMode = createAnyFilter("any", true, 0, true); assertTrue(deleteTestMode.isTestMode()); final FileVisitor<Path> testVisitor = deleteTestMode.createFileVisitor(delete.getBasePath(), delete.getPathConditions()); assertTrue(testVisitor instanceof DeletingVisitor); assertTrue(((DeletingVisitor) testVisitor).isTestMode()); } }
private static DeleteAction create(final String path, final boolean followLinks, final int maxDepth, final boolean testMode, final PathCondition[] conditions) { final Configuration config = new BasicConfigurationFactory().new BasicConfiguration(); final DeleteAction delete = DeleteAction.createDeleteAction(path, followLinks, maxDepth, testMode, null, conditions, null, config); return delete; }
@Test public void testGetBasePathStringReturnsOriginalParam() { final DeleteAction delete = createAnyFilter("${sys:user.home}/a/b/c", false, 1, false); assertEquals("${sys:user.home}/a/b/c", delete.getBasePathString()); }
@Test public void testCreateFileVisitorReturnsDeletingVisitor() { final DeleteAction delete = createAnyFilter("any", true, 0, false); final FileVisitor<Path> visitor = delete.createFileVisitor(delete.getBasePath(), delete.getPathConditions()); assertTrue(visitor instanceof DeletingVisitor); }
private List<PathWithAttributes> callScript() throws IOException { final List<PathWithAttributes> sortedPaths = getSortedPaths(); trace("Sorted paths:", sortedPaths); final List<PathWithAttributes> result = scriptCondition.selectFilesToDelete(getBasePath(), sortedPaths); return result; }
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); } } }
@Test public void testGetBasePathResolvesLookups() { final DeleteAction delete = createAnyFilter("${sys:user.home}/a/b/c", false, 1, false); final Path actual = delete.getBasePath(); final String expected = System.getProperty("user.home") + "/a/b/c"; assertEquals(FileSystems.getDefault().getPath(expected), actual); }
private boolean executeScript() throws IOException { final List<PathWithAttributes> selectedForDeletion = callScript(); if (selectedForDeletion == null) { LOGGER.trace("Script returned null list (no files to delete)"); return true; } deleteSelectedFiles(selectedForDeletion); return true; }
@Override public boolean execute() throws IOException { return scriptCondition != null ? executeScript() : super.execute(); }
return new DeleteAction(basePath, followLinks, maxDepth, testMode, sorter, pathConditions, scriptCondition, config.getStrSubstitutor());