(no commit message)
[utils] / system / general / src / test / java / org / wamblee / system / adapters / ConstructorConfigurationTest.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.system.adapters;
17
18 import java.util.List;
19
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;
26
27 /**
28  * 
29  * @author $author$
30  * @version $Revision$
31  */
32 public class ConstructorConfigurationTest extends AdapterTestCase {
33     public void testGreedyUnique() {
34         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
35             .greedy();
36         ProvidedInterface provided = new DefaultProvidedInterface("arg",
37             String.class);
38         List<RequiredInterface> required = config.getRequiredInterfaces();
39
40         assertEquals(1, required.size());
41         assertFalse(required.get(0).isOptional());
42
43         required.get(0).setProvider(provided);
44
45         Scope scope = getScope();
46         scope.publishInterface(provided, "hello");
47         config.create(scope);
48
49         AssertionUtils.assertEquals(new String[] { "x1(hello)" },
50             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
51                 .toArray());
52     }
53
54     public void testGreedyNonUnique() {
55         try {
56             ConstructorConfiguration config = new ConstructorConfiguration(
57                 X2.class).greedy();
58             ignoredVariable(config);
59         } catch (SystemAssemblyException e) {
60             // e.printStackTrace();
61             return;
62         }
63
64         fail();
65     }
66
67     private static void ignoredVariable(Object aObject) {
68         // for findbugs.
69     }
70
71     public void testSpecificConstructor() {
72         ConstructorConfiguration config = new ConstructorConfiguration(X2.class)
73             .select(String.class);
74         ProvidedInterface provided = new DefaultProvidedInterface("arg",
75             String.class);
76         List<RequiredInterface> required = config.getRequiredInterfaces();
77
78         assertEquals(1, required.size());
79         required.get(0).setProvider(provided);
80
81         Scope scope = getScope();
82         scope.publishInterface(provided, "hello");
83         config.create(scope);
84
85         AssertionUtils.assertEquals(new String[] { "x2(hello)" },
86             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
87                 .toArray());
88     }
89
90     public void testSetValue() {
91         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
92             .greedy();
93         config.getParameters().setValue(0, "bla");
94
95         Scope scope = getScope();
96         config.create(scope);
97
98         AssertionUtils.assertEquals(new String[] { "x1(bla)" }, AdapterTestCase
99             .getEventTracker().getEvents(Thread.currentThread()).toArray());
100     }
101
102     public void testOptionalValueProvided() {
103         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
104             .greedy();
105         config.getParameters().setOptional(0);
106
107         ProvidedInterface provided = new DefaultProvidedInterface("arg",
108             String.class);
109         List<RequiredInterface> required = config.getRequiredInterfaces();
110
111         assertEquals(1, required.size());
112         required.get(0).setProvider(provided);
113
114         Scope scope = getScope();
115         scope.publishInterface(provided, "hello");
116         config.create(scope);
117
118         AssertionUtils.assertEquals(new String[] { "x1(hello)" },
119             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
120                 .toArray());
121     }
122
123     public void testOptionalValueMissing() {
124         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
125             .greedy();
126         config.getParameters().setOptional(0);
127         assertTrue(config.getRequiredInterfaces().get(0).isOptional());
128
129         Scope scope = getScope();
130         config.create(scope);
131
132         AssertionUtils.assertEquals(new String[] { "x1(null)" },
133             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
134                 .toArray());
135     }
136
137     public void testIgnoredNonPublic() {
138         ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
139             .greedy();
140         assertEquals(0, config.getParameters().getTypes().length);
141     }
142
143     public void testNonPublicConstructor() {
144         ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
145             .setNonPublic(true).greedy();
146         ProvidedInterface provided = new DefaultProvidedInterface("arg",
147             String.class);
148         List<RequiredInterface> required = config.getRequiredInterfaces();
149
150         assertEquals(1, required.size());
151         assertFalse(required.get(0).isOptional());
152
153         required.get(0).setProvider(provided);
154
155         Scope scope = getScope();
156         scope.publishInterface(provided, "hello");
157         config.create(scope);
158
159         AssertionUtils.assertEquals(new String[] { "x3(hello)" },
160             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
161                 .toArray());
162     }
163 }