2 * Copyright 2005-2010 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;
44 public class SpringComponentTest extends TestCase {
45 private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
47 private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
49 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
51 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 = "test.org.wamblee.system.springWithProperties2.xml";
53 private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
55 public static EventTracker<String> EVENT_TRACKER;
57 private Scope externalScope;
60 protected void setUp() throws Exception {
62 EVENT_TRACKER = new EventTracker<String>();
63 externalScope = new DefaultScope(new ProvidedInterface[0]);
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>());
72 Scope runtime = system.start(externalScope);
73 assertEquals(0, externalScope.getProvidedInterfaces().size());
78 public void testOneProvidedService() {
79 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
80 provided.put("helloService", new DefaultProvidedInterface("hello",
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();
89 assertEquals(1, services.size());
91 Object service = runtime.getInterfaceImplementation(services.get(0),
93 assertTrue(service instanceof HelloService);
95 // BUG; Provided services should be made available in the external
97 Object service2 = externalScope.getInterfaceImplementation(provided
98 .get("helloService"), Object.class);
99 assertSame(service, service2);
101 assertEquals("Hello world!", ((HelloService) service).say());
102 system.stop(runtime);
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));
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);
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());
123 List<ProvidedInterface> services = scope.getProvidedInterfaces();
124 assertEquals("Property Value", scope.getInterfaceImplementation(
125 services.get(0), HelloService.class).say());
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));
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);
140 Scope scope = system.start(externalScope);
142 List<ProvidedInterface> services = scope.getProvidedInterfaces();
144 Properties props2 = scope.getInterfaceImplementation(services.get(0),
145 HelloService2.class).getProperties();
146 assertEquals(props, props2);
149 public void testWithMissingRequirement() {
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();
164 public void testWithRequirement() {
165 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
166 required.put(new DefaultRequiredInterface("hello", HelloService.class),
169 SpringComponent system = new SpringComponent("system",
170 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
171 new HashMap<String, ProvidedInterface>(), required);
173 HelloService helloObject = new HelloService("ladida");
174 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
176 Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
177 scope.publishInterface(helloService, helloObject);
178 system.getRequiredInterfaces().get(0).setProvider(helloService);
180 Scope runtime = system.start(scope);
181 system.stop(runtime);
184 public void testWithRequirementAndProvidedService() {
185 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
186 required.put(new DefaultRequiredInterface("hello", HelloService.class),
189 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
190 provided.put("blaService", new DefaultProvidedInterface("bla",
193 SpringComponent system = new SpringComponent("system",
194 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, provided,
197 HelloService helloObject = new HelloService("ladida");
198 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
200 Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
201 scope.publishInterface(helloService, helloObject);
202 system.getRequiredInterfaces().get(0).setProvider(helloService);
204 Scope runtime = system.start(scope);
205 ProvidedInterface started = runtime.getProvidedInterfaces().get(0);
207 Object impl = runtime.getInterfaceImplementation(started,
210 assertTrue(impl instanceof BlaService);
211 assertEquals("ladida", ((BlaService) impl).execute());
212 system.stop(runtime);
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.
220 public void testWithProvidedFromSubClassNotFromConfig() {
221 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
222 provided.put("helloService", new DefaultProvidedInterface("hello",
223 HelloService.class));
225 SubSpringComponent system = new SubSpringComponent("system",
226 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
227 new HashMap<RequiredInterface, String>());
229 Scope runtime = system.start(externalScope);
230 List<ProvidedInterface> services = runtime.getProvidedInterfaces();
232 assertEquals(2, services.size());
234 Object service = runtime.getInterfaceImplementation(services.get(0),
236 assertTrue(service instanceof HelloService);
238 // BUG; Provided services should be made available in the external
240 Object service2 = externalScope.getInterfaceImplementation(provided
241 .get("helloService"), Object.class);
242 assertSame(service, service2);
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);
249 assertEquals("Hello world!", ((HelloService) service).say());
250 system.stop(runtime);
254 * Tests the spring component with an additional requirement from the
255 * subclass which is not required by the spring config files inside.
257 public void testWithRequirementFromSubClass() {
258 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
259 required.put(new DefaultRequiredInterface("hello", HelloService.class),
262 SpringComponent system = new SubSpringComponent2("system",
263 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
264 new HashMap<String, ProvidedInterface>(), required);
266 HelloService helloObject = new HelloService("ladida");
267 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
270 ProvidedInterface floatService = new DefaultProvidedInterface("float",
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);
279 Scope runtime = system.start(scope);
280 system.stop(runtime);
282 assertEquals(100.234f,
283 ((Float) runtime.get("floatValue")).floatValue(), 0.0001f);