5e4f868ffda5a255224e87a9cdcd7b000c8da4dd
[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 import org.wamblee.test.AssertionUtils;
24
25 public class ClassAdapterTest extends AdapterTestCase {
26
27
28         public void testSimpleConstructorInjection() { 
29                 ClassConfiguration x1Config = new ClassConfiguration(X1.class);
30                 x1Config.getObjectConfig().getSetterConfig().initAllSetters();
31                 x1Config.getConstructorConfig().getParameters().setValue(0, "hello");
32         ClassConfiguration x4Config = new ClassConfiguration(X4.class);
33         x4Config.getObjectConfig().getSetterConfig().initAllSetters();
34         
35         ClassAdapter x1Adapter = new ClassAdapter("x1", x1Config);
36         ClassAdapter x4Adapter = new ClassAdapter("x4", x4Config);
37         
38         Container container = new Container("top", new Component[] { 
39                         x1Adapter, x4Adapter
40             }, new ProvidedInterface[0], new RequiredInterface[0]);
41         
42         Scope scope = container.start();
43         AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)" },
44                         EVENT_TRACKER.getEvents(Thread.currentThread()).toArray());
45         
46         Object obj1 = scope.getRuntime(x1Adapter);
47         assertTrue(obj1 instanceof X1); 
48         Object obj4 = scope.getRuntime(x4Adapter);
49         assertTrue(obj4 instanceof X4);
50         
51         X1 x1 = (X1) obj1;
52         X4 x4 = (X4) obj4;
53        
54         assertSame(x1, x4.getX1());
55         }
56         
57         public void testConstructorAndSetterInjection() {
58             ClassConfiguration x1Config = new ClassConfiguration(X1.class);
59             x1Config.getObjectConfig().getSetterConfig().initAllSetters();
60         x1Config.getConstructorConfig().getParameters().setValue(0, "hello");
61         ClassConfiguration x4Config = new ClassConfiguration(X4.class);
62         x4Config.getObjectConfig().getSetterConfig().initAllSetters();
63         ClassConfiguration x8Config = new ClassConfiguration(X8.class);
64         x8Config.getObjectConfig().getSetterConfig().initAllSetters();
65         
66         ClassAdapter x1Adapter = new ClassAdapter("x1", x1Config);
67         ClassAdapter x4Adapter = new ClassAdapter("x4", x4Config);
68         ClassAdapter x8Adapter = new ClassAdapter("x8", x8Config);
69         
70         Container container = new Container("top", new Component[] { 
71                 x1Adapter, x4Adapter, x8Adapter
72         }, new ProvidedInterface[0], new RequiredInterface[0]);
73         
74         Scope scope = container.start();
75         AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)", "x8(x1)", "x8.setX4(x4)" },
76                 EVENT_TRACKER.getEvents(Thread.currentThread()).toArray());
77         
78         Object obj1 = scope.getRuntime(x1Adapter);
79         assertTrue(obj1 instanceof X1); 
80         Object obj4 = scope.getRuntime(x4Adapter);
81         assertTrue(obj4 instanceof X4);
82         Object obj8 = scope.getRuntime(x8Adapter); 
83         
84         X1 x1 = (X1) obj1;
85         X4 x4 = (X4) obj4;
86         X8 x8 = (X8) obj8; 
87         
88         assertSame(x4, x8.getX4());
89         assertSame(x1, x8.getX1());
90         }
91 }