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.ProvidedInterface;
22 import org.wamblee.system.core.RequiredInterface;
23 import org.wamblee.system.core.SystemAssemblyException;
24 import org.wamblee.test.AssertionUtils;
26 public class ConstructorConfigurationTest extends AdapterTestCase {
28 public void testGreedyUnique() {
29 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
31 ProvidedInterface provided = new DefaultProvidedInterface("arg",
33 List<RequiredInterface> required = config.getRequiredInterfaces();
35 assertEquals(1, required.size());
36 assertFalse(required.get(0).isOptional());
38 required.get(0).setProvider(provided);
40 _scope.publishInterface(provided, "hello");
41 config.create(_scope);
43 AssertionUtils.assertEquals(new String[] { "x1(hello)" }, AdapterTestCase.EVENT_TRACKER
44 .getEvents(Thread.currentThread()).toArray());
47 public void testGreedyNonUnique() {
49 ConstructorConfiguration config = new ConstructorConfiguration(
51 } catch (SystemAssemblyException e) {
52 // e.printStackTrace();
58 public void testSpecificConstructor() {
59 ConstructorConfiguration config = new ConstructorConfiguration(X2.class)
60 .select(String.class);
61 ProvidedInterface provided = new DefaultProvidedInterface("arg",
63 List<RequiredInterface> required = config.getRequiredInterfaces();
65 assertEquals(1, required.size());
66 required.get(0).setProvider(provided);
68 _scope.publishInterface(provided, "hello");
69 config.create(_scope);
71 AssertionUtils.assertEquals(new String[] { "x2(hello)" }, AdapterTestCase.EVENT_TRACKER
72 .getEvents(Thread.currentThread()).toArray());
75 public void testSetValue() {
76 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
78 config.getParameters().setValue(0, "bla");
80 config.create(_scope);
82 AssertionUtils.assertEquals(new String[] { "x1(bla)" }, AdapterTestCase.EVENT_TRACKER
83 .getEvents(Thread.currentThread()).toArray());
86 public void testOptionalValueProvided() {
87 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
89 config.getParameters().setOptional(0);
90 ProvidedInterface provided = new DefaultProvidedInterface("arg",
92 List<RequiredInterface> required = config.getRequiredInterfaces();
94 assertEquals(1, required.size());
95 required.get(0).setProvider(provided);
97 _scope.publishInterface(provided, "hello");
98 config.create(_scope);
100 AssertionUtils.assertEquals(new String[] { "x1(hello)" }, AdapterTestCase.EVENT_TRACKER
101 .getEvents(Thread.currentThread()).toArray());
104 public void testOptionalValueMissing() {
105 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
107 config.getParameters().setOptional(0);
108 assertTrue(config.getRequiredInterfaces().get(0).isOptional());
110 config.create(_scope);
112 AssertionUtils.assertEquals(new String[] { "x1(null)" }, AdapterTestCase.EVENT_TRACKER
113 .getEvents(Thread.currentThread()).toArray());
116 public void testIgnoredNonPublic() {
117 ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
119 List<RequiredInterface> required = config.getRequiredInterfaces();
120 assertEquals(0, config.getParameters().getTypes().length);
123 public void testNonPublicConstructor() {
124 ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
125 .setNonPublic(true).greedy();
126 ProvidedInterface provided = new DefaultProvidedInterface("arg",
128 List<RequiredInterface> required = config.getRequiredInterfaces();
130 assertEquals(1, required.size());
131 assertFalse(required.get(0).isOptional());
133 required.get(0).setProvider(provided);
135 _scope.publishInterface(provided, "hello");
136 config.create(_scope);
138 AssertionUtils.assertEquals(new String[] { "x3(hello)" }, AdapterTestCase.EVENT_TRACKER
139 .getEvents(Thread.currentThread()).toArray());