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], Object.class);
80 assertTrue(service instanceof HelloService);
82 // BUG; Provided services should be made available in the external scope.
83 Object service2 = _externalScope.getInterfaceImplementation(provided.get("helloService"), Object.class);
84 assertSame(service, service2);
86 assertEquals("Hello world!", ((HelloService) service).say());
90 public void testWithProperties() throws IOException {
91 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
92 provided.put("helloService", new DefaultProvidedInterface("hello",
94 SpringComponent system = new SpringComponent("system",
95 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
96 provided, new HashMap<RequiredInterface, String>());
97 Properties props = new Properties();
98 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
99 system.addProperties(props);
101 Scope scope = system.start(_externalScope);
102 // BUG: Hello service was constructed multiple times. Once with the unprocessed property
103 // and another time with the processed property.
104 assertEquals(1, EVENT_TRACKER.getEventCount());
105 ProvidedInterface[] services = scope.getProvidedInterfaces();
106 assertEquals("Property Value", scope.getInterfaceImplementation(services[0], HelloService.class).say());
109 public void testWithPropertiesAsBean() throws IOException {
110 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
111 provided.put("helloService", new DefaultProvidedInterface("hello",
112 HelloService2.class));
113 SpringComponent system = new SpringComponent("system",
114 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 },
115 provided, new HashMap<RequiredInterface, String>());
116 Properties props = new Properties();
117 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
118 system.addProperties("properties", props);
120 Scope scope = system.start(_externalScope);
122 ProvidedInterface[] services = scope.getProvidedInterfaces();
124 Properties props2 = scope.getInterfaceImplementation(services[0], HelloService2.class).getProperties();
125 assertEquals(props, props2);
128 public void testWithMissingRequirement() {
130 SpringComponent system = new SpringComponent("system",
131 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
132 new HashMap<String, ProvidedInterface>(),
133 new HashMap<RequiredInterface, String>());
134 system.start(_externalScope);
135 } catch (SystemAssemblyException e) {
136 // e.printStackTrace();
142 public void testWithRequirement() {
143 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
144 required.put(new DefaultRequiredInterface("hello", HelloService.class),
146 SpringComponent system = new SpringComponent("system",
147 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
148 new HashMap<String, ProvidedInterface>(), required);
150 HelloService helloObject = new HelloService("ladida");
151 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
153 Scope scope = new DefaultScope(new ProvidedInterface[]{ helloService });
154 scope.publishInterface(helloService, helloObject);
155 system.getRequiredInterfaces()[0].setProvider(helloService);
157 Scope runtime = system.start(scope);
158 system.stop(runtime);
161 public void testWithRequirementAndProvidedService() {
162 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
163 required.put(new DefaultRequiredInterface("hello", HelloService.class),
165 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
166 provided.put("blaService", new DefaultProvidedInterface("bla",
169 SpringComponent system = new SpringComponent("system",
170 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, provided,
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()[0].setProvider(helloService);
179 Scope runtime = system.start(scope);
180 ProvidedInterface started = runtime.getProvidedInterfaces()[0];
182 Object impl = runtime.getInterfaceImplementation(started, BlaService.class);
184 assertTrue(impl instanceof BlaService);
185 assertEquals("ladida", ((BlaService)impl).execute());
186 system.stop(runtime);