From de11c592264fa957ea32179fdbe8a9c3d5ce7654 Mon Sep 17 00:00:00 2001 From: Derek Hulley Date: Mon, 30 Jan 2006 02:57:19 +0000 Subject: [PATCH] More tests for property persistence and fixes to ensure that d:any always returns the same type as put in git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2247 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../alfresco/repo/domain/PropertyValue.java | 133 +++++++++--------- .../repo/node/BaseNodeServiceTest.java | 32 +++++ 2 files changed, 101 insertions(+), 64 deletions(-) diff --git a/source/java/org/alfresco/repo/domain/PropertyValue.java b/source/java/org/alfresco/repo/domain/PropertyValue.java index 56706871ed..ef755dac9b 100644 --- a/source/java/org/alfresco/repo/domain/PropertyValue.java +++ b/source/java/org/alfresco/repo/domain/PropertyValue.java @@ -145,66 +145,6 @@ public class PropertyValue implements Cloneable, Serializable }, SERIALIZABLE { - /** - * As Serializable persistence is the last resort, being both - * more verbose as well as unusable in queries, the value is used to - * determine a more appropriate persistent type. This method defers to - * the other, more basic types' version of this method to fulfill the request. - */ - @Override - protected ValueType getPersistedType(Serializable value) - { - if (value == null) - { - throw new IllegalArgumentException("Persisted type cannot be determined by null value"); - } - // check the value to determine the most appropriate type to defer to - if (value instanceof Boolean) - { - return ValueType.BOOLEAN.getPersistedType(value); - } - else if ((value instanceof Integer) || (value instanceof Long)) - { - return ValueType.LONG.getPersistedType(value); - } - else if (value instanceof Float) - { - return ValueType.FLOAT.getPersistedType(value); - } - else if (value instanceof Double) - { - return ValueType.DOUBLE.getPersistedType(value); - } - else if (value instanceof String) - { - return ValueType.STRING.getPersistedType(value); - } - else if (value instanceof Date) - { - return ValueType.DATE.getPersistedType(value); - } - else if (value instanceof ContentData) - { - return ValueType.CONTENT.getPersistedType(value); - } - else if (value instanceof NodeRef) - { - return ValueType.NODEREF.getPersistedType(value); - } - else if (value instanceof QName) - { - return ValueType.QNAME.getPersistedType(value); - } - else if (value instanceof Path) - { - return ValueType.PATH.getPersistedType(value); - } - else - { - return this; - } - } - @Override Serializable convert(Serializable value) { @@ -269,7 +209,7 @@ public class PropertyValue implements Cloneable, Serializable }; /** - * Override if the type gets persisted in a different format + * Override if the type gets persisted in a different format. * * @param value the actual value that is to be persisted. May not be null. */ @@ -308,6 +248,65 @@ public class PropertyValue implements Cloneable, Serializable } } + /** + * Determine the actual value type to aid in more concise persistence. + * + * @param value the value that is to be persisted + * @return Returns the value type equivalent of the + */ + private static ValueType getActualType(Serializable value) + { + if (value == null) + { + return ValueType.NULL; + } + else if (value instanceof Boolean) + { + return ValueType.BOOLEAN; + } + else if ((value instanceof Integer) || (value instanceof Long)) + { + return ValueType.LONG; + } + else if (value instanceof Float) + { + return ValueType.FLOAT; + } + else if (value instanceof Double) + { + return ValueType.DOUBLE; + } + else if (value instanceof String) + { + return ValueType.STRING; + } + else if (value instanceof Date) + { + return ValueType.DATE; + } + else if (value instanceof ContentData) + { + return ValueType.CONTENT; + } + else if (value instanceof NodeRef) + { + return ValueType.NODEREF; + } + else if (value instanceof QName) + { + return ValueType.QNAME; + } + else if (value instanceof Path) + { + return ValueType.PATH; + } + else + { + // type is not recognised as belonging to any particular slot + return ValueType.SERIALIZABLE; + } + } + /** a mapping from a property type QName to the corresponding value type */ private static Map valueTypesByPropertyType; static @@ -362,7 +361,8 @@ public class PropertyValue implements Cloneable, Serializable */ public PropertyValue(QName typeQName, Serializable value) { - this.actualType = makeValueType(typeQName); + this.actualType = PropertyValue.getActualType(value); +// this.actualType = makeValueType(typeQName); if (value == null) { setPersistedValue(ValueType.NULL, null); @@ -382,8 +382,8 @@ public class PropertyValue implements Cloneable, Serializable else { // get the persisted type - ValueType valueType = makeValueType(typeQName); - ValueType persistedValueType = valueType.getPersistedType(value); +// ValueType valueType = makeValueType(typeQName); + ValueType persistedValueType = this.actualType.getPersistedType(value); // convert to the persistent type value = persistedValueType.convert(value); setPersistedValue(persistedValueType, value); @@ -586,6 +586,11 @@ public class PropertyValue implements Cloneable, Serializable // first check for null ValueType requiredType = makeValueType(typeQName); + if (requiredType == ValueType.SERIALIZABLE) + { + // the required type must be the actual type + requiredType = this.actualType; + } // we need to convert Serializable ret = null; diff --git a/source/java/org/alfresco/repo/node/BaseNodeServiceTest.java b/source/java/org/alfresco/repo/node/BaseNodeServiceTest.java index a22d619c46..d86b011395 100644 --- a/source/java/org/alfresco/repo/node/BaseNodeServiceTest.java +++ b/source/java/org/alfresco/repo/node/BaseNodeServiceTest.java @@ -27,6 +27,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import junit.framework.TestCase; + import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.model.ContentModel; import org.alfresco.repo.content.MimetypeMap; @@ -893,6 +895,36 @@ public abstract class BaseNodeServiceTest extends BaseSpringTest } } + /** + * Ensures that the type you get out of a d:any property is the type that you + * put in. + */ + public void testSerializableProperties() throws Exception + { + ContentData contentData = new ContentData(null, null, 0L, null); + QName qname = PROP_QNAME_CONTENT_VALUE; + + Map properties = new HashMap(17); + properties.put(PROP_QNAME_CONTENT_VALUE, contentData); + properties.put(PROP_QNAME_SERIALIZABLE_VALUE, qname); + // create node + NodeRef nodeRef = nodeService.createNode( + rootNodeRef, + ASSOC_TYPE_QNAME_TEST_CHILDREN, + QName.createQName("pathA"), + ContentModel.TYPE_CONTAINER, + properties).getChildRef(); + // persist + flushAndClear(); + + // get the properties back + Map checkProperties = nodeService.getProperties(nodeRef); + Serializable checkPropertyContentData = checkProperties.get(PROP_QNAME_CONTENT_VALUE); + Serializable checkPropertyQname = checkProperties.get(PROP_QNAME_SERIALIZABLE_VALUE); + assertTrue("Serialization/deserialization of ContentData failed", checkPropertyContentData instanceof ContentData); + assertTrue("Serialization/deserialization failed", checkPropertyQname instanceof QName); + } + /** * Check that properties go in and come out in the correct format */