Removed DOCUMENT ME comments that were generated and applied source code
[utils] / system / spring / src / test / java / org / wamblee / system / spring / SpringComponentTest.java
1 /*
2  * Copyright 2007 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.spring;
17
18 import junit.framework.TestCase;
19
20 import org.wamblee.io.ClassPathResource;
21
22 import org.wamblee.system.core.DefaultProvidedInterface;
23 import org.wamblee.system.core.DefaultRequiredInterface;
24 import org.wamblee.system.core.DefaultScope;
25 import org.wamblee.system.core.ProvidedInterface;
26 import org.wamblee.system.core.RequiredInterface;
27 import org.wamblee.system.core.Scope;
28 import org.wamblee.system.core.SystemAssemblyException;
29
30 import org.wamblee.test.EventTracker;
31
32 import java.io.IOException;
33
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Properties;
38
39 /**
40  * 
41  * @author $author$
42  * @version $Revision$
43  */
44 public class SpringComponentTest extends TestCase {
45     private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
46
47     private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
48
49     private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
50
51     private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 = "test.org.wamblee.system.springWithProperties2.xml";
52
53     private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
54
55     public static EventTracker<String> EVENT_TRACKER;
56
57     private Scope externalScope;
58
59     @Override
60     protected void setUp() throws Exception {
61         super.setUp();
62         EVENT_TRACKER = new EventTracker<String>();
63         externalScope = new DefaultScope(new ProvidedInterface[0]);
64     }
65
66     public void testBlackboxSystem() {
67         SpringComponent system = new SpringComponent("system",
68             new String[] { HELLO_SERVICE_SPRING_XML },
69             new HashMap<String, ProvidedInterface>(),
70             new HashMap<RequiredInterface, String>());
71
72         Scope runtime = system.start(externalScope);
73         assertEquals(0, externalScope.getProvidedInterfaces().size());
74
75         system.stop(runtime);
76     }
77
78     public void testOneProvidedService() {
79         Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
80         provided.put("helloService", new DefaultProvidedInterface("hello",
81             HelloService.class));
82
83         SpringComponent system = new SpringComponent("system",
84             new String[] { HELLO_SERVICE_SPRING_XML }, provided,
85             new HashMap<RequiredInterface, String>());
86         Scope runtime = system.start(externalScope);
87         List<ProvidedInterface> services = runtime.getProvidedInterfaces();
88
89         assertEquals(1, services.size());
90
91         Object service = runtime.getInterfaceImplementation(services.get(0),
92             Object.class);
93         assertTrue(service instanceof HelloService);
94
95         // BUG; Provided services should be made available in the external
96         // scope.
97         Object service2 = externalScope.getInterfaceImplementation(provided
98             .get("helloService"), Object.class);
99         assertSame(service, service2);
100
101         assertEquals("Hello world!", ((HelloService) service).say());
102         system.stop(runtime);
103     }
104
105     public void testWithProperties() throws IOException {
106         Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
107         provided.put("helloService", new DefaultProvidedInterface("hello",
108             HelloService.class));
109
110         SpringComponent system = new SpringComponent("system",
111             new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
112             provided, new HashMap<RequiredInterface, String>());
113         Properties props = new Properties();
114         props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
115         system.addProperties(props);
116
117         Scope scope = system.start(externalScope);
118         // BUG: Hello service was constructed multiple times. Once with the
119         // unprocessed property
120         // and another time with the processed property.
121         assertEquals(1, EVENT_TRACKER.getEventCount());
122
123         List<ProvidedInterface> services = scope.getProvidedInterfaces();
124         assertEquals("Property Value", scope.getInterfaceImplementation(
125             services.get(0), HelloService.class).say());
126     }
127
128     public void testWithPropertiesAsBean() throws IOException {
129         Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
130         provided.put("helloService", new DefaultProvidedInterface("hello",
131             HelloService2.class));
132
133         SpringComponent system = new SpringComponent("system",
134             new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 },
135             provided, new HashMap<RequiredInterface, String>());
136         Properties props = new Properties();
137         props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
138         system.addProperties("properties", props);
139
140         Scope scope = system.start(externalScope);
141
142         List<ProvidedInterface> services = scope.getProvidedInterfaces();
143
144         Properties props2 = scope.getInterfaceImplementation(services.get(0),
145             HelloService2.class).getProperties();
146         assertEquals(props, props2);
147     }
148
149     public void testWithMissingRequirement() {
150         try {
151             SpringComponent system = new SpringComponent("system",
152                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
153                 new HashMap<String, ProvidedInterface>(),
154                 new HashMap<RequiredInterface, String>());
155             system.start(externalScope);
156         } catch (SystemAssemblyException e) {
157             // e.printStackTrace();
158             return;
159         }
160
161         fail();
162     }
163
164     public void testWithRequirement() {
165         Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
166         required.put(new DefaultRequiredInterface("hello", HelloService.class),
167             "helloService");
168
169         SpringComponent system = new SpringComponent("system",
170             new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
171             new HashMap<String, ProvidedInterface>(), required);
172
173         HelloService helloObject = new HelloService("ladida");
174         ProvidedInterface helloService = new DefaultProvidedInterface("hello",
175             HelloService.class);
176         Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
177         scope.publishInterface(helloService, helloObject);
178         system.getRequiredInterfaces().get(0).setProvider(helloService);
179
180         Scope runtime = system.start(scope);
181         system.stop(runtime);
182     }
183
184     public void testWithRequirementAndProvidedService() {
185         Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
186         required.put(new DefaultRequiredInterface("hello", HelloService.class),
187             "helloService");
188
189         Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
190         provided.put("blaService", new DefaultProvidedInterface("bla",
191             BlaService.class));
192
193         SpringComponent system = new SpringComponent("system",
194             new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, provided,
195             required);
196
197         HelloService helloObject = new HelloService("ladida");
198         ProvidedInterface helloService = new DefaultProvidedInterface("hello",
199             HelloService.class);
200         Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
201         scope.publishInterface(helloService, helloObject);
202         system.getRequiredInterfaces().get(0).setProvider(helloService);
203
204         Scope runtime = system.start(scope);
205         ProvidedInterface started = runtime.getProvidedInterfaces().get(0);
206
207         Object impl = runtime.getInterfaceImplementation(started,
208             BlaService.class);
209         assertNotNull(impl);
210         assertTrue(impl instanceof BlaService);
211         assertEquals("ladida", ((BlaService) impl).execute());
212         system.stop(runtime);
213     }
214
215     /**
216      * Tests a scenario where a subclass of SpringComponent adds a new provided
217      * interface where the interface is provided by the subclass itself and not
218      * by the spring configs inside.
219      */
220     public void testWithProvidedFromSubClassNotFromConfig() {
221         Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
222         provided.put("helloService", new DefaultProvidedInterface("hello",
223             HelloService.class));
224
225         SubSpringComponent system = new SubSpringComponent("system",
226             new String[] { HELLO_SERVICE_SPRING_XML }, provided,
227             new HashMap<RequiredInterface, String>());
228
229         Scope runtime = system.start(externalScope);
230         List<ProvidedInterface> services = runtime.getProvidedInterfaces();
231
232         assertEquals(2, services.size());
233
234         Object service = runtime.getInterfaceImplementation(services.get(0),
235             Object.class);
236         assertTrue(service instanceof HelloService);
237
238         // BUG; Provided services should be made available in the external
239         // scope.
240         Object service2 = externalScope.getInterfaceImplementation(provided
241             .get("helloService"), Object.class);
242         assertSame(service, service2);
243
244         Object floatsvc = externalScope.getInterfaceImplementation(system
245             .getProvidedInterfaces().get(1), Object.class);
246         assertTrue(floatsvc instanceof Float);
247         assertTrue((((Float) floatsvc).floatValue() - 100.345f) < 0.00001);
248
249         assertEquals("Hello world!", ((HelloService) service).say());
250         system.stop(runtime);
251     }
252
253     /**
254      * Tests the spring component with an additional requirement from the
255      * subclass which is not required by the spring config files inside.
256      */
257     public void testWithRequirementFromSubClass() {
258         Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
259         required.put(new DefaultRequiredInterface("hello", HelloService.class),
260             "helloService");
261
262         SpringComponent system = new SubSpringComponent2("system",
263             new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
264             new HashMap<String, ProvidedInterface>(), required);
265
266         HelloService helloObject = new HelloService("ladida");
267         ProvidedInterface helloService = new DefaultProvidedInterface("hello",
268             HelloService.class);
269
270         ProvidedInterface floatService = new DefaultProvidedInterface("float",
271             Float.class);
272
273         Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
274         scope.publishInterface(helloService, helloObject);
275         scope.publishInterface(floatService, 100.234f);
276         system.getRequiredInterfaces().get(0).setProvider(helloService);
277         system.getRequiredInterfaces().get(1).setProvider(floatService);
278
279         Scope runtime = system.start(scope);
280         system.stop(runtime);
281
282         assertEquals(100.234f,
283             ((Float) runtime.get("floatValue")).floatValue(), 0.0001f);
284     }
285 }