added setter injection to the class adapter.
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 12 May 2008 10:02:38 +0000 (10:02 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 12 May 2008 10:02:38 +0000 (10:02 +0000)
Next step is creating the object adapter.

system/general/src/main/java/org/wamblee/system/adapters/ClassAdapter.java
system/general/src/main/java/org/wamblee/system/adapters/ClassConfiguration.java
system/general/src/test/java/org/wamblee/system/adapters/ClassAdapterTest.java
system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java

index d7d273b5f4e4ffd6fda9d2a9ea41a08a21ced624..ace1525dfd9b7c01ba03325b00943cc039b9c607 100644 (file)
@@ -17,6 +17,7 @@ package org.wamblee.system.adapters;
 
 import org.wamblee.system.core.AbstractComponent;
 import org.wamblee.system.core.ProvidedInterface;
+import org.wamblee.system.core.RequiredInterface;
 import org.wamblee.system.core.Scope;
 
 /**
@@ -30,8 +31,8 @@ public class ClassAdapter extends AbstractComponent<Object> {
 
        public ClassAdapter(String aName, ClassConfiguration aClassConfig) {
                super(aName, 
-                               aClassConfig.getProvidedInterfaces(),
-                               aClassConfig.getRequiredInterface());
+                               aClassConfig.getProvidedInterfaces().toArray(new ProvidedInterface[0]),
+                               aClassConfig.getRequiredInterfaces().toArray(new RequiredInterface[0]));
                _classConfig = aClassConfig; 
        }
        
@@ -39,6 +40,7 @@ public class ClassAdapter extends AbstractComponent<Object> {
        protected Object doStart(Scope aScope) {
                
            Object obj =  _classConfig.create(aScope);
+           _classConfig.inject(aScope, obj);
            
            for (ProvidedInterface provided: getProvidedInterfaces()) { 
                addInterface(provided, obj, aScope); 
index 96d976d32972642de93a1d840f265fc9e20be796..a4114175340495ca81740a6073851df7aed2aec7 100644 (file)
@@ -15,6 +15,9 @@
  */
 package org.wamblee.system.adapters;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.wamblee.system.core.DefaultProvidedInterface;
 import org.wamblee.system.core.ProvidedInterface;
 import org.wamblee.system.core.RequiredInterface;
@@ -29,7 +32,8 @@ import org.wamblee.system.core.Scope;
 public class ClassConfiguration {
 
        private Class _class; 
-       private ConstructorConfiguration _constructorConfig; 
+       private ConstructorConfiguration _constructorConfig;
+       private SetterConfiguration _setterConfig; 
        
        /**
         * Constructs the configuration. By default no constructor is selected and 
@@ -39,12 +43,17 @@ public class ClassConfiguration {
         */
        public ClassConfiguration(Class aClass) {
                _class = aClass; 
-               _constructorConfig = new ConstructorConfiguration(aClass);  
+               _constructorConfig = new ConstructorConfiguration(aClass);
+               _setterConfig = new SetterConfiguration(aClass);
        }
        
        public ConstructorConfiguration getConstructorConfig() {
                return _constructorConfig;
        }
+       
+       public SetterConfiguration getSetterConfiguration() { 
+           return _setterConfig; 
+       }
 
        /**
         * Creates the object in the given scope. 
@@ -55,13 +64,25 @@ public class ClassConfiguration {
                return _constructorConfig.create(aScope);
        }
        
-       public ProvidedInterface[] getProvidedInterfaces() { 
-               return new ProvidedInterface[] { 
-                       new DefaultProvidedInterface("provided", _class)        
-               };
+       /**
+        * Injects required interfaces through the setters 
+        * @param aObject Object to inject into. 
+        * @param aScope Scope in which injection takes place. 
+        */
+       public void inject(Scope aScope, Object aObject) { 
+           _setterConfig.invoke(aScope, aObject);
+       }
+       
+       public List<ProvidedInterface> getProvidedInterfaces() {
+           List<ProvidedInterface> result = new ArrayList<ProvidedInterface>();
+           result.add(new DefaultProvidedInterface("provided", _class));       
+           return result;
        }
        
-       public RequiredInterface[] getRequiredInterface() { 
-               return _constructorConfig.getRequiredInterfaces().toArray(new RequiredInterface[0]);
+       public List<RequiredInterface> getRequiredInterfaces() { 
+           List<RequiredInterface> result = new ArrayList<RequiredInterface>();
+           result.addAll(_constructorConfig.getRequiredInterfaces());
+           result.addAll(_setterConfig.getRequiredInterfaces());
+           return result; 
        }
 }
index 835e811027ad6e03d0c537ea177e2dc4b0a67146..f25d345917e111dd56c5b8570bbfd618451c8f85 100644 (file)
  */ 
 package org.wamblee.system.adapters;
 
+import java.util.Collections;
+import java.util.List;
+
 import org.wamblee.system.core.Component;
 import org.wamblee.system.core.Container;
+import org.wamblee.system.core.DefaultProvidedInterface;
 import org.wamblee.system.core.ProvidedInterface;
 import org.wamblee.system.core.RequiredInterface;
+import org.wamblee.system.core.RequiredInterfaceComparator;
 import org.wamblee.system.core.Scope;
 import org.wamblee.test.AssertionUtils;
 
@@ -41,9 +46,46 @@ public class ClassAdapterTest extends AdapterTestCase {
         AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)" },
                        EVENT_TRACKER.getEvents(Thread.currentThread()).toArray());
         
-        Object obj = scope.getRuntime(x1Adapter);
-        assertTrue(obj instanceof X1); 
-        obj = scope.getRuntime(x4Adapter);
-        assertTrue(obj instanceof X4);
+        Object obj1 = scope.getRuntime(x1Adapter);
+        assertTrue(obj1 instanceof X1); 
+        Object obj4 = scope.getRuntime(x4Adapter);
+        assertTrue(obj4 instanceof X4);
+        
+        X1 x1 = (X1) obj1;
+        X4 x4 = (X4) obj4;
+       
+        assertSame(x1, x4.getX1());
+       }
+       
+       public void testConstructorAndSetterInjection() {
+           ClassConfiguration x1Config = new ClassConfiguration(X1.class);
+        x1Config.getConstructorConfig().getParameters().setValue(0, "hello");
+        ClassConfiguration x4Config = new ClassConfiguration(X4.class);
+        ClassConfiguration x8Config = new ClassConfiguration(X8.class);
+        
+        ClassAdapter x1Adapter = new ClassAdapter("x1", x1Config);
+        ClassAdapter x4Adapter = new ClassAdapter("x4", x4Config);
+        ClassAdapter x8Adapter = new ClassAdapter("x8", x8Config);
+        
+        Container container = new Container("top", new Component[] { 
+                x1Adapter, x4Adapter, x8Adapter
+        }, new ProvidedInterface[0], new RequiredInterface[0]);
+        
+        Scope scope = container.start();
+        AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)", "x8(x1)", "x8.setX4(x4)" },
+                EVENT_TRACKER.getEvents(Thread.currentThread()).toArray());
+        
+        Object obj1 = scope.getRuntime(x1Adapter);
+        assertTrue(obj1 instanceof X1); 
+        Object obj4 = scope.getRuntime(x4Adapter);
+        assertTrue(obj4 instanceof X4);
+        Object obj8 = scope.getRuntime(x8Adapter); 
+        
+        X1 x1 = (X1) obj1;
+        X4 x4 = (X4) obj4;
+        X8 x8 = (X8) obj8; 
+        
+        assertSame(x4, x8.getX4());
+        assertSame(x1, x8.getX1());
        }
 }
index 4b3d02ea0fc0d3e5bfc4ac4c4f8812358bee9ad8..411b86f8ac0e31f3a3895ff00e107f7e185b0b86 100644 (file)
  */
 package org.wamblee.system.adapters;
 
+import java.util.Collections;
 import java.util.List;
 
 import org.wamblee.system.core.DefaultProvidedInterface;
 import org.wamblee.system.core.ProvidedInterface;
 import org.wamblee.system.core.RequiredInterface;
+import org.wamblee.system.core.RequiredInterfaceComparator;
 import org.wamblee.test.AssertionUtils;
 
 public class ClassConfigurationTest extends AdapterTestCase {
 
+    public void testConstructorConfig() {
+        ClassConfiguration classConfig = new ClassConfiguration(X1.class);
 
-       public void testConstructorConfig() {
-               ClassConfiguration classConfig = new ClassConfiguration(X1.class);
-               
-               ConstructorConfiguration config = classConfig.getConstructorConfig()
-                               .greedy();
-               ProvidedInterface provided = new DefaultProvidedInterface("arg",
-                               String.class);
-               List<RequiredInterface> required = config.getRequiredInterfaces();
+        ConstructorConfiguration config = classConfig.getConstructorConfig()
+                .greedy();
+        ProvidedInterface provided = new DefaultProvidedInterface("arg",
+                String.class);
+        List<RequiredInterface> required = classConfig.getRequiredInterfaces();
 
-               assertEquals(1, required.size());
-               assertFalse(required.get(0).isOptional());
+        assertEquals(1, required.size());
+        assertFalse(required.get(0).isOptional());
 
-               required.get(0).setProvider(provided);
+        required.get(0).setProvider(provided);
 
-               provided.publish("hello", _scope);
-               config.create(_scope);
+        provided.publish("hello", _scope);
+        classConfig.create(_scope);
+
+        AssertionUtils.assertEquals(new String[] { "x1(hello)" },
+                AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
+                        .toArray());
+    }
+
+    public void testConstructorConfigWithSetters() {
+        ClassConfiguration classConfig = new ClassConfiguration(X7.class);
+
+        classConfig.getConstructorConfig().select(Boolean.class);
+        classConfig.getSetterConfiguration().values("port").setValue(0, 10);
+        
+        ProvidedInterface providedBoolean = new DefaultProvidedInterface("boolean",
+                Boolean.class);
+        ProvidedInterface providedHost = new DefaultProvidedInterface("host", String.class);
+        List<RequiredInterface> required = classConfig.getRequiredInterfaces();
+
+        Collections.sort(required, new RequiredInterfaceComparator());
+        assertEquals(2, required.size());
+        assertEquals("arg0", required.get(0).getName());
+
+        required.get(0).setProvider(providedBoolean);
+        required.get(1).setProvider(providedHost);
+
+        providedBoolean.publish(true, _scope);
+        providedHost.publish("host.name.org", _scope);
+        
+        Object obj = classConfig.create(_scope);
+        assertTrue(obj instanceof X7);
+        X7 x7 = (X7)obj;
+        assertNotNull(x7.getBoolean());
+        assertTrue(x7.getBoolean());
+        assertNull(x7.getHost());
+        assertNull(x7.getPort());
+
+        classConfig.inject(_scope, obj);
+        
+        assertEquals("host.name.org", x7.getHost());
+        assertEquals(10, x7.getPort().intValue());
+    }
 
-               AssertionUtils.assertEquals(new String[] { "x1(hello)" }, AdapterTestCase.EVENT_TRACKER
-                               .getEvents(Thread.currentThread()).toArray());
-       }
 }