Added getStatus() method and now also testing for the correct status.
[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.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;
18
19 public class SpringSystemTest extends TestCase {
20
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";
23
24         private ServiceRegistry _registry;
25
26         @Override
27         protected void setUp() throws Exception {
28                 super.setUp();
29                 _registry = new DefaultServiceRegistry();
30         }
31
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);
40                 
41                 system.stop();
42         }
43
44         public void testOneProvidedService() {
45                 Map<String, ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
46                 provided.put("helloService", new DefaultProvidedInterfaceDescriptor(
47                                 "hello", HelloService.class));
48
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)
57                                 .say());
58                 system.stop();
59         }
60
61         public void testWithMissingRequiremnt() {
62                 try {
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();
70                         return;
71                 }
72                 fail();
73         }
74
75         public void testWithRequirement() {
76                 Map<RequiredInterfaceDescriptor, String> required = new HashMap<RequiredInterfaceDescriptor, String>();
77                 required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
78                                 "helloService");
79                 SpringSystem system = new SpringSystem("system", _registry,
80                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
81                                 new HashMap<String, ProvidedInterfaceDescriptor>(), required);
82                 
83                 HelloService helloObject = new HelloService("ladida"); 
84                 Service helloService = _registry.register(new DefaultProvidedInterfaceDescriptor("hello", HelloService.class), helloObject);
85                 system.start("Bla", new Service[] { helloService } );
86                 system.stop();
87         }
88         
89         public void testWithRequirementAndProvidedService() {
90                 Map<RequiredInterfaceDescriptor, String> required = new HashMap<RequiredInterfaceDescriptor, String>();
91                 required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
92                                 "helloService");
93                 Map<String,ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
94                 provided.put("blaService", new DefaultProvidedInterfaceDescriptor("bla",
95                                 BlaService.class));
96
97                 SpringSystem system = new SpringSystem("system", _registry,
98                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
99                                 provided, required);
100                 
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);
105                 
106                 assertTrue(services[0].reference(BlaService.class) instanceof BlaService);
107                 assertEquals("ladida", services[0].reference(BlaService.class)
108                                 .execute());
109                 system.stop();
110         }
111
112 }