2 * Copyright 2008 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.system.adapters;
18 import java.lang.reflect.Method;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.List;
24 import javax.sql.DataSource;
26 import org.wamblee.system.core.DefaultProvidedInterface;
27 import org.wamblee.system.core.ProvidedInterface;
28 import org.wamblee.system.core.RequiredInterface;
29 import org.wamblee.system.core.RequiredInterfaceComparator;
30 import org.wamblee.test.AssertionUtils;
32 public class SetterConfigurationTest extends AdapterTestCase {
34 public void testOneSetter() {
35 SetterConfiguration config = new SetterConfiguration(X5.class);
36 config.initAllSetters();
37 List<RequiredInterface> required = config.getRequiredInterfaces();
38 assertEquals(1, required.size());
39 assertEquals("setValue.0", required.get(0).getName());
41 ProvidedInterface provided = new DefaultProvidedInterface("janse",
43 required.get(0).setProvider(provided);
44 _scope.publishInterface(provided, "hello");
47 assertNull(obj.getValue());
48 config.inject(_scope, obj);
49 assertEquals("hello", obj.getValue());
52 public void testPrivateSetter() {
53 SetterConfiguration config = new SetterConfiguration(X5.class);
54 config.setNonPublic(true);
55 config.initAllSetters();
56 List<RequiredInterface> required = new ArrayList<RequiredInterface>(
57 config.getRequiredInterfaces());
58 Collections.sort(required, new Comparator<RequiredInterface>() {
60 public int compare(RequiredInterface aO1, RequiredInterface aO2) {
61 return aO1.getName().compareTo(aO2.getName());
64 assertEquals(2, required.size());
65 assertEquals("setValue.0", required.get(0).getName());
66 assertEquals("setXyz.0", required.get(1).getName());
68 ProvidedInterface providedString = new DefaultProvidedInterface(
69 "janse", String.class);
70 assertTrue(required.get(0).implementedBy(providedString));
71 required.get(0).setProvider(providedString);
72 _scope.publishInterface(providedString, "hello");
74 ProvidedInterface providedInt = new DefaultProvidedInterface("xxx",
76 assertTrue(required.get(1).implementedBy(providedInt));
77 required.get(1).setProvider(providedInt);
78 _scope.publishInterface(providedInt, 100);
81 assertNull(obj.getValue());
82 assertNull(obj.getXyz());
83 config.inject(_scope, obj);
84 assertEquals("hello", obj.getValue());
85 assertEquals(100, obj.getXyz().intValue());
88 public void testInheritance() {
89 SetterConfiguration config = new SetterConfiguration(X9.class);
90 config.setNonPublic(true);
91 config.initAllSetters();
92 List<RequiredInterface> required = new ArrayList<RequiredInterface>(
93 config.getRequiredInterfaces());
94 Collections.sort(required, new Comparator<RequiredInterface>() {
96 public int compare(RequiredInterface aO1, RequiredInterface aO2) {
97 return aO1.getName().compareTo(aO2.getName());
100 assertEquals(3, required.size());
101 assertEquals("setFlag.0", required.get(0).getName());
102 assertEquals("setValue.0", required.get(1).getName());
103 assertEquals("setXyz.0", required.get(2).getName());
105 ProvidedInterface providedBoolean = new DefaultProvidedInterface(
106 "janse", Boolean.class);
107 assertTrue(required.get(0).implementedBy(providedBoolean));
108 required.get(0).setProvider(providedBoolean);
109 _scope.publishInterface(providedBoolean, true);
111 ProvidedInterface providedString = new DefaultProvidedInterface(
112 "janse", String.class);
113 assertTrue(required.get(1).implementedBy(providedString));
114 required.get(1).setProvider(providedString);
115 _scope.publishInterface(providedString, "hello");
117 ProvidedInterface providedInt = new DefaultProvidedInterface("xxx",
119 assertTrue(required.get(2).implementedBy(providedInt));
120 required.get(2).setProvider(providedInt);
121 _scope.publishInterface(providedInt, 100);
124 assertNull(obj.getValue());
125 assertNull(obj.getXyz());
126 assertFalse(obj.isFlag());
127 config.inject(_scope, obj);
128 assertEquals("hello", obj.getValue());
129 assertEquals(100, obj.getXyz().intValue());
130 assertTrue(obj.isFlag());
133 public void testMultipleSetters() {
134 SetterConfiguration config = new SetterConfiguration(X6.class).initAllSetters();
135 List<RequiredInterface> required = config.getRequiredInterfaces();
136 Collections.sort(required, new RequiredInterfaceComparator());
137 assertEquals(2, required.size());
138 assertEquals("setHost.0", required.get(0).getName());
139 assertEquals("setPort.0", required.get(1).getName());
141 ProvidedInterface provided0 = new DefaultProvidedInterface("janse",
143 required.get(0).setProvider(provided0);
144 _scope.publishInterface(provided0, "hello");
146 ProvidedInterface provided1 = new DefaultProvidedInterface("port",
148 required.get(1).setProvider(provided1);
149 _scope.publishInterface(provided1, 10);
152 assertNull(obj.getHost());
153 assertNull(obj.getPort());
155 config.inject(_scope, obj);
156 assertEquals("hello", obj.getHost());
157 assertEquals(10, obj.getPort().intValue());
160 public void testInvokeWrongType() {
161 final SetterConfiguration config = new SetterConfiguration(X5.class).initAllSetters();
162 List<RequiredInterface> required = config.getRequiredInterfaces();
163 assertEquals(1, required.size());
164 assertEquals("setValue.0", required.get(0).getName());
166 ProvidedInterface provided = new DefaultProvidedInterface("janse",
168 required.get(0).setProvider(provided);
169 _scope.publishInterface(provided, "hello");
171 final X6 obj = new X6();
172 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
174 public void run() throws Exception {
175 config.inject(_scope, obj);
177 }, IllegalArgumentException.class);
180 public void testSetExplicitValue() {
181 SetterConfiguration config = new SetterConfiguration(X5.class).initAllSetters();
182 config.values("setValue").setValue(0, "bladibla");
184 List<RequiredInterface> required = config.getRequiredInterfaces();
185 assertEquals(0, required.size());
188 assertNull(obj.getValue());
189 config.inject(_scope, obj);
190 assertEquals("bladibla", obj.getValue());
193 public void testClear() {
194 SetterConfiguration config = new SetterConfiguration(X6.class);
196 List<RequiredInterface> required = config.getRequiredInterfaces();
197 Collections.sort(required, new RequiredInterfaceComparator());
198 assertEquals(0, required.size());
201 assertNull(obj.getHost());
202 assertNull(obj.getPort());
204 config.inject(_scope, obj);
206 assertNull(obj.getHost());
207 assertNull(obj.getPort());
210 public void testAddByName() {
211 SetterConfiguration config = new SetterConfiguration(X6.class);
212 config.clear().add("setHost");
213 List<RequiredInterface> required = config.getRequiredInterfaces();
214 Collections.sort(required, new RequiredInterfaceComparator());
215 assertEquals(1, required.size());
216 assertEquals("setHost.0", required.get(0).getName());
218 ProvidedInterface provided0 = new DefaultProvidedInterface("janse",
220 required.get(0).setProvider(provided0);
221 _scope.publishInterface(provided0, "hello");
224 assertNull(obj.getHost());
225 assertNull(obj.getPort());
227 config.inject(_scope, obj);
228 assertEquals("hello", obj.getHost());
229 assertNull(obj.getPort());
232 public void testAddByType() {
233 SetterConfiguration config = new SetterConfiguration(X6.class);
234 config.clear().addSetter(String.class);
235 List<RequiredInterface> required = config.getRequiredInterfaces();
236 Collections.sort(required, new RequiredInterfaceComparator());
237 assertEquals(1, required.size());
238 assertEquals("setHost.0", required.get(0).getName());
240 ProvidedInterface provided0 = new DefaultProvidedInterface("janse",
242 required.get(0).setProvider(provided0);
243 _scope.publishInterface(provided0, "hello");
246 assertNull(obj.getHost());
247 assertNull(obj.getPort());
249 config.inject(_scope, obj);
250 assertEquals("hello", obj.getHost());
251 assertNull(obj.getPort());
254 public void testAddPrivate() {
256 final SetterConfiguration config = new SetterConfiguration(X5.class);
257 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
259 public void run() throws Exception {
262 }, IllegalArgumentException.class);
264 config.setNonPublic(true);
266 config.add("setXyz");
267 assertEquals(1, config.getRequiredInterfaces().size());
270 public void testAddNonExisting() {
271 final SetterConfiguration config = new SetterConfiguration(X6.class);
274 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
276 public void run() throws Exception {
277 config.add("bladibla");
279 }, IllegalArgumentException.class);
280 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
282 public void run() throws Exception {
283 config.addSetter(DataSource.class);
285 }, IllegalArgumentException.class);
288 public void testAddByTypeNonUnique() {
289 final SetterConfiguration config = new SetterConfiguration(X11.class);
291 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
293 public void run() throws Exception {
294 config.addSetter(String.class);
296 }, IllegalArgumentException.class);
299 public void testRemove() {
300 SetterConfiguration config = new SetterConfiguration(X6.class).initAllSetters();
301 config.remove("setPort");
302 List<RequiredInterface> required = config.getRequiredInterfaces();
303 Collections.sort(required, new RequiredInterfaceComparator());
304 assertEquals(1, required.size());
305 assertEquals("setHost.0", required.get(0).getName());
307 ProvidedInterface provided0 = new DefaultProvidedInterface("janse",
309 required.get(0).setProvider(provided0);
310 _scope.publishInterface(provided0, "hello");
313 assertNull(obj.getHost());
314 assertNull(obj.getPort());
316 config.inject(_scope, obj);
317 assertEquals("hello", obj.getHost());
318 assertNull(obj.getPort());
321 public void testRemoveByMethodObject() throws NoSuchMethodException {
322 SetterConfiguration config = new SetterConfiguration(X6.class).initAllSetters();
323 config.remove(X6.class.getMethod("setPort", Integer.class));
324 List<RequiredInterface> required = config.getRequiredInterfaces();
325 Collections.sort(required, new RequiredInterfaceComparator());
326 assertEquals(1, required.size());
327 assertEquals("setHost.0", required.get(0).getName());
329 ProvidedInterface provided0 = new DefaultProvidedInterface("janse",
331 required.get(0).setProvider(provided0);
332 _scope.publishInterface(provided0, "hello");
335 assertNull(obj.getHost());
336 assertNull(obj.getPort());
338 config.inject(_scope, obj);
339 assertEquals("hello", obj.getHost());
340 assertNull(obj.getPort());
343 public void testRemoveNonExisting() {
344 final SetterConfiguration config = new SetterConfiguration(X6.class);
346 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
348 public void run() throws Exception {
349 config.remove("bladibla");
351 }, IllegalArgumentException.class);
354 public void testOverridingSetters() {
355 SetterConfiguration config = new SetterConfiguration(X10.class).initAllSetters();
356 assertEquals(2, config.getRequiredInterfaces().size());
357 List<Method> methods = config.getSetters();
358 assertEquals(2, methods.size());
359 for (Method method: methods) {
360 assertEquals(X10.class, method.getDeclaringClass());