geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r769059 - in /geronimo/sandbox/blueprint/blueprint-core/src: main/java/org/apache/geronimo/blueprint/context/ main/java/org/apache/geronimo/blueprint/utils/ test/java/org/apache/geronimo/blueprint/ test/java/org/apache/geronimo/blueprint/po...
Date Mon, 27 Apr 2009 16:56:11 GMT
Author: gawor
Date: Mon Apr 27 16:56:10 2009
New Revision: 769059

URL: http://svn.apache.org/viewvc?rev=769059&view=rev
Log:
handle attributes with null value and a type

Added:
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/NullRecipe.java
  (with props)
Modified:
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java
    geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/ArgumentsMatcher.java
    geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java
    geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/Multiple.java
    geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java?rev=769059&r1=769058&r2=769059&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java
Mon Apr 27 16:56:10 2009
@@ -139,7 +139,9 @@
             BeanArgument argument = beanArguments.get(i);
             Class type = loadClass(argument.getValueType());
             Object obj = arguments.get(i);
-            if (obj instanceof Recipe) {                
+            if (obj == null) {
+                obj = new NullRecipe(type);
+            } else if (obj instanceof Recipe) {                
                 if (type != null || shouldPreinstantiate(argument.getValue())) {
                     obj = RecipeHelper.convert(Object.class, obj, refAllowed);
                     obj = convert(obj, type);

Added: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/NullRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/NullRecipe.java?rev=769059&view=auto
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/NullRecipe.java
(added)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/NullRecipe.java
Mon Apr 27 16:56:10 2009
@@ -0,0 +1,56 @@
+/*
+ * 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.geronimo.blueprint.context;
+
+import java.lang.reflect.Type;
+
+import org.apache.xbean.recipe.AbstractRecipe;
+import org.apache.xbean.recipe.ConstructionException;
+import org.apache.xbean.recipe.RecipeHelper;
+
+/**
+ * Recipe that always returns null but only for a specified type.
+ *
+ * @author <a href="mailto:dev@geronimo.apache.org">Apache Geronimo Project</a>
+ * @version $Rev$, $Date$
+ */
+public class NullRecipe extends AbstractRecipe {
+
+    private Class type;
+
+    public NullRecipe(Class type) {
+        this.type = (type == null) ? Object.class : type;
+    }
+
+    @Override
+    protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throws ConstructionException
{
+        Class expectedClass = RecipeHelper.toClass(expectedType);
+        if (expectedClass.isAssignableFrom(type)) {
+            return null;
+        } else {
+            throw new ConstructionException("");
+        }
+    }
+
+    public boolean canCreate(Type expectedType) {
+        Class expectedClass = RecipeHelper.toClass(expectedType);
+        return expectedClass.isAssignableFrom(type);
+    }
+
+}

Propchange: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/NullRecipe.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/NullRecipe.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/NullRecipe.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/ArgumentsMatcher.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/ArgumentsMatcher.java?rev=769059&r1=769058&r2=769059&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/ArgumentsMatcher.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/utils/ArgumentsMatcher.java
Mon Apr 27 16:56:10 2009
@@ -57,7 +57,7 @@
         }
         
         boolean allowReorder = options.contains(Option.ARGUMENT_REORDER); 
-        if (matches.size() == 0 && allowReorder) {
+        if (matches.size() == 0 && arguments.size() > 1 && allowReorder)
{
             // we did not find any matching method, let's try re-ordering the arguments 
      
             for (Method method : methods) {
                 if (method.getName().equals(name) && isAcceptable(method, options))
{
@@ -96,7 +96,7 @@
         }
             
         boolean allowReorder = options.contains(Option.ARGUMENT_REORDER);
-        if (matches.size() == 0 && allowReorder) {
+        if (matches.size() == 0 && arguments.size() > 1 && allowReorder)
{
             // we did not find any matching constructor, let's try re-ordering the arguments
           
             for (Constructor constructor : constructors) {
                 Class[] parameterTypes = constructor.getParameterTypes();               
  

Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java?rev=769059&r1=769058&r2=769059&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java
Mon Apr 27 16:56:10 2009
@@ -150,64 +150,68 @@
         PojoA pojoa = (PojoA) obj1;
         
         Object obj2 = graph.create("pojoB");
-        assertNotNull(obj2);
-        assertTrue(obj2 instanceof PojoB);
-        PojoB pojob = (PojoB) obj2;
+        testPojoB(obj2, URI.create("urn:myuri"), 10);
         
-        assertEquals(URI.create("urn:myuri"), pojob.getUri());
-        assertEquals(10, pojob.getNumber());
-        
-        assertEquals(pojob, pojoa.getPojob());
+        assertEquals(obj2, pojoa.getPojob());
         assertEquals(new BigInteger("10"), pojoa.getNumber());
         
         Object obj3 = graph.create("pojoC");
-        assertNotNull(obj3);
-        assertTrue(obj3 instanceof PojoB);
-        pojob = (PojoB) obj3;
-        
-        assertEquals(URI.create("urn:myuri-static"), pojob.getUri());
-        assertEquals(15, pojob.getNumber());
+        testPojoB(obj3, URI.create("urn:myuri-static"), 15);
         
         Object obj4 = graph.create("pojoD");
-        assertNotNull(obj4);
-        assertTrue(obj4 instanceof PojoB);
-        pojob = (PojoB) obj4;
-        
-        assertEquals(URI.create("urn:myuri-static"), pojob.getUri());
-        assertEquals(15, pojob.getNumber());
+        testPojoB(obj4, URI.create("urn:myuri-static"), 15);
         
         Object obj5 = graph.create("pojoE");
-        assertNotNull(obj5);
-        assertTrue(obj5 instanceof PojoB);
-        pojob = (PojoB) obj5;
-        
-        assertEquals(URI.create("urn:myuri-dynamic"), pojob.getUri());
-        assertEquals(20, pojob.getNumber());
+        testPojoB(obj5, URI.create("urn:myuri-dynamic"), 20);
         
         try {
             graph.create("multipleFail");
             fail("Did not throw exception");
         } catch (RuntimeException e) {
             // we expect exception 
+            // TODO: check the exception string?
         }
         
         Object obj6 = graph.create("multipleInt");
-        assertNotNull(obj6);
-        assertTrue(obj6 instanceof Multiple);
-        assertEquals(123, ((Multiple)obj6).getInt());
-        assertNull(((Multiple)obj6).getString());
+        testMultiple(obj6, null, 123);
         
         Object obj7 = graph.create("multipleString");
-        assertNotNull(obj7);
-        assertTrue(obj7 instanceof Multiple);
-        assertEquals(-1, ((Multiple)obj7).getInt());
-        assertEquals("123", ((Multiple)obj7).getString());
+        testMultiple(obj7, "123", -1);
         
         Object obj8 = graph.create("multipleStringConvertable");
-        assertNotNull(obj8);
-        assertTrue(obj8 instanceof Multiple);
-        assertEquals(-1, ((Multiple)obj8).getInt());
-        assertEquals("hello", ((Multiple)obj8).getString());
+        testMultiple(obj8, "hello", -1);
+        
+        Object obj9 = graph.create("multipleFactory1");
+        testMultiple(obj9, null, 1234);
+
+        Object obj10 = graph.create("multipleFactory2");
+        testMultiple(obj10, "helloCreate-boolean", -1);        
+        
+        try {
+            graph.create("multipleFactoryNull");
+            fail("Did not throw exception");
+        } catch (RuntimeException e) {
+            // we expect exception 
+            // TODO: check the exception string?
+        }
+        
+        Object obj11 = graph.create("multipleFactoryTypedNull");
+        testMultiple(obj11, "hello-boolean", -1);          
+    }
+    
+    private void testPojoB(Object obj, URI uri, int intValue) {
+        assertNotNull(obj);
+        assertTrue(obj instanceof PojoB);
+        PojoB pojob = (PojoB) obj;
+        assertEquals(uri, pojob.getUri());
+        assertEquals(intValue, pojob.getNumber());
+    }
+    
+    private void testMultiple(Object obj, String stringValue, int intValue) {
+        assertNotNull(obj);
+        assertTrue(obj instanceof Multiple);
+        assertEquals(intValue, ((Multiple)obj).getInt());
+        assertEquals(stringValue, ((Multiple)obj).getString());
     }
      
 }

Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/Multiple.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/Multiple.java?rev=769059&r1=769058&r2=769059&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/Multiple.java
(original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/Multiple.java
Mon Apr 27 16:56:10 2009
@@ -38,4 +38,13 @@
     public String getString() {
         return stringValue;
     }
+    
+    public static Multiple create(String arg1, Integer arg2) {
+        return new Multiple(arg2.intValue());
+    }
+    
+    public static Multiple create(String arg1, Boolean arg2) {
+        return new Multiple(arg1 + "-boolean");
+    }
+    
 }

Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml?rev=769059&r1=769058&r2=769059&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml Mon
Apr 27 16:56:10 2009
@@ -51,4 +51,28 @@
         <argument value="hello"/>
     </bean>
     
+    <bean id="multipleFactory1" class="org.apache.geronimo.blueprint.pojos.Multiple"
+          factory-method = "create" >
+        <argument value="hello"/>
+        <argument value="1234"/>
+    </bean>
+        
+    <bean id="multipleFactory2" class="org.apache.geronimo.blueprint.pojos.Multiple"
+          factory-method = "create" >
+        <argument value="helloCreate"/>
+        <argument value="yes"/>
+    </bean>
+
+    <bean id="multipleFactoryNull" class="org.apache.geronimo.blueprint.pojos.Multiple"
+          factory-method = "create" >
+        <argument value="helloNull"/>
+        <argument><null/></argument>
+    </bean>
+    
+     <bean id="multipleFactoryTypedNull" class="org.apache.geronimo.blueprint.pojos.Multiple"
+          factory-method = "create" >
+        <argument value="hello"/>
+        <argument type = "java.lang.Boolean"><null/></argument>
+    </bean>
+    
 </blueprint>



Mime
View raw message