jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r705033 - in /jackrabbit/trunk/jackrabbit-ocm/src: main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/ main/java/org/apache/jackrabbit/ocm/manager/enumconverter/ test/java/org/apache/jackrabbit/ocm/ test/java/org/apache/ja...
Date Wed, 15 Oct 2008 20:38:36 GMT
Author: clombart
Date: Wed Oct 15 13:38:36 2008
New Revision: 705033

URL: http://svn.apache.org/viewvc?rev=705033&view=rev
Log:
Apply patch provided by Boni Gopalan for JCR-1804 (Added the functionality to Map and Manage
Type Enum).

Added:
    jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/EnumCollectionConverterImpl.java
    jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/enumconverter/
    jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/enumconverter/EnumTypeConverter.java
    jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/
    jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/AnnotationSimpleEnumerationTest.java
    jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/DigesterSimpleEnumerationTest.java
    jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/SimpleEnumerationTestBase.java
    jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/testmodel/enumeration/
    jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/testmodel/enumeration/Odyssey.java
    jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/testmodel/enumeration/Planet.java
    jackrabbit/trunk/jackrabbit-ocm/src/test/test-config/jcrmapping-Enum.xml
Modified:
    jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/AnnotationTestBase.java
    jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/DigesterTestBase.java

Added: jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/EnumCollectionConverterImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/EnumCollectionConverterImpl.java?rev=705033&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/EnumCollectionConverterImpl.java
(added)
+++ jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/EnumCollectionConverterImpl.java
Wed Oct 15 13:38:36 2008
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.ocm.manager.collectionconverter.impl;
+
+
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.Value;
+import javax.jcr.ValueFactory;
+import javax.jcr.ValueFormatException;
+
+import org.apache.jackrabbit.ocm.exception.JcrMappingException;
+import org.apache.jackrabbit.ocm.exception.ObjectContentManagerException;
+import org.apache.jackrabbit.ocm.manager.atomictypeconverter.AtomicTypeConverter;
+import org.apache.jackrabbit.ocm.manager.atomictypeconverter.impl.UndefinedTypeConverterImpl;
+import org.apache.jackrabbit.ocm.manager.collectionconverter.ManageableCollection;
+import org.apache.jackrabbit.ocm.manager.collectionconverter.ManageableObjectsUtil;
+import org.apache.jackrabbit.ocm.manager.collectionconverter.ManageableObjects;
+import org.apache.jackrabbit.ocm.manager.enumconverter.EnumTypeConverter;
+import org.apache.jackrabbit.ocm.manager.objectconverter.ObjectConverter;
+import org.apache.jackrabbit.ocm.mapper.Mapper;
+import org.apache.jackrabbit.ocm.mapper.model.CollectionDescriptor;
+import org.apache.jackrabbit.ocm.reflection.ReflectionUtils;
+
+/**
+ * Collection Mapping/convertion implementation used for enum collections
+ *
+ * This collection mapping strategy maps a collection into a JCR multi value property
+ *
+ * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
+ * @author <a href='mailto:boni.g@bioimagene.com'>Boni Gopalan</a>
+ */
+public class EnumCollectionConverterImpl extends AbstractCollectionConverterImpl {
+
+	private EnumTypeConverter enumConverter = new EnumTypeConverter();
+    /**
+     * Constructor
+     *
+     * @param atomicTypeConverters
+     * @param objectConverter
+     * @param mapper
+     */
+    public EnumCollectionConverterImpl(		Map atomicTypeConverters,
+                                             ObjectConverter objectConverter,
+                                             Mapper mapper) {
+        super(atomicTypeConverters, objectConverter, mapper);
+    }
+
+    /**
+     *
+     * @see AbstractCollectionConverterImpl#doInsertCollection(Session, Node, CollectionDescriptor,
ManageableCollection)
+     */
+    protected void doInsertCollection(Session session,
+                                      Node parentNode,
+                                      CollectionDescriptor collectionDescriptor,
+                                      ManageableObjects objects) throws RepositoryException
{
+        try {
+            if (objects == null) {
+                return;
+            }
+
+            String jcrName = getCollectionJcrName(collectionDescriptor);
+            Value[] values = new Value[objects.getSize()];
+            ValueFactory valueFactory = session.getValueFactory();
+            Iterator collectionIterator = objects.getIterator();
+            for (int i = 0; i < objects.getSize(); i++) {
+                Object fieldValue = collectionIterator.next();
+                values[i] = enumConverter.getValue(valueFactory, fieldValue);
+            }
+
+            parentNode.setProperty(jcrName, values);
+        }
+        catch(ValueFormatException vfe) {
+            throw new ObjectContentManagerException("Cannot insert collection field : "
+                    + collectionDescriptor.getFieldName()
+                    + " of class "
+                    + collectionDescriptor.getClassDescriptor().getClassName(), vfe);
+        }
+    }
+
+    /**
+     *
+     * @see AbstractCollectionConverterImpl#doUpdateCollection(Session, Node, CollectionDescriptor,
ManageableCollection)
+     */
+    protected void doUpdateCollection(Session session,
+                                 Node parentNode,
+                                 CollectionDescriptor collectionDescriptor,
+                                 ManageableObjects objects) throws RepositoryException {
+        String jcrName = getCollectionJcrName(collectionDescriptor);
+
+        // Delete existing values
+        if (parentNode.hasProperty(jcrName)) {
+            parentNode.setProperty(jcrName, (Value[]) null);
+        }
+
+        if (objects == null) {
+            return;
+        }
+
+
+        // Add all collection element into an Value array
+        Value[] values = new Value[objects.getSize()];
+        ValueFactory valueFactory = session.getValueFactory();
+        int i = 0;
+        for (Iterator collectionIterator = objects.getIterator(); collectionIterator.hasNext();
i++) {
+            Object fieldValue = collectionIterator.next();
+            values[i] = enumConverter.getValue(valueFactory, fieldValue);
+        }
+
+        parentNode.setProperty(jcrName, values);
+    }
+
+    /**
+     * @see AbstractCollectionConverterImpl#doGetCollection(Session, Node, CollectionDescriptor,
Class)
+     */
+    protected ManageableObjects doGetCollection(Session session,
+                                                   Node parentNode,
+                                                   CollectionDescriptor collectionDescriptor,
+                                                   Class collectionFieldClass) throws RepositoryException
{
+        try {
+            String jcrName = getCollectionJcrName(collectionDescriptor);
+            if (!parentNode.hasProperty(jcrName)) {
+                return null;
+            }
+            Property property = parentNode.getProperty(jcrName);
+            Value[] values = property.getValues();
+            if (values == null || values.length <= 0){
+            	return null;
+            }
+            
+            ManageableObjects objects = ManageableObjectsUtil.getManageableObjects(collectionFieldClass);
+            for (int i = 0; i < values.length; i++) {
+                ((ManageableCollection) objects).addObject(enumConverter.getObject(values[i]));
+            }
+
+            return objects;
+        }
+        catch(ValueFormatException vfe) {
+          throw new ObjectContentManagerException("Cannot get the collection field : "
+                  + collectionDescriptor.getFieldName()
+                  + "for class " + collectionDescriptor.getClassDescriptor().getClassName(),
+                  vfe);
+        }
+    }
+
+    /**
+     * @see AbstractCollectionConverterImpl#doIsNull(Session, Node, CollectionDescriptor,
Class)
+     */
+    protected boolean doIsNull(Session session,
+                                              Node parentNode,
+                                              CollectionDescriptor collectionDescriptor,
+                                              Class collectionFieldClass) throws RepositoryException
{
+        String jcrName = getCollectionJcrName(collectionDescriptor);
+
+         if (!parentNode.hasProperty(jcrName)) {
+            return true;
+        }
+        return false;
+    }
+}
\ No newline at end of file

Added: jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/enumconverter/EnumTypeConverter.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/enumconverter/EnumTypeConverter.java?rev=705033&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/enumconverter/EnumTypeConverter.java
(added)
+++ jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/enumconverter/EnumTypeConverter.java
Wed Oct 15 13:38:36 2008
@@ -0,0 +1,79 @@
+/*
+* Copyright © BioImagene Inc. 2005-2008 All right reserved.
+
+* The copyright to the computer program(s) herein is the property of Bioimagene Inc.
+* The program(s) may be used and/or copied only with the written permission
+* of Bioimagene or in accordance with the terms and conditions stipulated in the
+* agreement/contract under which the program(s) have been supplied.
+*/
+package org.apache.jackrabbit.ocm.manager.enumconverter;
+
+import java.io.InvalidClassException;
+
+import javax.jcr.Value;
+import javax.jcr.ValueFactory;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.jackrabbit.ocm.manager.atomictypeconverter.AtomicTypeConverter;
+
+
+/**
+*
+* SimpleEnumerationTypeConverter class.
+*
+* This converter can map type Enum (java.lang.Enum) to JCR properties and back.
+*
+* @author <a href="mailto:boni.g@bioimagene.com">Boni Gopalan</a>
+*/
+public class EnumTypeConverter implements AtomicTypeConverter {
+    
+	public Object getObject(Value value) {
+        try {
+            String propertyValue = value.getString();
+            String[] enumerationDef = StringUtils.split(propertyValue, ':');
+            String enumerationClass = enumerationDef[0];
+            String enumerationValue = enumerationDef[1];
+            Enum[] enumerations = (Enum[]) Class.forName(enumerationClass)
+                                                .getEnumConstants();
+            int size = enumerations.length;
+
+            for (int i = 0; i < size; i++) {
+                if (enumerations[i].name().equals(enumerationValue)) {
+                    return enumerations[i];
+                }
+            }
+
+            throw new RuntimeException(new InvalidClassException(enumerationClass +
+                    " Does not contain an enumeration " + enumerationValue));
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public Value getValue(ValueFactory valueFactory, Object object) {
+        if (object == null) {
+            return null;
+        }
+
+        if (!(object instanceof Enum)) {
+            throw new RuntimeException(new InvalidClassException(EnumTypeConverter.class.getSimpleName()
+
+                    " Can only convert simple Enumerations"));
+        }
+
+        String value;
+        Enum anEnum = (Enum) (object);
+        value = anEnum.getDeclaringClass().getName() + ":" + anEnum.name();
+
+        return valueFactory.createValue(value);
+    }
+
+    public String getXPathQueryValue(ValueFactory valueFactory, Object object) {
+        Value value = getValue(valueFactory, object);
+
+        try {
+            return "'" + value.getString() + "'";
+        } catch (Exception e) {
+            throw new RuntimeException(e.fillInStackTrace());
+        }
+    }
+}

Modified: jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/AnnotationTestBase.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/AnnotationTestBase.java?rev=705033&r1=705032&r2=705033&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/AnnotationTestBase.java
(original)
+++ jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/AnnotationTestBase.java
Wed Oct 15 13:38:36 2008
@@ -53,6 +53,7 @@
 import org.apache.jackrabbit.ocm.testmodel.collection.Element;
 import org.apache.jackrabbit.ocm.testmodel.collection.Main;
 import org.apache.jackrabbit.ocm.testmodel.collection.Person;
+import org.apache.jackrabbit.ocm.testmodel.enumeration.Odyssey;
 import org.apache.jackrabbit.ocm.testmodel.inheritance.Ancestor;
 import org.apache.jackrabbit.ocm.testmodel.inheritance.AnotherDescendant;
 import org.apache.jackrabbit.ocm.testmodel.inheritance.Descendant;
@@ -172,6 +173,7 @@
 		classes.add(SimpleAnnotedClass.class);
 		classes.add(SimpleInterface.class);
 		classes.add(Person.class);
+		classes.add(Odyssey.class);
 		
 		Mapper mapper = new AnnotationMapperImpl(classes);
 		ocm = new ObjectContentManagerImpl(session, mapper);

Modified: jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/DigesterTestBase.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/DigesterTestBase.java?rev=705033&r1=705032&r2=705033&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/DigesterTestBase.java
(original)
+++ jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/DigesterTestBase.java
Wed Oct 15 13:38:36 2008
@@ -58,7 +58,9 @@
                            "./src/test/test-config/jcrmapping-inheritance.xml",
                            "./src/test/test-config/jcrmapping-jcrnodetypes.xml",
                            "./src/test/test-config/jcrmapping-uuid.xml",
-                           "./src/test/test-config/jcrmapping-complex-collections.xml"};
+                           "./src/test/test-config/jcrmapping-complex-collections.xml",
+                           "./src/test/test-config/jcrmapping-Enum.xml"
+		};
 		session = RepositoryUtil.login(repository, "superuser", "superuser");
 		ocm = new ObjectContentManagerImpl(session, files);
 		

Added: jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/AnnotationSimpleEnumerationTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/AnnotationSimpleEnumerationTest.java?rev=705033&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/AnnotationSimpleEnumerationTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/AnnotationSimpleEnumerationTest.java
Wed Oct 15 13:38:36 2008
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.ocm.manager.enumeration;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jackrabbit.ocm.AnnotationTestBase;
+import org.apache.jackrabbit.ocm.RepositoryLifecycleTestSetup;
+import org.apache.jackrabbit.ocm.DigesterTestBase;
+import org.apache.jackrabbit.ocm.manager.ObjectContentManager;
+import org.apache.jackrabbit.ocm.testmodel.A;
+import org.apache.jackrabbit.ocm.testmodel.B;
+import org.apache.jackrabbit.ocm.testmodel.C;
+import org.apache.jackrabbit.ocm.testmodel.Discriminator;
+import org.apache.jackrabbit.ocm.testmodel.enumeration.Odyssey;
+import org.apache.jackrabbit.ocm.testmodel.enumeration.Planet;
+
+/**
+ * Test Simple Enumeration mappings
+ *
+ * @author <a href="mailto:boni.g@bioimagene.com">Boni Gopalan</a>
+ */
+public class AnnotationSimpleEnumerationTest extends AnnotationTestBase
+{
+    private final static Log logger = LogFactory.getLog(AnnotationSimpleEnumerationTest.class);
+
+    /**
+     * <p>Defines the test case name for junit.</p>
+     * @param testName The test case name.
+     */
+    public AnnotationSimpleEnumerationTest(String testName)  throws Exception
+    {
+        super(testName);
+    }
+
+    public static Test suite()
+    {
+        // All methods starting with "test" will be executed in the test suite.
+        return new RepositoryLifecycleTestSetup(
+                new TestSuite(AnnotationSimpleEnumerationTest.class));
+    }
+
+    public void testMapSimpleEnumeration()
+    {
+    	try {
+    		new SimpleEnumerationTestBase(getObjectContentManager()).testMapSimpleEnumeration();
+		} catch (Throwable e) {
+			e.printStackTrace();
+			fail("Tests resulted in exception");
+		}
+    }
+
+}

Added: jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/DigesterSimpleEnumerationTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/DigesterSimpleEnumerationTest.java?rev=705033&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/DigesterSimpleEnumerationTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/DigesterSimpleEnumerationTest.java
Wed Oct 15 13:38:36 2008
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.ocm.manager.enumeration;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+import junit.runner.TestCaseClassLoader;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jackrabbit.ocm.DigesterTestBase;
+import org.apache.jackrabbit.ocm.RepositoryLifecycleTestSetup;
+
+/**
+ * Test Persisting and retrieving Enum values.
+ *
+ * @author <a href="mailto:boni.g@bioimagene.com">Boni Gopalan</a>
+ */
+public class DigesterSimpleEnumerationTest extends DigesterTestBase
+{
+    private final static Log log = LogFactory.getLog(DigesterSimpleEnumerationTest.class);
+
+    /**
+     * <p>Defines the test case name for junit.</p>
+     * @param testName The test case name.
+     */
+    public DigesterSimpleEnumerationTest(String testName)  throws Exception
+    {
+        super(testName);
+    }
+
+    public static Test suite()
+    {
+        // All methods starting with "test" will be executed in the test suite.
+        return new RepositoryLifecycleTestSetup(
+                new TestSuite(DigesterSimpleEnumerationTest.class));
+    }
+
+    public void testMapSimpleEnumeration()
+    {
+    	try {
+			new SimpleEnumerationTestBase(getObjectContentManager()).testMapSimpleEnumeration();
+		} catch (Throwable e) {
+			e.printStackTrace();
+			fail("Tests resulted in exception");
+		}
+    }
+
+}

Added: jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/SimpleEnumerationTestBase.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/SimpleEnumerationTestBase.java?rev=705033&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/SimpleEnumerationTestBase.java
(added)
+++ jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/SimpleEnumerationTestBase.java
Wed Oct 15 13:38:36 2008
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.jackrabbit.ocm.manager.enumeration;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jackrabbit.ocm.manager.ObjectContentManager;
+import org.apache.jackrabbit.ocm.testmodel.enumeration.Odyssey;
+import org.apache.jackrabbit.ocm.testmodel.enumeration.Planet;
+
+/**
+ * Test Simple Enumeration mappings
+ *
+ * @author <a href="mailto:boni.g@bioimagene.com">Boni Gopalan</a>
+ */
+public class SimpleEnumerationTestBase extends TestCase
+{
+    private final static Log logger = LogFactory.getLog(SimpleEnumerationTestBase.class);
+    ObjectContentManager ocm;
+    public SimpleEnumerationTestBase(ObjectContentManager ocm){
+    	this.ocm = ocm;
+    }
+
+    public void testMapSimpleEnumeration()
+    {
+        try
+        {
+            // --------------------------------------------------------------------------------
+            // Create and store an object graph in the repository
+            // --------------------------------------------------------------------------------
+            Odyssey odyssey = new Odyssey(); 
+            odyssey.setPath("/odesseyToMars");
+            odyssey.setGoingTo(Planet.MARS);
+            odyssey.setStartingFrom(Planet.EARTH);
+            odyssey.setStops(getStops());
+            ocm.insert(odyssey);
+            Odyssey fbOdessey = (Odyssey)ocm.getObject("/odesseyToMars");
+            assertTrue("Fetched back Enum did not match the saved data", fbOdessey.getGoingTo()
== Planet.MARS);
+            assertTrue("Fetched back Enum did not match the saved data", fbOdessey.getStartingFrom()
== Planet.EARTH);
+            assertTrue("Fetched back Enum did not match the saved Enum Collection Size",
fbOdessey.getStops().size() == odyssey.getStops().size());
+            List<Planet> stops = getStops();
+            List<Planet> fbStops = fbOdessey.getStops();
+            for (Planet aStop : stops){
+            	assertContains("Fetched back list did not contain :" + aStop.toString(), aStop,
fbStops);
+            	logger.info("Contains Enum : " + aStop.toString());
+            }
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+            fail("Exception occurs during the unit test : " + e);
+        }
+
+    }
+
+    private static void assertContains(String message, Planet value, List<Planet> aList){
+    	for (Enum anObject : aList){
+    		if (anObject == null){
+    			if (value == null) return;
+    			continue;
+    		}
+    		if (anObject.equals(value)) return;
+    	}
+    	fail(message);
+    }
+    
+    private List<Planet> getStops(){
+    	List<Planet> stops = new ArrayList<Planet>();
+    	stops.add(Planet.MARS);
+    	stops.add(Planet.MERCURY);
+    	stops.add(Planet.JUPITER);
+    	return stops;
+    }
+
+}

Added: jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/testmodel/enumeration/Odyssey.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/testmodel/enumeration/Odyssey.java?rev=705033&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/testmodel/enumeration/Odyssey.java
(added)
+++ jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/testmodel/enumeration/Odyssey.java
Wed Oct 15 13:38:36 2008
@@ -0,0 +1,161 @@
+package org.apache.jackrabbit.ocm.testmodel.enumeration;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+import org.apache.jackrabbit.ocm.manager.collectionconverter.impl.EnumCollectionConverterImpl;
+import org.apache.jackrabbit.ocm.manager.enumconverter.EnumTypeConverter;
+import org.apache.jackrabbit.ocm.mapper.impl.annotation.Field;
+import org.apache.jackrabbit.ocm.mapper.impl.annotation.Node;
+
+@Node
+public class Odyssey {
+	@Field(path=true)
+	private String path = null;
+	@Field(converter=EnumTypeConverter.class)
+	private Planet startingFrom;
+	@Field(converter=EnumTypeConverter.class)
+	private Planet goingTo;
+	public Odyssey(){
+		startingFrom = Planet.EARTH;
+		goingTo = Planet.PLUTO;
+		stops = new ArrayList<Planet>();
+	}
+	@org.apache.jackrabbit.ocm.mapper.impl.annotation.Collection(collectionConverter=EnumCollectionConverterImpl.class)
+	private java.util.List<Planet> stops;
+
+	public void add(int index, Planet element) {
+		stops.add(index, element);
+	}
+
+	public boolean add(Planet e) {
+		return stops.add(e);
+	}
+
+	public boolean addAll(Collection<? extends Planet> c) {
+		return stops.addAll(c);
+	}
+
+	public boolean addAll(int index, Collection<? extends Planet> c) {
+		return stops.addAll(index, c);
+	}
+
+	public void clear() {
+		stops.clear();
+	}
+
+	public boolean contains(Object o) {
+		return stops.contains(o);
+	}
+
+	public boolean containsAll(Collection<?> c) {
+		return stops.containsAll(c);
+	}
+
+	public boolean equals(Object o) {
+		return stops.equals(o);
+	}
+
+	public Planet get(int index) {
+		return stops.get(index);
+	}
+
+	public int hashCode() {
+		return stops.hashCode();
+	}
+
+	public int indexOf(Object o) {
+		return stops.indexOf(o);
+	}
+
+	public boolean isEmpty() {
+		return stops.isEmpty();
+	}
+
+	public Iterator<Planet> iterator() {
+		return stops.iterator();
+	}
+
+	public int lastIndexOf(Object o) {
+		return stops.lastIndexOf(o);
+	}
+
+	public ListIterator<Planet> listIterator() {
+		return stops.listIterator();
+	}
+
+	public ListIterator<Planet> listIterator(int index) {
+		return stops.listIterator(index);
+	}
+
+	public Planet remove(int index) {
+		return stops.remove(index);
+	}
+
+	public boolean remove(Object o) {
+		return stops.remove(o);
+	}
+
+	public boolean removeAll(Collection<?> c) {
+		return stops.removeAll(c);
+	}
+
+	public boolean retainAll(Collection<?> c) {
+		return stops.retainAll(c);
+	}
+
+	public Planet set(int index, Planet element) {
+		return stops.set(index, element);
+	}
+
+	public int size() {
+		return stops.size();
+	}
+
+	public List<Planet> subList(int fromIndex, int toIndex) {
+		return stops.subList(fromIndex, toIndex);
+	}
+
+	public Object[] toArray() {
+		return stops.toArray();
+	}
+
+	public <T> T[] toArray(T[] a) {
+		return stops.toArray(a);
+	}
+
+	public java.util.List<Planet> getStops() {
+		return stops;
+	}
+
+	public void setStops(java.util.List<Planet> stops) {
+		this.stops = stops;
+	}
+
+	public Planet getStartingFrom() {
+		return startingFrom;
+	}
+
+	public void setStartingFrom(Planet startingFrom) {
+		this.startingFrom = startingFrom;
+	}
+
+	public Planet getGoingTo() {
+		return goingTo;
+	}
+
+	public void setGoingTo(Planet goingTo) {
+		this.goingTo = goingTo;
+	}
+
+	public String getPath() {
+		return path;
+	}
+
+	public void setPath(String path) {
+		this.path = path;
+	}
+}

Added: jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/testmodel/enumeration/Planet.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/testmodel/enumeration/Planet.java?rev=705033&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/testmodel/enumeration/Planet.java
(added)
+++ jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/testmodel/enumeration/Planet.java
Wed Oct 15 13:38:36 2008
@@ -0,0 +1,32 @@
+package org.apache.jackrabbit.ocm.testmodel.enumeration;
+
+public enum Planet {
+    MERCURY (3.303e+23, 2.4397e6),
+    VENUS   (4.869e+24, 6.0518e6),
+    EARTH   (5.976e+24, 6.37814e6),
+    MARS    (6.421e+23, 3.3972e6),
+    JUPITER (1.9e+27,   7.1492e7),
+    SATURN  (5.688e+26, 6.0268e7),
+    URANUS  (8.686e+25, 2.5559e7),
+    NEPTUNE (1.024e+26, 2.4746e7),
+    PLUTO   (1.27e+22,  1.137e6);
+
+    private final double mass;   // in kilograms
+    private final double radius; // in meters
+    Planet(double mass, double radius) {
+        this.mass = mass;
+        this.radius = radius;
+    }
+    public double mass()   { return mass; }
+    public double radius() { return radius; }
+
+    // universal gravitational constant  (m3 kg-1 s-2)
+    public static final double G = 6.67300E-11;
+
+    public double surfaceGravity() {
+        return G * mass / (radius * radius);
+    }
+    public double surfaceWeight(double otherMass) {
+        return otherMass * surfaceGravity();
+    }
+}

Added: jackrabbit/trunk/jackrabbit-ocm/src/test/test-config/jcrmapping-Enum.xml
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/test/test-config/jcrmapping-Enum.xml?rev=705033&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/test/test-config/jcrmapping-Enum.xml (added)
+++ jackrabbit/trunk/jackrabbit-ocm/src/test/test-config/jcrmapping-Enum.xml Wed Oct 15 13:38:36
2008
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You 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.
+  -->
+
+<jackrabbit-ocm>
+	<class-descriptor className="org.apache.jackrabbit.ocm.testmodel.enumeration.Odyssey">
+		<field-descriptor fieldName="path" path="true"/>
+		<field-descriptor 	fieldName="startingFrom" 
+							jcrName="startingFrom" 
+							converter="org.apache.jackrabbit.ocm.manager.enumconverter.EnumTypeConverter" />
+		<field-descriptor 	fieldName="goingTo" 
+							jcrName="goingTo" 
+							converter="org.apache.jackrabbit.ocm.manager.enumconverter.EnumTypeConverter" />
+		<collection-descriptor 	fieldName="stops" 
+								jcrName="stops" 
+								collectionConverter="org.apache.jackrabbit.ocm.manager.collectionconverter.impl.EnumCollectionConverterImpl"
/>
+	</class-descriptor>
+</jackrabbit-ocm>



Mime
View raw message