FilteringNodeState extends AbstractDecoratedNodeState { @NotNull public static NodeState wrap( @NotNull final String path, @NotNull final NodeState delegate, @Nullable final Set<String> includePaths, @Nullable final Set<String> excludePaths, @Nullable final Set<String> fragmentPaths, @Nullable final Set<String> excludedFragments ) { final Set<String> includes = defaultIfEmpty(includePaths, ALL); final Set<String> excludes = defaultIfEmpty(excludePaths, NONE); final Set<String> safeFragmentPaths = defaultIfEmpty(fragmentPaths, NONE); final Set<String> safeExcludedFragments = defaultIfEmpty(excludedFragments, NONE); if (hasHiddenDescendants(path, includes, excludes, safeFragmentPaths, safeExcludedFragments)) { return new FilteringNodeState(path, delegate, includes, excludes, fragmentPaths, safeExcludedFragments); } return delegate; } private  FilteringNodeState(
            @NotNull final String path,
            @NotNull final NodeState delegate,
            @NotNull final Set<String> includedPaths,
            @NotNull final Set<String> excludedPaths,
            @NotNull final Set<String> fragmentPaths,
            @NotNull final Set<String> excludedFragments
    ); @NotNull static NodeState wrap(
            @NotNull final String path,
            @NotNull final NodeState delegate,
            @Nullable final Set<String> includePaths,
            @Nullable final Set<String> excludePaths,
            @Nullable final Set<String> fragmentPaths,
            @Nullable final Set<String> excludedFragments
    ); static final Set<String> ALL; static final Set<String> NONE; }
@Test public void shouldNotDecorateForNullArgs() { final NodeState decorated = wrap("/", rootNodeState, null, null, null, null); assertSame("root should be identical to decorated", rootNodeState, decorated); }
@Test public void shouldNotDecorateForDefaultIncludes() { final NodeState decorated = wrap("/", rootNodeState, DEFAULT_INCLUDES, null, null, null); assertSame("root should be identical to decorated", rootNodeState, decorated); }
@Test public void shouldNotDecorateForDefaultExcludes() { final NodeState decorated = wrap("/", rootNodeState, null, DEFAULT_EXCLUDES, null, null); assertSame("root should be identical to decorated", rootNodeState, decorated); }
@Test public void shouldNotDecorateForDefaultIncludesAndExcludes() { final NodeState decorated = wrap("/", rootNodeState, DEFAULT_INCLUDES, DEFAULT_EXCLUDES, null, null); assertSame("root should be identical to decorated", rootNodeState, decorated); }
@Test public void shouldNotDecorateIncludedPath() { final NodeState content = getNodeState(rootNodeState, "/content"); final NodeState decorated = wrap("/content", content, of("/content"), null, null, null); assertSame("content should be identical to decorated", content, decorated); }
@Test public void shouldNotDecorateIncludedDescendants() { final NodeState foo = getNodeState(rootNodeState, "/content/foo"); final NodeState decorated = wrap("/content/foo", foo, of("/content"), null, null, null); assertSame("foo should be identical to decorated", foo, decorated); }
@Test public void shouldDecorateAncestorOfExcludedDescendants() { final NodeState foo = getNodeState(rootNodeState, "/content/foo"); final NodeState decorated = wrap("/content/foo", foo, of("/content"), of("/content/foo/de"), null, null); assertNotSame("foo should not be identical to decorated", foo, decorated); assertMissing(decorated, "de"); assertExists(decorated, "en"); assertFalse("child nodes \"de\" should not be equal", getNodeState(foo, "de").equals(getNodeState(decorated, "de"))); final NodeState en = getNodeState(decorated, "en"); assertEquals("child nodes \"en\" should be equal", getNodeState(foo, "en"), en); assertTrue("child node \"en\" should not be decorated", !(en instanceof FilteringNodeState)); }
@Test public void shouldHaveCorrectChildOrderProperty() throws CommitFailedException { final NodeState content = rootNodeState.getChildNode("content"); final NodeState decorated = wrap("/content", content, null, of("/content/foo"), null, null); assertTrue(decorated.hasProperty(OAK_CHILD_ORDER)); { final PropertyState childOrder = decorated.getProperty(OAK_CHILD_ORDER); final Iterable<String> values = childOrder.getValue(Type.STRINGS); assertEquals(newArrayList("football"), newArrayList(values)); } { final Predicate<PropertyState> isChildOrderProperty = new Predicate<PropertyState>() { @Override public boolean apply(PropertyState propertyState) { return OAK_CHILD_ORDER.equals(propertyState.getName()); } }; final PropertyState childOrder = Iterables.find(decorated.getProperties(), isChildOrderProperty); final Iterable<String> values = childOrder.getValue(Type.STRINGS); assertEquals(newArrayList("football"), newArrayList(values)); } }
@Test public void shouldDecorateExcludedNode() { final NodeState decoratedRoot = wrap("/", rootNodeState, of("/content"), of("/content/foo/de"), null, null); final NodeState de = getNodeState(rootNodeState, "/content/foo/de"); final NodeState decorated = getNodeState(decoratedRoot, "/content/foo/de"); assertFalse("de should not be equal to decorated", de.equals(decorated)); assertFalse("decorated should not exist", decorated.exists()); }
@Test public void shouldDecorateImplicitlyExcludedNode() { final NodeState content = getNodeState(rootNodeState, "/content"); final NodeState decorated = wrap("/content", content, of("/apps"), null, null, null); assertNotSame("content should not be identical to decorated", content, decorated); }
@Test public void shouldHideExcludedPathsViaExists() { final NodeState decorated = wrap("/", rootNodeState, null, of("/apps", "/libs"), null, null); assertMissing(decorated, "apps"); assertMissing(decorated, "libs/foo/install"); assertExists(decorated, "content/foo/de"); assertExists(decorated, "content/foo/en"); }
@Test public void shouldHideExcludedPathsViaHasChildNode() { final NodeState decorated = wrap("/", rootNodeState, null, of("/apps", "/libs"), null, null); assertExistingHasChildNode(decorated, "content"); assertMissingHasChildNode(decorated, "apps"); assertMissingHasChildNode(decorated, "libs"); }
@Test public void shouldHideExcludedPathsViaGetChildNodeNames() { final NodeState decorated = wrap("/", rootNodeState, null, of("/apps", "/libs"), null, null); assertExistingChildNodeName(decorated, "content"); assertMissingChildNodeName(decorated, "apps"); assertMissingChildNodeName(decorated, "libs"); }
@Test public void shouldHideMissingIncludedPathsViaExists() { final NodeState decorated = wrap("/", rootNodeState, of("/content"), null, null, null); assertMissing(decorated, "apps"); assertMissing(decorated, "libs/foo/install"); assertExists(decorated, "content/foo/de"); assertExists(decorated, "content/foo/en"); }
@Test public void shouldHideMissingIncludedPathsViaHasChildNode() { final NodeState decorated = wrap("/", rootNodeState, of("/content"), null, null, null); assertExistingHasChildNode(decorated, "content"); assertMissingHasChildNode(decorated, "apps"); assertMissingHasChildNode(decorated, "libs"); }
@Test public void shouldHideMissingIncludedPathsViaGetChildNodeNames() { final NodeState decorated = wrap("/", rootNodeState, of("/content"), null, null, null); assertExistingChildNodeName(decorated, "content"); assertMissingChildNodeName(decorated, "apps"); assertMissingChildNodeName(decorated, "libs"); }
@Test public void shouldGivePrecedenceForExcludesOverIncludes() { final NodeState conflictingRules = wrap("/", rootNodeState, of("/content"), of("/content"), null, null); assertMissingChildNodeName(conflictingRules, "content"); final NodeState overlappingRules = wrap("/", rootNodeState, of("/content"), of("/content/foo"), null, null); assertExistingChildNodeName(overlappingRules, "content"); assertMissingChildNodeName(overlappingRules.getChildNode("content"), "foo"); final NodeState overlappingRules2 = wrap("/", rootNodeState, of("/content/foo"), of("/content"), null, null); assertMissingChildNodeName(overlappingRules2, "content"); assertMissingChildNodeName(overlappingRules2.getChildNode("content"), "foo"); }
@Test public void shouldRespectPathBoundariesForIncludes() { final NodeState decorated = wrap("/", rootNodeState, of("/content/foo"), null, null, null); assertExistingChildNodeName(decorated, "content"); assertExistingChildNodeName(decorated.getChildNode("content"), "foo"); assertMissingChildNodeName(decorated.getChildNode("content"), "football"); }
@Test public void shouldRespectPathBoundariesForExcludes() { final NodeState decorated = wrap("/", rootNodeState, null, of("/content/foo"), null, null); assertExistingChildNodeName(decorated, "content"); assertMissingChildNodeName(decorated.getChildNode("content"), "foo"); assertExistingChildNodeName(decorated.getChildNode("content"), "football"); }
@Test public void shouldDelegatePropertyCount() { final NodeState decorated = wrap("/", rootNodeState, null, of("/content/foo/de"), null, null); assertEquals(1, getNodeState(decorated, "/content").getPropertyCount()); assertEquals(0, getNodeState(decorated, "/content/foo").getPropertyCount()); }
@Test public void shouldDelegateGetProperty() { final NodeState decorated = wrap("/", rootNodeState, null, of("/content/foo"), null, null); final NodeState content = getNodeState(decorated, "/content"); assertNotNull(content.getProperty(OAK_CHILD_ORDER)); assertNull(content.getProperty("nonexisting")); }
@Test public void shouldDelegateHasProperty() { final NodeState decorated = wrap("/", rootNodeState, null, of("/content/foo/de"), null, null); assertTrue(getNodeState(decorated, "/content").hasProperty(OAK_CHILD_ORDER)); assertFalse(getNodeState(decorated, "/content").hasProperty("foo")); }
@Test public void exists() { final NodeState decorated = wrap("/", rootNodeState, null, of("/content/foo"), null, null); assertTrue("/content should exist and be visible", getNodeState(decorated, "/content").exists()); assertFalse("/content/foo should be hidden", getNodeState(decorated, "/content/foo").exists()); assertFalse("/nonexisting should not exist", getNodeState(decorated, "/nonexisting").exists()); }