23c3bd9cbd3c265652a9be2912562fd898f8d8c1
[utils] / system / general / src / test / java / org / wamblee / system / adapters / ObjectAdapterTest.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.DefaultProvidedInterface;
21 import org.wamblee.system.core.ProvidedInterface;
22 import org.wamblee.system.core.RequiredInterface;
23 import org.wamblee.system.core.RequiredInterfaceComparator;
24 import org.wamblee.system.core.Scope;
25
26 import org.wamblee.test.AssertionUtils;
27
28 import java.util.Collections;
29 import java.util.List;
30
31 /**
32  * 
33  * @author $author$
34  * @version $Revision$
35  */
36 public class ObjectAdapterTest extends AdapterTestCase {
37     public void testSetterInjection() {
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         ObjectConfiguration x8Config = new ObjectConfiguration(X8.class);
46         x8Config.getSetterConfig().initAllSetters();
47
48         X1 x1 = new X1();
49         X8 x8 = new X8(x1);
50
51         ClassAdapter x1Adapter = new ClassAdapter("x1", x1Config);
52         ClassAdapter x4Adapter = new ClassAdapter("x4", x4Config);
53         ObjectAdapter x8Adapter = new ObjectAdapter("x8", x8, x8Config);
54
55         Container container = new Container("top", new Component[] { x1Adapter,
56             x4Adapter, x8Adapter }, new ProvidedInterface[0],
57             new RequiredInterface[0]);
58
59         getEventTracker().clear();
60
61         Scope scope = container.start();
62         AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)",
63             "x8.setX4(x4)" }, getEventTracker().getEvents(Thread.currentThread())
64             .toArray());
65
66         Object obj1 = scope.getRuntime(x1Adapter);
67         assertTrue(obj1 instanceof X1);
68
69         Object obj4 = scope.getRuntime(x4Adapter);
70         assertTrue(obj4 instanceof X4);
71
72         Object obj8 = scope.getRuntime(x8Adapter);
73         assertSame(x8, obj8);
74
75         X4 x4 = (X4) obj4;
76
77         assertSame(x4, x8.getX4());
78         assertSame(x1, x8.getX1());
79     }
80 }