@Test public void testAcceptIgnoresBasePathAndAttributes() { final IfFileName pathFilter = IfFileName.createNameCondition("path", null); final Path relativePath = Paths.get("path"); assertTrue(pathFilter.accept(null, relativePath, null)); final IfFileName regexFilter = IfFileName.createNameCondition(null, "regex"); final Path pathMatchingRegex = Paths.get("regex"); assertTrue(regexFilter.accept(null, pathMatchingRegex, null)); }
@Test public void testGetSyntaxAndPattern() { assertEquals("glob:path", IfFileName.createNameCondition("path", null).getSyntaxAndPattern()); assertEquals("glob:path", IfFileName.createNameCondition("glob:path", null).getSyntaxAndPattern()); assertEquals("regex:bar", IfFileName.createNameCondition(null, "bar").getSyntaxAndPattern()); assertEquals("regex:bar", IfFileName.createNameCondition(null, "regex:bar").getSyntaxAndPattern()); }
@Test public void testBeforeTreeWalk() { final CountingCondition counter = new CountingCondition(true); final IfFileName pathFilter = IfFileName.createNameCondition("path", null, counter, counter, counter); pathFilter.beforeFileTreeWalk(); assertEquals(3, counter.getBeforeFileTreeWalkCount()); } }
@Test() public void testCreateNameConditionAcceptsIfEitherRegexOrPathOrBothAreNonNull() { IfFileName.createNameCondition("bar", null); IfFileName.createNameCondition(null, "foo"); IfFileName.createNameCondition("bar", "foo"); }
/** * Constructs a FileNameFilter filter. If both a regular expression and a glob pattern are specified the glob * pattern is used and the regular expression is ignored. * * @param glob the baseDir-relative path pattern of the files to delete (may contain '*' and '?' wildcarts) * @param regex the regular expression that matches the baseDir-relative path of the file(s) to delete * @param nestedConditions nested conditions to evaluate if this condition accepts a path */ private IfFileName(final String glob, final String regex, final PathCondition[] nestedConditions) { if (regex == null && glob == null) { throw new IllegalArgumentException("Specify either a path glob or a regular expression. " + "Both cannot be null."); } this.syntaxAndPattern = createSyntaxAndPatternString(glob, regex); this.pathMatcher = FileSystems.getDefault().getPathMatcher(syntaxAndPattern); this.nestedConditions = nestedConditions == null ? new PathCondition[0] : Arrays.copyOf(nestedConditions, nestedConditions.length); }
/** * Creates a IfFileName condition that returns true if either the specified * {@linkplain FileSystem#getPathMatcher(String) glob pattern} or the regular expression matches the relative path. * If both a regular expression and a glob pattern are specified the glob pattern is used and the regular expression * is ignored. * * @param glob the baseDir-relative path pattern of the files to delete (may contain '*' and '?' wildcarts) * @param regex the regular expression that matches the baseDir-relative path of the file(s) to delete * @param nestedConditions nested conditions to evaluate if this condition accepts a path * @return A IfFileName condition. * @see FileSystem#getPathMatcher(String) */ @PluginFactory public static IfFileName createNameCondition( // @formatter:off @PluginAttribute("glob") final String glob, @PluginAttribute("regex") final String regex, @PluginElement("PathConditions") final PathCondition... nestedConditions) { // @formatter:on return new IfFileName(glob, regex, nestedConditions); }
@Test(expected = IllegalArgumentException.class) public void testCreateNameConditionFailsIfBothRegexAndPathAreNull() { IfFileName.createNameCondition(null, null); }
@Test public void testAcceptUsesPathPatternIfExists() { final IfFileName filter = IfFileName.createNameCondition("path", "regex"); final Path relativePath = Paths.get("path"); assertTrue(filter.accept(null, relativePath, null)); final Path pathMatchingRegex = Paths.get("regex"); assertFalse(filter.accept(null, pathMatchingRegex, null)); }
@Test public void testAcceptUsesRegexIfNoPathPatternExists() { final IfFileName regexFilter = IfFileName.createNameCondition(null, "regex"); final Path pathMatchingRegex = Paths.get("regex"); assertTrue(regexFilter.accept(null, pathMatchingRegex, null)); final Path noMatch = Paths.get("nomatch"); assertFalse(regexFilter.accept(null, noMatch, null)); }
@Test public void testAcceptCallsNestedConditionsOnlyIfPathAccepted1() { final CountingCondition counter = new CountingCondition(true); final IfFileName regexFilter = IfFileName.createNameCondition(null, "regex", counter); final Path pathMatchingRegex = Paths.get("regex"); assertTrue(regexFilter.accept(null, pathMatchingRegex, null)); assertEquals(1, counter.getAcceptCount()); assertTrue(regexFilter.accept(null, pathMatchingRegex, null)); assertEquals(2, counter.getAcceptCount()); assertTrue(regexFilter.accept(null, pathMatchingRegex, null)); assertEquals(3, counter.getAcceptCount()); final Path noMatch = Paths.get("nomatch"); assertFalse(regexFilter.accept(null, noMatch, null)); assertEquals(3, counter.getAcceptCount()); // no increase assertFalse(regexFilter.accept(null, noMatch, null)); assertEquals(3, counter.getAcceptCount()); assertFalse(regexFilter.accept(null, noMatch, null)); assertEquals(3, counter.getAcceptCount()); }
@Test public void testAcceptCallsNestedConditionsOnlyIfPathAccepted2() { final CountingCondition counter = new CountingCondition(true); final IfFileName globFilter = IfFileName.createNameCondition("glob", null, counter); final Path pathMatchingGlob = Paths.get("glob"); assertTrue(globFilter.accept(null, pathMatchingGlob, null)); assertEquals(1, counter.getAcceptCount()); assertTrue(globFilter.accept(null, pathMatchingGlob, null)); assertEquals(2, counter.getAcceptCount()); assertTrue(globFilter.accept(null, pathMatchingGlob, null)); assertEquals(3, counter.getAcceptCount()); final Path noMatch = Paths.get("nomatch"); assertFalse(globFilter.accept(null, noMatch, null)); assertEquals(3, counter.getAcceptCount()); // no increase assertFalse(globFilter.accept(null, noMatch, null)); assertEquals(3, counter.getAcceptCount()); assertFalse(globFilter.accept(null, noMatch, null)); assertEquals(3, counter.getAcceptCount()); }