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.AbstractProvidedInterfaceDescriptor;
12 import org.wamblee.system.AbstractProvidedInterfaceDescriptor;
13 import org.wamblee.system.DefaultRequiredInterfaceDescriptor;
14 import org.wamblee.system.DefaultServiceRegistry;
15 import org.wamblee.system.ProvidedInterfaceDescriptor;
16 import org.wamblee.system.RequiredInterfaceDescriptor;
17 import org.wamblee.system.Service;
18 import org.wamblee.system.InterfaceDescriptor;
19 import org.wamblee.system.ServiceRegistry;
20 import org.wamblee.system.SystemAssemblyException;
22 public class SpringComponentTest extends TestCase {
24 private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
25 private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
26 private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
27 private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
29 private ServiceRegistry _registry;
32 protected void setUp() throws Exception {
34 _registry = new DefaultServiceRegistry();
37 public void testBlackboxSystem() {
38 SpringComponent system = new SpringComponent("system", _registry,
39 new String[] { HELLO_SERVICE_SPRING_XML },
40 new HashMap<String, ProvidedInterfaceDescriptor>(),
41 new HashMap<RequiredInterfaceDescriptor, String>());
42 system.start("Hello", new Service[0]);
43 Service[] services = system.getRunningServices();
44 assertEquals(0, services.length);
49 public void testOneProvidedService() {
50 Map<String, ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
51 provided.put("helloService", new AbstractProvidedInterfaceDescriptor(
52 "hello", HelloService.class));
54 SpringComponent system = new SpringComponent("system", _registry,
55 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
56 new HashMap<RequiredInterfaceDescriptor, String>());
57 system.start("Hello", new Service[0]);
58 Service[] services = system.getRunningServices();
59 assertEquals(1, services.length);
60 assertTrue(services[0].reference(HelloService.class) instanceof HelloService);
61 assertEquals("Hello world!", services[0].reference(HelloService.class)
66 public void testWithProperties() throws IOException {
67 Map<String, ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
68 provided.put("helloService", new AbstractProvidedInterfaceDescriptor(
69 "hello", HelloService.class));
70 SpringComponent system = new SpringComponent("system", _registry,
71 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
73 new HashMap<RequiredInterfaceDescriptor, String>());
74 Properties props = new Properties();
75 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
76 system.addProperties(props);
78 system.start("Hello", new Service[0]);
79 Service[] services = system.getRunningServices();
80 assertEquals("Property Value", services[0].reference(HelloService.class).say());
83 public void testWithMissingRequirement() {
85 SpringComponent system = new SpringComponent("system", _registry,
86 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
87 new HashMap<String, ProvidedInterfaceDescriptor>(),
88 new HashMap<RequiredInterfaceDescriptor, String>());
89 system.start("Bla", new Service[0]);
90 } catch (SystemAssemblyException e) {
91 //e.printStackTrace();
97 public void testWithRequirement() {
98 Map<RequiredInterfaceDescriptor, String> required = new HashMap<RequiredInterfaceDescriptor, String>();
99 required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
101 SpringComponent system = new SpringComponent("system", _registry,
102 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
103 new HashMap<String, ProvidedInterfaceDescriptor>(), required);
105 HelloService helloObject = new HelloService("ladida");
106 Service helloService = _registry.register(new AbstractProvidedInterfaceDescriptor("hello", HelloService.class), helloObject);
107 system.start("Bla", new Service[] { helloService } );
111 public void testWithRequirementAndProvidedService() {
112 Map<RequiredInterfaceDescriptor, String> required = new HashMap<RequiredInterfaceDescriptor, String>();
113 required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
115 Map<String,ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
116 provided.put("blaService", new AbstractProvidedInterfaceDescriptor("bla",
119 SpringComponent system = new SpringComponent("system", _registry,
120 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
123 HelloService helloObject = new HelloService("ladida");
124 Service helloService = _registry.register(new AbstractProvidedInterfaceDescriptor("hello", HelloService.class), helloObject);
125 Service[] services = system.start("Bla", new Service[] { helloService } );
126 assertEquals(1, services.length);
128 assertTrue(services[0].reference(BlaService.class) instanceof BlaService);
129 assertEquals("ladida", services[0].reference(BlaService.class)