1 package org.wamblee.system.spring;
3 import java.util.HashMap;
6 import junit.framework.TestCase;
8 import org.wamblee.system.AbstractInterfaceDescriptor;
9 import org.wamblee.system.DefaultProvidedInterfaceDescriptor;
10 import org.wamblee.system.DefaultRequiredInterfaceDescriptor;
11 import org.wamblee.system.DefaultServiceRegistry;
12 import org.wamblee.system.ProvidedInterfaceDescriptor;
13 import org.wamblee.system.RequiredInterfaceDescriptor;
14 import org.wamblee.system.Service;
15 import org.wamblee.system.InterfaceDescriptor;
16 import org.wamblee.system.ServiceRegistry;
17 import org.wamblee.system.SystemAssemblyException;
19 public class SpringSystemTest 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";
24 private ServiceRegistry _registry;
27 protected void setUp() throws Exception {
29 _registry = new DefaultServiceRegistry();
32 public void testBlackboxSystem() {
33 SpringSystem system = new SpringSystem("system", _registry,
34 new String[] { HELLO_SERVICE_SPRING_XML },
35 new HashMap<String, ProvidedInterfaceDescriptor>(),
36 new HashMap<RequiredInterfaceDescriptor, String>());
37 system.start("Hello", new Service[0]);
38 Service[] services = system.getRunningServices();
39 assertEquals(0, services.length);
44 public void testOneProvidedService() {
45 Map<String, ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
46 provided.put("helloService", new DefaultProvidedInterfaceDescriptor(
47 "hello", HelloService.class));
49 SpringSystem system = new SpringSystem("system", _registry,
50 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
51 new HashMap<RequiredInterfaceDescriptor, String>());
52 system.start("Hello", new Service[0]);
53 Service[] services = system.getRunningServices();
54 assertEquals(1, services.length);
55 assertTrue(services[0].reference(HelloService.class) instanceof HelloService);
56 assertEquals("Hello world!", services[0].reference(HelloService.class)
61 public void testWithMissingRequiremnt() {
63 SpringSystem system = new SpringSystem("system", _registry,
64 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
65 new HashMap<String, ProvidedInterfaceDescriptor>(),
66 new HashMap<RequiredInterfaceDescriptor, String>());
67 system.start("Bla", new Service[0]);
68 } catch (SystemAssemblyException e) {
69 //e.printStackTrace();
75 public void testWithRequirement() {
76 Map<RequiredInterfaceDescriptor, String> required = new HashMap<RequiredInterfaceDescriptor, String>();
77 required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
79 SpringSystem system = new SpringSystem("system", _registry,
80 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
81 new HashMap<String, ProvidedInterfaceDescriptor>(), required);
83 HelloService helloObject = new HelloService("ladida");
84 Service helloService = _registry.register(new DefaultProvidedInterfaceDescriptor("hello", HelloService.class), helloObject);
85 system.start("Bla", new Service[] { helloService } );
89 public void testWithRequirementAndProvidedService() {
90 Map<RequiredInterfaceDescriptor, String> required = new HashMap<RequiredInterfaceDescriptor, String>();
91 required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
93 Map<String,ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
94 provided.put("blaService", new DefaultProvidedInterfaceDescriptor("bla",
97 SpringSystem system = new SpringSystem("system", _registry,
98 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
101 HelloService helloObject = new HelloService("ladida");
102 Service helloService = _registry.register(new DefaultProvidedInterfaceDescriptor("hello", HelloService.class), helloObject);
103 Service[] services = system.start("Bla", new Service[] { helloService } );
104 assertEquals(1, services.length);
106 assertTrue(services[0].reference(BlaService.class) instanceof BlaService);
107 assertEquals("ladida", services[0].reference(BlaService.class)