geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r769852 - in /geronimo/xbean/trunk/xbean-reflect/src: main/java/org/apache/xbean/recipe/ test/java/org/apache/xbean/recipe/
Date Wed, 29 Apr 2009 17:52:32 GMT
Author: gawor
Date: Wed Apr 29 17:52:32 2009
New Revision: 769852

URL: http://svn.apache.org/viewvc?rev=769852&view=rev
Log:
bunch more MapRecipe and CollectionRecipe tests and fixes

Modified:
    geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/CollectionRecipe.java
    geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/MapRecipe.java
    geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/CollectionRecipeTest.java
    geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/MapRecipeTest.java

Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/CollectionRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/CollectionRecipe.java?rev=769852&r1=769851&r2=769852&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/CollectionRecipe.java
(original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/CollectionRecipe.java
Wed Apr 29 17:52:32 2009
@@ -20,9 +20,11 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Dictionary;
 import java.util.EnumSet;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
@@ -47,8 +49,6 @@
 
     public CollectionRecipe(Class type) {
         if (type == null) throw new NullPointerException("type is null");
-        if (!RecipeHelper.hasDefaultConstructor(type)) throw new IllegalArgumentException("Collection
type does not have a default constructor " + type);
-
         this.list = new ArrayList<Object>();
         this.typeClass = type;
     }
@@ -168,9 +168,8 @@
 
     private Class getType(Type expectedType) {
         Class expectedClass = RecipeHelper.toClass(expectedType);
-        Class type = expectedClass;
         if (typeClass != null || typeName != null) {
-            type = typeClass;
+            Class type = typeClass;
             if (type == null) {
                 try {
                     type = RecipeHelper.loadClass(typeName);
@@ -181,25 +180,31 @@
 
             // if expectedType is a subclass of the assigned type,
             // we use it assuming it has a default constructor
-            if (type.isAssignableFrom(expectedClass) && RecipeHelper.hasDefaultConstructor(expectedClass))
{
-                type = expectedClass;
+            if (type.isAssignableFrom(expectedClass)) {
+                return getCollection(expectedClass);                
+            } else {
+                return getCollection(type);
             }
         }
-
+        
         // no type explicitly set
+        return getCollection(expectedClass);
+    }
+
+    private Class getCollection(Class type) {
         if (RecipeHelper.hasDefaultConstructor(type)) {
             return type;
-        } else if (SortedSet.class.isAssignableFrom(expectedClass)) {
+        } else if (SortedSet.class.isAssignableFrom(type)) {
             return TreeSet.class;
-        } else if (Set.class.isAssignableFrom(expectedClass)) {
+        } else if (Set.class.isAssignableFrom(type)) {
             return LinkedHashSet.class;
-        } else if (List.class.isAssignableFrom(expectedClass)) {
+        } else if (List.class.isAssignableFrom(type)) {
             return ArrayList.class;
         } else {
             return ArrayList.class;
         }
     }
-
+    
     public void add(Object value) {
         list.add(value);
     }

Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/MapRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/MapRecipe.java?rev=769852&r1=769851&r2=769852&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/MapRecipe.java
(original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/recipe/MapRecipe.java
Wed Apr 29 17:52:32 2009
@@ -51,8 +51,8 @@
     }
 
     public MapRecipe(Class type) {
+        if (type == null) throw new NullPointerException("type is null");
         this.typeClass = type;
-        if (!RecipeHelper.hasDefaultConstructor(type)) throw new IllegalArgumentException("Type
does not have a default constructor " + type);
         entries = new ArrayList<Object[]>();
     }
 
@@ -203,24 +203,29 @@
 
             // if expectedType is a subclass of the assigned type,
             // we use it assuming it has a default constructor
-            if (expectedClass.isAssignableFrom(type) && RecipeHelper.hasDefaultConstructor(type))
{
-                return type;
+            if (type.isAssignableFrom(expectedClass)) {
+                return getMap(expectedClass);                
+            } else {
+                return getMap(type);
             }
         }
 
         // no type explicitly set
-        if (RecipeHelper.hasDefaultConstructor(expectedClass)) {
-            return expectedClass;
-        } else if (SortedMap.class.isAssignableFrom(expectedClass)) {
+        return getMap(expectedClass);
+    }
+    
+    private Class getMap(Class type) {
+        if (RecipeHelper.hasDefaultConstructor(type)) {
+            return type;
+        } else if (SortedMap.class.isAssignableFrom(type)) {
             return TreeMap.class;
-        } else if (ConcurrentMap.class.isAssignableFrom(expectedClass)) {
+        } else if (ConcurrentMap.class.isAssignableFrom(type)) {
             return ConcurrentHashMap.class;
         } else {
             return LinkedHashMap.class;
         }
     }
 
-
     public void put(Object key, Object value) {
         if (key == null) throw new NullPointerException("key is null");
         entries.add(new Object[] { key, value});

Modified: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/CollectionRecipeTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/CollectionRecipeTest.java?rev=769852&r1=769851&r2=769852&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/CollectionRecipeTest.java
(original)
+++ geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/CollectionRecipeTest.java
Wed Apr 29 17:52:32 2009
@@ -16,7 +16,9 @@
  */
 package org.apache.xbean.recipe;
 
+import java.net.URI;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
@@ -24,6 +26,7 @@
 import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
+import java.util.Vector;
 
 import junit.framework.TestCase;
 
@@ -43,4 +46,42 @@
         assertEquals(TreeSet.class, new CollectionRecipe().create(SortedSet.class, false).getClass());
         assertEquals(HashSet.class, new CollectionRecipe().create(HashSet.class, false).getClass());
     }
+        
+    public void testListRecipe() throws Exception {
+        CollectionRecipe recipe = new CollectionRecipe(List.class);
+        assertEquals(ArrayList.class, recipe.create(List.class, false).getClass());
+        assertEquals(ArrayList.class, recipe.create(ArrayList.class, false).getClass());
+        assertEquals(LinkedList.class, recipe.create(LinkedList.class, false).getClass());
+        assertEquals(Vector.class, recipe.create(Vector.class, false).getClass());
+        
+        assertTrue(recipe.canCreate(List.class));
+        assertTrue(recipe.canCreate(ArrayList.class));
+        assertTrue(recipe.canCreate(LinkedList.class));
+        assertTrue(recipe.canCreate(Vector.class));
+        assertTrue(recipe.canCreate(Object.class));
+        
+        assertFalse(recipe.canCreate(Date.class));   
+        assertFalse(recipe.canCreate(URI.class));
+        
+        assertFalse(recipe.canCreate(Set.class));
+    }
+    
+    public void testSetRecipe() throws Exception {
+        CollectionRecipe recipe = new CollectionRecipe(Set.class);
+        assertEquals(LinkedHashSet.class, recipe.create(Set.class, false).getClass());
+        assertEquals(HashSet.class, recipe.create(HashSet.class, false).getClass());
+        assertEquals(TreeSet.class, recipe.create(SortedSet.class, false).getClass());
+        assertEquals(TreeSet.class, recipe.create(TreeSet.class, false).getClass());
+        
+        assertTrue(recipe.canCreate(Set.class));
+        assertTrue(recipe.canCreate(HashSet.class));
+        assertTrue(recipe.canCreate(SortedSet.class));
+        assertTrue(recipe.canCreate(TreeSet.class));
+        assertTrue(recipe.canCreate(Object.class));
+        
+        assertFalse(recipe.canCreate(Date.class));   
+        assertFalse(recipe.canCreate(URI.class));
+        
+        assertFalse(recipe.canCreate(List.class));
+    }
 }

Modified: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/MapRecipeTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/MapRecipeTest.java?rev=769852&r1=769851&r2=769852&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/MapRecipeTest.java
(original)
+++ geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/recipe/MapRecipeTest.java
Wed Apr 29 17:52:32 2009
@@ -16,6 +16,9 @@
  */
 package org.apache.xbean.recipe;
 
+import java.net.URI;
+import java.util.Date;
+import java.util.Hashtable;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.SortedMap;
@@ -37,4 +40,26 @@
         assertEquals(ConcurrentHashMap.class, new MapRecipe().create(ConcurrentMap.class,
false).getClass());
         assertEquals(HashMap.class, new MapRecipe(HashMap.class).create(Map.class, false).getClass());
     }
+    
+    public void testRecipe() throws Exception {
+        MapRecipe recipe = new MapRecipe(Map.class);
+        assertEquals(LinkedHashMap.class, recipe.create(Map.class, false).getClass());
+        assertEquals(ConcurrentHashMap.class, recipe.create(ConcurrentMap.class, false).getClass());
+        assertEquals(ConcurrentHashMap.class, recipe.create(ConcurrentHashMap.class, false).getClass());
+        assertEquals(TreeMap.class, recipe.create(SortedMap.class, false).getClass());
+        assertEquals(LinkedHashMap.class, recipe.create(LinkedHashMap.class, false).getClass());
+        assertEquals(Hashtable.class, recipe.create(Hashtable.class, false).getClass());
+        assertEquals(LinkedHashMap.class, recipe.create(Object.class, false).getClass());
+        
+        assertTrue(recipe.canCreate(Map.class));
+        assertTrue(recipe.canCreate(ConcurrentMap.class));
+        assertTrue(recipe.canCreate(SortedMap.class));
+        assertTrue(recipe.canCreate(TreeMap.class));
+        assertTrue(recipe.canCreate(LinkedHashMap.class));
+        assertTrue(recipe.canCreate(Hashtable.class));
+        assertTrue(recipe.canCreate(Object.class));
+        
+        assertFalse(recipe.canCreate(Date.class));   
+        assertFalse(recipe.canCreate(URI.class));
+    }
 }



Mime
View raw message