938a12228763493b6596d13a832e6a7c1bc9e98c
[utils] / system / general / src / test / java / org / wamblee / system / adapters / DefaultContainerTest.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.core.Scope;
19 import org.wamblee.test.AssertionUtils;
20
21 public class DefaultContainerTest extends AdapterTestCase {
22
23     public void testConstructorInjection() {
24         ClassConfiguration x1Config = new ClassConfiguration(X1.class);
25         x1Config.getConstructorConfig().getParameters().setValue(0, "hello");
26         x1Config.getObjectConfig().getSetterConfig().initAllSetters();
27         
28         ClassConfiguration x4Config = new ClassConfiguration(X4.class);
29         x4Config.getObjectConfig().getSetterConfig().initAllSetters();
30
31         DefaultContainer container = new DefaultContainer("top").addComponent(
32                 "x1", x1Config).addComponent("x4", x4Config);
33
34         Scope scope = container.start();
35         AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)" },
36                 EVENT_TRACKER.getEvents(Thread.currentThread()).toArray());
37
38         Object obj = scope.getRuntime("x1");
39         assertTrue(obj instanceof X1);
40         obj = scope.getRuntime("x4");
41         assertTrue(obj instanceof X4);
42     }
43     
44     public void testConstructorInjectionAndSetterInjection() {
45         ClassConfiguration x1Config = new ClassConfiguration(X1.class);
46         x1Config.getConstructorConfig().getParameters().setValue(0, "hello");
47         x1Config.getObjectConfig().getSetterConfig().initAllSetters();
48         
49         X8 x8 = new X8(null); 
50         EVENT_TRACKER.clear();
51
52         ClassConfiguration x4Config = new ClassConfiguration(X4.class);
53         x4Config.getObjectConfig().getSetterConfig().initAllSetters();
54         
55         ObjectConfiguration x8Config = new ObjectConfiguration(X8.class);
56         x8Config.getSetterConfig().initAllSetters();
57         
58         DefaultContainer container = new DefaultContainer("top").addComponent(
59                 "x1", x1Config).addComponent("x4", x4Config).addComponent("x8", x8, x8Config);
60
61         Scope scope = container.start();
62         AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)",
63                 "x8.setX4(x4)"},
64                 EVENT_TRACKER.getEvents(Thread.currentThread()).toArray());
65
66         Object obj1 = scope.getRuntime("x1");
67         assertTrue(obj1 instanceof X1);
68         Object obj4 = scope.getRuntime("x4");
69         assertTrue(obj4 instanceof X4);
70         Object obj8 = scope.getRuntime("x8");
71         assertSame(x8, obj8);
72         assertSame(obj4, x8.getX4());
73     }
74     
75     public void testWrongObjectType() { 
76         final DefaultContainer container = new DefaultContainer("top");
77         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
78             @Override
79             public void run() throws Exception {
80                 container.addComponent("x", "y", new ObjectConfiguration(Integer.class));   
81             }
82         }, IllegalArgumentException.class);
83     }
84 }