incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r465729 - in /incubator/graffito/trunk/jcr/jcr-nodemanagement/src: java/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/ test/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/
Date Thu, 19 Oct 2006 19:01:12 GMT
Author: clombart
Date: Thu Oct 19 12:01:11 2006
New Revision: 465729

URL: http://svn.apache.org/viewvc?view=rev&rev=465729
Log:
Review unit test for the node type manager (see GRFT-111).
This patch is provided by Felix Meschberger

Modified:
    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/impl/jackrabbit/NodeTypeManagerImpl.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-nodemanagement/src/java/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NodeTypeManagerImpl.java?view=diff&rev=465729&r1=465728&r2=465729
==============================================================================
--- 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
Thu Oct 19 12:01:11 2006
@@ -19,8 +19,8 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-import javax.jcr.PropertyType;
 
+import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Workspace;
@@ -29,19 +29,22 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-
 import org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException;
+import org.apache.jackrabbit.core.nodetype.NodeDef;
+import org.apache.jackrabbit.core.nodetype.NodeDefImpl;
 import org.apache.jackrabbit.core.nodetype.NodeTypeDef;
 import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;
 import org.apache.jackrabbit.core.nodetype.PropDef;
 import org.apache.jackrabbit.core.nodetype.PropDefImpl;
 import org.apache.jackrabbit.core.nodetype.xml.NodeTypeReader;
 import org.apache.jackrabbit.name.QName;
-
+import org.apache.portals.graffito.jcr.mapper.model.BeanDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.ChildNodeDefDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.FieldDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.MappingDescriptor;
-
+import org.apache.portals.graffito.jcr.mapper.model.PropertyDefDescriptor;
 import org.apache.portals.graffito.jcr.nodemanagement.NodeTypeManager;
 import org.apache.portals.graffito.jcr.nodemanagement.exception.NamespaceCreationException;
 import org.apache.portals.graffito.jcr.nodemanagement.exception.NodeTypeCreationException;
@@ -160,22 +163,47 @@
                         classDescriptor.getJcrSuperTypes(),
                         classDescriptor.getClassName());
 
+                List propDefs = new ArrayList();
+                List nodeDefs = new ArrayList();
                 if (classDescriptor.getFieldDescriptors() != null)
                 {
                     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++;
+                        if (!field.isPath() && !field.isId()) {
+                            propDefs.add(getPropertyDefinition(field.getFieldName(), field,
nodeTypeDef.getName()));
+                        }
                     }
+                }
 
-                    nodeTypeDef.setPropertyDefs(properties);
+                if (classDescriptor.getBeanDescriptors() != null) {
+                    Iterator beanIterator = classDescriptor.getBeanDescriptors().iterator();
+                    while (beanIterator.hasNext()) {
+                        BeanDescriptor field = (BeanDescriptor) beanIterator.next();
+                        if (field.getJcrType() != null) {
+                            propDefs.add(getPropertyDefinition(field.getFieldName(), field,
nodeTypeDef.getName()));
+                        } else {
+                            nodeDefs.add(getNodeDefinition(field.getFieldName(), field, nodeTypeDef.getName()));
+                        }
+                    }
+                }
+                
+                if (classDescriptor.getCollectionDescriptors() != null) {
+                    Iterator collectionIterator = classDescriptor.getCollectionDescriptors().iterator();
+                    while (collectionIterator.hasNext()) {
+                        CollectionDescriptor field = (CollectionDescriptor) collectionIterator.next();
+                        if (field.getJcrType() != null) {
+                            propDefs.add(getPropertyDefinition(field.getFieldName(), field,
nodeTypeDef.getName()));
+                        } else {
+                            nodeDefs.add(getNodeDefinition(field.getFieldName(), field, nodeTypeDef.getName()));
+                        }
+                    }
                 }
 
+                nodeTypeDef.setPropertyDefs((PropDef[]) propDefs.toArray(new PropDef[propDefs.size()]));
+                nodeTypeDef.setChildNodeDefs((NodeDef[]) nodeDefs.toArray(new NodeDef[nodeDefs.size()]));
+
                 list.add(nodeTypeDef);
                 createNodeTypesFromList(session, list);
                 log.info("Registered JCR node type '" + nodeTypeDef.getName() +
@@ -255,12 +283,13 @@
     
     /** Creates a PropDefImpl object.
      * 
-     * @param field Field descriptor
+     * @param fieldName The name of the field
+     * @param field property definition descriptor
      * @param declaringNodeType Node Type QName where the property belongs to
      * @return property
      */
-    public PropDefImpl getPropertyDefinition(FieldDescriptor field,
-            QName declaringNodeType)
+    public PropDefImpl getPropertyDefinition(String fieldName,
+            PropertyDefDescriptor field, QName declaringNodeType)
     {
         PropDefImpl property = new PropDefImpl();
         
@@ -270,7 +299,7 @@
         }
         else
         {
-            property.setName(getNamespaceHelper().getQName(field.getFieldName()));
+            property.setName(getNamespaceHelper().getQName(fieldName));
         }
         
         if (field.getJcrType() != null)
@@ -297,6 +326,42 @@
         
         property.setProtected(field.isJcrProtected());
         return property;
+    }
+
+    /** Creates a NodeDefImpl object.
+     * 
+     * @param fieldName Name of the field
+     * @param field child node definition descriptor
+     * @param declaringNodeType Node Type QName where the chid node belongs to
+     * @return child node definition
+     */
+    private NodeDefImpl getNodeDefinition(String fieldName,
+        ChildNodeDefDescriptor field, QName declaringNodeType) {
+        
+        NodeDefImpl node = new NodeDefImpl();
+
+        if (field.getJcrName() != null) {
+            node.setName(getNamespaceHelper().getQName(field.getJcrName()));
+        } else {
+            node.setName(getNamespaceHelper().getQName(fieldName));
+        }
+
+        if (field.getJcrNodeType() != null) {
+            node.setRequiredPrimaryTypes(getJcrSuperTypes(field.getJcrNodeType()));
+        }
+
+        node.setDeclaringNodeType(declaringNodeType);
+        node.setAutoCreated(field.isJcrAutoCreated());
+        node.setMandatory(field.isJcrMandatory());
+        node.setAllowsSameNameSiblings(field.isJcrSameNameSiblings());
+
+        if (field.getJcrOnParentVersion() != null
+            && field.getJcrOnParentVersion().length() > 0) {
+            node.setOnParentVersion(OnParentVersionAction.valueFromName(field.getJcrOnParentVersion()));
+        }
+
+        node.setProtected(field.isJcrProtected());
+        return node;
     }
 
     /**

Modified: incubator/graffito/trunk/jcr/jcr-nodemanagement/src/test/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NodeTypeManagerImplTest.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-nodemanagement/src/test/org/apache/portals/graffito/jcr/nodemanagement/impl/jackrabbit/NodeTypeManagerImplTest.java?view=diff&rev=465729&r1=465728&r2=465729
==============================================================================
--- 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
Thu Oct 19 12:01:11 2006
@@ -17,11 +17,16 @@
 
 import java.io.FileInputStream;
 
+import javax.jcr.PropertyType;
+import javax.jcr.nodetype.NodeDefinition;
 import javax.jcr.nodetype.NodeType;
 import javax.jcr.nodetype.PropertyDefinition;
 
 import junit.framework.*;
+
+import org.apache.portals.graffito.jcr.mapper.model.BeanDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.FieldDescriptor;
 
 import org.apache.portals.graffito.jcr.nodemanagement.TestBase;
@@ -261,6 +266,189 @@
     }
     
     /**
+     * Test of createSingleNodeType method, of class org.apache.portals.graffito.jcr.nodemanagement.impl.jackrabbit.NodeTypeManagerImpl.
+     * @throws java.lang.Exception 
+     */
+    public void testCreateSingleNodeTypeWithPropertyForCollection() throws Exception
+    {
+        ClassDescriptor classDescriptor = new ClassDescriptor();
+        classDescriptor.setClassName("test.Test9Class");
+        classDescriptor.setJcrNodeType("graffito:test9");
+        classDescriptor.setJcrSuperTypes("nt:base");
+        
+        CollectionDescriptor collection1 = new CollectionDescriptor();
+        collection1.setFieldName("a");
+        collection1.setJcrName("a");
+        collection1.setJcrType("String");
+        classDescriptor.addCollectionDescriptor(collection1);
+        
+        getJackrabbitNodeTypeManagerImpl().createSingleNodeType(session, classDescriptor);
+        
+        NodeType test9 = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test9");
+        assertNotNull(test9);
+        // not check node type definition, assuming other tests have done that
+        
+        // assert property definition a
+        PropertyDefinition propDef = getPropertyDefinition(test9.getPropertyDefinitions(),
"graffito:a");
+        assertNotNull(propDef);
+        assertEquals(propDef.getRequiredType(), PropertyType.STRING);
+    }
+    
+    /**
+     * Test of createSingleNodeType method, of class org.apache.portals.graffito.jcr.nodemanagement.impl.jackrabbit.NodeTypeManagerImpl.
+     * @throws java.lang.Exception 
+     */
+    public void testCreateSingleNodeTypeWithPropertyForBean() throws Exception
+    {
+        ClassDescriptor classDescriptor = new ClassDescriptor();
+        classDescriptor.setClassName("test.Test10Class");
+        classDescriptor.setJcrNodeType("graffito:test10");
+        classDescriptor.setJcrSuperTypes("nt:base");
+        
+        BeanDescriptor bean1 = new BeanDescriptor();
+        bean1.setFieldName("a");
+        bean1.setJcrName("a");
+        bean1.setJcrType("String");
+        classDescriptor.addBeanDescriptor(bean1);
+        
+        getJackrabbitNodeTypeManagerImpl().createSingleNodeType(session, classDescriptor);
+        
+        NodeType test10 = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test10");
+        assertNotNull(test10);
+        // not check node type definition, assuming other tests have done that
+        
+        // assert property definition a
+        PropertyDefinition propDef = getPropertyDefinition(test10.getPropertyDefinitions(),
"graffito:a");
+        assertNotNull(propDef);
+        assertEquals(propDef.getRequiredType(), PropertyType.STRING);
+        
+    }
+    
+    /**
+     * Test of createSingleNodeType method, of class org.apache.portals.graffito.jcr.nodemanagement.impl.jackrabbit.NodeTypeManagerImpl.
+     * @throws java.lang.Exception 
+     */
+    public void testCreateSingleNodeTypeWithPropertyForCollectionDefinitionConflict() throws
Exception
+    {
+        ClassDescriptor classDescriptor = new ClassDescriptor();
+        classDescriptor.setClassName("test.Test13Class");
+        classDescriptor.setJcrNodeType("graffito:test13");
+        classDescriptor.setJcrSuperTypes("nt:base");
+
+        CollectionDescriptor collection1 = new CollectionDescriptor();
+        collection1.setFieldName("a");
+        collection1.setJcrName("a");
+        collection1.setJcrType("String");      // should overwrite setJcrNodeType
+        collection1.setJcrNodeType("nt:base"); // should be ignored
+        classDescriptor.addCollectionDescriptor(collection1);
+
+        getJackrabbitNodeTypeManagerImpl().createSingleNodeType(session, classDescriptor);
+        
+        NodeType test13 = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test13");
+        assertNotNull(test13);
+        // not check node type definition, assuming other tests have done that
+        
+        // assert property definition a
+        PropertyDefinition propDef = getPropertyDefinition(test13.getPropertyDefinitions(),
"graffito:a");
+        assertNotNull(propDef);
+        assertEquals(propDef.getRequiredType(), PropertyType.STRING);
+    }
+    
+    /**
+     * Test of createSingleNodeType method, of class org.apache.portals.graffito.jcr.nodemanagement.impl.jackrabbit.NodeTypeManagerImpl.
+     * @throws java.lang.Exception 
+     */
+    public void testCreateSingleNodeTypeWithPropertyForBeanDefinitionConflict() throws Exception
+    {
+        ClassDescriptor classDescriptor = new ClassDescriptor();
+        classDescriptor.setClassName("test.Test14Class");
+        classDescriptor.setJcrNodeType("graffito:test14");
+        classDescriptor.setJcrSuperTypes("nt:base");
+
+        BeanDescriptor bean1 = new BeanDescriptor();
+        bean1.setFieldName("a");
+        bean1.setJcrName("a");
+        bean1.setJcrType("String");      // should overwrite setJcrNodeType
+        bean1.setJcrNodeType("nt:base"); // should be ignored
+        classDescriptor.addBeanDescriptor(bean1);
+
+        getJackrabbitNodeTypeManagerImpl().createSingleNodeType(session, classDescriptor);
+        
+        NodeType test14 = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test14");
+        assertNotNull(test14);
+        // not check node type definition, assuming other tests have done that
+        
+        // assert property definition a
+        PropertyDefinition propDef = getPropertyDefinition(test14.getPropertyDefinitions(),
"graffito:a");
+        assertNotNull(propDef);
+        assertEquals(propDef.getRequiredType(), PropertyType.STRING);
+
+    }
+    
+    /**
+     * Test of createSingleNodeType method, of class org.apache.portals.graffito.jcr.nodemanagement.impl.jackrabbit.NodeTypeManagerImpl.
+     * @throws java.lang.Exception 
+     */
+    public void testCreateSingleNodeTypeWithChildNodeForCollection() throws Exception
+    {
+        ClassDescriptor classDescriptor = new ClassDescriptor();
+        classDescriptor.setClassName("test.Test11Class");
+        classDescriptor.setJcrNodeType("graffito:test11");
+        classDescriptor.setJcrSuperTypes("nt:base");
+
+        CollectionDescriptor collection1 = new CollectionDescriptor();
+        collection1.setFieldName("a");
+        collection1.setJcrName("b");
+        collection1.setJcrNodeType("nt:unstructured");
+        classDescriptor.addCollectionDescriptor(collection1);
+
+        getJackrabbitNodeTypeManagerImpl().createSingleNodeType(session, classDescriptor);
+        
+        NodeType test11 = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test11");
+        assertNotNull(test11);
+        // not check node type definition, assuming other tests have done that
+        
+        // assert child node definition a
+        NodeDefinition nodeDef = getChildNodeDefinition(test11.getChildNodeDefinitions(),
"graffito:b");
+        assertNotNull(nodeDef);
+        assertNotNull(nodeDef.getRequiredPrimaryTypes());
+        assertEquals(nodeDef.getRequiredPrimaryTypes().length, 1);
+        assertEquals(nodeDef.getRequiredPrimaryTypes()[0].getName(), "nt:unstructured");
+    }
+    
+    /**
+     * Test of createSingleNodeType method, of class org.apache.portals.graffito.jcr.nodemanagement.impl.jackrabbit.NodeTypeManagerImpl.
+     * @throws java.lang.Exception 
+     */
+    public void testCreateSingleNodeTypeWithChildNodeForBean() throws Exception
+    {
+        ClassDescriptor classDescriptor = new ClassDescriptor();
+        classDescriptor.setClassName("test.Test12Class");
+        classDescriptor.setJcrNodeType("graffito:test12");
+        classDescriptor.setJcrSuperTypes("nt:base");
+
+        BeanDescriptor bean1 = new BeanDescriptor();
+        bean1.setFieldName("a");
+        bean1.setJcrName("b");
+        bean1.setJcrNodeType("nt:unstructured");
+        classDescriptor.addBeanDescriptor(bean1);
+
+        getJackrabbitNodeTypeManagerImpl().createSingleNodeType(session, classDescriptor);
+        
+        NodeType test12 = session.getWorkspace().getNodeTypeManager().getNodeType("graffito:test12");
+        assertNotNull(test12);
+        // not check node type definition, assuming other tests have done that
+        
+        // assert property definition a
+        NodeDefinition nodeDef = getChildNodeDefinition(test12.getChildNodeDefinitions(),
"graffito:b");
+        assertNotNull(nodeDef);
+        assertNotNull(nodeDef);
+        assertNotNull(nodeDef.getRequiredPrimaryTypes());
+        assertEquals(nodeDef.getRequiredPrimaryTypes().length, 1);
+        assertEquals(nodeDef.getRequiredPrimaryTypes()[0].getName(), "nt:unstructured");
+    }
+    
+    /**
      * Test of createNodeTypes method, of class org.apache.portals.graffito.jcr.nodemanagement.impl.jackrabbit.NodeTypeManagerImpl.
      * @throws java.lang.Exception 
      */    
@@ -346,19 +534,19 @@
      * @return true/false 
      */
     protected boolean containsProperty(String propertyName,
-            PropertyDefinition[] propDefs)
+        PropertyDefinition[] propDefs)
     {
         boolean found = false;
         
         for (int i = 0; i < propDefs.length; i++)
         {
-           if (propDefs[i].getName().equals(propertyName))
-           {
-               found = true;
-               break;
-           }
+            if (propDefs[i].getName().equals(propertyName))
+            {
+                found = true;
+                break;
+            }
         }
-
+        
         return found;
     }
     
@@ -369,15 +557,62 @@
      * @return found
      */
     protected PropertyDefinition getPropertyDefinition(PropertyDefinition[] propDefs,
-            String propertyName)
+        String propertyName)
     {
         PropertyDefinition found = null;
         
         for (int i = 0; i < propDefs.length; i++)
         {
-           if (propDefs[i].getName().equals(propertyName))
+            if (propDefs[i].getName().equals(propertyName))
+            {
+                found = propDefs[i];
+                break;
+            }
+        }
+        
+        return found;
+    }
+    
+    /** Returns true if a given child node is found in an array of child node
+     * definitions.
+     * 
+     * @param childNodeName Name of child node to find
+     * @param childNodeDefs Child nodes of a node type
+     * @return true/false 
+     */
+    protected boolean containsChildNode(String childNodeName,
+            NodeDefinition[] childNodeDefs)
+    {
+        boolean found = false;
+        
+        for (int i = 0; i < childNodeDefs.length; i++)
+        {
+           if (childNodeDefs[i].getName().equals(childNodeName))
+           {
+               found = true;
+               break;
+           }
+        }
+
+        return found;
+    }
+    
+    /** Returns a property defintion identified by its name.
+     * 
+     * @param childNodeDefs Child nodes of a node type
+     * @param childNodeName Name of child node to find
+     * @return found
+     */
+    protected NodeDefinition getChildNodeDefinition(NodeDefinition[] childNodeDefs,
+            String childNodeName)
+    {
+        NodeDefinition found = null;
+        
+        for (int i = 0; i < childNodeDefs.length; i++)
+        {
+           if (childNodeDefs[i].getName().equals(childNodeName))
            {
-               found = propDefs[i];
+               found = childNodeDefs[i];
                break;
            }
         }



Mime
View raw message