source code formatting.
[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 org.wamblee.system.core.DefaultProvidedInterface;
19 import org.wamblee.system.core.ProvidedInterface;
20 import org.wamblee.system.core.RequiredInterface;
21 import org.wamblee.system.core.RequiredInterfaceComparator;
22
23 import org.wamblee.test.AssertionUtils;
24
25 import java.util.Collections;
26 import java.util.List;
27
28
29 /**
30  * DOCUMENT ME!
31  *
32  * @author $author$
33  * @version $Revision$
34   */
35 public class ClassConfigurationTest extends AdapterTestCase {
36     /**
37      * DOCUMENT ME!
38      */
39     public void testConstructorConfig() {
40         ClassConfiguration       classConfig = new ClassConfiguration(X1.class);
41
42         ConstructorConfiguration config      = classConfig.getConstructorConfig()
43             .greedy();
44
45         ProvidedInterface        provided    = new DefaultProvidedInterface("arg",
46                 String.class);
47         List<RequiredInterface>  required    = classConfig.getRequiredInterfaces();
48
49         assertEquals(1, required.size());
50         assertFalse(required.get(0).isOptional());
51
52         required.get(0).setProvider(provided);
53
54         scope.publishInterface(provided, "hello");
55         classConfig.create(scope);
56
57         AssertionUtils.assertEquals(new String[] { "x1(hello)" },
58             AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
59             .toArray());
60     }
61
62     /**
63      * DOCUMENT ME!
64      */
65     public void testConstructorConfigWithSetters() {
66         ClassConfiguration classConfig = new ClassConfiguration(X7.class);
67
68         classConfig.getConstructorConfig().select(Boolean.class);
69         classConfig.getObjectConfig().getSetterConfig().initAllSetters()
70         .values("setPort").setValue(0, 10);
71
72         ProvidedInterface       providedBoolean = new DefaultProvidedInterface("boolean",
73                 Boolean.class);
74         ProvidedInterface       providedHost    = new DefaultProvidedInterface("host",
75                 String.class);
76         List<RequiredInterface> required        = classConfig
77             .getRequiredInterfaces();
78
79         Collections.sort(required, new RequiredInterfaceComparator());
80         assertEquals(2, required.size());
81         assertEquals("arg0", required.get(0).getName());
82
83         required.get(0).setProvider(providedBoolean);
84         required.get(1).setProvider(providedHost);
85
86         scope.publishInterface(providedBoolean, true);
87         scope.publishInterface(providedHost, "host.name.org");
88
89         Object obj = classConfig.create(scope);
90         assertTrue(obj instanceof X7);
91
92         X7 x7 = (X7) obj;
93         assertNotNull(x7.getBoolean());
94         assertTrue(x7.getBoolean());
95         assertNull(x7.getHost());
96         assertNull(x7.getPort());
97
98         classConfig.inject(scope, obj);
99
100         assertEquals("host.name.org", x7.getHost());
101         assertEquals(10, x7.getPort().intValue());
102     }
103 }