2 * Copyright 2008 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.DefaultScope;
22 import org.wamblee.system.core.ProvidedInterface;
23 import org.wamblee.system.core.RequiredInterface;
24 import org.wamblee.system.core.Scope;
25 import org.wamblee.system.core.SystemAssemblyException;
26 import org.wamblee.test.AssertionUtils;
27 import org.wamblee.test.EventTracker;
29 import junit.framework.TestCase;
31 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 provided.publish("hello", _scope);
46 config.create(_scope);
48 AssertionUtils.assertEquals(new String[] { "x1(hello)" }, AdapterTestCase.EVENT_TRACKER
49 .getEvents(Thread.currentThread()).toArray());
52 public void testGreedyNonUnique() {
54 ConstructorConfiguration config = new ConstructorConfiguration(
56 } catch (SystemAssemblyException e) {
57 // e.printStackTrace();
63 public void testSpecificConstructor() {
64 ConstructorConfiguration config = new ConstructorConfiguration(X2.class)
65 .select(String.class);
66 ProvidedInterface provided = new DefaultProvidedInterface("arg",
68 List<RequiredInterface> required = config.getRequiredInterfaces();
70 assertEquals(1, required.size());
71 required.get(0).setProvider(provided);
73 provided.publish("hello", _scope);
74 config.create(_scope);
76 AssertionUtils.assertEquals(new String[] { "x2(hello)" }, AdapterTestCase.EVENT_TRACKER
77 .getEvents(Thread.currentThread()).toArray());
80 public void testSetValue() {
81 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
83 config.getParameters().setValue(0, "bla");
85 config.create(_scope);
87 AssertionUtils.assertEquals(new String[] { "x1(bla)" }, AdapterTestCase.EVENT_TRACKER
88 .getEvents(Thread.currentThread()).toArray());
91 public void testOptionalValueProvided() {
92 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
94 config.getParameters().setOptional(0);
95 ProvidedInterface provided = new DefaultProvidedInterface("arg",
97 List<RequiredInterface> required = config.getRequiredInterfaces();
99 assertEquals(1, required.size());
100 required.get(0).setProvider(provided);
102 provided.publish("hello", _scope);
103 config.create(_scope);
105 AssertionUtils.assertEquals(new String[] { "x1(hello)" }, AdapterTestCase.EVENT_TRACKER
106 .getEvents(Thread.currentThread()).toArray());
109 public void testOptionalValueMissing() {
110 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
112 config.getParameters().setOptional(0);
113 assertTrue(config.getRequiredInterfaces().get(0).isOptional());
115 config.create(_scope);
117 AssertionUtils.assertEquals(new String[] { "x1(null)" }, AdapterTestCase.EVENT_TRACKER
118 .getEvents(Thread.currentThread()).toArray());
121 public void testIgnoredNonPublic() {
122 ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
124 List<RequiredInterface> required = config.getRequiredInterfaces();
125 assertEquals(0, config.getParameters().getTypes().length);
128 public void testNonPublicConstructor() {
129 ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
130 .setNonPublic(true).greedy();
131 ProvidedInterface provided = new DefaultProvidedInterface("arg",
133 List<RequiredInterface> required = config.getRequiredInterfaces();
135 assertEquals(1, required.size());
136 assertFalse(required.get(0).isOptional());
138 required.get(0).setProvider(provided);
140 provided.publish("hello", _scope);
141 config.create(_scope);
143 AssertionUtils.assertEquals(new String[] { "x3(hello)" }, AdapterTestCase.EVENT_TRACKER
144 .getEvents(Thread.currentThread()).toArray());