cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From torehal...@apache.org
Subject svn commit: r476912 - in /incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src: main/java/org/apache/cayenne/access/types/ test/java/org/apache/cayenne/access/types/
Date Sun, 19 Nov 2006 20:34:27 GMT
Author: torehalset
Date: Sun Nov 19 12:34:26 2006
New Revision: 476912

URL: http://svn.apache.org/viewvc?view=rev&rev=476912
Log:
CAY-540: extended type and subclasses

Added:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/SubclassTypeFactory.java
Modified:
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/ExtendedTypeMap.java
    incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/types/ExtendedTypeMapTst.java

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/ExtendedTypeMap.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/ExtendedTypeMap.java?view=diff&rev=476912&r1=476911&r2=476912
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/ExtendedTypeMap.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/ExtendedTypeMap.java
Sun Nov 19 12:34:26 2006
@@ -140,6 +140,9 @@
      */
     public void registerType(ExtendedType type) {
         typeMap.put(type.getClassName(), type);
+        
+        // factory to handle subclasses of type.className
+        addFactory(new SubclassTypeFactory(type));
     }
 
     /**

Added: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/SubclassTypeFactory.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/SubclassTypeFactory.java?view=auto&rev=476912
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/SubclassTypeFactory.java
(added)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/main/java/org/apache/cayenne/access/types/SubclassTypeFactory.java
Sun Nov 19 12:34:26 2006
@@ -0,0 +1,62 @@
+/*****************************************************************
+ *   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.cayenne.access.types;
+
+import org.apache.cayenne.util.Util;
+
+/**
+ * A {@link ExtendedTypeFactory} that makes a {@link ExtendedType} usable for subclasses
+ * of the className in the {@link ExtendedType}.
+ * 
+ * @since 3.0
+ * @author halset
+ */
+class SubclassTypeFactory implements ExtendedTypeFactory {
+
+    private ExtendedType type;
+    private Class javaClass;
+
+    SubclassTypeFactory(ExtendedType type) {
+        this.type = type;
+
+        try {
+            javaClass = Util.getJavaClass(type.getClassName());
+
+            // some classes that should not be handled here..
+            if (javaClass.isArray()
+                    || javaClass.equals(Object.class)
+                    || javaClass.isPrimitive()) {
+                javaClass = null;
+            }
+        }
+        catch (ClassNotFoundException e) {
+            // ignore.
+        }
+    }
+
+    public ExtendedType getType(Class objectClass) {
+
+        if ((javaClass != null) && javaClass.isAssignableFrom(objectClass)) {
+            return type;
+        }
+
+        return null;
+    }
+
+}

Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/types/ExtendedTypeMapTst.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/types/ExtendedTypeMapTst.java?view=diff&rev=476912&r1=476911&r2=476912
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/types/ExtendedTypeMapTst.java
(original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.4/src/test/java/org/apache/cayenne/access/types/ExtendedTypeMapTst.java
Sun Nov 19 12:34:26 2006
@@ -19,6 +19,9 @@
 
 package org.apache.cayenne.access.types;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import junit.framework.TestCase;
 
 public class ExtendedTypeMapTst extends TestCase {
@@ -36,6 +39,42 @@
         assertSame(map.getDefaultType(), map.getRegisteredType(tstType.getClassName()));
     }
 
+    public void testRegisterTypeSubclasses() throws Exception {
+        ExtendedTypeMap map = new ExtendedTypeMap();
+        ExtendedType tstType1 = new MockExtendedType(List.class);
+        ExtendedType tstType2 = new MockExtendedType(ArrayList.class);
+
+        // List
+        map.registerType(tstType1);
+        assertSame(tstType1, map.getRegisteredType(List.class));
+        assertSame(tstType1, map.getRegisteredType(ArrayList.class));
+        
+        map = new ExtendedTypeMap();
+        
+        // ArrayList
+        map.registerType(tstType2);
+        assertNotSame(tstType2, map.getRegisteredType(List.class));
+        assertSame(tstType2, map.getRegisteredType(ArrayList.class));
+        
+        map = new ExtendedTypeMap();
+        
+        // both
+        map.registerType(tstType1);
+        map.registerType(tstType2);
+        assertSame(tstType1, map.getRegisteredType(List.class));
+        assertSame(tstType2, map.getRegisteredType(ArrayList.class));
+        
+        
+        map = new ExtendedTypeMap();
+        
+        // both - different order
+        map.registerType(tstType2);
+        map.registerType(tstType1);
+        assertSame(tstType2, map.getRegisteredType(ArrayList.class));
+        assertSame(tstType1, map.getRegisteredType(List.class));
+        
+    }
+    
     public void testRegisterArrayType() throws Exception {
         ExtendedTypeMap map = new ExtendedTypeMap();
         ByteArrayType tstType = new ByteArrayType(false, true);



Mime
View raw message