(no commit message)
[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 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.SystemAssemblyException;
24 import org.wamblee.test.AssertionUtils;
25
26 public class ConstructorConfigurationTest extends AdapterTestCase {
27
28         public void testGreedyUnique() {
29                 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
30                                 .greedy();
31                 ProvidedInterface provided = new DefaultProvidedInterface("arg",
32                                 String.class);
33                 List<RequiredInterface> required = config.getRequiredInterfaces();
34
35                 assertEquals(1, required.size());
36                 assertFalse(required.get(0).isOptional());
37
38                 required.get(0).setProvider(provided);
39
40                 provided.publish("hello", _scope);
41                 config.create(_scope);
42
43                 AssertionUtils.assertEquals(new String[] { "x1(hello)" }, AdapterTestCase.EVENT_TRACKER
44                                 .getEvents(Thread.currentThread()).toArray());
45         }
46
47         public void testGreedyNonUnique() {
48                 try {
49                         ConstructorConfiguration config = new ConstructorConfiguration(
50                                         X2.class).greedy();
51                 } catch (SystemAssemblyException e) {
52                         // e.printStackTrace();
53                         return;
54                 }
55                 fail();
56         }
57
58         public void testSpecificConstructor() {
59                 ConstructorConfiguration config = new ConstructorConfiguration(X2.class)
60                                 .select(String.class);
61                 ProvidedInterface provided = new DefaultProvidedInterface("arg",
62                                 String.class);
63                 List<RequiredInterface> required = config.getRequiredInterfaces();
64
65                 assertEquals(1, required.size());
66                 required.get(0).setProvider(provided);
67
68                 provided.publish("hello", _scope);
69                 config.create(_scope);
70
71                 AssertionUtils.assertEquals(new String[] { "x2(hello)" }, AdapterTestCase.EVENT_TRACKER
72                                 .getEvents(Thread.currentThread()).toArray());
73         }
74
75         public void testSetValue() {
76                 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
77                                 .greedy();
78                 config.getParameters().setValue(0, "bla");
79
80                 config.create(_scope);
81
82                 AssertionUtils.assertEquals(new String[] { "x1(bla)" }, AdapterTestCase.EVENT_TRACKER
83                                 .getEvents(Thread.currentThread()).toArray());
84         }
85
86         public void testOptionalValueProvided() {
87                 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
88                                 .greedy();
89                 config.getParameters().setOptional(0);
90                 ProvidedInterface provided = new DefaultProvidedInterface("arg",
91                                 String.class);
92                 List<RequiredInterface> required = config.getRequiredInterfaces();
93
94                 assertEquals(1, required.size());
95                 required.get(0).setProvider(provided);
96
97                 provided.publish("hello", _scope);
98                 config.create(_scope);
99
100                 AssertionUtils.assertEquals(new String[] { "x1(hello)" }, AdapterTestCase.EVENT_TRACKER
101                                 .getEvents(Thread.currentThread()).toArray());
102         }
103
104         public void testOptionalValueMissing() {
105                 ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
106                                 .greedy();
107                 config.getParameters().setOptional(0);
108                 assertTrue(config.getRequiredInterfaces().get(0).isOptional());
109
110                 config.create(_scope);
111
112                 AssertionUtils.assertEquals(new String[] { "x1(null)" }, AdapterTestCase.EVENT_TRACKER
113                                 .getEvents(Thread.currentThread()).toArray());
114         }
115
116         public void testIgnoredNonPublic() {
117                 ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
118                                 .greedy();
119                 List<RequiredInterface> required = config.getRequiredInterfaces();
120                 assertEquals(0, config.getParameters().getTypes().length);
121         }
122
123         public void testNonPublicConstructor() {
124                 ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
125                                 .setNonPublic(true).greedy();
126                 ProvidedInterface provided = new DefaultProvidedInterface("arg",
127                                 String.class);
128                 List<RequiredInterface> required = config.getRequiredInterfaces();
129
130                 assertEquals(1, required.size());
131                 assertFalse(required.get(0).isOptional());
132
133                 required.get(0).setProvider(provided);
134
135                 provided.publish("hello", _scope);
136                 config.create(_scope);
137
138                 AssertionUtils.assertEquals(new String[] { "x3(hello)" }, AdapterTestCase.EVENT_TRACKER
139                                 .getEvents(Thread.currentThread()).toArray());
140         }
141 }