1 package org.wamblee.system.spring;
3 import java.io.IOException;
4 import java.util.HashMap;
6 import java.util.Properties;
8 import junit.framework.TestCase;
10 import org.wamblee.io.ClassPathResource;
11 import org.wamblee.system.DefaultProvidedInterface;
12 import org.wamblee.system.DefaultRequiredInterface;
13 import org.wamblee.system.ProvidedInterface;
14 import org.wamblee.system.RequiredInterface;
15 import org.wamblee.system.SystemAssemblyException;
17 public class SpringComponentTest extends TestCase {
19 private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
20 private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
21 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
22 private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
25 protected void setUp() throws Exception {
29 public void testBlackboxSystem() {
30 SpringComponent system = new SpringComponent("system",
31 new String[] { HELLO_SERVICE_SPRING_XML },
32 new HashMap<String, ProvidedInterface>(),
33 new HashMap<RequiredInterface, String>());
34 system.start("Hello");
35 ProvidedInterface[] services = system.getRunningServices();
36 assertEquals(0, services.length);
41 public void testOneProvidedService() {
42 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
43 provided.put("helloService", new DefaultProvidedInterface(
44 "hello", HelloService.class));
46 SpringComponent system = new SpringComponent("system",
47 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
48 new HashMap<RequiredInterface, String>());
49 system.start("Hello");
50 ProvidedInterface[] services = system.getRunningServices();
51 assertEquals(1, services.length);
52 assertTrue(services[0].getImplementation() instanceof HelloService);
53 assertEquals("Hello world!", ((HelloService)services[0].getImplementation())
58 public void testWithProperties() throws IOException {
59 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
60 provided.put("helloService", new DefaultProvidedInterface(
61 "hello", HelloService.class));
62 SpringComponent system = new SpringComponent("system",
63 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
65 new HashMap<RequiredInterface, String>());
66 Properties props = new Properties();
67 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
68 system.addProperties(props);
70 system.start("Hello");
71 ProvidedInterface[] services = system.getRunningServices();
72 assertEquals("Property Value",
73 ((HelloService)services[0].getImplementation()).say());
76 public void testWithMissingRequirement() {
78 SpringComponent system = new SpringComponent("system",
79 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
80 new HashMap<String, ProvidedInterface>(),
81 new HashMap<RequiredInterface, String>());
83 } catch (SystemAssemblyException e) {
84 //e.printStackTrace();
90 public void testWithRequirement() {
91 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
92 required.put(new DefaultRequiredInterface("hello", HelloService.class),
94 SpringComponent system = new SpringComponent("system",
95 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
96 new HashMap<String, ProvidedInterface>(), required);
98 HelloService helloObject = new HelloService("ladida");
99 ProvidedInterface helloService = new DefaultProvidedInterface("hello", HelloService.class);
100 helloService.publish(helloObject);
101 system.getRequiredServices()[0].setProvider(helloService);
107 public void testWithRequirementAndProvidedService() {
108 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
109 required.put(new DefaultRequiredInterface("hello", HelloService.class),
111 Map<String,ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
112 provided.put("blaService", new DefaultProvidedInterface("bla",
115 SpringComponent system = new SpringComponent("system",
116 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
119 HelloService helloObject = new HelloService("ladida");
120 ProvidedInterface helloService =
121 new DefaultProvidedInterface("hello", HelloService.class);
122 helloService.publish(helloObject);
123 system.getRequiredServices()[0].setProvider(helloService);
125 ProvidedInterface started = system.getProvidedServices()[0];
127 assertNotNull(started.getImplementation());
128 assertTrue(started.getImplementation() instanceof BlaService);
129 assertEquals("ladida",
130 ((BlaService)started.getImplementation()).execute());