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;
34 public class SpringComponentTest extends TestCase {
36 private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
37 private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
38 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
39 private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
42 protected void setUp() throws Exception {
46 public void testBlackboxSystem() {
47 SpringComponent system = new SpringComponent("system",
48 new String[] { HELLO_SERVICE_SPRING_XML },
49 new HashMap<String, ProvidedInterface>(),
50 new HashMap<RequiredInterface, String>());
51 Scope runtime = system.start();
52 assertEquals(0, runtime.getProvidedInterfaces().length);
57 public void testOneProvidedService() {
58 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
59 provided.put("helloService", new DefaultProvidedInterface("hello",
62 SpringComponent system = new SpringComponent("system",
63 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
64 new HashMap<RequiredInterface, String>());
65 Scope runtime = system.start();
66 ProvidedInterface[] services = runtime.getProvidedInterfaces();
67 assertEquals(1, services.length);
68 Object service = runtime.getInterfaceImplementation(services[0], Object.class);
69 assertTrue(service instanceof HelloService);
70 assertEquals("Hello world!", ((HelloService) service).say());
74 public void testWithProperties() throws IOException {
75 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
76 provided.put("helloService", new DefaultProvidedInterface("hello",
78 SpringComponent system = new SpringComponent("system",
79 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
80 provided, new HashMap<RequiredInterface, String>());
81 Properties props = new Properties();
82 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
83 system.addProperties(props);
85 Scope scope = system.start();
86 ProvidedInterface[] services = scope.getProvidedInterfaces();
87 assertEquals("Property Value", scope.getInterfaceImplementation(services[0], HelloService.class).say());
90 public void testWithMissingRequirement() {
92 SpringComponent system = new SpringComponent("system",
93 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
94 new HashMap<String, ProvidedInterface>(),
95 new HashMap<RequiredInterface, String>());
97 } catch (SystemAssemblyException e) {
98 // e.printStackTrace();
104 public void testWithRequirement() {
105 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
106 required.put(new DefaultRequiredInterface("hello", HelloService.class),
108 SpringComponent system = new SpringComponent("system",
109 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
110 new HashMap<String, ProvidedInterface>(), required);
112 HelloService helloObject = new HelloService("ladida");
113 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
115 Scope scope = new DefaultScope(new ProvidedInterface[]{ helloService });
116 scope.publishInterface(helloService, helloObject);
117 system.getRequiredInterfaces()[0].setProvider(helloService);
119 Scope runtime = system.start(scope);
120 system.stop(runtime);
123 public void testWithRequirementAndProvidedService() {
124 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
125 required.put(new DefaultRequiredInterface("hello", HelloService.class),
127 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
128 provided.put("blaService", new DefaultProvidedInterface("bla",
131 SpringComponent system = new SpringComponent("system",
132 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, provided,
135 HelloService helloObject = new HelloService("ladida");
136 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
138 Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
139 scope.publishInterface(helloService, helloObject);
140 system.getRequiredInterfaces()[0].setProvider(helloService);
141 Scope runtime = system.start(scope);
142 ProvidedInterface started = runtime.getProvidedInterfaces()[0];
144 Object impl = runtime.getInterfaceImplementation(started, BlaService.class);
146 assertTrue(impl instanceof BlaService);
147 assertEquals("ladida", ((BlaService)impl).execute());
148 system.stop(runtime);