092a5bc11e47907ce74c93387388893ef8e55873
[utils] / system / spring / src / test / java / org / wamblee / system / spring / SpringSystemTest.java
1 package org.wamblee.system.spring;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import junit.framework.TestCase;
7
8 import org.wamblee.system.DefaultServiceDescriptor;
9 import org.wamblee.system.DefaultServiceRegistry;
10 import org.wamblee.system.Service;
11 import org.wamblee.system.ServiceDescriptor;
12 import org.wamblee.system.ServiceRegistry;
13 import org.wamblee.system.SystemAssemblyException;
14
15 public class SpringSystemTest extends TestCase {
16
17         private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
18         private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
19
20         private ServiceRegistry _registry;
21
22         @Override
23         protected void setUp() throws Exception {
24                 super.setUp();
25                 _registry = new DefaultServiceRegistry();
26         }
27
28         public void testBlackboxSystem() {
29                 SpringSystem system = new SpringSystem("system", _registry,
30                                 new String[] { HELLO_SERVICE_SPRING_XML },
31                                 new HashMap<String, ServiceDescriptor>(),
32                                 new HashMap<ServiceDescriptor, String>());
33                 system.start("Hello", new Service[0]);
34                 Service[] services = system.getRunningServices();
35                 assertEquals(0, services.length);
36                 
37                 system.stop();
38         }
39
40         public void testOneProvidedService() {
41                 Map<String, ServiceDescriptor> provided = new HashMap<String, ServiceDescriptor>();
42                 provided.put("helloService", new DefaultServiceDescriptor(
43                                 HelloService.class));
44
45                 SpringSystem system = new SpringSystem("system", _registry,
46                                 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
47                                 new HashMap<ServiceDescriptor, String>());
48                 system.start("Hello", new Service[0]);
49                 Service[] services = system.getRunningServices();
50                 assertEquals(1, services.length);
51                 assertTrue(services[0].reference(HelloService.class) instanceof HelloService);
52                 assertEquals("Hello world!", services[0].reference(HelloService.class)
53                                 .say());
54                 system.stop();
55         }
56
57         public void testWithMissingRequiremnt() {
58                 try {
59                         SpringSystem system = new SpringSystem("system", _registry,
60                                         new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
61                                         new HashMap<String, ServiceDescriptor>(),
62                                         new HashMap<ServiceDescriptor, String>());
63                         system.start("Bla", new Service[0]);
64                 } catch (SystemAssemblyException e) {
65                         //e.printStackTrace();
66                         return;
67                 }
68                 fail();
69         }
70
71         public void testWithRequirement() {
72                 Map<ServiceDescriptor, String> required = new HashMap<ServiceDescriptor, String>();
73                 required.put(new DefaultServiceDescriptor(HelloService.class),
74                                 "helloService");
75                 SpringSystem system = new SpringSystem("system", _registry,
76                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
77                                 new HashMap<String, ServiceDescriptor>(), required);
78                 
79                 HelloService helloObject = new HelloService("ladida"); 
80                 Service helloService = _registry.register(new DefaultServiceDescriptor(HelloService.class), helloObject);
81                 system.start("Bla", new Service[] { helloService } );
82                 system.stop();
83         }
84         
85         public void testWithRequirementAndProvidedService() {
86                 Map<ServiceDescriptor, String> required = new HashMap<ServiceDescriptor, String>();
87                 required.put(new DefaultServiceDescriptor(HelloService.class),
88                                 "helloService");
89                 Map<String,ServiceDescriptor> provided = new HashMap<String, ServiceDescriptor>();
90                 provided.put("blaService", new DefaultServiceDescriptor(
91                                 BlaService.class));
92
93                 SpringSystem system = new SpringSystem("system", _registry,
94                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
95                                 provided, required);
96                 
97                 HelloService helloObject = new HelloService("ladida"); 
98                 Service helloService = _registry.register(new DefaultServiceDescriptor(HelloService.class), helloObject);
99                 Service[] services = system.start("Bla", new Service[] { helloService } );
100                 assertEquals(1, services.length);
101                 
102                 assertTrue(services[0].reference(BlaService.class) instanceof BlaService);
103                 assertEquals("ladida", services[0].reference(BlaService.class)
104                                 .execute());
105                 system.stop();
106         }
107
108 }