1 package org.wamblee.system.spring;
3 import java.util.HashMap;
6 import junit.framework.TestCase;
8 import org.wamblee.system.AbstractServiceDescriptor;
9 import org.wamblee.system.DefaultProvidedServiceDescriptor;
10 import org.wamblee.system.DefaultRequiredServiceDescriptor;
11 import org.wamblee.system.DefaultServiceRegistry;
12 import org.wamblee.system.ProvidedServiceDescriptor;
13 import org.wamblee.system.RequiredServiceDescriptor;
14 import org.wamblee.system.Service;
15 import org.wamblee.system.ServiceDescriptor;
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, ProvidedServiceDescriptor>(),
36 new HashMap<RequiredServiceDescriptor, 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, ProvidedServiceDescriptor> provided = new HashMap<String, ProvidedServiceDescriptor>();
46 provided.put("helloService", new DefaultProvidedServiceDescriptor(
47 "hello", HelloService.class));
49 SpringSystem system = new SpringSystem("system", _registry,
50 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
51 new HashMap<RequiredServiceDescriptor, 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, ProvidedServiceDescriptor>(),
66 new HashMap<RequiredServiceDescriptor, String>());
67 system.start("Bla", new Service[0]);
68 } catch (SystemAssemblyException e) {
69 //e.printStackTrace();
75 public void testWithRequirement() {
76 Map<RequiredServiceDescriptor, String> required = new HashMap<RequiredServiceDescriptor, String>();
77 required.put(new DefaultRequiredServiceDescriptor("hello", HelloService.class),
79 SpringSystem system = new SpringSystem("system", _registry,
80 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
81 new HashMap<String, ProvidedServiceDescriptor>(), required);
83 HelloService helloObject = new HelloService("ladida");
84 Service helloService = _registry.register(new DefaultProvidedServiceDescriptor("hello", HelloService.class), helloObject);
85 system.start("Bla", new Service[] { helloService } );
89 public void testWithRequirementAndProvidedService() {
90 Map<RequiredServiceDescriptor, String> required = new HashMap<RequiredServiceDescriptor, String>();
91 required.put(new DefaultRequiredServiceDescriptor("hello", HelloService.class),
93 Map<String,ProvidedServiceDescriptor> provided = new HashMap<String, ProvidedServiceDescriptor>();
94 provided.put("blaService", new DefaultProvidedServiceDescriptor("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 DefaultProvidedServiceDescriptor("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)