TreeUtil { public static boolean isNodeType(Tree tree, String typeName, Tree typeRoot) { String primaryName = TreeUtil.getName(tree, JCR_PRIMARYTYPE); if (typeName.equals(primaryName)) { return true; } else if (primaryName != null) { Tree type = typeRoot.getChild(primaryName); if (contains(getNames(type, REP_SUPERTYPES), typeName)) { return true; } } for (String mixinName : getNames(tree, JCR_MIXINTYPES)) { if (typeName.equals(mixinName)) { return true; } else { Tree type = typeRoot.getChild(mixinName); if (contains(getNames(type, REP_SUPERTYPES), typeName)) { return true; } } } return false; } 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 testIsNodeType() { assertTrue(TreeUtil.isNodeType(child, NT_OAK_UNSTRUCTURED, typeRoot)); assertFalse(TreeUtil.isNodeType(child, NT_UNSTRUCTURED, typeRoot)); }
@Test public void testIsNodeTypeMissingTypeProperties() { Tree tree = mockTree(PathUtils.ROOT_PATH, true); assertFalse(TreeUtil.isNodeType(tree, NT_REP_ROOT, typeRoot)); }
@Test public void testIsNodeTypeContainedInSupertypes() { PropertyState supertypes = PropertyStates.createProperty(REP_SUPERTYPES, ImmutableList.of(NT_BASE), Type.NAMES); when(ntDef.getProperty(REP_SUPERTYPES)).thenReturn(supertypes); assertTrue(TreeUtil.isNodeType(child, NT_BASE, typeRoot)); assertFalse(TreeUtil.isNodeType(child, NT_HIERARCHYNODE, typeRoot)); }
@Test public void testIsNodeTypeMixin() { assertTrue(TreeUtil.isNodeType(child, JcrConstants.MIX_LOCKABLE, typeRoot)); assertFalse(TreeUtil.isNodeType(z, JcrConstants.MIX_LOCKABLE, typeRoot)); }
@Test public void testIsNodeTypeMixinContainedInSupertypes() { PropertyState supertypes = PropertyStates.createProperty(REP_SUPERTYPES, ImmutableList.of(MIX_REFERENCEABLE), Type.NAMES); when(ntDef.getProperty(REP_SUPERTYPES)).thenReturn(supertypes); PropertyState mixinNames = PropertyStates.createProperty(JcrConstants.JCR_MIXINTYPES, Lists.newArrayList(JcrConstants.MIX_VERSIONABLE), Type.NAMES); Tree tree = when(mockTree(CHILD_PATH, z, true).getProperty(JCR_MIXINTYPES)).thenReturn(mixinNames).getMock(); assertTrue(TreeUtil.isNodeType(tree, MIX_REFERENCEABLE, typeRoot)); assertFalse(TreeUtil.isNodeType(child, MIX_LASTMODIFIED, typeRoot)); }