PrefixPattern implements RestrictionPattern { @Override public boolean matches(@NotNull Tree tree, @Nullable PropertyState property) { String name = (property != null) ? property.getName() : tree.getName(); String prefix = Text.getNamespacePrefix(name); if (!prefix.isEmpty()) { for (String p : prefixes) { if (prefix.equals(p)) { return true; } } } return false; } PrefixPattern(@NotNull Iterable<String> prefixes); @Override boolean matches(@NotNull Tree tree, @Nullable PropertyState property); @Override boolean matches(@NotNull String path); @Override boolean matches(); @Override int hashCode(); @Override String toString(); @Override boolean equals(Object obj);  }
@Test public void testMatchesItem() throws Exception { Tree rootTree = root.getTree("/"); for (String prefix : prefixes) { Tree testTree = TreeUtil.addChild(rootTree, prefix + ":name", NodeTypeConstants.NT_OAK_UNSTRUCTURED); assertTrue(pattern.matches(testTree, null)); assertTrue(pattern.matches(testTree, PropertyStates.createProperty(prefix + ":f", "anyval"))); assertFalse(pattern.matches(testTree, PropertyStates.createProperty("a", Boolean.FALSE))); testTree.remove(); } List<String> notMatching = ImmutableList.of(NamespaceRegistry.PREFIX_EMPTY, NamespaceRegistry.PREFIX_MIX, "any"); for (String prefix : notMatching) { String name = (prefix.isEmpty()) ? "name" : prefix + ":name"; Tree testTree = TreeUtil.addChild(rootTree, name, NodeTypeConstants.NT_OAK_UNSTRUCTURED); assertFalse(pattern.matches(testTree, null)); assertFalse(pattern.matches(testTree, PropertyStates.createProperty("f", "anyval"))); assertTrue(pattern.matches(testTree, PropertyStates.createProperty("jcr:a", Boolean.FALSE))); testTree.remove(); } }
@Test public void testMatchesPath() { List<String> notMatching = ImmutableList.of("/", "/a", "/jcr:b", "/d/jcr:e/a", "/a/b/c/d/jcr:b"); for (String p : notMatching) { assertFalse(pattern.matches(p)); } }
@Test public void testMatchesNull() { assertFalse(pattern.matches()); }