RestrictionProviderImpl extends AbstractRestrictionProvider { @NotNull @Override public RestrictionPattern getPattern(String oakPath, @NotNull Tree tree) { if (oakPath == null) { return RestrictionPattern.EMPTY; } else { List<RestrictionPattern> patterns = new ArrayList<>(NUMBER_OF_DEFINITIONS); PropertyState glob = tree.getProperty(REP_GLOB); if (glob != null) { patterns.add(GlobPattern.create(oakPath, glob.getValue(Type.STRING))); } PropertyState ntNames = tree.getProperty(REP_NT_NAMES); if (ntNames != null) { patterns.add(new NodeTypePattern(ntNames.getValue(Type.NAMES))); } PropertyState prefixes = tree.getProperty(REP_PREFIXES); if (prefixes != null) { patterns.add(new PrefixPattern(prefixes.getValue(Type.STRINGS))); } PropertyState itemNames = tree.getProperty(REP_ITEM_NAMES); if (itemNames != null) { patterns.add(new ItemNamePattern(itemNames.getValue(Type.NAMES))); } return CompositePattern.create(patterns); } } RestrictionProviderImpl(); @NotNull @Override RestrictionPattern getPattern(String oakPath, @NotNull Tree tree); @NotNull @Override RestrictionPattern getPattern(@Nullable String oakPath, @NotNull Set<Restriction> restrictions); @Override void validateRestrictions(String oakPath, @NotNull Tree aceTree);  }
@Test public void testGetRestrictionPattern() throws Exception { Map<PropertyState, RestrictionPattern> map = newHashMap(); map.put(PropertyStates.createProperty(REP_GLOB, "jcr:content")); List<String> ntNames = ImmutableList.of(JcrConstants.NT_FOLDER, JcrConstants.NT_LINKEDFILE); map.put(PropertyStates.createProperty(REP_NT_NAMES, ntNames, Type.NAMES), new NodeTypePattern(ntNames)); Tree tree = TreeUtil.getOrAddChild(root.getTree("/"), "testPath", JcrConstants.NT_UNSTRUCTURED); Tree restrictions = TreeUtil.addChild(tree, REP_RESTRICTIONS, NT_REP_RESTRICTIONS); for (Map.Entry<PropertyState, RestrictionPattern> entry : map.entrySet()) { restrictions.setProperty(entry.getKey()); RestrictionPattern pattern = provider.getPattern("/testPath", restrictions); assertEquals(entry.getValue(), pattern); restrictions.removeProperty(entry.getKey().getName()); } for (Map.Entry<PropertyState, RestrictionPattern> entry : map.entrySet()) { restrictions.setProperty(entry.getKey()); } RestrictionPattern pattern = provider.getPattern("/testPath", restrictions); assertTrue(pattern instanceof CompositePattern); }
@Test public void testGetPatternForAllSupported() throws Exception { Map<PropertyState, RestrictionPattern> map = newHashMap(); map.put(PropertyStates.createProperty(REP_GLOB, "jcr:content")); List<String> ntNames = ImmutableList.of(JcrConstants.NT_FOLDER, JcrConstants.NT_LINKEDFILE); map.put(PropertyStates.createProperty(REP_NT_NAMES, ntNames, Type.NAMES), new NodeTypePattern(ntNames)); List<String> prefixes = ImmutableList.of("rep", "jcr"); map.put(PropertyStates.createProperty(REP_PREFIXES, prefixes, Type.STRINGS), new PrefixPattern(prefixes)); List<String> itemNames = ImmutableList.of("abc", "jcr:primaryType"); map.put(PropertyStates.createProperty(REP_ITEM_NAMES, prefixes, Type.NAMES), new ItemNamePattern(itemNames)); Tree tree = TreeUtil.getOrAddChild(root.getTree("/"), "testPath", JcrConstants.NT_UNSTRUCTURED); Tree restrictions = TreeUtil.addChild(tree, REP_RESTRICTIONS, NT_REP_RESTRICTIONS); for (Map.Entry<PropertyState, RestrictionPattern> entry : map.entrySet()) { restrictions.setProperty(entry.getKey()); } RestrictionPattern pattern = provider.getPattern("/testPath", restrictions); assertTrue(pattern instanceof CompositePattern); }
@Test public void testGetPatternFromRestrictions() throws Exception { Map<PropertyState, RestrictionPattern> map = newHashMap(); map.put(PropertyStates.createProperty(REP_GLOB, "jcr:content")); List<String> ntNames = ImmutableList.of(JcrConstants.NT_FOLDER, JcrConstants.NT_LINKEDFILE); map.put(PropertyStates.createProperty(REP_NT_NAMES, ntNames, Type.NAMES), new NodeTypePattern(ntNames)); List<String> prefixes = ImmutableList.of("rep", "jcr"); map.put(PropertyStates.createProperty(REP_PREFIXES, prefixes, Type.STRINGS), new PrefixPattern(prefixes)); List<String> itemNames = ImmutableList.of("abc", "jcr:primaryType"); map.put(PropertyStates.createProperty(REP_ITEM_NAMES, itemNames, Type.NAMES), new ItemNamePattern(itemNames)); Tree tree = TreeUtil.getOrAddChild(root.getTree("/"), "testPath", JcrConstants.NT_UNSTRUCTURED); Tree restrictions = TreeUtil.addChild(tree, REP_RESTRICTIONS, NT_REP_RESTRICTIONS); for (Map.Entry<PropertyState, RestrictionPattern> entry : map.entrySet()) { restrictions.setProperty(entry.getKey()); RestrictionPattern pattern = provider.getPattern("/testPath", provider.readRestrictions("/testPath", tree)); assertEquals(entry.getValue(), pattern); restrictions.removeProperty(entry.getKey().getName()); } for (Map.Entry<PropertyState, RestrictionPattern> entry : map.entrySet()) { restrictions.setProperty(entry.getKey()); } RestrictionPattern pattern = provider.getPattern("/testPath", provider.readRestrictions("/testPath", tree)); assertTrue(pattern instanceof CompositePattern); }
@Test public void testGetPatternFromTreeNullPath() { assertSame(RestrictionPattern.EMPTY, provider.getPattern(null, mock(Tree.class))); }
@Test public void testGetPatternFromRestrictionsNullPath() { assertSame(RestrictionPattern.EMPTY, provider.getPattern(null, ImmutableSet.of(mock(Restriction.class)))); }
@Test public void testGetPatternFromEmptyRestrictions() { assertSame(RestrictionPattern.EMPTY, provider.getPattern("/testPath", ImmutableSet.of())); }