From: erik Date: Mon, 12 May 2008 10:02:38 +0000 (+0000) Subject: added setter injection to the class adapter. X-Git-Tag: wamblee-utils-0.2~1^2~159 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=ea1f15ca41f5093232d4d4548a948c2cdce7ec42;p=utils added setter injection to the class adapter. Next step is creating the object adapter. --- diff --git a/system/general/src/main/java/org/wamblee/system/adapters/ClassAdapter.java b/system/general/src/main/java/org/wamblee/system/adapters/ClassAdapter.java index d7d273b5..ace1525d 100644 --- a/system/general/src/main/java/org/wamblee/system/adapters/ClassAdapter.java +++ b/system/general/src/main/java/org/wamblee/system/adapters/ClassAdapter.java @@ -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 { 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 { protected Object doStart(Scope aScope) { Object obj = _classConfig.create(aScope); + _classConfig.inject(aScope, obj); for (ProvidedInterface provided: getProvidedInterfaces()) { addInterface(provided, obj, aScope); diff --git a/system/general/src/main/java/org/wamblee/system/adapters/ClassConfiguration.java b/system/general/src/main/java/org/wamblee/system/adapters/ClassConfiguration.java index 96d976d3..a4114175 100644 --- a/system/general/src/main/java/org/wamblee/system/adapters/ClassConfiguration.java +++ b/system/general/src/main/java/org/wamblee/system/adapters/ClassConfiguration.java @@ -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 getProvidedInterfaces() { + List result = new ArrayList(); + result.add(new DefaultProvidedInterface("provided", _class)); + return result; } - public RequiredInterface[] getRequiredInterface() { - return _constructorConfig.getRequiredInterfaces().toArray(new RequiredInterface[0]); + public List getRequiredInterfaces() { + List result = new ArrayList(); + result.addAll(_constructorConfig.getRequiredInterfaces()); + result.addAll(_setterConfig.getRequiredInterfaces()); + return result; } } diff --git a/system/general/src/test/java/org/wamblee/system/adapters/ClassAdapterTest.java b/system/general/src/test/java/org/wamblee/system/adapters/ClassAdapterTest.java index 835e8110..f25d3459 100644 --- a/system/general/src/test/java/org/wamblee/system/adapters/ClassAdapterTest.java +++ b/system/general/src/test/java/org/wamblee/system/adapters/ClassAdapterTest.java @@ -15,10 +15,15 @@ */ 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()); } } diff --git a/system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java b/system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java index 4b3d02ea..411b86f8 100644 --- a/system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java +++ b/system/general/src/test/java/org/wamblee/system/adapters/ClassConfigurationTest.java @@ -15,34 +15,72 @@ */ 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 required = config.getRequiredInterfaces(); + ConstructorConfiguration config = classConfig.getConstructorConfig() + .greedy(); + ProvidedInterface provided = new DefaultProvidedInterface("arg", + String.class); + List 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 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()); - } }