Added getStatus() method and now also testing for the correct status.
[utils] / system / spring / src / test / java / org / wamblee / system / spring / SpringComponentTest.java
1 package org.wamblee.system.spring;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Properties;
7
8 import junit.framework.TestCase;
9
10 import org.wamblee.io.ClassPathResource;
11 import org.wamblee.system.AbstractInterfaceDescriptor;
12 import org.wamblee.system.DefaultProvidedInterfaceDescriptor;
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;
21
22 public class SpringComponentTest extends TestCase {
23
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";
28
29         private ServiceRegistry _registry;
30
31         @Override
32         protected void setUp() throws Exception {
33                 super.setUp();
34                 _registry = new DefaultServiceRegistry();
35         }
36
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);
45                 
46                 system.stop();
47         }
48
49         public void testOneProvidedService() {
50                 Map<String, ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
51                 provided.put("helloService", new DefaultProvidedInterfaceDescriptor(
52                                 "hello", HelloService.class));
53
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)
62                                 .say());
63                 system.stop();
64         }
65         
66         public void testWithProperties() throws IOException {
67                 Map<String, ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
68                 provided.put("helloService", new DefaultProvidedInterfaceDescriptor(
69                                 "hello", HelloService.class));
70                 SpringComponent system = new SpringComponent("system", _registry,
71                                 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
72                                 provided,
73                                 new HashMap<RequiredInterfaceDescriptor, String>());
74                 Properties props = new Properties();
75                 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
76                 system.addProperties(props);
77                 
78                 system.start("Hello", new Service[0]);
79                 Service[] services = system.getRunningServices();
80                 assertEquals("Property Value", services[0].reference(HelloService.class).say());
81         }
82
83         public void testWithMissingRequirement() {
84                 try {
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();
92                         return;
93                 }
94                 fail();
95         }
96
97         public void testWithRequirement() {
98                 Map<RequiredInterfaceDescriptor, String> required = new HashMap<RequiredInterfaceDescriptor, String>();
99                 required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
100                                 "helloService");
101                 SpringComponent system = new SpringComponent("system", _registry,
102                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
103                                 new HashMap<String, ProvidedInterfaceDescriptor>(), required);
104                 
105                 HelloService helloObject = new HelloService("ladida"); 
106                 Service helloService = _registry.register(new DefaultProvidedInterfaceDescriptor("hello", HelloService.class), helloObject);
107                 system.start("Bla", new Service[] { helloService } );
108                 system.stop();
109         }
110         
111         public void testWithRequirementAndProvidedService() {
112                 Map<RequiredInterfaceDescriptor, String> required = new HashMap<RequiredInterfaceDescriptor, String>();
113                 required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
114                                 "helloService");
115                 Map<String,ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
116                 provided.put("blaService", new DefaultProvidedInterfaceDescriptor("bla",
117                                 BlaService.class));
118
119                 SpringComponent system = new SpringComponent("system", _registry,
120                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
121                                 provided, required);
122                 
123                 HelloService helloObject = new HelloService("ladida"); 
124                 Service helloService = _registry.register(new DefaultProvidedInterfaceDescriptor("hello", HelloService.class), helloObject);
125                 Service[] services = system.start("Bla", new Service[] { helloService } );
126                 assertEquals(1, services.length);
127                 
128                 assertTrue(services[0].reference(BlaService.class) instanceof BlaService);
129                 assertEquals("ladida", services[0].reference(BlaService.class)
130                                 .execute());
131                 system.stop();
132         }
133
134 }