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 PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
42 public static EventTracker<String> EVENT_TRACKER;
44 private Scope _externalScope;
47 protected void setUp() throws Exception {
49 EVENT_TRACKER = new EventTracker<String>();
50 _externalScope = new DefaultScope(new ProvidedInterface[0]);
53 public void testBlackboxSystem() {
54 SpringComponent system = new SpringComponent("system",
55 new String[] { HELLO_SERVICE_SPRING_XML },
56 new HashMap<String, ProvidedInterface>(),
57 new HashMap<RequiredInterface, String>());
59 Scope runtime = system.start(_externalScope);
60 assertEquals(0, _externalScope.getProvidedInterfaces().length);
65 public void testOneProvidedService() {
66 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
67 provided.put("helloService", new DefaultProvidedInterface("hello",
70 SpringComponent system = new SpringComponent("system",
71 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
72 new HashMap<RequiredInterface, String>());
73 Scope runtime = system.start(_externalScope);
74 ProvidedInterface[] services = runtime.getProvidedInterfaces();
76 assertEquals(1, services.length);
77 Object service = runtime.getInterfaceImplementation(services[0], Object.class);
78 assertTrue(service instanceof HelloService);
80 // BUG; Provided services should be made available in the external scope.
81 Object service2 = _externalScope.getInterfaceImplementation(provided.get("helloService"), Object.class);
82 assertSame(service, service2);
84 assertEquals("Hello world!", ((HelloService) service).say());
88 public void testWithProperties() throws IOException {
89 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
90 provided.put("helloService", new DefaultProvidedInterface("hello",
92 SpringComponent system = new SpringComponent("system",
93 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
94 provided, new HashMap<RequiredInterface, String>());
95 Properties props = new Properties();
96 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
97 system.addProperties(props);
99 Scope scope = system.start(_externalScope);
100 // BUG: Hello service was constructed multiple times. Once with the unprocessed property
101 // and another time with the processed property.
102 assertEquals(1, EVENT_TRACKER.getEventCount());
103 ProvidedInterface[] services = scope.getProvidedInterfaces();
104 assertEquals("Property Value", scope.getInterfaceImplementation(services[0], HelloService.class).say());
107 public void testWithMissingRequirement() {
109 SpringComponent system = new SpringComponent("system",
110 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
111 new HashMap<String, ProvidedInterface>(),
112 new HashMap<RequiredInterface, String>());
113 system.start(_externalScope);
114 } catch (SystemAssemblyException e) {
115 // e.printStackTrace();
121 public void testWithRequirement() {
122 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
123 required.put(new DefaultRequiredInterface("hello", HelloService.class),
125 SpringComponent system = new SpringComponent("system",
126 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
127 new HashMap<String, ProvidedInterface>(), required);
129 HelloService helloObject = new HelloService("ladida");
130 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
132 Scope scope = new DefaultScope(new ProvidedInterface[]{ helloService });
133 scope.publishInterface(helloService, helloObject);
134 system.getRequiredInterfaces()[0].setProvider(helloService);
136 Scope runtime = system.start(scope);
137 system.stop(runtime);
140 public void testWithRequirementAndProvidedService() {
141 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
142 required.put(new DefaultRequiredInterface("hello", HelloService.class),
144 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
145 provided.put("blaService", new DefaultProvidedInterface("bla",
148 SpringComponent system = new SpringComponent("system",
149 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, provided,
152 HelloService helloObject = new HelloService("ladida");
153 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
155 Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
156 scope.publishInterface(helloService, helloObject);
157 system.getRequiredInterfaces()[0].setProvider(helloService);
158 Scope runtime = system.start(scope);
159 ProvidedInterface started = runtime.getProvidedInterfaces()[0];
161 Object impl = runtime.getInterfaceImplementation(started, BlaService.class);
163 assertTrue(impl instanceof BlaService);
164 assertEquals("ladida", ((BlaService)impl).execute());
165 system.stop(runtime);