2 * Copyright 2007 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.system.spring;
18 import junit.framework.TestCase;
20 import org.wamblee.io.ClassPathResource;
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;
30 import org.wamblee.test.EventTracker;
32 import java.io.IOException;
34 import java.util.HashMap;
35 import java.util.List;
37 import java.util.Properties;
46 public class SpringComponentTest extends TestCase {
50 private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
55 private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
60 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
65 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 = "test.org.wamblee.system.springWithProperties2.xml";
70 private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
75 public static EventTracker<String> EVENT_TRACKER;
80 private Scope externalScope;
85 * @throws Exception DOCUMENT ME!
88 protected void setUp() throws Exception {
90 EVENT_TRACKER = new EventTracker<String>();
91 externalScope = new DefaultScope(new ProvidedInterface[0]);
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>());
103 Scope runtime = system.start(externalScope);
104 assertEquals(0, externalScope.getProvidedInterfaces().size());
106 system.stop(runtime);
112 public void testOneProvidedService() {
113 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
114 provided.put("helloService",
115 new DefaultProvidedInterface("hello", HelloService.class));
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();
123 assertEquals(1, services.size());
125 Object service = runtime.getInterfaceImplementation(services.get(0),
127 assertTrue(service instanceof HelloService);
129 // BUG; Provided services should be made available in the external
131 Object service2 = externalScope.getInterfaceImplementation(provided.get(
132 "helloService"), Object.class);
133 assertSame(service, service2);
135 assertEquals("Hello world!", ((HelloService) service).say());
136 system.stop(runtime);
142 * @throws IOException DOCUMENT ME!
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));
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);
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());
162 List<ProvidedInterface> services = scope.getProvidedInterfaces();
163 assertEquals("Property Value",
164 scope.getInterfaceImplementation(services.get(0), HelloService.class)
171 * @throws IOException DOCUMENT ME!
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));
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);
185 Scope scope = system.start(externalScope);
187 List<ProvidedInterface> services = scope.getProvidedInterfaces();
189 Properties props2 = scope.getInterfaceImplementation(services
190 .get(0), HelloService2.class).getProperties();
191 assertEquals(props, props2);
197 public void testWithMissingRequirement() {
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();
215 public void testWithRequirement() {
216 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
217 required.put(new DefaultRequiredInterface("hello", HelloService.class),
220 SpringComponent system = new SpringComponent("system",
221 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
222 new HashMap<String, ProvidedInterface>(), required);
224 HelloService helloObject = new HelloService("ladida");
225 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
227 Scope scope = new DefaultScope(new ProvidedInterface[] {
230 scope.publishInterface(helloService, helloObject);
231 system.getRequiredInterfaces().get(0).setProvider(helloService);
233 Scope runtime = system.start(scope);
234 system.stop(runtime);
240 public void testWithRequirementAndProvidedService() {
241 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
242 required.put(new DefaultRequiredInterface("hello", HelloService.class),
245 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
246 provided.put("blaService",
247 new DefaultProvidedInterface("bla", BlaService.class));
249 SpringComponent system = new SpringComponent("system",
250 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, provided,
253 HelloService helloObject = new HelloService("ladida");
254 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
256 Scope scope = new DefaultScope(new ProvidedInterface[] {
259 scope.publishInterface(helloService, helloObject);
260 system.getRequiredInterfaces().get(0).setProvider(helloService);
262 Scope runtime = system.start(scope);
263 ProvidedInterface started = runtime.getProvidedInterfaces().get(0);
265 Object impl = runtime.getInterfaceImplementation(started,
268 assertTrue(impl instanceof BlaService);
269 assertEquals("ladida", ((BlaService) impl).execute());
270 system.stop(runtime);
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.
278 public void testWithProvidedFromSubClassNotFromConfig() {
279 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
280 provided.put("helloService",
281 new DefaultProvidedInterface("hello", HelloService.class));
283 SubSpringComponent system = new SubSpringComponent("system",
284 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
285 new HashMap<RequiredInterface, String>());
287 Scope runtime = system.start(externalScope);
288 List<ProvidedInterface> services = runtime.getProvidedInterfaces();
290 assertEquals(2, services.size());
292 Object service = runtime.getInterfaceImplementation(services.get(0),
294 assertTrue(service instanceof HelloService);
296 // BUG; Provided services should be made available in the external
298 Object service2 = externalScope.getInterfaceImplementation(provided.get(
299 "helloService"), Object.class);
300 assertSame(service, service2);
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);
307 assertEquals("Hello world!", ((HelloService) service).say());
308 system.stop(runtime);
312 * Tests the spring component with an additional requirement from
313 * the subclass which is not required by the spring config files inside.
315 public void testWithRequirementFromSubClass() {
316 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
317 required.put(new DefaultRequiredInterface("hello", HelloService.class),
320 SpringComponent system = new SubSpringComponent2("system",
321 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
322 new HashMap<String, ProvidedInterface>(), required);
324 HelloService helloObject = new HelloService("ladida");
325 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
328 ProvidedInterface floatService = new DefaultProvidedInterface("float",
331 Scope scope = new DefaultScope(new ProvidedInterface[] {
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);
339 Scope runtime = system.start(scope);
340 system.stop(runtime);
342 assertEquals(100.234f,
343 ((Float) runtime.get("floatValue")).floatValue(), 0.0001f);