(no commit message)
[utils] / system / general / src / test / java / org / wamblee / system / adapters / ClassAdapterTest.java
1 /*
2  * Copyright 2005-2010 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.container.Container;
19 import org.wamblee.system.core.Component;
20 import org.wamblee.system.core.ProvidedInterface;
21 import org.wamblee.system.core.RequiredInterface;
22 import org.wamblee.system.core.Scope;
23
24 import org.wamblee.test.AssertionUtils;
25
26 /**
27  * 
28  * @author $author$
29  * @version $Revision$
30  */
31 public class ClassAdapterTest extends AdapterTestCase {
32     public void testSimpleConstructorInjection() {
33         ClassConfiguration x1Config = new ClassConfiguration(X1.class);
34         x1Config.getObjectConfig().getSetterConfig().initAllSetters();
35         x1Config.getConstructorConfig().getParameters().setValue(0, "hello");
36
37         ClassConfiguration x4Config = new ClassConfiguration(X4.class);
38         x4Config.getObjectConfig().getSetterConfig().initAllSetters();
39
40         ClassAdapter x1Adapter = new ClassAdapter("x1", x1Config);
41         ClassAdapter x4Adapter = new ClassAdapter("x4", x4Config);
42
43         Container container = new Container("top", new Component[] { x1Adapter,
44             x4Adapter }, new ProvidedInterface[0], new RequiredInterface[0]);
45
46         Scope scope = container.start();
47         AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)" },
48             getEventTracker().getEvents(Thread.currentThread()).toArray());
49
50         Object obj1 = scope.getRuntime(x1Adapter);
51         assertTrue(obj1 instanceof X1);
52
53         Object obj4 = scope.getRuntime(x4Adapter);
54         assertTrue(obj4 instanceof X4);
55
56         X1 x1 = (X1) obj1;
57         X4 x4 = (X4) obj4;
58
59         assertSame(x1, x4.getX1());
60     }
61
62     public void testConstructorAndSetterInjection() {
63         ClassConfiguration x1Config = new ClassConfiguration(X1.class);
64         x1Config.getObjectConfig().getSetterConfig().initAllSetters();
65         x1Config.getConstructorConfig().getParameters().setValue(0, "hello");
66
67         ClassConfiguration x4Config = new ClassConfiguration(X4.class);
68         x4Config.getObjectConfig().getSetterConfig().initAllSetters();
69
70         ClassConfiguration x8Config = new ClassConfiguration(X8.class);
71         x8Config.getObjectConfig().getSetterConfig().initAllSetters();
72
73         ClassAdapter x1Adapter = new ClassAdapter("x1", x1Config);
74         ClassAdapter x4Adapter = new ClassAdapter("x4", x4Config);
75         ClassAdapter x8Adapter = new ClassAdapter("x8", x8Config);
76
77         Container container = new Container("top", new Component[] { x1Adapter,
78             x4Adapter, x8Adapter }, new ProvidedInterface[0],
79             new RequiredInterface[0]);
80
81         Scope scope = container.start();
82         AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)",
83             "x8(x1)", "x8.setX4(x4)" }, getEventTracker().getEvents(
84             Thread.currentThread()).toArray());
85
86         Object obj1 = scope.getRuntime(x1Adapter);
87         assertTrue(obj1 instanceof X1);
88
89         Object obj4 = scope.getRuntime(x4Adapter);
90         assertTrue(obj4 instanceof X4);
91
92         Object obj8 = scope.getRuntime(x8Adapter);
93
94         X1 x1 = (X1) obj1;
95         X4 x4 = (X4) obj4;
96         X8 x8 = (X8) obj8;
97
98         assertSame(x4, x8.getX4());
99         assertSame(x1, x8.getX1());
100     }
101 }