Utils { @NotNull static Tree getOrAddTree(@NotNull Tree tree, @NotNull String relativePath, @NotNull String primaryTypeName) throws AccessDeniedException { if (PathUtils.denotesCurrent(relativePath)) { return tree; } else if (PathUtils.denotesParent(relativePath)) { return tree.getParent(); } else if (relativePath.indexOf('/') == -1) { return TreeUtil.getOrAddChild(tree, relativePath, primaryTypeName); } else { for (String element : PathUtils.elements(relativePath)) { if (PathUtils.denotesParent(element)) { tree = tree.getParent(); } else if (!PathUtils.denotesCurrent(element)) { tree = TreeUtil.getOrAddChild(tree, element, primaryTypeName); } } return tree; } } private  Utils();   }
@Test public void testGetOrAddTreeCurrentElement() throws Exception { Tree result = Utils.getOrAddTree(tree, ".", NodeTypeConstants.NT_OAK_UNSTRUCTURED); assertSame(tree, result); }
@Test public void testGetOrAddTreeParentElement() throws Exception { Tree child = Utils.getOrAddTree(tree, "child", NodeTypeConstants.NT_OAK_UNSTRUCTURED); Tree parent = Utils.getOrAddTree(child, "..", NodeTypeConstants.NT_OAK_UNSTRUCTURED); assertEqualPath(tree, parent); }
@Test(expected = IllegalStateException.class) public void testGetOrAddTreeParentElementFromRoot() throws Exception { Utils.getOrAddTree(tree, "..", NodeTypeConstants.NT_OAK_UNSTRUCTURED); }
@Test public void testGetOrAddTreeSingleElement() throws Exception { Tree child = Utils.getOrAddTree(tree, "child", NodeTypeConstants.NT_OAK_UNSTRUCTURED); assertEqualPath(root.getTree("/child"), child); }
@Test public void testGetOrAddTree() throws Exception { Map<String, String> map = ImmutableMap.of( "a/b/c", "/a/b/c", "a/../b/c", "/b/c", "a/b/c/../..", "/a", "a/././././b/c", "/a/b/c" ); for (String relPath : map.keySet()) { Tree t = Utils.getOrAddTree(tree, relPath, NodeTypeConstants.NT_OAK_UNSTRUCTURED); assertEqualPath(root.getTree(map.get(relPath)), t); } }
@Test(expected = IllegalStateException.class) public void testGetOrAddTreeReachesParentOfRoot() throws Exception { Utils.getOrAddTree(tree, "a/../../b", NodeTypeConstants.NT_OAK_UNSTRUCTURED); }
@Test(expected = AccessDeniedException.class) public void testGetOrAddTreeTargetNotAccessible() throws Exception { Tree nonExisting = mock(Tree.class); when(nonExisting.exists()).thenReturn(false); Tree t = mock(Tree.class); when(t.exists()).thenReturn(true); when(t.getParent()).thenReturn(t); when(t.getChild("a")).thenReturn(t); when(t.getChild("b")).thenReturn(nonExisting); when(t.addChild("b")).thenReturn(nonExisting); Utils.getOrAddTree(t, "a/a/b", NodeTypeConstants.NT_OAK_UNSTRUCTURED); }