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.util.Collections;
19 import java.util.List;
21 import org.wamblee.system.container.Container;
22 import org.wamblee.system.core.Component;
23 import org.wamblee.system.core.DefaultProvidedInterface;
24 import org.wamblee.system.core.ProvidedInterface;
25 import org.wamblee.system.core.RequiredInterface;
26 import org.wamblee.system.core.RequiredInterfaceComparator;
27 import org.wamblee.system.core.Scope;
28 import org.wamblee.test.AssertionUtils;
30 public class ClassAdapterTest extends AdapterTestCase {
33 public void testSimpleConstructorInjection() {
34 ClassConfiguration x1Config = new ClassConfiguration(X1.class);
35 x1Config.getObjectConfig().getSetterConfig().initAllSetters();
36 x1Config.getConstructorConfig().getParameters().setValue(0, "hello");
37 ClassConfiguration x4Config = new ClassConfiguration(X4.class);
38 x4Config.getObjectConfig().getSetterConfig().initAllSetters();
40 ClassAdapter x1Adapter = new ClassAdapter("x1", x1Config);
41 ClassAdapter x4Adapter = new ClassAdapter("x4", x4Config);
43 Container container = new Container("top", new Component[] {
45 }, new ProvidedInterface[0], new RequiredInterface[0]);
47 Scope scope = container.start();
48 AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)" },
49 EVENT_TRACKER.getEvents(Thread.currentThread()).toArray());
51 Object obj1 = scope.getRuntime(x1Adapter);
52 assertTrue(obj1 instanceof X1);
53 Object obj4 = scope.getRuntime(x4Adapter);
54 assertTrue(obj4 instanceof X4);
59 assertSame(x1, x4.getX1());
62 public void testConstructorAndSetterInjection() {
63 ClassConfiguration x1Config = new ClassConfiguration(X1.class);
64 x1Config.getObjectConfig().getSetterConfig().initAllSetters();
65 x1Config.getConstructorConfig().getParameters().setValue(0, "hello");
66 ClassConfiguration x4Config = new ClassConfiguration(X4.class);
67 x4Config.getObjectConfig().getSetterConfig().initAllSetters();
68 ClassConfiguration x8Config = new ClassConfiguration(X8.class);
69 x8Config.getObjectConfig().getSetterConfig().initAllSetters();
71 ClassAdapter x1Adapter = new ClassAdapter("x1", x1Config);
72 ClassAdapter x4Adapter = new ClassAdapter("x4", x4Config);
73 ClassAdapter x8Adapter = new ClassAdapter("x8", x8Config);
75 Container container = new Container("top", new Component[] {
76 x1Adapter, x4Adapter, x8Adapter
77 }, new ProvidedInterface[0], new RequiredInterface[0]);
79 Scope scope = container.start();
80 AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)", "x8(x1)", "x8.setX4(x4)" },
81 EVENT_TRACKER.getEvents(Thread.currentThread()).toArray());
83 Object obj1 = scope.getRuntime(x1Adapter);
84 assertTrue(obj1 instanceof X1);
85 Object obj4 = scope.getRuntime(x4Adapter);
86 assertTrue(obj4 instanceof X4);
87 Object obj8 = scope.getRuntime(x8Adapter);
93 assertSame(x4, x8.getX4());
94 assertSame(x1, x8.getX1());