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;
20 import java.util.List;
22 import java.util.Properties;
24 import junit.framework.TestCase;
26 import org.wamblee.io.ClassPathResource;
27 import org.wamblee.system.core.DefaultProvidedInterface;
28 import org.wamblee.system.core.DefaultRequiredInterface;
29 import org.wamblee.system.core.DefaultScope;
30 import org.wamblee.system.core.ProvidedInterface;
31 import org.wamblee.system.core.RequiredInterface;
32 import org.wamblee.system.core.Scope;
33 import org.wamblee.system.core.SystemAssemblyException;
34 import org.wamblee.test.EventTracker;
36 public class SpringComponentTest extends TestCase {
38 private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
39 private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
40 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
41 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 = "test.org.wamblee.system.springWithProperties2.xml";
43 private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
45 public static EventTracker<String> EVENT_TRACKER;
47 private Scope _externalScope;
50 protected void setUp() throws Exception {
52 EVENT_TRACKER = new EventTracker<String>();
53 _externalScope = new DefaultScope(new ProvidedInterface[0]);
56 public void testBlackboxSystem() {
57 SpringComponent system = new SpringComponent("system",
58 new String[] { HELLO_SERVICE_SPRING_XML },
59 new HashMap<String, ProvidedInterface>(),
60 new HashMap<RequiredInterface, String>());
62 Scope runtime = system.start(_externalScope);
63 assertEquals(0, _externalScope.getProvidedInterfaces().size());
68 public void testOneProvidedService() {
69 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
70 provided.put("helloService", new DefaultProvidedInterface("hello",
73 SpringComponent system = new SpringComponent("system",
74 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
75 new HashMap<RequiredInterface, String>());
76 Scope runtime = system.start(_externalScope);
77 List<ProvidedInterface> services = runtime.getProvidedInterfaces();
79 assertEquals(1, services.size());
80 Object service = runtime.getInterfaceImplementation(services.get(0),
82 assertTrue(service instanceof HelloService);
84 // BUG; Provided services should be made available in the external
86 Object service2 = _externalScope.getInterfaceImplementation(provided
87 .get("helloService"), Object.class);
88 assertSame(service, service2);
90 assertEquals("Hello world!", ((HelloService) service).say());
94 public void testWithProperties() throws IOException {
95 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
96 provided.put("helloService", new DefaultProvidedInterface("hello",
98 SpringComponent system = new SpringComponent("system",
99 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
100 provided, new HashMap<RequiredInterface, String>());
101 Properties props = new Properties();
102 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
103 system.addProperties(props);
105 Scope scope = system.start(_externalScope);
106 // BUG: Hello service was constructed multiple times. Once with the
107 // unprocessed property
108 // and another time with the processed property.
109 assertEquals(1, EVENT_TRACKER.getEventCount());
110 List<ProvidedInterface> services = scope.getProvidedInterfaces();
111 assertEquals("Property Value", scope.getInterfaceImplementation(
112 services.get(0), HelloService.class).say());
115 public void testWithPropertiesAsBean() throws IOException {
116 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
117 provided.put("helloService", new DefaultProvidedInterface("hello",
118 HelloService2.class));
119 SpringComponent system = new SpringComponent("system",
120 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 },
121 provided, new HashMap<RequiredInterface, String>());
122 Properties props = new Properties();
123 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
124 system.addProperties("properties", props);
126 Scope scope = system.start(_externalScope);
128 List<ProvidedInterface> services = scope.getProvidedInterfaces();
130 Properties props2 = scope.getInterfaceImplementation(services.get(0),
131 HelloService2.class).getProperties();
132 assertEquals(props, props2);
135 public void testWithMissingRequirement() {
137 SpringComponent system = new SpringComponent("system",
138 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
139 new HashMap<String, ProvidedInterface>(),
140 new HashMap<RequiredInterface, String>());
141 system.start(_externalScope);
142 } catch (SystemAssemblyException e) {
143 // e.printStackTrace();
149 public void testWithRequirement() {
150 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
151 required.put(new DefaultRequiredInterface("hello", HelloService.class),
153 SpringComponent system = new SpringComponent("system",
154 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
155 new HashMap<String, ProvidedInterface>(), required);
157 HelloService helloObject = new HelloService("ladida");
158 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
160 Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
161 scope.publishInterface(helloService, helloObject);
162 system.getRequiredInterfaces().get(0).setProvider(helloService);
164 Scope runtime = system.start(scope);
165 system.stop(runtime);
168 public void testWithRequirementAndProvidedService() {
169 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
170 required.put(new DefaultRequiredInterface("hello", HelloService.class),
172 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
173 provided.put("blaService", new DefaultProvidedInterface("bla",
176 SpringComponent system = new SpringComponent("system",
177 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, provided,
180 HelloService helloObject = new HelloService("ladida");
181 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
183 Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
184 scope.publishInterface(helloService, helloObject);
185 system.getRequiredInterfaces().get(0).setProvider(helloService);
186 Scope runtime = system.start(scope);
187 ProvidedInterface started = runtime.getProvidedInterfaces().get(0);
189 Object impl = runtime.getInterfaceImplementation(started,
192 assertTrue(impl instanceof BlaService);
193 assertEquals("ladida", ((BlaService) impl).execute());
194 system.stop(runtime);
198 * Tests a scenario where a subclass of SpringComponent adds a new provided
199 * interface where the interface is provided by the subclass itself and not
200 * by the spring configs inside.
202 public void testWithProvidedFromSubClassNotFromConfig() {
203 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
204 provided.put("helloService", new DefaultProvidedInterface("hello",
205 HelloService.class));
207 SubSpringComponent system = new SubSpringComponent("system",
208 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
209 new HashMap<RequiredInterface, String>());
211 Scope runtime = system.start(_externalScope);
212 List<ProvidedInterface> services = runtime.getProvidedInterfaces();
214 assertEquals(2, services.size());
215 Object service = runtime.getInterfaceImplementation(services.get(0),
217 assertTrue(service instanceof HelloService);
219 // BUG; Provided services should be made available in the external
221 Object service2 = _externalScope.getInterfaceImplementation(provided
222 .get("helloService"), Object.class);
223 assertSame(service, service2);
225 Object floatsvc = _externalScope.getInterfaceImplementation(system
226 .getProvidedInterfaces().get(1), Object.class);
227 assertTrue(floatsvc instanceof Float);
228 assertTrue((((Float) floatsvc).floatValue() - 100.345f) < 0.00001);
230 assertEquals("Hello world!", ((HelloService) service).say());
231 system.stop(runtime);
235 * Tests the spring component with an additional requirement from the subclass
236 * which is not required by the spring config files inside.
238 public void testWithRequirementFromSubClass() {
239 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
240 required.put(new DefaultRequiredInterface("hello", HelloService.class),
242 SpringComponent system = new SubSpringComponent2("system",
243 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
244 new HashMap<String, ProvidedInterface>(), required);
246 HelloService helloObject = new HelloService("ladida");
247 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
250 ProvidedInterface floatService = new DefaultProvidedInterface("float", Float.class);
252 Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
253 scope.publishInterface(helloService, helloObject);
254 scope.publishInterface(floatService, 100.234f);
255 system.getRequiredInterfaces().get(0).setProvider(helloService);
256 system.getRequiredInterfaces().get(1).setProvider(floatService);
258 Scope runtime = system.start(scope);
259 system.stop(runtime);
261 assertEquals(100.234f, ((Float)runtime.get("floatValue")).floatValue(), 0.0001f);