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