checkstyle
[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         } catch (SystemAssemblyException e) {
59             // e.printStackTrace();
60             return;
61         }
62
63         fail();
64     }
65
66     public void testSpecificConstructor() {
67         ConstructorConfiguration config = new ConstructorConfiguration(X2.class)
68             .select(String.class);
69         ProvidedInterface provided = new DefaultProvidedInterface("arg",
70             String.class);
71         List<RequiredInterface> required = config.getRequiredInterfaces();
72
73         assertEquals(1, required.size());
74         required.get(0).setProvider(provided);
75
76         Scope scope = getScope();
77         scope.publishInterface(provided, "hello");
78         config.create(scope);
79
80         AssertionUtils.assertEquals(new String[] { "x2(hello)" },
81             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
82                 .toArray());
83     }
84
85     public void testSetValue() {
86         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
87             .greedy();
88         config.getParameters().setValue(0, "bla");
89
90         Scope scope = getScope();
91         config.create(scope);
92
93         AssertionUtils.assertEquals(new String[] { "x1(bla)" },
94             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
95                 .toArray());
96     }
97
98     public void testOptionalValueProvided() {
99         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
100             .greedy();
101         config.getParameters().setOptional(0);
102
103         ProvidedInterface provided = new DefaultProvidedInterface("arg",
104             String.class);
105         List<RequiredInterface> required = config.getRequiredInterfaces();
106
107         assertEquals(1, required.size());
108         required.get(0).setProvider(provided);
109
110         Scope scope = getScope();
111         scope.publishInterface(provided, "hello");
112         config.create(scope);
113
114         AssertionUtils.assertEquals(new String[] { "x1(hello)" },
115             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
116                 .toArray());
117     }
118
119     public void testOptionalValueMissing() {
120         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
121             .greedy();
122         config.getParameters().setOptional(0);
123         assertTrue(config.getRequiredInterfaces().get(0).isOptional());
124
125         Scope scope = getScope();
126         config.create(scope);
127
128         AssertionUtils.assertEquals(new String[] { "x1(null)" },
129             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
130                 .toArray());
131     }
132
133     public void testIgnoredNonPublic() {
134         ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
135             .greedy();
136         List<RequiredInterface> required = config.getRequiredInterfaces();
137         assertEquals(0, config.getParameters().getTypes().length);
138     }
139
140     public void testNonPublicConstructor() {
141         ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
142             .setNonPublic(true).greedy();
143         ProvidedInterface provided = new DefaultProvidedInterface("arg",
144             String.class);
145         List<RequiredInterface> required = config.getRequiredInterfaces();
146
147         assertEquals(1, required.size());
148         assertFalse(required.get(0).isOptional());
149
150         required.get(0).setProvider(provided);
151
152         Scope scope = getScope();
153         scope.publishInterface(provided, "hello");
154         config.create(scope);
155
156         AssertionUtils.assertEquals(new String[] { "x3(hello)" },
157             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
158                 .toArray());
159     }
160 }