You are viewing a plain text version of this content. The canonical link for it is here.
Posted to graffito-commits@incubator.apache.org by ap...@apache.org on 2006/02/08 11:13:16 UTC

svn commit: r375924 - in /incubator/graffito/trunk/jcr/jcr-mapping/src: java/org/apache/portals/graffito/jcr/persistence/objectconverter/ java/org/apache/portals/graffito/jcr/reflection/ test-config/ test/org/apache/portals/graffito/jcr/persistence/obj...

Author: apopescu
Date: Wed Feb  8 03:13:12 2006
New Revision: 375924

URL: http://svn.apache.org/viewcvs?rev=375924&view=rev
Log:
bean-descriptor with custom converter + test

Added:
    incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/E.java
Modified:
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/BeanConverter.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/reflection/ReflectionUtils.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-beandescriptor.xml
    incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/BeanDescriptorTest.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/D.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/DFull.java

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/BeanConverter.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/BeanConverter.java?rev=375924&r1=375923&r2=375924&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/BeanConverter.java (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/BeanConverter.java Wed Feb  8 03:13:12 2006
@@ -19,64 +19,90 @@
 import javax.jcr.Node;
 import javax.jcr.Session;
 
+import org.apache.portals.graffito.jcr.exception.JcrMappingException;
 import org.apache.portals.graffito.jcr.exception.PersistenceException;
+import org.apache.portals.graffito.jcr.exception.RepositoryException;
 import org.apache.portals.graffito.jcr.mapper.Mapper;
 
 /**
- * Convert any kind of beans into JCR nodes & properties
+ * Interface describing a custom bean converter. 
  *
  * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
  */
 public interface BeanConverter {
     /**
-     * Insert the object
+     * Insert the object.
      *
      * @param session the JCR session
-     * @param parentNode The parent node used to store the new JCR element (object)
-     * @param nodeName The node name used to store the object
-     * @param object the object to insert
-     * @throws PersistenceException when it is not possible to insert the object
+     * @param parentNode The parent node
+     * @param mapper available mappings
+     * @param beanName bean name to be inserter
+     * @param object bean
+     * 
+     * @throws PersistenceException thrown in case the insert fails; marks a failure due to logic of
+     *  the insert (parent node cannot be accessed, the insert fails, etc.)
+     * @throws RepositoryException thrown in case the underlying repository has thrown a
+     *  <code>javax.jcr.RepositoryException</code> that is not possible to be handled or
+     *  wrapped in PersistenceException; marks a repository failure
+     * @throws JcrMappingException throws in case the mapping of the bean is not correct
      */
-    public void insert(Session session, 
-                       Node parentNode,
-                       Mapper mapper,
-                       String beanName,
-                       Object object)
-    throws PersistenceException;
+    void insert(Session session, Node parentNode, Mapper mapper, String beanName, Object object)
+    throws PersistenceException, RepositoryException, JcrMappingException;
 
     /**
-     * Update the object
+     * Update repository from bean values.
      *
      * @param session the JCR session
-     * @param parentNode The parent node used to store the new JCR element (object)
-     * @param nodeName The node name used to store the object
-     * @param object the object to update
-     * @throws PersistenceException when it is not possible to update the object
+     * @param parentNode The parent node
+     * @param mapper available mappings
+     * @param beanName bean name to be updated
+     * @param object bean
+     * 
+     * @throws PersistenceException thrown in case the update fails; marks a failure due to logic
+     *  of update (parent node cannot be accessed, the update fails, etc.)
+     * @throws RepositoryException thrown in case the underlying repository has thrown a
+     *  <code>javax.jcr.RepositoryException</code> that is not possible to be handled or
+     *  wrapped in PersistenceException; marks a repository failure
+     * @throws JcrMappingException throws in case the mapping of the bean is not correct
      */
-    public void update(Session session, 
-                       Node parentNode, 
-                       Mapper mapper, 
-                       String beanName, 
-                       Object object)
-    throws PersistenceException;
+    void update(Session session, Node parentNode, Mapper mapper, String beanName, Object object)
+    throws PersistenceException, RepositoryException, JcrMappingException;
     
     /**
-     * ???
+     * Retrieve a bean from the repository.
+     * 
+     * @param session the JCR session
+     * @param parentNode The parent node
+     * @param mapper available mappings
+     * @param beanName bean name to be retrieved
+     * @param beanClass class of the bean to be retrieved
+     * 
+     * @throws PersistenceException thrown in case the bean cannot be retrieved or initialized; 
+     *  marks a failure due to logic of retrieval
+     * @throws RepositoryException thrown in case the underlying repository has thrown a
+     *  <code>javax.jcr.RepositoryException</code> that is not possible to be handled or
+     *  wrapped in PersistenceException; marks a repository failure
+     * @throws JcrMappingException throws in case the mapping of the bean is not correct
      */
-    public Object getObject(Session session,
-                            Node parentNode,
-                            Mapper mapper,
-                            String beanName,
-                            Class beanClass) 
-    throws PersistenceException;
+    Object getObject(Session session, Node parentNode, Mapper mapper, String beanName, Class beanClass) 
+    throws PersistenceException, RepositoryException, JcrMappingException;
 
 
     /**
-     * ???
+     * Remove the bean from the repository.
+     * 
+     * @param session the JCR session
+     * @param parentNode The parent node
+     * @param mapper available mappings
+     * @param beanName bean name to be retrieved
+     * 
+     * @throws PersistenceException thrown in case the bean cannot be removed; 
+     *  marks a failure due to logic of removal
+     * @throws RepositoryException thrown in case the underlying repository has thrown a
+     *  <code>javax.jcr.RepositoryException</code> that is not possible to be handled or
+     *  wrapped in PersistenceException; marks a repository failure
+     * @throws JcrMappingException throws in case the mapping of the bean is not correct
      */
-    void remove(Session session, 
-                Node parentNode, 
-                Mapper mapper, 
-                String beanName)
-    throws PersistenceException;
+    void remove(Session session, Node parentNode, Mapper mapper, String beanName)
+    throws PersistenceException, RepositoryException, JcrMappingException;
 }

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/reflection/ReflectionUtils.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/reflection/ReflectionUtils.java?rev=375924&r1=375923&r2=375924&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/reflection/ReflectionUtils.java (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/reflection/ReflectionUtils.java Wed Feb  8 03:13:12 2006
@@ -1,3 +1,18 @@
+/*
+ * Copyright 2000-2004 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.reflection;
 
 import java.lang.reflect.InvocationTargetException;
@@ -6,11 +21,12 @@
 import org.apache.commons.beanutils.PropertyUtils;
 import org.apache.portals.graffito.jcr.exception.JcrMappingException;
 import org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter;
-import org.apache.portals.graffito.jcr.persistence.objectconverter.BeanConverter;
 
 
 /**
- * This class/interface 
+ * Utility class for handling reflection using BeanUtils.
+ * 
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
  */
 abstract public class ReflectionUtils {
     public static Object getNestedProperty(Object object, String fieldName) {

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-beandescriptor.xml
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-beandescriptor.xml?rev=375924&r1=375923&r2=375924&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-beandescriptor.xml (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-beandescriptor.xml Wed Feb  8 03:13:12 2006
@@ -11,6 +11,23 @@
 
 	</class-descriptor>
 	
+	<class-descriptor className="org.apache.portals.graffito.jcr.testmodel.E"  jcrNodeType="nt:unstructured" jcrSuperTypes="nt:base">
+		<field-descriptor fieldName="path" path="true" />
+		<field-descriptor fieldName="d1" 
+						  jcrName="d1" 
+						  jcrType="String" 
+						  jcrAutoCreated="false" 
+						  jcrMandatory="false"
+        				  jcrOnParentVersion="IGNORE" 
+        				  jcrProtected="false" 
+        				  jcrMultiple="false" />
+		
+		<bean-descriptor  fieldName="b1" 
+						  jcrName="b1"
+						  converter="org.apache.portals.graffito.jcr.persistence.objectconverter.impl.BeanDescriptorTest$FakeBeanConverter" />
+
+	</class-descriptor>
+	
 	<class-descriptor className="org.apache.portals.graffito.jcr.testmodel.DFull"  jcrNodeType="nt:unstructured" jcrSuperTypes="nt:base">
 		<field-descriptor fieldName="path" path="true" />
 		<field-descriptor fieldName="d1" jcrName="d1" jcrType="String" jcrAutoCreated="false" jcrMandatory="false"

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/BeanDescriptorTest.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/BeanDescriptorTest.java?rev=375924&r1=375923&r2=375924&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/BeanDescriptorTest.java (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/BeanDescriptorTest.java Wed Feb  8 03:13:12 2006
@@ -1,18 +1,49 @@
+/*
+ * Copyright 2000-2004 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.persistence.objectconverter.impl;
 
 
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
 import org.apache.portals.graffito.jcr.RepositoryLifecycleTestSetup;
 import org.apache.portals.graffito.jcr.TestBase;
+import org.apache.portals.graffito.jcr.exception.PersistenceException;
+import org.apache.portals.graffito.jcr.mapper.Mapper;
+import org.apache.portals.graffito.jcr.mapper.model.BeanDescriptor;
+import org.apache.portals.graffito.jcr.persistence.objectconverter.BeanConverter;
 import org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter;
 import org.apache.portals.graffito.jcr.testmodel.B;
 import org.apache.portals.graffito.jcr.testmodel.D;
 import org.apache.portals.graffito.jcr.testmodel.DFull;
+import org.apache.portals.graffito.jcr.testmodel.E;
 
 /**
- * This class/interface
+ * ObjectConverter test for bean-descriptor with inner bean inlined and inner bean with
+ * custom converter.
+ * 
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
  */
 public class BeanDescriptorTest extends TestBase {
     private ObjectConverter objectConverter;
@@ -112,7 +143,126 @@
         assertNull("b1 was removed", actDFull.getB1());
         assertNull("B.b2 is protected", actDFull.getB2());
 
-        getSession().getItem("/someD").remove();
+    }
+    
+    public void testBeanDescriptorConverter() throws Exception {
+        BeanDescriptor beanDescriptor = this.mapper.getClassDescriptor(E.class).getBeanDescriptor("b1");
+        FakeBeanConverter converter = (FakeBeanConverter) beanDescriptor.getBeanConverter();
+        List messages = new ArrayList();
+        converter.setLog(messages);
+        
+        assertNotNull("E.b1 should be using the FakeBeanConverter", converter);
+        
+        B expB = new B();
+        expB.setB1("b1value");
+        expB.setB2("b2value");
+        E expE = new E();
+        expE.setPath("/someD");
+        expE.setD1("d1value");
+        expE.setB1(expB);
+        
+        this.objectConverter.insert(getSession(), expE);
+        getSession().save();
+        
+        // HINT: FakeBeanConverter should set expB
+        converter.setB(expB);
+        E actE = (E) this.objectConverter.getObject(getSession(), E.class, "/someD");
+        
+        
+        assertEquals(expE.getD1(), actE.getD1());
+        assertEquals(expB, actE.getB1());
+        
+        expE.setD1("updatedvalueD1");
+        expB.setB1("updatedvalue1");
+        
+        this.objectConverter.update(getSession(), expE);
         getSession().save();
+        
+        converter.setB(expB);
+        actE = (E) this.objectConverter.getObject(getSession(), E.class, "/someD");
+        
+        assertEquals(expE.getD1(), actE.getD1());
+        assertEquals(expB, actE.getB1());
+                
+        expE.setB1(null);
+        this.objectConverter.update(getSession(), expE);
+        getSession().save();
+        
+        converter.setB(null);
+        actE = (E) this.objectConverter.getObject(getSession(), E.class, "/someD");
+        
+        assertEquals(expE.getD1(), actE.getD1());
+        assertNull(actE.getB1());
+        
+        // HINT: check messages
+        assertEquals(6, messages.size());
+        assertEquals("insert at path /someD", messages.get(0));
+        assertEquals("get from path /someD", messages.get(1));
+        assertEquals("update at path /someD", messages.get(2));
+        assertEquals("get from path /someD", messages.get(3));
+        assertEquals("remove from path /someD", messages.get(4));
+        assertEquals("get from path /someD", messages.get(5));
+    }
+    
+    public static class FakeBeanConverter implements BeanConverter {
+        private B returnB;
+        private List log;
+        
+        public void setLog(List log) {
+            this.log = log;
+        }
+        
+        public void setB(B b) {
+            this.returnB = b;
+        }
+        
+        /**
+         * @see org.apache.portals.graffito.jcr.persistence.objectconverter.BeanConverter#insert(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.Mapper, java.lang.String, java.lang.Object)
+         */
+        public void insert(Session session, Node parentNode, Mapper mapper, String beanName, Object object) throws PersistenceException {
+            try {
+                log.add("insert at path " + parentNode.getPath());
+            }
+            catch(RepositoryException re) {
+                throw new PersistenceException(re);
+            }
+        } 
+
+        /**
+         * @see org.apache.portals.graffito.jcr.persistence.objectconverter.BeanConverter#update(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.Mapper, java.lang.String, java.lang.Object)
+         */
+        public void update(Session session, Node parentNode, Mapper mapper, String beanName, Object object) throws PersistenceException {
+            try {
+                log.add("update at path " + parentNode.getPath());
+            }
+            catch(RepositoryException re) {
+                throw new PersistenceException(re);
+            }
+        }
+
+        /**
+         * @see org.apache.portals.graffito.jcr.persistence.objectconverter.BeanConverter#getObject(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.Mapper, java.lang.String, java.lang.Class)
+         */
+        public Object getObject(Session session, Node parentNode, Mapper mapper, String beanName, Class beanClass) throws PersistenceException {
+            try {
+                log.add("get from path " + parentNode.getPath());
+            }
+            catch(RepositoryException re) {
+                throw new PersistenceException(re);
+            }
+            return this.returnB;
+        }
+
+        /**
+         * @see org.apache.portals.graffito.jcr.persistence.objectconverter.BeanConverter#remove(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.Mapper, java.lang.String)
+         */
+        public void remove(Session session, Node parentNode, Mapper mapper, String beanName) throws PersistenceException {
+            try {
+                log.add("remove from path " + parentNode.getPath());
+            }
+            catch(RepositoryException re) {
+                throw new PersistenceException(re);
+            }
+        }
     }
 }

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/D.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/D.java?rev=375924&r1=375923&r2=375924&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/D.java (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/D.java Wed Feb  8 03:13:12 2006
@@ -1,8 +1,23 @@
+/*
+ * Copyright 2000-2004 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.testmodel;
 
 
 /**
- * This class/interface
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
  */
 public class D {
     private String path;

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/DFull.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/DFull.java?rev=375924&r1=375923&r2=375924&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/DFull.java (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/DFull.java Wed Feb  8 03:13:12 2006
@@ -1,8 +1,23 @@
+/*
+ * Copyright 2000-2004 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.testmodel;
 
 
 /**
- * This class/interface
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
  */
 public class DFull {
     private String path;

Added: incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/E.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/E.java?rev=375924&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/E.java (added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/E.java Wed Feb  8 03:13:12 2006
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2000-2004 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.testmodel;
+
+
+/**
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
+ */
+public class E extends D {
+}