ee57526f3aa1db0c2a66ea3cb9f25a2bb3469697
[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)" },
99             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
100                 .toArray());
101     }
102
103     public void testOptionalValueProvided() {
104         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
105             .greedy();
106         config.getParameters().setOptional(0);
107
108         ProvidedInterface provided = new DefaultProvidedInterface("arg",
109             String.class);
110         List<RequiredInterface> required = config.getRequiredInterfaces();
111
112         assertEquals(1, required.size());
113         required.get(0).setProvider(provided);
114
115         Scope scope = getScope();
116         scope.publishInterface(provided, "hello");
117         config.create(scope);
118
119         AssertionUtils.assertEquals(new String[] { "x1(hello)" },
120             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
121                 .toArray());
122     }
123
124     public void testOptionalValueMissing() {
125         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
126             .greedy();
127         config.getParameters().setOptional(0);
128         assertTrue(config.getRequiredInterfaces().get(0).isOptional());
129
130         Scope scope = getScope();
131         config.create(scope);
132
133         AssertionUtils.assertEquals(new String[] { "x1(null)" },
134             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
135                 .toArray());
136     }
137
138     public void testIgnoredNonPublic() {
139         ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
140             .greedy();
141         assertEquals(0, config.getParameters().getTypes().length);
142     }
143
144     public void testNonPublicConstructor() {
145         ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
146             .setNonPublic(true).greedy();
147         ProvidedInterface provided = new DefaultProvidedInterface("arg",
148             String.class);
149         List<RequiredInterface> required = config.getRequiredInterfaces();
150
151         assertEquals(1, required.size());
152         assertFalse(required.get(0).isOptional());
153
154         required.get(0).setProvider(provided);
155
156         Scope scope = getScope();
157         scope.publishInterface(provided, "hello");
158         config.create(scope);
159
160         AssertionUtils.assertEquals(new String[] { "x3(hello)" },
161             AdapterTestCase.getEventTracker().getEvents(Thread.currentThread())
162                 .toArray());
163     }
164 }