0c6825e78e5605049ee76b1eb3063ba4155bbbd0
[utils] / system / general / src / test / java / org / wamblee / system / adapters / ObjectAdapterTest.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 java.util.Collections;
19 import java.util.List;
20
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;
29
30 public class ObjectAdapterTest extends AdapterTestCase {
31
32         public void testSetterInjection() {
33             
34             ClassConfiguration x1Config = new ClassConfiguration(X1.class);
35             x1Config.getObjectConfig().getSetterConfig().initAllSetters();
36             x1Config.getConstructorConfig().getParameters().setValue(0, "hello");
37                
38         ClassConfiguration x4Config = new ClassConfiguration(X4.class);
39         x4Config.getObjectConfig().getSetterConfig().initAllSetters();
40         ObjectConfiguration x8Config = new ObjectConfiguration(X8.class);
41         x8Config.getSetterConfig().initAllSetters();
42         
43         X1 x1 = new X1(); 
44         X8 x8 = new X8(x1);
45         
46         ClassAdapter x1Adapter = new ClassAdapter("x1", x1Config);
47         ClassAdapter x4Adapter = new ClassAdapter("x4", x4Config);
48         ObjectAdapter x8Adapter = new ObjectAdapter("x8", x8, x8Config);
49         
50         Container container = new Container("top", new Component[] { 
51                 x1Adapter, x4Adapter, x8Adapter
52         }, new ProvidedInterface[0], new RequiredInterface[0]);
53         
54         EVENT_TRACKER.clear();
55         Scope scope = container.start();
56         AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)", "x8.setX4(x4)" },
57                 EVENT_TRACKER.getEvents(Thread.currentThread()).toArray());
58         
59         Object obj1 = scope.getRuntime(x1Adapter);
60         assertTrue(obj1 instanceof X1); 
61         Object obj4 = scope.getRuntime(x4Adapter);
62         assertTrue(obj4 instanceof X4);
63         Object obj8 = scope.getRuntime(x8Adapter);
64         assertSame(x8, obj8);
65         
66         X4 x4 = (X4) obj4;
67        
68         
69         assertSame(x4, x8.getX4());
70         assertSame(x1, x8.getX1());
71         }
72 }