incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From okiess...@apache.org
Subject svn commit: r368110 - in /incubator/graffito/trunk/jcr/jcr-nodemanagement/src: java/org/apache/portals/graffito/jcr/nodemanagement/exception/ java/org/apache/portals/graffito/jcr/nodemanagement/impl/ java/org/apache/portals/graffito/jcr/nodemanagement/...
Date Wed, 11 Jan 2006 19:56:29 GMT
Author: okiessler
Date: Wed Jan 11 11:56:18 2006
New Revision: 368110

URL: http://svn.apache.org/viewcvs?rev=368110&view=rev
Log:
Issue: GRFT-79

Added:
    incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/BaseNamespaceHelper.java
Modified:
    incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/exception/BaseNodeManagementException.java
    incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/exception/NodeTypeCreationException.java
    incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NamespaceHelper.java
    incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NodeTypeManagerImpl.java
    incubator/graffito/trunk/jcr/jcr-nodemanagement/src/test/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NodeTypeManagerImplTest.java

Modified: incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/exception/BaseNodeManagementException.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/exception/BaseNodeManagementException.java?rev=368110&r1=368109&r2=368110&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/exception/BaseNodeManagementException.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/exception/BaseNodeManagementException.java
Wed Jan 11 11:56:18 2006
@@ -30,6 +30,22 @@
     public BaseNodeManagementException()
     {
     }
+    
+    /** Creates a new instance of BaseNodeManagementException.
+     * @param message Exception message
+     */
+    public BaseNodeManagementException(String message)
+    {
+        super(message);
+    }
+    
+    /** Creates a new instance of BaseNodeManagementException.
+     * @param rootException Root Exception
+     */
+    public BaseNodeManagementException(Exception rootException)
+    {
+        setWrappedException(rootException);
+    }    
 
     /** Getter for property wrappedException.
      * 

Modified: incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/exception/NodeTypeCreationException.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/exception/NodeTypeCreationException.java?rev=368110&r1=368109&r2=368110&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/exception/NodeTypeCreationException.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/exception/NodeTypeCreationException.java
Wed Jan 11 11:56:18 2006
@@ -27,6 +27,12 @@
     {
     }
     
+    /** Creates a new instance of NodeTypeCreationException. */
+    public NodeTypeCreationException(String message)
+    {
+        super(message);
+    }    
+    
     /** Creates a new instance of NodeTypeCreationException. 
      * @param wrappedException Root exception
      */

Added: incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/BaseNamespaceHelper.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/BaseNamespaceHelper.java?rev=368110&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/BaseNamespaceHelper.java
(added)
+++ incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/BaseNamespaceHelper.java
Wed Jan 11 11:56:18 2006
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2000-2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.graffito.jcr.nodemanagement.impl;
+
+/** Base class of all namespace helpers.
+ *
+ * @author <a href="mailto:okiessler@apache.org">Oliver Kiessler</a>
+ */
+public abstract class BaseNamespaceHelper {
+    
+    /** Default namespace if none is specified.
+     */
+    public static final String DEFAULT_NAMESPACE = "graffito";
+    
+    /** Default namespace URI if none is specified.
+     */
+    public static final String DEFAULT_NAMESPACE_URI = "http://incubator.apache.org/graffito";
   
+    
+    /** Creates a new instance of BaseNamespaceHelper. */
+    public BaseNamespaceHelper() {
+    }
+}

Modified: incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NamespaceHelper.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NamespaceHelper.java?rev=368110&r1=368109&r2=368110&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NamespaceHelper.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NamespaceHelper.java
Wed Jan 11 11:56:18 2006
@@ -20,11 +20,13 @@
 import org.apache.jackrabbit.core.nodetype.ItemDef;
 import org.apache.jackrabbit.name.QName;
 
+import org.apache.portals.graffito.jcr.nodemanagement.impl.BaseNamespaceHelper;
+
 /** Jackrabbit namespace helper class.
  *
  * @author <a href="mailto:okiessler@apache.org">Oliver Kiessler</a>
  */
-public class NamespaceHelper
+public class NamespaceHelper extends BaseNamespaceHelper
 {
     
     /** JCR namespace registry.
@@ -54,8 +56,17 @@
             else
             {
                 String[] parts = itemName.split(":");
-                qName = new QName(getNamespaceUri(parts[0]),
-                        parts[1]); 
+                if (parts.length == 2)
+                {
+                    qName = new QName(getNamespaceUri(parts[0]),
+                            parts[1]);
+                }
+                else if (parts.length == 1) 
+                {
+                    // no namespace set, use default graffito namespace
+                    qName = new QName(DEFAULT_NAMESPACE_URI,
+                            parts[0]);
+                }
             }
         }
         

Modified: incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NodeTypeManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NodeTypeManagerImpl.java?rev=368110&r1=368109&r2=368110&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NodeTypeManagerImpl.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NodeTypeManagerImpl.java
Wed Jan 11 11:56:18 2006
@@ -24,6 +24,7 @@
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Workspace;
+import javax.jcr.nodetype.PropertyDefinition;
 import javax.jcr.version.OnParentVersionAction;
 
 import org.apache.commons.logging.Log;
@@ -58,7 +59,7 @@
      */
     private static Log log = LogFactory.getLog(NodeTypeManagerImpl.class);
     
-    /**
+    /** Namespace helper class for Jackrabbit.
      */
     private NamespaceHelper namespaceHelper = new NamespaceHelper();
     
@@ -94,6 +95,14 @@
     public void createNodeTypes(Session session, MappingDescriptor mappingDescriptor)
     throws NodeTypeCreationException
     {
+        if (mappingDescriptor != null && mappingDescriptor.getClassDescriptors().size()
> 0)
+        {
+            
+        }
+        else
+        {
+            throw new NodeTypeCreationException("The MappingDescriptor can't be null or empty.");
+        }
     }
     
     /**
@@ -102,8 +111,21 @@
     public void createNodeTypes(Session session, ClassDescriptor[] classDescriptors)
     throws NodeTypeCreationException
     {
+        if (classDescriptors != null && classDescriptors.length > 0)
+        {
+            log.info("Trying to create " + classDescriptors.length +
+                    " JCR node types.");
+            for (int i = 0; i < classDescriptors.length; i++)
+            {
+                createSingleNodeType(session, classDescriptors[i]);
+            }
+        }
+        else
+        {
+            throw new NodeTypeCreationException("The ClassDescriptor can't be null or empty.");
+        }
     }
-    
+
     /**
      * @see org.apache.portals.graffito.jcr.nodemanagement.NodeTypeManager#createNodeTypesFromMappingFiles
      */       
@@ -123,36 +145,86 @@
         {
             getNamespaceHelper().setRegistry(session.getWorkspace().getNamespaceRegistry());
             ArrayList list = new ArrayList();
-            NodeTypeDef nodeTypeDef = getNodeTypeDef(classDescriptor.getJcrNodeType(),
-                        classDescriptor.getJcrSuperTypes());
             
-            if (classDescriptor.getFieldDescriptors() != null)
+            if (classDescriptor.getJcrNodeType() != null &&
+                    (classDescriptor.getJcrNodeType().startsWith("nt:")
+                    || classDescriptor.getJcrNodeType().startsWith("mix:")))
+            {
+                throw new NodeTypeCreationException("Namespace nt and mix are reserved namespaces.
Please specify your own.");
+            }
+            
+            if (checkSuperTypes(session.getWorkspace().getNodeTypeManager(),
+                    classDescriptor.getJcrSuperTypes()))
             {
-                Iterator fieldIterator = classDescriptor.getFieldDescriptors().iterator();
-                PropDef[] properties = new PropDef[classDescriptor.getFieldDescriptors().size()];
-                int i = 0;
-                while (fieldIterator.hasNext())
+                NodeTypeDef nodeTypeDef = getNodeTypeDef(classDescriptor.getJcrNodeType(),
+                        classDescriptor.getJcrSuperTypes(),
+                        classDescriptor.getClassName());
+
+                if (classDescriptor.getFieldDescriptors() != null)
                 {
-                    FieldDescriptor field = (FieldDescriptor) fieldIterator.next();
-                    properties[i] = getPropertyDefinition(field,
-                            nodeTypeDef.getName());
-                    i++;
+                    Iterator fieldIterator = classDescriptor.getFieldDescriptors().iterator();
+                    PropDef[] properties = new PropDef[classDescriptor.getFieldDescriptors().size()];
+                    int i = 0;
+                    while (fieldIterator.hasNext())
+                    {
+                        FieldDescriptor field = (FieldDescriptor) fieldIterator.next();
+                        properties[i] = getPropertyDefinition(field,
+                                nodeTypeDef.getName());
+                        i++;
+                    }
+
+                    nodeTypeDef.setPropertyDefs(properties);
                 }
-                
-                nodeTypeDef.setPropertyDefs(properties);
-            }
 
-            list.add(nodeTypeDef);
-            createNodeTypesFromList(session, list);
-            log.info("Registered JCR node type '" + classDescriptor.getJcrNodeType() +
-                    "' for class '" + classDescriptor.getClassName() + "'");
+                list.add(nodeTypeDef);
+                createNodeTypesFromList(session, list);
+                log.info("Registered JCR node type '" + nodeTypeDef.getName() +
+                        "' for class '" + classDescriptor.getClassName() + "'");
+            }
+            else
+            {
+                throw new NodeTypeCreationException("JCR supertypes could not be resolved.");
+            }
         }
         catch (Exception e)
         {
             e.printStackTrace();
             log.error("Could not create node types from class descriptor.", e);
             throw new NodeTypeCreationException(e);
-        }        
+        }
+    }
+
+    /** Checks if all JCR super types for a given node type exist.
+     * 
+     * @param ntMgr NodeTypeManager
+     * @param superTypes Comma separated String with JCR node types
+     * @return true/false
+     */
+    private boolean checkSuperTypes(javax.jcr.nodetype.NodeTypeManager ntMgr,
+            String superTypes)
+    {
+        boolean exists = true;
+
+        if (superTypes != null && superTypes.length() > 0)
+        {
+            String[] superTypesArray = superTypes.split(",");
+            log.debug("JCR super types found: " + superTypesArray.length);
+            for (int i = 0; i < superTypesArray.length; i++)
+            {
+                try
+                {
+                    ntMgr.getNodeType(superTypesArray[i]);
+                }
+                catch (Exception e)
+                {
+                    log.error("JCR super type '" + superTypesArray[i] + "' does not exist!");
+                    exists = false;
+                    break;
+                }
+            }
+        }
+
+        return exists;
     }
     
     /** Creates a NodeTypeDef object.
@@ -161,11 +233,21 @@
      * @param jcrSuperTypes JCR node super types
      * @return type
      */
-    public NodeTypeDef getNodeTypeDef(String jcrNodeType, String jcrSuperTypes)
+    public NodeTypeDef getNodeTypeDef(String jcrNodeType, String jcrSuperTypes,
+            String className)
     {
         NodeTypeDef type = new NodeTypeDef();
         type.setMixin(false);
-        type.setName(getNamespaceHelper().getQName(jcrNodeType));
+        
+        if (jcrNodeType != null)
+        {
+            type.setName(getNamespaceHelper().getQName(jcrNodeType));
+        }
+        else
+        {
+            type.setName(getNamespaceHelper().getQName(className));
+        }
+        
         type.setSupertypes(getJcrSuperTypes(jcrSuperTypes));
         type.setPrimaryItemName(getNamespaceHelper().getQName(jcrNodeType));
         return type;
@@ -181,8 +263,27 @@
             QName declaringNodeType)
     {
         PropDefImpl property = new PropDefImpl();
-        property.setName(getNamespaceHelper().getQName(field.getJcrName()));
-        property.setRequiredType(PropertyType.valueFromName(field.getJcrType()));
+        
+        if (field.getJcrName() != null)
+        {
+            property.setName(getNamespaceHelper().getQName(field.getJcrName()));
+        }
+        else
+        {
+            property.setName(getNamespaceHelper().getQName(field.getFieldName()));
+        }
+        
+        if (field.getJcrType() != null)
+        {
+            property.setRequiredType(PropertyType.valueFromName(field.getJcrType()));
+        }
+        else
+        {
+            log.info("No property type set for " + property.getName() +
+                    ". Setting 'String' type.");
+            property.setRequiredType(PropertyType.valueFromName("String"));
+        }
+
         property.setDeclaringNodeType(declaringNodeType);
         property.setAutoCreated(field.isJcrAutoCreated());
         property.setMandatory(field.isJcrMandatory());
@@ -198,6 +299,22 @@
         return property;
     }
 
+    /**
+     * 
+     * @param propDef 
+     * @return 
+     */
+    protected String showPropertyDefinition(PropertyDefinition propDef)
+    {
+        StringBuffer sb = new StringBuffer();
+        sb.append("----");
+        sb.append("\nName: " + propDef.getName());
+        sb.append("\nAutocreated: " + propDef.isAutoCreated());
+        sb.append("\nMandatory: " + propDef.isMandatory());
+        sb.append("\n----");
+        return sb.toString();
+    }
+
     /** Creates a QName array from a comma separated list of JCR super types in
      * a given String.
      * 
@@ -222,7 +339,7 @@
 
         return qNameSuperTypes;
     }
-    
+
     /**
      * @see org.apache.portals.graffito.jcr.nodemanagement.NodeTypeManager#createSingleNodeTypeFromMappingFile
      */     
@@ -278,12 +395,7 @@
     public void createNodeTypesFromList(Session session, List nodeTypes)
     throws InvalidNodeTypeDefException, RepositoryException
     {
-        Workspace wsp = session.getWorkspace();
-        javax.jcr.nodetype.NodeTypeManager ntMgr = wsp.getNodeTypeManager();
-        NodeTypeRegistry ntReg =
-                ((org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl) ntMgr).getNodeTypeRegistry();
-        
-        ntReg.registerNodeTypes(nodeTypes);
+        getNodeTypeRegistry(session).registerNodeTypes(nodeTypes);
     }
     
     /**
@@ -300,8 +412,31 @@
     public void removeSingleNodeType(Session session, String jcrNodeType)
     throws NodeTypeRemovalException
     {
+        try
+        {
+            getNodeTypeRegistry(session).unregisterNodeType(getNamespaceHelper().getQName(jcrNodeType));
+        }
+        catch (Exception e)
+        {
+            throw new NodeTypeRemovalException(e);
+        }      
+    }
+
+    /** Returns the jackrabbit NodeTypeRegistry from an open session.
+     * 
+     * @param session Repository session
+     * @return nodeTypeRegistry
+     */
+    private NodeTypeRegistry getNodeTypeRegistry(Session session)
+    throws RepositoryException
+    {
+        Workspace wsp = session.getWorkspace();
+        javax.jcr.nodetype.NodeTypeManager ntMgr = wsp.getNodeTypeManager();
+        NodeTypeRegistry ntReg =
+                ((org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl) ntMgr).getNodeTypeRegistry();
+        return ntReg;
     }
-    
+
     /**
      * @see org.apache.portals.graffito.jcr.nodemanagement.NodeTypeManager#getPrimaryNodeTypeNames
      */    

Modified: incubator/graffito/trunk/jcr/jcr-nodemanagement/src/test/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NodeTypeManagerImplTest.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-nodemanagement/src/test/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NodeTypeManagerImplTest.java?rev=368110&r1=368109&r2=368110&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-nodemanagement/src/test/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NodeTypeManagerImplTest.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-nodemanagement/src/test/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NodeTypeManagerImplTest.java
Wed Jan 11 11:56:18 2006
@@ -26,6 +26,7 @@
 
 import org.apache.portals.graffito.jcr.nodemanagement.TestBase;
 import org.apache.portals.graffito.jcr.nodemanagement.exception.NamespaceCreationException;
+import org.apache.portals.graffito.jcr.nodemanagement.exception.NodeTypeCreationException;
 
 /** JUnit test for NodeTypeManagerImpl.
  *
@@ -106,33 +107,238 @@
         field1.setFieldName("a");
         field1.setJcrName("graffito:a");
         field1.setJcrType("String");
+        field1.setJcrAutoCreated(true);
+        field1.setJcrMandatory(true);
+        field1.setJcrMultiple(true);
         classDescriptor.addFieldDescriptor(field1);
 
         FieldDescriptor field2 = new FieldDescriptor();
         field2.setFieldName("b");
         field2.setJcrName("graffito:b");
         field2.setJcrType("Long");
+        field1.setJcrAutoCreated(false);
+        field1.setJcrMandatory(true);
+        field1.setJcrMultiple(false);        
         classDescriptor.addFieldDescriptor(field2);        
 
         getJackrabbitNodeTypeManagerImpl().createSingleNodeType(session, classDescriptor);
         
-        NodeType test2 = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test2");
-        assertNotNull(test2);
-        assertFalse(test2.isMixin());
-        assertEquals(test2.getName(), "graffito:test2");
-        assertEquals(test2.getSupertypes().length, 1);
-        assertEquals(test2.getSupertypes()[0].getName(), "nt:base");
+        NodeType testNodeType = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test2");
+        assertNotNull(testNodeType);
+        assertFalse(testNodeType.isMixin());
+        assertEquals(testNodeType.getName(), "graffito:test2");
+        assertEquals(testNodeType.getSupertypes().length, 1);
+        assertEquals(testNodeType.getSupertypes()[0].getName(), "nt:base");
 
         // 2 defined in graffito:test2 and 2 inherited from nt:base
-        assertEquals(test2.getPropertyDefinitions().length, 4);
+        assertEquals(testNodeType.getPropertyDefinitions().length, 4);
         
-        assertTrue(containsProperty("graffito:a", test2.getPropertyDefinitions()));
-        assertTrue(containsProperty("graffito:b", test2.getPropertyDefinitions()));
-        assertTrue(containsProperty("jcr:primaryType", test2.getPropertyDefinitions()));
-        assertTrue(containsProperty("jcr:mixinTypes", test2.getPropertyDefinitions()));
+        assertTrue(containsProperty("graffito:a", testNodeType.getPropertyDefinitions()));
+        assertTrue(containsProperty("graffito:b", testNodeType.getPropertyDefinitions()));
+        assertTrue(containsProperty("jcr:primaryType", testNodeType.getPropertyDefinitions()));
+        assertTrue(containsProperty("jcr:mixinTypes", testNodeType.getPropertyDefinitions()));
+        
+        PropertyDefinition propDef1 = getPropertyDefinition(testNodeType.getPropertyDefinitions(),
"graffito:a");
+        System.out.println(getJackrabbitNodeTypeManagerImpl().showPropertyDefinition(propDef1));
+        // TODO test all properties
+        
+        PropertyDefinition propDef2 = getPropertyDefinition(testNodeType.getPropertyDefinitions(),
"graffito:b");
+        System.out.println(getJackrabbitNodeTypeManagerImpl().showPropertyDefinition(propDef2));
+        // TODO test all properties
     }
+    
+    /**
+     * Test of createSingleNodeType method, of class org.apache.portals.graffito.jcr.nodemanagement.impl.jackrabbit.NodeTypeManagerImpl.
+     * @throws java.lang.Exception 
+     */
+    public void testCreateSingleNodeTypeNoNamespace() throws Exception
+    {
+        ClassDescriptor classDescriptor = new ClassDescriptor();
+        classDescriptor.setClassName("test.Test3Class");
+        classDescriptor.setJcrNodeType("test3");
+        classDescriptor.setJcrSuperTypes("nt:base");
 
-    /** Returns true if a given property is found in an arry of property
+        FieldDescriptor field1 = new FieldDescriptor();
+        field1.setFieldName("a");
+        field1.setJcrName("a");
+        field1.setJcrType("String");
+        classDescriptor.addFieldDescriptor(field1);
+
+        getJackrabbitNodeTypeManagerImpl().createSingleNodeType(session, classDescriptor);
+        
+        NodeType test3 = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test3");
+        assertNotNull(test3);
+        assertFalse(test3.isMixin());
+        assertEquals(test3.getName(), "graffito:test3");
+        assertEquals(test3.getSupertypes().length, 1);
+        assertEquals(test3.getSupertypes()[0].getName(), "nt:base");        
+    }
+    
+    /**
+     * Test of createSingleNodeType method, of class org.apache.portals.graffito.jcr.nodemanagement.impl.jackrabbit.NodeTypeManagerImpl.
+     * @throws java.lang.Exception 
+     */
+    public void testCreateSingleNodeTypeNoJcrNodeTypeSet() throws Exception
+    {
+        ClassDescriptor classDescriptor = new ClassDescriptor();
+        classDescriptor.setClassName("test.Test4Class");
+        classDescriptor.setJcrSuperTypes("nt:base");
+
+        FieldDescriptor field1 = new FieldDescriptor();
+        field1.setFieldName("a");
+        field1.setJcrName("a");
+        field1.setJcrType("String");
+        classDescriptor.addFieldDescriptor(field1);
+
+        getJackrabbitNodeTypeManagerImpl().createSingleNodeType(session, classDescriptor);
+        
+        NodeType test4 = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test.Test4Class");
+        assertNotNull(test4);
+        assertFalse(test4.isMixin());
+        assertEquals(test4.getName(), "graffito:test.Test4Class");
+        assertEquals(test4.getSupertypes().length, 1);
+        assertEquals(test4.getSupertypes()[0].getName(), "nt:base");        
+    }
+    
+    /**
+     * Test of createSingleNodeType method, of class org.apache.portals.graffito.jcr.nodemanagement.impl.jackrabbit.NodeTypeManagerImpl.
+     * @throws java.lang.Exception 
+     */
+    public void testCreateSingleNodeTypeIncompleteFieldDescriptorProperties() throws Exception
+    {
+        ClassDescriptor classDescriptor = new ClassDescriptor();
+        classDescriptor.setClassName("test.Test5Class");
+        classDescriptor.setJcrNodeType("graffito:test5");
+        classDescriptor.setJcrSuperTypes("graffito:test2");
+        
+        FieldDescriptor field1 = new FieldDescriptor();
+        field1.setFieldName("abc");
+        classDescriptor.addFieldDescriptor(field1);
+
+        getJackrabbitNodeTypeManagerImpl().createSingleNodeType(session, classDescriptor);
+        
+        NodeType test5 = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test5");
+        assertNotNull(test5);
+        assertFalse(test5.isMixin());
+        assertEquals(test5.getName(), "graffito:test5");
+        // nt:base and graffito:test2
+        assertEquals(test5.getSupertypes().length, 2);
+        assertTrue(containsSuperType("graffito:test2", test5.getSupertypes()));
+        assertTrue(containsSuperType("nt:base", test5.getSupertypes()));
+        assertTrue(containsProperty("graffito:abc", test5.getPropertyDefinitions()));
+    }
+    
+    /**
+     * Test of createSingleNodeType method, of class org.apache.portals.graffito.jcr.nodemanagement.impl.jackrabbit.NodeTypeManagerImpl.
+     * @throws java.lang.Exception 
+     */
+    public void testCreateSingleNodeTypeNtNamespace() throws Exception
+    {
+        ClassDescriptor classDescriptor = new ClassDescriptor();
+        classDescriptor.setClassName("test.Test6Class");
+        classDescriptor.setJcrNodeType("nt:test3");
+        classDescriptor.setJcrSuperTypes("nt:base");
+
+        FieldDescriptor field1 = new FieldDescriptor();
+        field1.setFieldName("a");
+        field1.setJcrName("a");
+        field1.setJcrType("String");
+        classDescriptor.addFieldDescriptor(field1);
+        
+        boolean failed = false;
+        
+        try
+        {
+            getJackrabbitNodeTypeManagerImpl().createSingleNodeType(session, classDescriptor);
+        }
+        catch (NodeTypeCreationException nce)
+        {
+            // excepted
+            failed = true;
+        }
+        
+        assertTrue(failed);
+    }
+    
+    /**
+     * Test of createNodeTypes method, of class org.apache.portals.graffito.jcr.nodemanagement.impl.jackrabbit.NodeTypeManagerImpl.
+     * @throws java.lang.Exception 
+     */    
+    public void testCreateNodeTypes() throws Exception
+    {
+        ClassDescriptor classDescriptor = new ClassDescriptor();
+        classDescriptor.setClassName("test.Test6Class");
+        classDescriptor.setJcrNodeType("graffito:test6");
+        classDescriptor.setJcrSuperTypes("nt:base");
+        
+        FieldDescriptor field1 = new FieldDescriptor();
+        field1.setFieldName("a");
+        field1.setJcrName("graffito:a");
+        field1.setJcrType("String");
+        classDescriptor.addFieldDescriptor(field1);
+
+        FieldDescriptor field2 = new FieldDescriptor();
+        field2.setFieldName("b");
+        field2.setJcrName("graffito:b");
+        field2.setJcrType("Long");
+        classDescriptor.addFieldDescriptor(field2);
+        
+        ClassDescriptor classDescriptor2 = new ClassDescriptor();
+        classDescriptor2.setClassName("test.Test7Class");
+        classDescriptor2.setJcrNodeType("graffito:test7");
+        classDescriptor2.setJcrSuperTypes("nt:base");
+        
+        FieldDescriptor field3 = new FieldDescriptor();
+        field3.setFieldName("a");
+        field3.setJcrName("graffito:a");
+        field3.setJcrType("String");
+        classDescriptor2.addFieldDescriptor(field3);
+
+        FieldDescriptor field4 = new FieldDescriptor();
+        field4.setFieldName("b");
+        field4.setJcrName("graffito:b");
+        field4.setJcrType("Long");
+        classDescriptor2.addFieldDescriptor(field4);
+        
+        ClassDescriptor[] classDescriptorArray = new ClassDescriptor[2];
+        classDescriptorArray[0] = classDescriptor;
+        classDescriptorArray[1] = classDescriptor2;
+        
+        getJackrabbitNodeTypeManagerImpl().createNodeTypes(session, classDescriptorArray);
+        
+        NodeType test6 = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test6");
+        assertNotNull(test6);
+        
+        NodeType test7 = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test7");
+        assertNotNull(test7);
+    }
+    
+    /**
+     * Test of removeSingleNodeType method, of class org.apache.portals.graffito.jcr.nodemanagement.impl.jackrabbit.NodeTypeManagerImpl.
+     * @throws java.lang.Exception 
+     */    
+    public void testRemoveSingleNodeType() throws Exception
+    {
+        ClassDescriptor classDescriptor = new ClassDescriptor();
+        classDescriptor.setClassName("test.Test8Class");
+        classDescriptor.setJcrNodeType("graffito:test8");
+        classDescriptor.setJcrSuperTypes("nt:base");
+
+        FieldDescriptor field1 = new FieldDescriptor();
+        field1.setFieldName("a");
+        field1.setJcrName("a");
+        field1.setJcrType("String");
+        classDescriptor.addFieldDescriptor(field1);
+
+        getJackrabbitNodeTypeManagerImpl().createSingleNodeType(session, classDescriptor);
+        
+        NodeType test8 = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test8");
+        assertNotNull(test8);
+        // not implemented yet in jackrabbit
+        // getJackrabbitNodeTypeManagerImpl().removeSingleNodeType(session, "graffito:test8");
   
+    }    
+
+    /** Returns true if a given property is found in an array of property
      * definitions.
      * 
      * @param propertyName Name of property to find
@@ -155,6 +361,52 @@
 
         return found;
     }
+    
+    /** Returns a property defintion identified by its name.
+     * 
+     * @param propDefs All property definitions of a node type
+     * @param propertyName Name of property definition
+     * @return found
+     */
+    protected PropertyDefinition getPropertyDefinition(PropertyDefinition[] propDefs,
+            String propertyName)
+    {
+        PropertyDefinition found = null;
+        
+        for (int i = 0; i < propDefs.length; i++)
+        {
+           if (propDefs[i].getName().equals(propertyName))
+           {
+               found = propDefs[i];
+               break;
+           }
+        }
+
+        return found;
+    }
+    
+    /** Returns true if a given super type is found in an arry of super types.
+     * 
+     * @param superType Name of super type to find
+     * @param propDefs Properties of a node type
+     * @return true/false 
+     */
+    protected boolean containsSuperType(String superType,
+            NodeType[] nodeTypes)
+    {
+        boolean found = false;
+        
+        for (int i = 0; i < nodeTypes.length; i++)
+        {
+           if (nodeTypes[i].getName().equals(superType))
+           {
+               found = true;
+               break;
+           }
+        }
+
+        return found;
+    }    
     
     /** Getter for property jackrabbitNodeTypeManagerImpl.
      * 



Mime
View raw message