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.DefaultServiceRegistry;
14 import org.wamblee.system.ProvidedInterface;
15 import org.wamblee.system.RequiredInterface;
16 import org.wamblee.system.Service;
17 import org.wamblee.system.ServiceRegistry;
18 import org.wamblee.system.SystemAssemblyException;
20 public class SpringComponentTest extends TestCase {
22 private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
23 private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
24 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
25 private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
27 private ServiceRegistry _registry;
30 protected void setUp() throws Exception {
32 _registry = new DefaultServiceRegistry();
35 public void testBlackboxSystem() {
36 SpringComponent system = new SpringComponent("system", _registry,
37 new String[] { HELLO_SERVICE_SPRING_XML },
38 new HashMap<String, ProvidedInterface>(),
39 new HashMap<RequiredInterface, String>());
40 system.start("Hello", new Service[0]);
41 Service[] services = system.getRunningServices();
42 assertEquals(0, services.length);
47 public void testOneProvidedService() {
48 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
49 provided.put("helloService", new DefaultProvidedInterfaceDescriptor(
50 "hello", HelloService.class));
52 SpringComponent system = new SpringComponent("system", _registry,
53 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
54 new HashMap<RequiredInterface, String>());
55 system.start("Hello", new Service[0]);
56 Service[] services = system.getRunningServices();
57 assertEquals(1, services.length);
58 assertTrue(services[0].reference(HelloService.class) instanceof HelloService);
59 assertEquals("Hello world!", services[0].reference(HelloService.class)
64 public void testWithProperties() throws IOException {
65 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
66 provided.put("helloService", new DefaultProvidedInterfaceDescriptor(
67 "hello", HelloService.class));
68 SpringComponent system = new SpringComponent("system", _registry,
69 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
71 new HashMap<RequiredInterface, String>());
72 Properties props = new Properties();
73 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
74 system.addProperties(props);
76 system.start("Hello", new Service[0]);
77 Service[] services = system.getRunningServices();
78 assertEquals("Property Value", services[0].reference(HelloService.class).say());
81 public void testWithMissingRequirement() {
83 SpringComponent system = new SpringComponent("system", _registry,
84 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
85 new HashMap<String, ProvidedInterface>(),
86 new HashMap<RequiredInterface, String>());
87 system.start("Bla", new Service[0]);
88 } catch (SystemAssemblyException e) {
89 //e.printStackTrace();
95 public void testWithRequirement() {
96 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
97 required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
99 SpringComponent system = new SpringComponent("system", _registry,
100 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
101 new HashMap<String, ProvidedInterface>(), required);
103 HelloService helloObject = new HelloService("ladida");
104 Service helloService = _registry.register(new DefaultProvidedInterfaceDescriptor("hello", HelloService.class), helloObject);
105 system.start("Bla", new Service[] { 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", _registry,
118 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
121 HelloService helloObject = new HelloService("ladida");
122 Service helloService = _registry.register(new DefaultProvidedInterfaceDescriptor("hello", HelloService.class), helloObject);
123 Service[] services = system.start("Bla", new Service[] { helloService } );
124 assertEquals(1, services.length);
126 assertTrue(services[0].reference(BlaService.class) instanceof BlaService);
127 assertEquals("ladida", services[0].reference(BlaService.class)