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