2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.system.adapters;
18 import java.util.List;
20 import org.wamblee.system.core.DefaultProvidedInterface;
21 import org.wamblee.system.core.ProvidedInterface;
22 import org.wamblee.system.core.RequiredInterface;
23 import org.wamblee.system.core.Scope;
24 import org.wamblee.system.core.SystemAssemblyException;
25 import org.wamblee.test.AssertionUtils;
32 public class ConstructorConfigurationTest extends AdapterTestCase {
33 public void testGreedyUnique() {
34 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
36 ProvidedInterface provided = new DefaultProvidedInterface("arg",
38 List<RequiredInterface> required = config.getRequiredInterfaces();
40 assertEquals(1, required.size());
41 assertFalse(required.get(0).isOptional());
43 required.get(0).setProvider(provided);
45 Scope scope = getScope();
46 scope.publishInterface(provided, "hello");
49 AssertionUtils.assertEquals(new String[] { "x1(hello)" },
50 AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
54 public void testGreedyNonUnique() {
56 ConstructorConfiguration config = new ConstructorConfiguration(
58 ignoredVariable(config);
59 } catch (SystemAssemblyException e) {
60 // e.printStackTrace();
67 private static void ignoredVariable(Object aObject) {
71 public void testSpecificConstructor() {
72 ConstructorConfiguration config = new ConstructorConfiguration(X2.class)
73 .select(String.class);
74 ProvidedInterface provided = new DefaultProvidedInterface("arg",
76 List<RequiredInterface> required = config.getRequiredInterfaces();
78 assertEquals(1, required.size());
79 required.get(0).setProvider(provided);
81 Scope scope = getScope();
82 scope.publishInterface(provided, "hello");
85 AssertionUtils.assertEquals(new String[] { "x2(hello)" },
86 AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
90 public void testSetValue() {
91 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
93 config.getParameters().setValue(0, "bla");
95 Scope scope = getScope();
98 AssertionUtils.assertEquals(new String[] { "x1(bla)" }, AdapterTestCase
99 .getEventTracker().getEvents(Thread.currentThread()).toArray());
102 public void testOptionalValueProvided() {
103 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
105 config.getParameters().setOptional(0);
107 ProvidedInterface provided = new DefaultProvidedInterface("arg",
109 List<RequiredInterface> required = config.getRequiredInterfaces();
111 assertEquals(1, required.size());
112 required.get(0).setProvider(provided);
114 Scope scope = getScope();
115 scope.publishInterface(provided, "hello");
116 config.create(scope);
118 AssertionUtils.assertEquals(new String[] { "x1(hello)" },
119 AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
123 public void testOptionalValueMissing() {
124 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
126 config.getParameters().setOptional(0);
127 assertTrue(config.getRequiredInterfaces().get(0).isOptional());
129 Scope scope = getScope();
130 config.create(scope);
132 AssertionUtils.assertEquals(new String[] { "x1(null)" },
133 AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
137 public void testIgnoredNonPublic() {
138 ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
140 assertEquals(0, config.getParameters().getTypes().length);
143 public void testNonPublicConstructor() {
144 ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
145 .setNonPublic(true).greedy();
146 ProvidedInterface provided = new DefaultProvidedInterface("arg",
148 List<RequiredInterface> required = config.getRequiredInterfaces();
150 assertEquals(1, required.size());
151 assertFalse(required.get(0).isOptional());
153 required.get(0).setProvider(provided);
155 Scope scope = getScope();
156 scope.publishInterface(provided, "hello");
157 config.create(scope);
159 AssertionUtils.assertEquals(new String[] { "x3(hello)" },
160 AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())