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