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.DefaultProvidedInterfaceDescriptor;
12 import org.wamblee.system.DefaultRequiredInterfaceDescriptor;
13 import org.wamblee.system.DefaultService;
14 import org.wamblee.system.ProvidedInterface;
15 import org.wamblee.system.RequiredInterface;
16 import org.wamblee.system.Service;
17 import org.wamblee.system.SystemAssemblyException;
19 public class SpringComponentTest extends TestCase {
21 private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
22 private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
23 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
24 private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
27 protected void setUp() throws Exception {
31 public void testBlackboxSystem() {
32 SpringComponent system = new SpringComponent("system",
33 new String[] { HELLO_SERVICE_SPRING_XML },
34 new HashMap<String, ProvidedInterface>(),
35 new HashMap<RequiredInterface, String>());
36 system.start("Hello");
37 ProvidedInterface[] services = system.getRunningServices();
38 assertEquals(0, services.length);
43 public void testOneProvidedService() {
44 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
45 provided.put("helloService", new DefaultProvidedInterfaceDescriptor(
46 "hello", HelloService.class));
48 SpringComponent system = new SpringComponent("system",
49 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
50 new HashMap<RequiredInterface, String>());
51 system.start("Hello");
52 ProvidedInterface[] services = system.getRunningServices();
53 assertEquals(1, services.length);
54 assertTrue(services[0].getImplementation() instanceof HelloService);
55 assertEquals("Hello world!", ((HelloService)services[0].getImplementation())
60 public void testWithProperties() throws IOException {
61 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
62 provided.put("helloService", new DefaultProvidedInterfaceDescriptor(
63 "hello", HelloService.class));
64 SpringComponent system = new SpringComponent("system",
65 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
67 new HashMap<RequiredInterface, String>());
68 Properties props = new Properties();
69 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
70 system.addProperties(props);
72 system.start("Hello");
73 ProvidedInterface[] services = system.getRunningServices();
74 assertEquals("Property Value",
75 ((HelloService)services[0].getImplementation()).say());
78 public void testWithMissingRequirement() {
80 SpringComponent system = new SpringComponent("system",
81 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
82 new HashMap<String, ProvidedInterface>(),
83 new HashMap<RequiredInterface, String>());
85 } catch (SystemAssemblyException e) {
86 //e.printStackTrace();
92 public void testWithRequirement() {
93 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
94 required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
96 SpringComponent system = new SpringComponent("system",
97 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
98 new HashMap<String, ProvidedInterface>(), required);
100 HelloService helloObject = new HelloService("ladida");
101 ProvidedInterface helloService = new DefaultProvidedInterfaceDescriptor("hello", HelloService.class);
102 helloService.publish(helloObject);
103 system.getRequiredServices()[0].setProvider(helloService);
109 public void testWithRequirementAndProvidedService() {
110 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
111 required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
113 Map<String,ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
114 provided.put("blaService", new DefaultProvidedInterfaceDescriptor("bla",
117 SpringComponent system = new SpringComponent("system",
118 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
121 HelloService helloObject = new HelloService("ladida");
122 ProvidedInterface helloService =
123 new DefaultProvidedInterfaceDescriptor("hello", HelloService.class);
124 helloService.publish(helloObject);
125 system.getRequiredServices()[0].setProvider(helloService);
127 ProvidedInterface started = system.getProvidedServices()[0];
129 assertNotNull(started.getImplementation());
130 assertTrue(started.getImplementation() instanceof BlaService);
131 assertEquals("ladida",
132 ((BlaService)started.getImplementation()).execute());