Added ObjectConfiguration as a holder for setter configuration (there will be more...
[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
27         DefaultContainer container = new DefaultContainer("top").addComponent(
28                 "x1", x1Config).addComponent("x4", X4.class);
29
30         Scope scope = container.start();
31         AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)" },
32                 EVENT_TRACKER.getEvents(Thread.currentThread()).toArray());
33
34         Object obj = scope.getRuntime("x1");
35         assertTrue(obj instanceof X1);
36         obj = scope.getRuntime("x4");
37         assertTrue(obj instanceof X4);
38     }
39     
40     public void testConstructorInjectionAndSetterInjection() {
41         ClassConfiguration x1Config = new ClassConfiguration(X1.class);
42         x1Config.getConstructorConfig().getParameters().setValue(0, "hello");
43         
44         X8 x8 = new X8(null); 
45         EVENT_TRACKER.clear();
46
47         DefaultContainer container = new DefaultContainer("top").addComponent(
48                 "x1", x1Config).addComponent("x4", X4.class).addComponent("x8", x8);
49
50         Scope scope = container.start();
51         AssertionUtils.assertEquals(new String[] { "x1(hello)", "x4(x1)",
52                 "x8.setX4(x4)"},
53                 EVENT_TRACKER.getEvents(Thread.currentThread()).toArray());
54
55         Object obj1 = scope.getRuntime("x1");
56         assertTrue(obj1 instanceof X1);
57         Object obj4 = scope.getRuntime("x4");
58         assertTrue(obj4 instanceof X4);
59         Object obj8 = scope.getRuntime("x8");
60         assertSame(x8, obj8);
61         assertSame(obj4, x8.getX4());
62     }
63     
64     public void testWrongObjectType() { 
65         final DefaultContainer container = new DefaultContainer("top");
66         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
67             @Override
68             public void run() throws Exception {
69                 container.addComponent("x", "y", new ObjectConfiguration(Integer.class));   
70             }
71         }, IllegalArgumentException.class);
72     }
73 }