(no commit message)
[utils] / system / general / src / test / java / org / wamblee / system / adapters / ClassConfigurationTest.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.core.DefaultProvidedInterface;
22 import org.wamblee.system.core.ProvidedInterface;
23 import org.wamblee.system.core.RequiredInterface;
24 import org.wamblee.system.core.RequiredInterfaceComparator;
25 import org.wamblee.test.AssertionUtils;
26
27 public class ClassConfigurationTest extends AdapterTestCase {
28
29     public void testConstructorConfig() {
30         ClassConfiguration classConfig = new ClassConfiguration(X1.class);
31
32         ConstructorConfiguration config = classConfig.getConstructorConfig()
33                 .greedy();
34         ProvidedInterface provided = new DefaultProvidedInterface("arg",
35                 String.class);
36         List<RequiredInterface> required = classConfig.getRequiredInterfaces();
37
38         assertEquals(1, required.size());
39         assertFalse(required.get(0).isOptional());
40
41         required.get(0).setProvider(provided);
42
43         provided.publish("hello", _scope);
44         classConfig.create(_scope);
45
46         AssertionUtils.assertEquals(new String[] { "x1(hello)" },
47                 AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
48                         .toArray());
49     }
50
51     public void testConstructorConfigWithSetters() {
52         ClassConfiguration classConfig = new ClassConfiguration(X7.class);
53
54         classConfig.getConstructorConfig().select(Boolean.class);
55         classConfig.getObjectConfig().getSetterConfig().values("port").setValue(0, 10);
56         
57         ProvidedInterface providedBoolean = new DefaultProvidedInterface("boolean",
58                 Boolean.class);
59         ProvidedInterface providedHost = new DefaultProvidedInterface("host", String.class);
60         List<RequiredInterface> required = classConfig.getRequiredInterfaces();
61
62         Collections.sort(required, new RequiredInterfaceComparator());
63         assertEquals(2, required.size());
64         assertEquals("arg0", required.get(0).getName());
65
66         required.get(0).setProvider(providedBoolean);
67         required.get(1).setProvider(providedHost);
68
69         providedBoolean.publish(true, _scope);
70         providedHost.publish("host.name.org", _scope);
71         
72         Object obj = classConfig.create(_scope);
73         assertTrue(obj instanceof X7);
74         X7 x7 = (X7)obj;
75         assertNotNull(x7.getBoolean());
76         assertTrue(x7.getBoolean());
77         assertNull(x7.getHost());
78         assertNull(x7.getPort());
79
80         classConfig.inject(_scope, obj);
81         
82         assertEquals("host.name.org", x7.getHost());
83         assertEquals(10, x7.getPort().intValue());
84     }
85
86 }