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