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 java.io.IOException;
19 import java.util.HashMap;
21 import java.util.Properties;
23 import junit.framework.TestCase;
25 import org.wamblee.io.ClassPathResource;
26 import org.wamblee.system.core.DefaultProvidedInterface;
27 import org.wamblee.system.core.DefaultRequiredInterface;
28 import org.wamblee.system.core.DefaultScope;
29 import org.wamblee.system.core.ProvidedInterface;
30 import org.wamblee.system.core.RequiredInterface;
31 import org.wamblee.system.core.Scope;
32 import org.wamblee.system.core.SystemAssemblyException;
33 import org.wamblee.test.EventTracker;
35 public class SpringComponentTest extends TestCase {
37 private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
38 private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
39 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
40 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 = "test.org.wamblee.system.springWithProperties2.xml";
42 private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
44 public static EventTracker<String> EVENT_TRACKER;
46 private Scope _externalScope;
49 protected void setUp() throws Exception {
51 EVENT_TRACKER = new EventTracker<String>();
52 _externalScope = new DefaultScope(new ProvidedInterface[0]);
55 public void testBlackboxSystem() {
56 SpringComponent system = new SpringComponent("system",
57 new String[] { HELLO_SERVICE_SPRING_XML },
58 new HashMap<String, ProvidedInterface>(),
59 new HashMap<RequiredInterface, String>());
61 Scope runtime = system.start(_externalScope);
62 assertEquals(0, _externalScope.getProvidedInterfaces().length);
67 public void testOneProvidedService() {
68 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
69 provided.put("helloService", new DefaultProvidedInterface("hello",
72 SpringComponent system = new SpringComponent("system",
73 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
74 new HashMap<RequiredInterface, String>());
75 Scope runtime = system.start(_externalScope);
76 ProvidedInterface[] services = runtime.getProvidedInterfaces();
78 assertEquals(1, services.length);
79 Object service = runtime.getInterfaceImplementation(services[0],
81 assertTrue(service instanceof HelloService);
83 // BUG; Provided services should be made available in the external
85 Object service2 = _externalScope.getInterfaceImplementation(provided
86 .get("helloService"), Object.class);
87 assertSame(service, service2);
89 assertEquals("Hello world!", ((HelloService) service).say());
93 public void testWithProperties() throws IOException {
94 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
95 provided.put("helloService", new DefaultProvidedInterface("hello",
97 SpringComponent system = new SpringComponent("system",
98 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
99 provided, new HashMap<RequiredInterface, String>());
100 Properties props = new Properties();
101 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
102 system.addProperties(props);
104 Scope scope = system.start(_externalScope);
105 // BUG: Hello service was constructed multiple times. Once with the
106 // unprocessed property
107 // and another time with the processed property.
108 assertEquals(1, EVENT_TRACKER.getEventCount());
109 ProvidedInterface[] services = scope.getProvidedInterfaces();
110 assertEquals("Property Value", scope.getInterfaceImplementation(
111 services[0], HelloService.class).say());
114 public void testWithPropertiesAsBean() throws IOException {
115 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
116 provided.put("helloService", new DefaultProvidedInterface("hello",
117 HelloService2.class));
118 SpringComponent system = new SpringComponent("system",
119 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 },
120 provided, new HashMap<RequiredInterface, String>());
121 Properties props = new Properties();
122 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
123 system.addProperties("properties", props);
125 Scope scope = system.start(_externalScope);
127 ProvidedInterface[] services = scope.getProvidedInterfaces();
129 Properties props2 = scope.getInterfaceImplementation(services[0],
130 HelloService2.class).getProperties();
131 assertEquals(props, props2);
134 public void testWithMissingRequirement() {
136 SpringComponent system = new SpringComponent("system",
137 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
138 new HashMap<String, ProvidedInterface>(),
139 new HashMap<RequiredInterface, String>());
140 system.start(_externalScope);
141 } catch (SystemAssemblyException e) {
142 // e.printStackTrace();
148 public void testWithRequirement() {
149 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
150 required.put(new DefaultRequiredInterface("hello", HelloService.class),
152 SpringComponent system = new SpringComponent("system",
153 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
154 new HashMap<String, ProvidedInterface>(), required);
156 HelloService helloObject = new HelloService("ladida");
157 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
159 Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
160 scope.publishInterface(helloService, helloObject);
161 system.getRequiredInterfaces()[0].setProvider(helloService);
163 Scope runtime = system.start(scope);
164 system.stop(runtime);
167 public void testWithRequirementAndProvidedService() {
168 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
169 required.put(new DefaultRequiredInterface("hello", HelloService.class),
171 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
172 provided.put("blaService", new DefaultProvidedInterface("bla",
175 SpringComponent system = new SpringComponent("system",
176 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, provided,
179 HelloService helloObject = new HelloService("ladida");
180 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
182 Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
183 scope.publishInterface(helloService, helloObject);
184 system.getRequiredInterfaces()[0].setProvider(helloService);
185 Scope runtime = system.start(scope);
186 ProvidedInterface started = runtime.getProvidedInterfaces()[0];
188 Object impl = runtime.getInterfaceImplementation(started,
191 assertTrue(impl instanceof BlaService);
192 assertEquals("ladida", ((BlaService) impl).execute());
193 system.stop(runtime);
197 * Tests a scenario where a subclass of SpringComponent adds a new provided
198 * interface where the interface is provided by the subclass itself and not
199 * by the spring configs inside.
201 public void testWithProvidedFromSubClassNotFromConfig() {
202 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
203 provided.put("helloService", new DefaultProvidedInterface("hello",
204 HelloService.class));
206 SubSpringComponent system = new SubSpringComponent("system",
207 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
208 new HashMap<RequiredInterface, String>());
210 Scope runtime = system.start(_externalScope);
211 ProvidedInterface[] services = runtime.getProvidedInterfaces();
213 assertEquals(2, services.length);
214 Object service = runtime.getInterfaceImplementation(services[0],
216 assertTrue(service instanceof HelloService);
218 // BUG; Provided services should be made available in the external
220 Object service2 = _externalScope.getInterfaceImplementation(provided
221 .get("helloService"), Object.class);
222 assertSame(service, service2);
224 Object floatsvc = _externalScope.getInterfaceImplementation(system
225 .getProvidedInterfaces()[1], Object.class);
226 assertTrue(floatsvc instanceof Float);
227 assertTrue((((Float) floatsvc).floatValue() - 100.345f) < 0.00001);
229 assertEquals("Hello world!", ((HelloService) service).say());
230 system.stop(runtime);
234 * Tests the spring component with an additional requirement from the subclass
235 * which is not required by the spring config files inside.
237 public void testWithRequirementFromSubClass() {
238 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
239 required.put(new DefaultRequiredInterface("hello", HelloService.class),
241 SpringComponent system = new SubSpringComponent2("system",
242 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
243 new HashMap<String, ProvidedInterface>(), required);
245 HelloService helloObject = new HelloService("ladida");
246 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
249 ProvidedInterface floatService = new DefaultProvidedInterface("float", Float.class);
251 Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
252 scope.publishInterface(helloService, helloObject);
253 scope.publishInterface(floatService, 100.234f);
254 system.getRequiredInterfaces()[0].setProvider(helloService);
255 system.getRequiredInterfaces()[1].setProvider(floatService);
257 Scope runtime = system.start(scope);
258 system.stop(runtime);
260 assertEquals(100.234f, ((Float)runtime.get("floatValue")).floatValue(), 0.0001f);