<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.wamblee</groupId>
+ <artifactId>wamblee-support-general</artifactId>
+ <version>0.2-SNAPSHOT</version>
+ </dependency>
<dependency>
<groupId>org.wamblee</groupId>
<artifactId>wamblee-support-general</artifactId>
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+import org.wamblee.system.core.AbstractComponent;
+import org.wamblee.system.core.Component;
+import org.wamblee.system.core.ProvidedInterface;
+import org.wamblee.system.core.RequiredInterface;
+import org.wamblee.system.core.Scope;
+
+public class ClassAdapter extends AbstractComponent<Object> {
+
+ private ClassConfiguration _classConfig;
+
+ public ClassAdapter(String aName, ClassConfiguration aClassConfig) {
+ super(aName,
+ aClassConfig.getProvidedInterfaces(),
+ aClassConfig.getRequiredInterface());
+ _classConfig = aClassConfig;
+ }
+
+ @Override
+ protected Object doStart(Scope aScope) {
+
+ Object obj = _classConfig.create(aScope);
+
+ for (ProvidedInterface provided: getProvidedInterfaces()) {
+ addInterface(provided, obj, aScope);
+ }
+
+ return obj;
+ }
+
+ @Override
+ protected void doStop(Object aRuntime) {
+ // Empty.
+ }
+}
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+import java.lang.reflect.Constructor;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.wamblee.collections.CollectionFilter;
+import org.wamblee.conditions.Condition;
+import org.wamblee.system.core.DefaultProvidedInterface;
+import org.wamblee.system.core.DefaultRequiredInterface;
+import org.wamblee.system.core.ProvidedInterface;
+import org.wamblee.system.core.RequiredInterface;
+import org.wamblee.system.core.Scope;
+import org.wamblee.system.core.SystemAssemblyException;
+
+/**
+ * The class configuration encapsulates the knowledge of how to wrap a class as a component.
+ * In particular, it provides:
+ * <ul>
+ * <li> Selection of a constructor using explicit selection
+ * {@link #select(Class...)} or using the most greedy constructor
+ * {@link #greedy()}.
+ * </li>
+ * <li>
+ * Selection of methods to invoke to inject other objects into the object.
+ * </li>
+ * <li> Selection of fields to set.
+ * </li>
+ * </ul>
+ *
+ * @author Erik Brakkee
+ *
+ */
+public class ClassConfiguration {
+
+ private Class _class;
+ private ConstructorConfiguration _constructorConfig;
+
+ /**
+ * Constructs the configuration. By default no constructor is selected and
+ * one of {@link #select(Class...)} or
+ * {@link #greedy()} must be called.
+ * @param aClass Class to construct.
+ */
+ public ClassConfiguration(Class aClass) {
+ _class = aClass;
+ _constructorConfig = new ConstructorConfiguration(aClass);
+ }
+
+ public ConstructorConfiguration getConstructorConfig() {
+ return _constructorConfig;
+ }
+
+ /**
+ * Creates the object in the given scope.
+ * @param aScope Scope containing required interfaces for this object.
+ * @return object.
+ */
+ public Object create(Scope aScope) {
+ return _constructorConfig.create(aScope);
+ }
+
+ public ProvidedInterface[] getProvidedInterfaces() {
+ return new ProvidedInterface[] {
+ new DefaultProvidedInterface("provided", _class)
+ };
+ }
+
+ public RequiredInterface[] getRequiredInterface() {
+ return _constructorConfig.getRequiredInterfaces().toArray(new RequiredInterface[0]);
+ }
+}
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Member;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.wamblee.collections.CollectionFilter;
+import org.wamblee.conditions.Condition;
+import org.wamblee.system.core.RequiredInterface;
+import org.wamblee.system.core.Scope;
+import org.wamblee.system.core.SystemAssemblyException;
+
+public class ConstructorConfiguration {
+ private Class _class;
+ private Constructor<?> _constructor;
+ private ParameterValues _values;
+ private boolean _publicOnly;
+
+ /**
+ * Constructs the configuration. By default no constructor is selected and
+ * one of {@link #select(Class...)} or
+ * {@link #greedy()} must be called.
+ * @param aClass Class to construct.
+ */
+ public ConstructorConfiguration(Class aClass) {
+ _class = aClass;
+ _constructor = null;
+ _publicOnly = true;
+ }
+
+ /**
+ * Sets whether or no non public constructors are also considered.
+ * Reset the choice of a constructor.
+ * @param aNonPublic
+ * @return
+ */
+ public ConstructorConfiguration setNonPublic(boolean aNonPublic) {
+ _publicOnly = !aNonPublic;
+ _constructor = null;
+ _values = null;
+ return this;
+ }
+
+ /**
+ * Selects an explicit constructor.
+ * @param aTypes Arguments of the constructor.
+ * @return Return the injector to allow call chaining.
+ */
+ public ConstructorConfiguration select(Class... aTypes) {
+ try {
+ _constructor = _class.getDeclaredConstructor(aTypes);
+ } catch (Exception e) {
+ throw new SystemAssemblyException(e.getMessage(), e);
+ }
+ resetValues();
+ return this;
+ }
+
+ /**
+ * Selects the greediest constructor.
+ * @return The injector to allow call chaining.
+ * @throws SystemAssemblyException if the greediest constructor cannot be uniquely
+ * identified.
+ */
+ public ConstructorConfiguration greedy() {
+ Constructor<?>[] declared = _class.getDeclaredConstructors();
+ if (declared.length == 0) {
+ throw new SystemAssemblyException("Class '" + _class
+ + " is an interface, primitive type, or array");
+ }
+ int max = -1;
+ List<Constructor<?>> checked = new ArrayList<Constructor<?>>();
+ CollectionFilter.filter(Arrays.asList(declared), checked,
+ new Condition<Constructor<?>>() {
+ @Override
+ public boolean matches(Constructor<?> aObject) {
+ if ( !_publicOnly ) {
+ return true;
+ } else {
+ return Modifier.isPublic(aObject.getModifiers());
+ }
+ }
+ });
+ for (Constructor ctor : checked) {
+ if (ctor.getParameterTypes().length > max) {
+ max = ctor.getParameterTypes().length;
+ }
+ }
+ final int max2 = max;
+ List<Constructor<?>> ctors = checked;
+ List<Constructor<?>> longest = new ArrayList<Constructor<?>>();
+ CollectionFilter.filter(ctors, longest,
+ new Condition<Constructor<?>>() {
+ @Override
+ public boolean matches(Constructor<?> aObject) {
+ return aObject.getParameterTypes().length == max2;
+ }
+ });
+ if (longest.size() > 1) {
+ throw new SystemAssemblyException(
+ "Greediest constructor cannot be uniquely determined");
+ }
+ _constructor = longest.get(0);
+ resetValues();
+ return this;
+ }
+
+ public ParameterValues getParameters() {
+ getConstructor(); // initialize constructor if needed.
+ return _values;
+ }
+
+ /**
+ * Resets the values.
+ */
+ private void resetValues() {
+ _constructor.setAccessible(true);
+ _values = new ParameterValues(_constructor.getParameterTypes());
+ }
+
+ /**
+ * Creates the object in the given scope.
+ * @param aScope Scope containing required interfaces for this object.
+ * @return object.
+ */
+ public Object create(Scope aScope) {
+ Object[] values = _values.values(aScope);
+ try {
+ return getConstructor().newInstance(values);
+ } catch (Exception e) {
+ throw new SystemAssemblyException("Could not construct object "
+ + getConstructor() + " " + Arrays.asList(values), e);
+ }
+ }
+
+ public List<RequiredInterface> getRequiredInterfaces() {
+ getConstructor(); // initialize constructor if needed.
+ return _values.getRequiredInterfaces();
+ }
+
+ private Constructor getConstructor() {
+ if (_constructor == null ) {
+ greedy();
+ }
+ return _constructor;
+ }
+}
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+import org.wamblee.system.core.Scope;
+
+public class FixedValueProvider implements ValueProvider {
+
+ private Object _value;
+
+ public FixedValueProvider(Object aValue) {
+ _value = aValue;
+ }
+
+ @Override
+ public Object getValue(Scope aScope) {
+ return _value;
+ }
+}
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.wamblee.system.core.DefaultRequiredInterface;
+import org.wamblee.system.core.RequiredInterface;
+import org.wamblee.system.core.Scope;
+
+public class ParameterValues {
+ private Class[] _types;
+ private ValueProvider[] _values;
+
+ /**
+ * Constructs the configuration. By default no constructor is selected and
+ * one of {@link #select(Class...)} or
+ * {@link #greedy()} must be called.
+ * @param aClass Class to construct.
+ */
+ public ParameterValues(Class[] aTypes) {
+ _types = aTypes;
+ resetValues();
+ }
+
+ public Class[] getTypes() {
+ return _types;
+ }
+
+ /**
+ * Sets argument i to be optional, meaning that null is allowed to be passed in.
+ * @param aArg Argument to set.
+ */
+ public ParameterValues setOptional(int aArg) {
+ _values[aArg] = new RequiredInterfaceProvider(new DefaultRequiredInterface(
+ "arg" + aArg, _types[aArg], true));
+ return this;
+ }
+
+ /**
+ * Sets the argument i to a fixed value.
+ * @param aArg Argument to set.
+ * @param aValue Value.
+ */
+ public ParameterValues setValue(int aArg, Object aValue) {
+ _values[aArg] = new FixedValueProvider(aValue);
+ return this;
+ }
+
+ /**
+ * Resets the values.
+ */
+ private void resetValues() {
+ _values = new ValueProvider[_types.length];
+ for (int i = 0; i < _values.length; i++) {
+ _values[i] = new RequiredInterfaceProvider(new DefaultRequiredInterface(
+ "arg" + i, _types[i]));
+ }
+ }
+
+ public List<RequiredInterface> getRequiredInterfaces() {
+ List<RequiredInterface> result = new ArrayList<RequiredInterface>();
+ for (ValueProvider provider: _values) {
+ if ( provider instanceof RequiredInterfaceProvider) {
+ result.add( ((RequiredInterfaceProvider)provider).getRequiredInterface());
+ }
+ }
+ return result;
+ }
+
+ public Object[] values(Scope aScope) {
+ Object[] values = new Object[_values.length];
+ for (int i = 0; i < _values.length; i++) {
+ values[i] = _values[i].getValue(aScope);
+ }
+ return values;
+ }
+
+}
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+import org.wamblee.system.core.RequiredInterface;
+import org.wamblee.system.core.Scope;
+
+public class RequiredInterfaceProvider implements ValueProvider {
+
+ private RequiredInterface _required;
+
+ public RequiredInterfaceProvider(RequiredInterface aRequired) {
+ _required = aRequired;
+ }
+
+ @Override
+ public Object getValue(Scope aScope) {
+ return aScope.getInterfaceImplementation(_required.getProvider(), Object.class);
+ }
+
+ public RequiredInterface getRequiredInterface() {
+ return _required;
+ }
+
+}
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+import org.wamblee.system.core.Scope;
+
+public interface ValueProvider {
+
+ Object getValue(Scope aScope);
+}
}
@Override
- public <T> T retrieveInterfaceImplementation(ProvidedInterface aInterface,
+ public <T> T getInterfaceImplementation(ProvidedInterface aInterface,
Class<T> aType) {
if ( aInterface == null ) {
return null;
ProvidedInterfaceImplementation provided = _provided.get(id);
if (provided == null) {
for (Scope parent : _parents) {
- T impl = parent.retrieveInterfaceImplementation(aInterface, aType);
+ T impl = parent.getInterfaceImplementation(aInterface, aType);
if ( impl != null ) {
return impl;
}
* @param aType Type of implementation that is expected.
* @return Retrieved interface.
*/
- <T> T retrieveInterfaceImplementation(ProvidedInterface aProvided, Class<T> aType );
+ <T> T getInterfaceImplementation(ProvidedInterface aProvided, Class<T> aType );
/**
* Gets the runtime for a component.
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+import org.wamblee.system.core.DefaultScope;
+import org.wamblee.system.core.ProvidedInterface;
+import org.wamblee.system.core.Scope;
+import org.wamblee.test.EventTracker;
+
+import junit.framework.TestCase;
+
+public class AdapterTestCase extends TestCase {
+
+ protected Scope _scope;
+ static EventTracker<String> EVENT_TRACKER;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ EVENT_TRACKER = new EventTracker<String>();
+ _scope = new DefaultScope(new ProvidedInterface[0]);
+ }
+
+}
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+import org.wamblee.system.core.Component;
+import org.wamblee.system.core.Container;
+import org.wamblee.system.core.DefaultScope;
+import org.wamblee.system.core.ProvidedInterface;
+import org.wamblee.system.core.RequiredInterface;
+import org.wamblee.system.core.Scope;
+import org.wamblee.test.AssertionUtils;
+
+import junit.framework.TestCase;
+
+public class ClassAdapterTest extends AdapterTestCase {
+
+
+ public void testSimpleConstructorInjection() {
+ ClassConfiguration x1Config = new ClassConfiguration(X1.class);
+ x1Config.getConstructorConfig().getParameters().setValue(0, "hello");
+ ClassConfiguration x4Config = new ClassConfiguration(X4.class);
+
+ ClassAdapter x1Adapter = new ClassAdapter("x1", x1Config);
+ ClassAdapter x4Adapter = new ClassAdapter("x4", x4Config);
+
+ Container container = new Container("top", new Component[] {
+ x1Adapter, x4Adapter
+ }, new ProvidedInterface[0], new RequiredInterface[0]);
+
+ Scope scope = container.start();
+ 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);
+ }
+}
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.wamblee.system.core.DefaultProvidedInterface;
+import org.wamblee.system.core.DefaultScope;
+import org.wamblee.system.core.ProvidedInterface;
+import org.wamblee.system.core.RequiredInterface;
+import org.wamblee.system.core.Scope;
+import org.wamblee.test.AssertionUtils;
+import org.wamblee.test.EventTracker;
+
+public class ClassConfigurationTest extends AdapterTestCase {
+
+
+ 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();
+
+ assertEquals(1, required.size());
+ assertFalse(required.get(0).isOptional());
+
+ required.get(0).setProvider(provided);
+
+ provided.publish("hello", _scope);
+ config.create(_scope);
+
+ AssertionUtils.assertEquals(new String[] { "x1(hello)" }, AdapterTestCase.EVENT_TRACKER
+ .getEvents(Thread.currentThread()).toArray());
+ }
+}
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+import java.util.List;
+
+import org.wamblee.system.core.DefaultProvidedInterface;
+import org.wamblee.system.core.DefaultScope;
+import org.wamblee.system.core.ProvidedInterface;
+import org.wamblee.system.core.RequiredInterface;
+import org.wamblee.system.core.Scope;
+import org.wamblee.system.core.SystemAssemblyException;
+import org.wamblee.test.AssertionUtils;
+import org.wamblee.test.EventTracker;
+
+import junit.framework.TestCase;
+
+public class ConstructorConfigurationTest extends AdapterTestCase {
+
+ public void testGreedyUnique() {
+ ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
+ .greedy();
+ ProvidedInterface provided = new DefaultProvidedInterface("arg",
+ String.class);
+ List<RequiredInterface> required = config.getRequiredInterfaces();
+
+ assertEquals(1, required.size());
+ assertFalse(required.get(0).isOptional());
+
+ required.get(0).setProvider(provided);
+
+ provided.publish("hello", _scope);
+ config.create(_scope);
+
+ AssertionUtils.assertEquals(new String[] { "x1(hello)" }, AdapterTestCase.EVENT_TRACKER
+ .getEvents(Thread.currentThread()).toArray());
+ }
+
+ public void testGreedyNonUnique() {
+ try {
+ ConstructorConfiguration config = new ConstructorConfiguration(
+ X2.class).greedy();
+ } catch (SystemAssemblyException e) {
+ // e.printStackTrace();
+ return;
+ }
+ fail();
+ }
+
+ public void testSpecificConstructor() {
+ ConstructorConfiguration config = new ConstructorConfiguration(X2.class)
+ .select(String.class);
+ ProvidedInterface provided = new DefaultProvidedInterface("arg",
+ String.class);
+ List<RequiredInterface> required = config.getRequiredInterfaces();
+
+ assertEquals(1, required.size());
+ required.get(0).setProvider(provided);
+
+ provided.publish("hello", _scope);
+ config.create(_scope);
+
+ AssertionUtils.assertEquals(new String[] { "x2(hello)" }, AdapterTestCase.EVENT_TRACKER
+ .getEvents(Thread.currentThread()).toArray());
+ }
+
+ public void testSetValue() {
+ ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
+ .greedy();
+ config.getParameters().setValue(0, "bla");
+
+ config.create(_scope);
+
+ AssertionUtils.assertEquals(new String[] { "x1(bla)" }, AdapterTestCase.EVENT_TRACKER
+ .getEvents(Thread.currentThread()).toArray());
+ }
+
+ public void testOptionalValueProvided() {
+ ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
+ .greedy();
+ config.getParameters().setOptional(0);
+ ProvidedInterface provided = new DefaultProvidedInterface("arg",
+ String.class);
+ List<RequiredInterface> required = config.getRequiredInterfaces();
+
+ assertEquals(1, required.size());
+ required.get(0).setProvider(provided);
+
+ provided.publish("hello", _scope);
+ config.create(_scope);
+
+ AssertionUtils.assertEquals(new String[] { "x1(hello)" }, AdapterTestCase.EVENT_TRACKER
+ .getEvents(Thread.currentThread()).toArray());
+ }
+
+ public void testOptionalValueMissing() {
+ ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
+ .greedy();
+ config.getParameters().setOptional(0);
+ assertTrue(config.getRequiredInterfaces().get(0).isOptional());
+
+ config.create(_scope);
+
+ AssertionUtils.assertEquals(new String[] { "x1(null)" }, AdapterTestCase.EVENT_TRACKER
+ .getEvents(Thread.currentThread()).toArray());
+ }
+
+ public void testIgnoredNonPublic() {
+ ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
+ .greedy();
+ List<RequiredInterface> required = config.getRequiredInterfaces();
+ assertEquals(0, config.getParameters().getTypes().length);
+ }
+
+ public void testNonPublicConstructor() {
+ ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
+ .setNonPublic(true).greedy();
+ ProvidedInterface provided = new DefaultProvidedInterface("arg",
+ String.class);
+ List<RequiredInterface> required = config.getRequiredInterfaces();
+
+ assertEquals(1, required.size());
+ assertFalse(required.get(0).isOptional());
+
+ required.get(0).setProvider(provided);
+
+ provided.publish("hello", _scope);
+ config.create(_scope);
+
+ AssertionUtils.assertEquals(new String[] { "x3(hello)" }, AdapterTestCase.EVENT_TRACKER
+ .getEvents(Thread.currentThread()).toArray());
+ }
+}
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+public class X1 {
+ public X1() {
+ AdapterTestCase.EVENT_TRACKER.eventOccurred("x1()");
+ }
+
+ public X1(String aValue) {
+ AdapterTestCase.EVENT_TRACKER.eventOccurred("x1(" + aValue + ")");
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+public class X2 {
+ public X2(Integer aInteger) {
+ AdapterTestCase.EVENT_TRACKER.eventOccurred("x2(" + aInteger + ")");
+ }
+
+ public X2(String aValue) {
+ AdapterTestCase.EVENT_TRACKER.eventOccurred("x2(" + aValue + ")");
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+public class X3 {
+ public X3() {
+ AdapterTestCase.EVENT_TRACKER.eventOccurred("x3()");
+ }
+
+ protected X3(String aValue) {
+ AdapterTestCase.EVENT_TRACKER.eventOccurred("x3(" + aValue + ")");
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed 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.wamblee.system.adapters;
+
+public class X4 {
+
+ private X1 _x1;
+
+ public X4(X1 aX1) {
+ AdapterTestCase.EVENT_TRACKER.eventOccurred("x4(x1)");
+ _x1 = aX1;
+ }
+
+ public X1 getX1() {
+ return _x1;
+ }
+}
@Override
protected Object doStart(Scope aScope) {
track("start." + getName());
- _string = aScope.retrieveInterfaceImplementation(getRequiredInterfaces()[0].getProvider(), String.class);
- _integer = aScope.retrieveInterfaceImplementation(getRequiredInterfaces()[1].getProvider(), Integer.class);
+ _string = aScope.getInterfaceImplementation(getRequiredInterfaces()[0].getProvider(), String.class);
+ _integer = aScope.getInterfaceImplementation(getRequiredInterfaces()[1].getProvider(), Integer.class);
return _random;
}
@Override
public Object getObject() throws Exception {
- return SpringComponent.SCOPE.get().retrieveInterfaceImplementation(
+ return SpringComponent.SCOPE.get().getInterfaceImplementation(
_required.getProvider(), Object.class);
}
Scope runtime = system.start();
ProvidedInterface[] services = runtime.getProvidedInterfaces();
assertEquals(1, services.length);
- Object service = runtime.retrieveInterfaceImplementation(services[0], Object.class);
+ Object service = runtime.getInterfaceImplementation(services[0], Object.class);
assertTrue(service instanceof HelloService);
assertEquals("Hello world!", ((HelloService) service).say());
system.stop(runtime);
Scope scope = system.start();
ProvidedInterface[] services = scope.getProvidedInterfaces();
- assertEquals("Property Value", scope.retrieveInterfaceImplementation(services[0], HelloService.class).say());
+ assertEquals("Property Value", scope.getInterfaceImplementation(services[0], HelloService.class).say());
}
public void testWithMissingRequirement() {
Scope runtime = system.start(scope);
ProvidedInterface started = runtime.getProvidedInterfaces()[0];
- Object impl = runtime.retrieveInterfaceImplementation(started, BlaService.class);
+ Object impl = runtime.getInterfaceImplementation(started, BlaService.class);
assertNotNull(impl);
assertTrue(impl instanceof BlaService);
assertEquals("ladida", ((BlaService)impl).execute());