(no commit message)
[utils] / system / spring / src / test / java / org / wamblee / system / spring / SpringComponentTest.java
1 /*
2  * Copyright 2007 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.system.spring;
17
18 import java.io.IOException;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Properties;
22
23 import junit.framework.TestCase;
24
25 import org.wamblee.io.ClassPathResource;
26 import org.wamblee.system.core.DefaultProvidedInterface;
27 import org.wamblee.system.core.DefaultRequiredInterface;
28 import org.wamblee.system.core.ProvidedInterface;
29 import org.wamblee.system.core.RequiredInterface;
30 import org.wamblee.system.core.SystemAssemblyException;
31
32 public class SpringComponentTest extends TestCase {
33
34         private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
35         private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
36         private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
37     private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
38
39         @Override
40         protected void setUp() throws Exception {
41                 super.setUp();
42         }
43
44         public void testBlackboxSystem() {
45                 SpringComponent system = new SpringComponent("system",
46                                 new String[] { HELLO_SERVICE_SPRING_XML },
47                                 new HashMap<String, ProvidedInterface>(),
48                                 new HashMap<RequiredInterface, String>());
49                 system.start();
50                 ProvidedInterface[] services = system.getRunningInterfaces();
51                 assertEquals(0, services.length);
52                 
53                 system.stop();
54         }
55
56         public void testOneProvidedService() {
57                 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
58                 provided.put("helloService", new DefaultProvidedInterface(
59                                 "hello", HelloService.class));
60
61                 SpringComponent system = new SpringComponent("system", 
62                                 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
63                                 new HashMap<RequiredInterface, String>());
64                 system.start();
65                 ProvidedInterface[] services = system.getRunningInterfaces();
66                 assertEquals(1, services.length);
67                 assertTrue(services[0].getImplementation() instanceof HelloService);
68                 assertEquals("Hello world!", ((HelloService)services[0].getImplementation())
69                                 .say());
70                 system.stop();
71         }
72         
73         public void testWithProperties() throws IOException {
74                 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
75                 provided.put("helloService", new DefaultProvidedInterface(
76                                 "hello", HelloService.class));
77                 SpringComponent system = new SpringComponent("system", 
78                                 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
79                                 provided,
80                                 new HashMap<RequiredInterface, String>());
81                 Properties props = new Properties();
82                 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
83                 system.addProperties(props);
84                 
85                 system.start();
86                 ProvidedInterface[] services = system.getRunningInterfaces();
87                 assertEquals("Property Value", 
88                                 ((HelloService)services[0].getImplementation()).say());
89         }
90
91         public void testWithMissingRequirement() {
92                 try {
93                         SpringComponent system = new SpringComponent("system",
94                                         new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
95                                         new HashMap<String, ProvidedInterface>(),
96                                         new HashMap<RequiredInterface, String>());
97                         system.start();
98                 } catch (SystemAssemblyException e) {
99                         //e.printStackTrace();
100                         return;
101                 }
102                 fail();
103         }
104
105         public void testWithRequirement() {
106                 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
107                 required.put(new DefaultRequiredInterface("hello", HelloService.class),
108                                 "helloService");
109                 SpringComponent system = new SpringComponent("system",
110                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
111                                 new HashMap<String, ProvidedInterface>(), required);
112                 
113                 HelloService helloObject = new HelloService("ladida"); 
114                 ProvidedInterface helloService = new DefaultProvidedInterface("hello", HelloService.class);
115                 helloService.publish(helloObject);
116                 system.getRequiredInterfaces()[0].setProvider(helloService);
117                 
118                 system.start();
119                 system.stop();
120         }
121         
122         public void testWithRequirementAndProvidedService() {
123                 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
124                 required.put(new DefaultRequiredInterface("hello", HelloService.class),
125                                 "helloService");
126                 Map<String,ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
127                 provided.put("blaService", new DefaultProvidedInterface("bla",
128                                 BlaService.class));
129
130                 SpringComponent system = new SpringComponent("system",
131                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
132                                 provided, required);
133                 
134                 HelloService helloObject = new HelloService("ladida"); 
135                 ProvidedInterface helloService = 
136                         new DefaultProvidedInterface("hello", HelloService.class);
137                 helloService.publish(helloObject);
138                 system.getRequiredInterfaces()[0].setProvider(helloService);
139                 system.start();
140                 ProvidedInterface started = system.getProvidedInterfaces()[0];
141                 
142             assertNotNull(started.getImplementation());
143             assertTrue(started.getImplementation() instanceof BlaService);
144                 assertEquals("ladida", 
145                                 ((BlaService)started.getImplementation()).execute());
146                 system.stop();
147         }
148
149 }