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