ContextImpl implements Context, Constants { @Override public boolean definesLocation(@NotNull TreeLocation location) { PropertyState p = location.getProperty(); Tree tree = (p == null) ? location.getTree() : location.getParent().getTree(); if (tree != null) { return (p == null) ? definesTree(tree) : definesProperty(tree, p); } else { if (isItemName(location.getName())) { return true; } TreeLocation parent = location.getParent(); String parentName = parent.getName(); return REP_PRINCIPAL_POLICY.equals(parentName) || REP_RESTRICTIONS.equals(parentName); } } private  ContextImpl(); @Override boolean definesProperty(@NotNull Tree parent, @NotNull PropertyState property); @Override boolean definesContextRoot(@NotNull Tree tree); @Override boolean definesTree(@NotNull Tree tree); @Override boolean definesLocation(@NotNull TreeLocation location); @Override boolean definesInternal(@NotNull Tree tree);  }
@Test public void testDefinesNodeLocation() { assertTrue(CTX.definesLocation(TreeLocation.create(mockTree(REP_PRINCIPAL_POLICY, NT_REP_PRINCIPAL_POLICY, true)))); assertTrue(CTX.definesLocation(TreeLocation.create(mockTree(REP_RESTRICTIONS, NT_REP_RESTRICTIONS, true)))); assertTrue(CTX.definesLocation(TreeLocation.create(mockTree("anyEntry", NT_REP_PRINCIPAL_ENTRY, true)))); }
@Test public void testDefinesPropertyLocation() { String[] propNames = new String[] {REP_PRINCIPAL_NAME, "prop1", "prop2"}; Tree policyTree = MockUtility.mockTree(REP_PRINCIPAL_POLICY, NT_REP_PRINCIPAL_POLICY, PathUtils.ROOT_PATH + REP_PRINCIPAL_POLICY, propNames); Tree rootTree = MockUtility.mockTree(PathUtils.ROOT_NAME, NodeTypeConstants.NT_REP_ROOT, PathUtils.ROOT_PATH, propNames); when(rootTree.getChild(REP_PRINCIPAL_POLICY)).thenReturn(policyTree); Root r = when(mock(Root.class).getTree(PathUtils.ROOT_PATH)).thenReturn(rootTree).getMock(); for (String propName : propNames) { assertTrue(CTX.definesLocation(TreeLocation.create(r, PathUtils.concat(policyTree.getPath(), propName)))); assertFalse(CTX.definesLocation(TreeLocation.create(r, PathUtils.concat(rootTree.getPath(), propName)))); } }
@Test public void testDefinesNonExistingLocation() { assertTrue(CTX.definesLocation(TreeLocation.create(mockTree(REP_PRINCIPAL_POLICY, NT_REP_PRINCIPAL_POLICY, false)))); assertTrue(CTX.definesLocation(TreeLocation.create(mockTree(REP_RESTRICTIONS, NT_REP_RESTRICTIONS, false)))); Tree t = mockTree(REP_RESTRICTIONS, NT_REP_RESTRICTIONS, false, "anyResidualProperty"); assertTrue(CTX.definesLocation(TreeLocation.create(t).getChild("anyResidualProperty"))); t = mockTree(REP_PRINCIPAL_POLICY, NT_REP_PRINCIPAL_POLICY, false, "anyResidualAceName"); assertTrue(CTX.definesLocation(TreeLocation.create(t).getChild("anyResidualAceName"))); String[] propNames = new String[] {REP_EFFECTIVE_PATH, REP_PRINCIPAL_NAME, REP_PRIVILEGES}; t = mockTree("anyEntry", "anyNt", false, propNames); for (String propName : propNames) { assertTrue(CTX.definesLocation(TreeLocation.create(t).getChild(propName))); } String[] nodeNames = new String[] {REP_PRINCIPAL_POLICY, REP_RESTRICTIONS}; for (String nodeName : nodeNames) { assertTrue(CTX.definesLocation(TreeLocation.create(mockTree(nodeName, "anyNt", false)))); } }
@Test public void testNotDefinesLocation() { Tree t = mockTree("anyEntry", "anyNt", false, "anyResidualProperty"); assertFalse(CTX.definesLocation(TreeLocation.create(t).getChild("anyResidualProperty"))); t = mockTree("anyEntry", "anyNt", true, "anyResidualProperty"); assertFalse(CTX.definesLocation(TreeLocation.create(t).getChild("anyResidualProperty"))); }