TreeUtil { public static String getDefaultChildType( Tree typeRoot, Tree parent, String childName) { String name = dropIndexFromName(childName); boolean sns = !name.equals(childName); List<Tree> types = getEffectiveType(parent, typeRoot); for (Tree type : types) { Tree definitions = type .getChild(REP_NAMED_CHILD_NODE_DEFINITIONS) .getChild(name); String defaultName = findDefaultPrimaryType(definitions, sns); if (defaultName != null) { return defaultName; } } for (Tree type : types) { Tree definitions = type .getChild(REP_RESIDUAL_CHILD_NODE_DEFINITIONS); String defaultName = findDefaultPrimaryType(definitions, sns); if (defaultName != null) { return defaultName; } } return null; } private  TreeUtil(); @Nullable static String getPrimaryTypeName(@NotNull Tree tree); @Nullable static String getPrimaryTypeName(@NotNull Tree tree, @NotNull LazyValue<Tree> readOnlyTree); @NotNull static Iterable<String> getMixinTypeNames(@NotNull Tree tree); @NotNull static Iterable<String> getMixinTypeNames(@NotNull Tree tree, @NotNull LazyValue<Tree> readOnlyTree); @Nullable static Iterable<String> getStrings(@NotNull Tree tree, @NotNull String propertyName); @Nullable static String getString(@NotNull Tree tree, @NotNull String propertyName); @Nullable static String getString(@NotNull Tree tree, @NotNull String name, @Nullable String defaultValue); static boolean getBoolean(@NotNull Tree tree, @NotNull String propertyName); @Nullable static String getName(@NotNull Tree tree, @NotNull String name); @NotNull static Iterable<String> getNames(@NotNull Tree tree, @NotNull String name); static long getLong(@NotNull Tree tree, @NotNull String name, long defaultValue); @Nullable static Tree getTree(@NotNull Tree tree, @NotNull String path); static Tree addChild(
            @NotNull Tree parent, @NotNull String name,
            @Nullable String typeName, @NotNull Tree typeRoot,
            @Nullable String userID); @NotNull static Tree addChild(@NotNull Tree tree, @NotNull String childName, @NotNull String primaryTypeName); @NotNull static Tree getOrAddChild(@NotNull Tree tree, @NotNull String childName, @NotNull String primaryTypeName); static void addMixin(@NotNull Tree tree, @NotNull String mixinName, @NotNull Tree typeRoot, @Nullable String userID); static void autoCreateItems(@NotNull Tree tree, @NotNull Tree type, @NotNull Tree typeRoot, @Nullable String userID); static PropertyState autoCreateProperty(@NotNull String name,
                                                   @NotNull Tree definition,
                                                   @Nullable String userID); static String getDefaultChildType(
            Tree typeRoot, Tree parent, String childName); static List<Tree> getEffectiveType(Tree tree, Tree typeRoot); static String findDefaultPrimaryType(Tree definitions, boolean sns); static boolean isNodeType(Tree tree, String typeName, Tree typeRoot); static boolean isReadOnlyTree(@NotNull Tree tree);  }
@Test public void testGetDefaultChildTypeFromNamed() { Tree def = mock(Tree.class); Tree definitions = when(mock(Tree.class).getChildren()).thenReturn(ImmutableList.of(def)).getMock(); PropertyState ps = PropertyStates.createProperty(JCR_DEFAULTPRIMARYTYPE, NT_RESOURCE, Type.NAME); when(def.getProperty(JCR_DEFAULTPRIMARYTYPE)).thenReturn(ps); PropertyState sns = PropertyStates.createProperty(JCR_SAMENAMESIBLINGS, false, Type.BOOLEAN); when(def.getProperty(JCR_SAMENAMESIBLINGS)).thenReturn(sns); Tree named = when(mock(Tree.class).getChild("newChild")).thenReturn(definitions).getMock(); when(ntDef.getChild(REP_NAMED_CHILD_NODE_DEFINITIONS)).thenReturn(named); Tree emptyDefinitions = when(mock(Tree.class).getChildren()).thenReturn(ImmutableList.of(def)).getMock(); when(ntDef.getChild(REP_RESIDUAL_CHILD_NODE_DEFINITIONS)).thenReturn(emptyDefinitions); assertEquals(NT_RESOURCE, TreeUtil.getDefaultChildType(typeRoot, child, "newChild")); assertNull(TreeUtil.getDefaultChildType(typeRoot, child, "newChild[4]")); }
@Test public void testGetDefaultChildTypeFromNamedWithSns() { Tree def = mock(Tree.class); Tree definitions = when(mock(Tree.class).getChildren()).thenReturn(ImmutableList.of(def)).getMock(); PropertyState ps = PropertyStates.createProperty(JCR_DEFAULTPRIMARYTYPE, NT_RESOURCE, Type.NAME); when(def.getProperty(JCR_DEFAULTPRIMARYTYPE)).thenReturn(ps); PropertyState sns = PropertyStates.createProperty(JCR_SAMENAMESIBLINGS, true, Type.BOOLEAN); when(def.getProperty(JCR_SAMENAMESIBLINGS)).thenReturn(sns); Tree named = when(mock(Tree.class).getChild("newChild")).thenReturn(definitions).getMock(); when(ntDef.getChild(REP_NAMED_CHILD_NODE_DEFINITIONS)).thenReturn(named); assertEquals(NT_RESOURCE, TreeUtil.getDefaultChildType(typeRoot, child, "newChild")); assertEquals(NT_RESOURCE, TreeUtil.getDefaultChildType(typeRoot, child, "newChild[4]")); }
@Test public void testGetDefaultChildTypeFromResidual() { Tree emptyDefinitions = when(mock(Tree.class).getChildren()).thenReturn(ImmutableList.of()).getMock(); Tree named = when(mock(Tree.class).getChild("newChild")).thenReturn(emptyDefinitions).getMock(); when(ntDef.getChild(REP_NAMED_CHILD_NODE_DEFINITIONS)).thenReturn(named); Tree def = mock(Tree.class); PropertyState ps = PropertyStates.createProperty(JCR_DEFAULTPRIMARYTYPE, NT_RESOURCE, Type.NAME); when(def.getProperty(JCR_DEFAULTPRIMARYTYPE)).thenReturn(ps); PropertyState sns = PropertyStates.createProperty(JCR_SAMENAMESIBLINGS, false, Type.BOOLEAN); when(def.getProperty(JCR_SAMENAMESIBLINGS)).thenReturn(sns); Tree definitions = when(mock(Tree.class).getChildren()).thenReturn(ImmutableList.of(def)).getMock(); when(ntDef.getChild(REP_RESIDUAL_CHILD_NODE_DEFINITIONS)).thenReturn(definitions); assertEquals(NT_RESOURCE, TreeUtil.getDefaultChildType(typeRoot, child, "newChild")); assertNull(TreeUtil.getDefaultChildType(typeRoot, child, "newChild[4]")); }
@Test public void testGetDefaultChildTypeFromResidualSns() { Tree emptyDefinitions = when(mock(Tree.class).getChildren()).thenReturn(ImmutableList.of()).getMock(); Tree named = when(mock(Tree.class).getChild("newChild")).thenReturn(emptyDefinitions).getMock(); when(ntDef.getChild(REP_NAMED_CHILD_NODE_DEFINITIONS)).thenReturn(named); Tree def = mock(Tree.class); PropertyState ps = PropertyStates.createProperty(JCR_DEFAULTPRIMARYTYPE, NT_RESOURCE, Type.NAME); when(def.getProperty(JCR_DEFAULTPRIMARYTYPE)).thenReturn(ps); PropertyState sns = PropertyStates.createProperty(JCR_SAMENAMESIBLINGS, true, Type.BOOLEAN); when(def.getProperty(JCR_SAMENAMESIBLINGS)).thenReturn(sns); Tree definitions = when(mock(Tree.class).getChildren()).thenReturn(ImmutableList.of(def)).getMock(); when(ntDef.getChild(REP_RESIDUAL_CHILD_NODE_DEFINITIONS)).thenReturn(definitions); assertEquals(NT_RESOURCE, TreeUtil.getDefaultChildType(typeRoot, child, "newChild")); assertEquals(NT_RESOURCE, TreeUtil.getDefaultChildType(typeRoot, child, "newChild[4]")); }