UserContext implements Context, UserConstants { @Override public boolean definesLocation(@NotNull TreeLocation location) { Tree tree = location.getTree(); if (tree != null && location.exists()) { PropertyState p = location.getProperty(); return (p == null) ? definesTree(tree) : definesProperty(tree, p); } else { String path = location.getPath(); String name = Text.getName(path); return USER_PROPERTY_NAMES.contains(name) || GROUP_PROPERTY_NAMES.contains(name) || path.contains(REP_MEMBERS_LIST) || path.contains(REP_MEMBERS) || path.contains(REP_PWD); } } private  UserContext(); @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 testDefinesLocation() { for (String ntName : NT_NAMES) { Tree t = mockTree("anyName", ntName); TreeLocation location = Mockito.mock(TreeLocation.class); when(location.getTree()).thenReturn(t); when(location.exists()).thenReturn(true); assertTrue(ctx.definesLocation(location)); } }
@Test public void testPropertyDefinesLocation() { Map<String, Collection<String>> m = ImmutableMap.of( NT_REP_GROUP, GROUP_PROPERTY_NAMES, NT_REP_USER, USER_PROPERTY_NAMES, NT_REP_PASSWORD, PWD_PROPERTY_NAMES, NT_REP_MEMBER_REFERENCES, ImmutableList.of(REP_MEMBERS) ); for (String ntName : m.keySet()) { Tree t = mockTree("anyName", ntName); TreeLocation location = Mockito.mock(TreeLocation.class); when(location.getTree()).thenReturn(t); when(location.exists()).thenReturn(true); for (String propName : m.get(ntName)) { PropertyState property = PropertyStates.createProperty(propName, "value"); when(location.getProperty()).thenReturn(property); assertTrue(ctx.definesLocation(location)); } PropertyState property = PropertyStates.createProperty("anyName", "value"); when(location.getProperty()).thenReturn(property); assertFalse(ctx.definesLocation(location)); } }
@Test public void testNonExistingTreeDefinesLocation() { for (String ntName : NT_NAMES) { Tree t = mockTree("anyName", ntName); TreeLocation location = Mockito.mock(TreeLocation.class); when(location.getTree()).thenReturn(t); when(location.exists()).thenReturn(false); when(location.getPath()).thenReturn("/somePath"); assertFalse(ctx.definesLocation(location)); } }
@Test public void testNonExistingTreeDefinesLocation2() { for (String name : Iterables.concat(USER_PROPERTY_NAMES, GROUP_PROPERTY_NAMES)) { String path = "/some/path/endingWith/reservedName/" + name; for (String ntName : NT_NAMES) { Tree t = mockTree("anyName", ntName); TreeLocation location = Mockito.mock(TreeLocation.class); when(location.getTree()).thenReturn(t); when(location.exists()).thenReturn(false); when(location.getPath()).thenReturn(path); assertTrue(ctx.definesLocation(location)); } } }
@Test public void testNoTreeDefinesLocationReservedEnding() { for (String name : Iterables.concat(USER_PROPERTY_NAMES, GROUP_PROPERTY_NAMES)) { String path = "/some/path/endingWith/reservedName/" + name; TreeLocation location = Mockito.mock(TreeLocation.class); when(location.getPath()).thenReturn(path); assertTrue(path, ctx.definesLocation(location)); } }
@Test public void testNoTreeDefinesLocationIntermediate() { for (String name : new String[] {REP_MEMBERS, REP_MEMBERS_LIST, REP_PWD}) { String path = "/some/path/with/intermediate/" +name+ "/reserved"; TreeLocation location = Mockito.mock(TreeLocation.class); when(location.getPath()).thenReturn(path); assertTrue(path, ctx.definesLocation(location)); } }
@Test public void testNotDefinesLocation() { List<String> paths = ImmutableList.of( PathUtils.ROOT_PATH, NodeTypeConstants.NODE_TYPES_PATH, "/content", "" ); for (String path : paths) { TreeLocation location = Mockito.mock(TreeLocation.class); when(location.getPath()).thenReturn(path); assertFalse(path, ctx.definesLocation(location)); } }