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