source code formatting.
[utils] / system / general / src / test / java / org / wamblee / system / adapters / ConstructorConfigurationTest.java
1 /*
2  * Copyright 2008 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 org.wamblee.system.core.DefaultProvidedInterface;
19 import org.wamblee.system.core.ProvidedInterface;
20 import org.wamblee.system.core.RequiredInterface;
21 import org.wamblee.system.core.SystemAssemblyException;
22
23 import org.wamblee.test.AssertionUtils;
24
25 import java.util.List;
26
27
28 /**
29  * DOCUMENT ME!
30  *
31  * @author $author$
32  * @version $Revision$
33   */
34 public class ConstructorConfigurationTest extends AdapterTestCase {
35     /**
36      * DOCUMENT ME!
37      */
38     public void testGreedyUnique() {
39         ConstructorConfiguration config   = new ConstructorConfiguration(X1.class)
40             .greedy();
41         ProvidedInterface        provided = new DefaultProvidedInterface("arg",
42                 String.class);
43         List<RequiredInterface>  required = config.getRequiredInterfaces();
44
45         assertEquals(1, required.size());
46         assertFalse(required.get(0).isOptional());
47
48         required.get(0).setProvider(provided);
49
50         scope.publishInterface(provided, "hello");
51         config.create(scope);
52
53         AssertionUtils.assertEquals(new String[] { "x1(hello)" },
54             AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
55             .toArray());
56     }
57
58     /**
59      * DOCUMENT ME!
60      */
61     public void testGreedyNonUnique() {
62         try {
63             ConstructorConfiguration config = new ConstructorConfiguration(X2.class)
64                 .greedy();
65         } catch (SystemAssemblyException e) {
66             // e.printStackTrace();
67             return;
68         }
69
70         fail();
71     }
72
73     /**
74      * DOCUMENT ME!
75      */
76     public void testSpecificConstructor() {
77         ConstructorConfiguration config   = new ConstructorConfiguration(X2.class)
78             .select(String.class);
79         ProvidedInterface        provided = new DefaultProvidedInterface("arg",
80                 String.class);
81         List<RequiredInterface>  required = config.getRequiredInterfaces();
82
83         assertEquals(1, required.size());
84         required.get(0).setProvider(provided);
85
86         scope.publishInterface(provided, "hello");
87         config.create(scope);
88
89         AssertionUtils.assertEquals(new String[] { "x2(hello)" },
90             AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
91             .toArray());
92     }
93
94     /**
95      * DOCUMENT ME!
96      */
97     public void testSetValue() {
98         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
99             .greedy();
100         config.getParameters().setValue(0, "bla");
101
102         config.create(scope);
103
104         AssertionUtils.assertEquals(new String[] { "x1(bla)" },
105             AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
106             .toArray());
107     }
108
109     /**
110      * DOCUMENT ME!
111      */
112     public void testOptionalValueProvided() {
113         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
114             .greedy();
115         config.getParameters().setOptional(0);
116
117         ProvidedInterface       provided = new DefaultProvidedInterface("arg",
118                 String.class);
119         List<RequiredInterface> required = config.getRequiredInterfaces();
120
121         assertEquals(1, required.size());
122         required.get(0).setProvider(provided);
123
124         scope.publishInterface(provided, "hello");
125         config.create(scope);
126
127         AssertionUtils.assertEquals(new String[] { "x1(hello)" },
128             AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
129             .toArray());
130     }
131
132     /**
133      * DOCUMENT ME!
134      */
135     public void testOptionalValueMissing() {
136         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
137             .greedy();
138         config.getParameters().setOptional(0);
139         assertTrue(config.getRequiredInterfaces().get(0).isOptional());
140
141         config.create(scope);
142
143         AssertionUtils.assertEquals(new String[] { "x1(null)" },
144             AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
145             .toArray());
146     }
147
148     /**
149      * DOCUMENT ME!
150      */
151     public void testIgnoredNonPublic() {
152         ConstructorConfiguration config   = new ConstructorConfiguration(X3.class)
153             .greedy();
154         List<RequiredInterface>  required = config.getRequiredInterfaces();
155         assertEquals(0, config.getParameters().getTypes().length);
156     }
157
158     /**
159      * DOCUMENT ME!
160      */
161     public void testNonPublicConstructor() {
162         ConstructorConfiguration config   = new ConstructorConfiguration(X3.class).setNonPublic(true)
163             .greedy();
164         ProvidedInterface        provided = new DefaultProvidedInterface("arg",
165                 String.class);
166         List<RequiredInterface>  required = config.getRequiredInterfaces();
167
168         assertEquals(1, required.size());
169         assertFalse(required.get(0).isOptional());
170
171         required.get(0).setProvider(provided);
172
173         scope.publishInterface(provided, "hello");
174         config.create(scope);
175
176         AssertionUtils.assertEquals(new String[] { "x3(hello)" },
177             AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
178             .toArray());
179     }
180 }