(no commit message)
[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.DefaultProvidedInterfaceDescriptor;
12 import org.wamblee.system.DefaultRequiredInterfaceDescriptor;
13 import org.wamblee.system.DefaultServiceRegistry;
14 import org.wamblee.system.ProvidedInterfaceDescriptor;
15 import org.wamblee.system.RequiredInterfaceDescriptor;
16 import org.wamblee.system.Service;
17 import org.wamblee.system.ServiceRegistry;
18 import org.wamblee.system.SystemAssemblyException;
19
20 public class SpringComponentTest extends TestCase {
21
22         private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
23         private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
24         private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
25     private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
26
27         private ServiceRegistry _registry;
28
29         @Override
30         protected void setUp() throws Exception {
31                 super.setUp();
32                 _registry = new DefaultServiceRegistry();
33         }
34
35         public void testBlackboxSystem() {
36                 SpringComponent system = new SpringComponent("system", _registry,
37                                 new String[] { HELLO_SERVICE_SPRING_XML },
38                                 new HashMap<String, ProvidedInterfaceDescriptor>(),
39                                 new HashMap<RequiredInterfaceDescriptor, String>());
40                 system.start("Hello", new Service[0]);
41                 Service[] services = system.getRunningServices();
42                 assertEquals(0, services.length);
43                 
44                 system.stop();
45         }
46
47         public void testOneProvidedService() {
48                 Map<String, ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
49                 provided.put("helloService", new DefaultProvidedInterfaceDescriptor(
50                                 "hello", HelloService.class));
51
52                 SpringComponent system = new SpringComponent("system", _registry,
53                                 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
54                                 new HashMap<RequiredInterfaceDescriptor, String>());
55                 system.start("Hello", new Service[0]);
56                 Service[] services = system.getRunningServices();
57                 assertEquals(1, services.length);
58                 assertTrue(services[0].reference(HelloService.class) instanceof HelloService);
59                 assertEquals("Hello world!", services[0].reference(HelloService.class)
60                                 .say());
61                 system.stop();
62         }
63         
64         public void testWithProperties() throws IOException {
65                 Map<String, ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
66                 provided.put("helloService", new DefaultProvidedInterfaceDescriptor(
67                                 "hello", HelloService.class));
68                 SpringComponent system = new SpringComponent("system", _registry,
69                                 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
70                                 provided,
71                                 new HashMap<RequiredInterfaceDescriptor, String>());
72                 Properties props = new Properties();
73                 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
74                 system.addProperties(props);
75                 
76                 system.start("Hello", new Service[0]);
77                 Service[] services = system.getRunningServices();
78                 assertEquals("Property Value", services[0].reference(HelloService.class).say());
79         }
80
81         public void testWithMissingRequirement() {
82                 try {
83                         SpringComponent system = new SpringComponent("system", _registry,
84                                         new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
85                                         new HashMap<String, ProvidedInterfaceDescriptor>(),
86                                         new HashMap<RequiredInterfaceDescriptor, String>());
87                         system.start("Bla", new Service[0]);
88                 } catch (SystemAssemblyException e) {
89                         //e.printStackTrace();
90                         return;
91                 }
92                 fail();
93         }
94
95         public void testWithRequirement() {
96                 Map<RequiredInterfaceDescriptor, String> required = new HashMap<RequiredInterfaceDescriptor, String>();
97                 required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
98                                 "helloService");
99                 SpringComponent system = new SpringComponent("system", _registry,
100                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
101                                 new HashMap<String, ProvidedInterfaceDescriptor>(), required);
102                 
103                 HelloService helloObject = new HelloService("ladida"); 
104                 Service helloService = _registry.register(new DefaultProvidedInterfaceDescriptor("hello", HelloService.class), helloObject);
105                 system.start("Bla", new Service[] { helloService } );
106                 system.stop();
107         }
108         
109         public void testWithRequirementAndProvidedService() {
110                 Map<RequiredInterfaceDescriptor, String> required = new HashMap<RequiredInterfaceDescriptor, String>();
111                 required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
112                                 "helloService");
113                 Map<String,ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
114                 provided.put("blaService", new DefaultProvidedInterfaceDescriptor("bla",
115                                 BlaService.class));
116
117                 SpringComponent system = new SpringComponent("system", _registry,
118                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
119                                 provided, required);
120                 
121                 HelloService helloObject = new HelloService("ladida"); 
122                 Service helloService = _registry.register(new DefaultProvidedInterfaceDescriptor("hello", HelloService.class), helloObject);
123                 Service[] services = system.start("Bla", new Service[] { helloService } );
124                 assertEquals(1, services.length);
125                 
126                 assertTrue(services[0].reference(BlaService.class) instanceof BlaService);
127                 assertEquals("ladida", services[0].reference(BlaService.class)
128                                 .execute());
129                 system.stop();
130         }
131
132 }