Next step is creating the object adapter.
import org.wamblee.system.core.AbstractComponent;
import org.wamblee.system.core.ProvidedInterface;
+import org.wamblee.system.core.RequiredInterface;
import org.wamblee.system.core.Scope;
/**
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;
}
protected Object doStart(Scope aScope) {
Object obj = _classConfig.create(aScope);
+ _classConfig.inject(aScope, obj);
for (ProvidedInterface provided: getProvidedInterfaces()) {
addInterface(provided, obj, aScope);
*/
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;
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
*/
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.
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;
}
}
*/
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;
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());
}
}
*/
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());
- }
}