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