Removed DOCUMENT ME comments that were generated and applied source code
[utils] / system / general / src / test / java / org / wamblee / system / adapters / ConstructorConfigurationTest.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.SystemAssemblyException;
22
23 import org.wamblee.test.AssertionUtils;
24
25 import java.util.List;
26
27 /**
28  * 
29  * @author $author$
30  * @version $Revision$
31  */
32 public class ConstructorConfigurationTest extends AdapterTestCase {
33     public void testGreedyUnique() {
34         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
35             .greedy();
36         ProvidedInterface provided = new DefaultProvidedInterface("arg",
37             String.class);
38         List<RequiredInterface> required = config.getRequiredInterfaces();
39
40         assertEquals(1, required.size());
41         assertFalse(required.get(0).isOptional());
42
43         required.get(0).setProvider(provided);
44
45         scope.publishInterface(provided, "hello");
46         config.create(scope);
47
48         AssertionUtils.assertEquals(new String[] { "x1(hello)" },
49             AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
50                 .toArray());
51     }
52
53     public void testGreedyNonUnique() {
54         try {
55             ConstructorConfiguration config = new ConstructorConfiguration(
56                 X2.class).greedy();
57         } catch (SystemAssemblyException e) {
58             // e.printStackTrace();
59             return;
60         }
61
62         fail();
63     }
64
65     public void testSpecificConstructor() {
66         ConstructorConfiguration config = new ConstructorConfiguration(X2.class)
67             .select(String.class);
68         ProvidedInterface provided = new DefaultProvidedInterface("arg",
69             String.class);
70         List<RequiredInterface> required = config.getRequiredInterfaces();
71
72         assertEquals(1, required.size());
73         required.get(0).setProvider(provided);
74
75         scope.publishInterface(provided, "hello");
76         config.create(scope);
77
78         AssertionUtils.assertEquals(new String[] { "x2(hello)" },
79             AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
80                 .toArray());
81     }
82
83     public void testSetValue() {
84         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
85             .greedy();
86         config.getParameters().setValue(0, "bla");
87
88         config.create(scope);
89
90         AssertionUtils.assertEquals(new String[] { "x1(bla)" },
91             AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
92                 .toArray());
93     }
94
95     public void testOptionalValueProvided() {
96         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
97             .greedy();
98         config.getParameters().setOptional(0);
99
100         ProvidedInterface provided = new DefaultProvidedInterface("arg",
101             String.class);
102         List<RequiredInterface> required = config.getRequiredInterfaces();
103
104         assertEquals(1, required.size());
105         required.get(0).setProvider(provided);
106
107         scope.publishInterface(provided, "hello");
108         config.create(scope);
109
110         AssertionUtils.assertEquals(new String[] { "x1(hello)" },
111             AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
112                 .toArray());
113     }
114
115     public void testOptionalValueMissing() {
116         ConstructorConfiguration config = new ConstructorConfiguration(X1.class)
117             .greedy();
118         config.getParameters().setOptional(0);
119         assertTrue(config.getRequiredInterfaces().get(0).isOptional());
120
121         config.create(scope);
122
123         AssertionUtils.assertEquals(new String[] { "x1(null)" },
124             AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
125                 .toArray());
126     }
127
128     public void testIgnoredNonPublic() {
129         ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
130             .greedy();
131         List<RequiredInterface> required = config.getRequiredInterfaces();
132         assertEquals(0, config.getParameters().getTypes().length);
133     }
134
135     public void testNonPublicConstructor() {
136         ConstructorConfiguration config = new ConstructorConfiguration(X3.class)
137             .setNonPublic(true).greedy();
138         ProvidedInterface provided = new DefaultProvidedInterface("arg",
139             String.class);
140         List<RequiredInterface> required = config.getRequiredInterfaces();
141
142         assertEquals(1, required.size());
143         assertFalse(required.get(0).isOptional());
144
145         required.get(0).setProvider(provided);
146
147         scope.publishInterface(provided, "hello");
148         config.create(scope);
149
150         AssertionUtils.assertEquals(new String[] { "x3(hello)" },
151             AdapterTestCase.EVENT_TRACKER.getEvents(Thread.currentThread())
152                 .toArray());
153     }
154 }