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.ProvidedInterface;
29 import org.wamblee.system.core.RequiredInterface;
30 import org.wamblee.system.core.SystemAssemblyException;
32 public class SpringComponentTest extends TestCase {
34 private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
35 private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
36 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
37 private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
40 protected void setUp() throws Exception {
44 public void testBlackboxSystem() {
45 SpringComponent system = new SpringComponent("system",
46 new String[] { HELLO_SERVICE_SPRING_XML },
47 new HashMap<String, ProvidedInterface>(),
48 new HashMap<RequiredInterface, String>());
50 ProvidedInterface[] services = system.getRunningInterfaces();
51 assertEquals(0, services.length);
56 public void testOneProvidedService() {
57 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
58 provided.put("helloService", new DefaultProvidedInterface(
59 "hello", HelloService.class));
61 SpringComponent system = new SpringComponent("system",
62 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
63 new HashMap<RequiredInterface, String>());
65 ProvidedInterface[] services = system.getRunningInterfaces();
66 assertEquals(1, services.length);
67 assertTrue(services[0].getImplementation() instanceof HelloService);
68 assertEquals("Hello world!", ((HelloService)services[0].getImplementation())
73 public void testWithProperties() throws IOException {
74 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
75 provided.put("helloService", new DefaultProvidedInterface(
76 "hello", HelloService.class));
77 SpringComponent system = new SpringComponent("system",
78 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
80 new HashMap<RequiredInterface, String>());
81 Properties props = new Properties();
82 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
83 system.addProperties(props);
86 ProvidedInterface[] services = system.getRunningInterfaces();
87 assertEquals("Property Value",
88 ((HelloService)services[0].getImplementation()).say());
91 public void testWithMissingRequirement() {
93 SpringComponent system = new SpringComponent("system",
94 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
95 new HashMap<String, ProvidedInterface>(),
96 new HashMap<RequiredInterface, String>());
98 } catch (SystemAssemblyException e) {
99 //e.printStackTrace();
105 public void testWithRequirement() {
106 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
107 required.put(new DefaultRequiredInterface("hello", HelloService.class),
109 SpringComponent system = new SpringComponent("system",
110 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
111 new HashMap<String, ProvidedInterface>(), required);
113 HelloService helloObject = new HelloService("ladida");
114 ProvidedInterface helloService = new DefaultProvidedInterface("hello", HelloService.class);
115 helloService.publish(helloObject);
116 system.getRequiredInterfaces()[0].setProvider(helloService);
122 public void testWithRequirementAndProvidedService() {
123 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
124 required.put(new DefaultRequiredInterface("hello", HelloService.class),
126 Map<String,ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
127 provided.put("blaService", new DefaultProvidedInterface("bla",
130 SpringComponent system = new SpringComponent("system",
131 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
134 HelloService helloObject = new HelloService("ladida");
135 ProvidedInterface helloService =
136 new DefaultProvidedInterface("hello", HelloService.class);
137 helloService.publish(helloObject);
138 system.getRequiredInterfaces()[0].setProvider(helloService);
140 ProvidedInterface started = system.getProvidedInterfaces()[0];
142 assertNotNull(started.getImplementation());
143 assertTrue(started.getImplementation() instanceof BlaService);
144 assertEquals("ladida",
145 ((BlaService)started.getImplementation()).execute());