ORMappingConfig is now provided by the datasource component.
[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.DefaultScope;
29 import org.wamblee.system.core.ProvidedInterface;
30 import org.wamblee.system.core.RequiredInterface;
31 import org.wamblee.system.core.Scope;
32 import org.wamblee.system.core.SystemAssemblyException;
33 import org.wamblee.test.EventTracker;
34
35 public class SpringComponentTest extends TestCase {
36
37     private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
38     private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
39     private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
40     private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 = "test.org.wamblee.system.springWithProperties2.xml";
41
42     private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
43
44     public static EventTracker<String> EVENT_TRACKER;
45
46     private Scope _externalScope;
47
48     @Override
49     protected void setUp() throws Exception {
50         super.setUp();
51         EVENT_TRACKER = new EventTracker<String>();
52         _externalScope = new DefaultScope(new ProvidedInterface[0]);
53     }
54
55     public void testBlackboxSystem() {
56         SpringComponent system = new SpringComponent("system",
57                 new String[] { HELLO_SERVICE_SPRING_XML },
58                 new HashMap<String, ProvidedInterface>(),
59                 new HashMap<RequiredInterface, String>());
60
61         Scope runtime = system.start(_externalScope);
62         assertEquals(0, _externalScope.getProvidedInterfaces().length);
63
64         system.stop(runtime);
65     }
66
67     public void testOneProvidedService() {
68         Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
69         provided.put("helloService", new DefaultProvidedInterface("hello",
70                 HelloService.class));
71
72         SpringComponent system = new SpringComponent("system",
73                 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
74                 new HashMap<RequiredInterface, String>());
75         Scope runtime = system.start(_externalScope);
76         ProvidedInterface[] services = runtime.getProvidedInterfaces();
77
78         assertEquals(1, services.length);
79         Object service = runtime.getInterfaceImplementation(services[0],
80                 Object.class);
81         assertTrue(service instanceof HelloService);
82
83         // BUG; Provided services should be made available in the external
84         // scope.
85         Object service2 = _externalScope.getInterfaceImplementation(provided
86                 .get("helloService"), Object.class);
87         assertSame(service, service2);
88
89         assertEquals("Hello world!", ((HelloService) service).say());
90         system.stop(runtime);
91     }
92
93     public void testWithProperties() throws IOException {
94         Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
95         provided.put("helloService", new DefaultProvidedInterface("hello",
96                 HelloService.class));
97         SpringComponent system = new SpringComponent("system",
98                 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
99                 provided, new HashMap<RequiredInterface, String>());
100         Properties props = new Properties();
101         props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
102         system.addProperties(props);
103
104         Scope scope = system.start(_externalScope);
105         // BUG: Hello service was constructed multiple times. Once with the
106         // unprocessed property
107         // and another time with the processed property.
108         assertEquals(1, EVENT_TRACKER.getEventCount());
109         ProvidedInterface[] services = scope.getProvidedInterfaces();
110         assertEquals("Property Value", scope.getInterfaceImplementation(
111                 services[0], HelloService.class).say());
112     }
113
114     public void testWithPropertiesAsBean() throws IOException {
115         Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
116         provided.put("helloService", new DefaultProvidedInterface("hello",
117                 HelloService2.class));
118         SpringComponent system = new SpringComponent("system",
119                 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 },
120                 provided, new HashMap<RequiredInterface, String>());
121         Properties props = new Properties();
122         props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
123         system.addProperties("properties", props);
124
125         Scope scope = system.start(_externalScope);
126
127         ProvidedInterface[] services = scope.getProvidedInterfaces();
128
129         Properties props2 = scope.getInterfaceImplementation(services[0],
130                 HelloService2.class).getProperties();
131         assertEquals(props, props2);
132     }
133
134     public void testWithMissingRequirement() {
135         try {
136             SpringComponent system = new SpringComponent("system",
137                     new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
138                     new HashMap<String, ProvidedInterface>(),
139                     new HashMap<RequiredInterface, String>());
140             system.start(_externalScope);
141         } catch (SystemAssemblyException e) {
142             // e.printStackTrace();
143             return;
144         }
145         fail();
146     }
147
148     public void testWithRequirement() {
149         Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
150         required.put(new DefaultRequiredInterface("hello", HelloService.class),
151                 "helloService");
152         SpringComponent system = new SpringComponent("system",
153                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
154                 new HashMap<String, ProvidedInterface>(), required);
155
156         HelloService helloObject = new HelloService("ladida");
157         ProvidedInterface helloService = new DefaultProvidedInterface("hello",
158                 HelloService.class);
159         Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
160         scope.publishInterface(helloService, helloObject);
161         system.getRequiredInterfaces()[0].setProvider(helloService);
162
163         Scope runtime = system.start(scope);
164         system.stop(runtime);
165     }
166
167     public void testWithRequirementAndProvidedService() {
168         Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
169         required.put(new DefaultRequiredInterface("hello", HelloService.class),
170                 "helloService");
171         Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
172         provided.put("blaService", new DefaultProvidedInterface("bla",
173                 BlaService.class));
174
175         SpringComponent system = new SpringComponent("system",
176                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, provided,
177                 required);
178
179         HelloService helloObject = new HelloService("ladida");
180         ProvidedInterface helloService = new DefaultProvidedInterface("hello",
181                 HelloService.class);
182         Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
183         scope.publishInterface(helloService, helloObject);
184         system.getRequiredInterfaces()[0].setProvider(helloService);
185         Scope runtime = system.start(scope);
186         ProvidedInterface started = runtime.getProvidedInterfaces()[0];
187
188         Object impl = runtime.getInterfaceImplementation(started,
189                 BlaService.class);
190         assertNotNull(impl);
191         assertTrue(impl instanceof BlaService);
192         assertEquals("ladida", ((BlaService) impl).execute());
193         system.stop(runtime);
194     }
195
196     /**
197      * Tests a scenario where a subclass of SpringComponent adds a new provided
198      * interface where the interface is provided by the subclass itself and not
199      * by the spring configs inside.
200      */
201     public void testWithProvidedFromSubClassNotFromConfig() {
202         Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
203         provided.put("helloService", new DefaultProvidedInterface("hello",
204                 HelloService.class));
205
206         SubSpringComponent system = new SubSpringComponent("system",
207                 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
208                 new HashMap<RequiredInterface, String>());
209
210         Scope runtime = system.start(_externalScope);
211         ProvidedInterface[] services = runtime.getProvidedInterfaces();
212
213         assertEquals(2, services.length);
214         Object service = runtime.getInterfaceImplementation(services[0],
215                 Object.class);
216         assertTrue(service instanceof HelloService);
217
218         // BUG; Provided services should be made available in the external
219         // scope.
220         Object service2 = _externalScope.getInterfaceImplementation(provided
221                 .get("helloService"), Object.class);
222         assertSame(service, service2);
223
224         Object floatsvc = _externalScope.getInterfaceImplementation(system
225                 .getProvidedInterfaces()[1], Object.class);
226         assertTrue(floatsvc instanceof Float);
227         assertTrue((((Float) floatsvc).floatValue() - 100.345f) < 0.00001);
228
229         assertEquals("Hello world!", ((HelloService) service).say());
230         system.stop(runtime);
231     }
232
233     /**
234      * Tests the spring component with an additional requirement from the subclass 
235      * which is not required by the spring config files inside. 
236      */
237     public void testWithRequirementFromSubClass() {
238         Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
239         required.put(new DefaultRequiredInterface("hello", HelloService.class),
240                 "helloService");
241         SpringComponent system = new SubSpringComponent2("system",
242                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
243                 new HashMap<String, ProvidedInterface>(), required);
244
245         HelloService helloObject = new HelloService("ladida");
246         ProvidedInterface helloService = new DefaultProvidedInterface("hello",
247                 HelloService.class);
248         
249         ProvidedInterface floatService = new DefaultProvidedInterface("float", Float.class);
250         
251         Scope scope = new DefaultScope(new ProvidedInterface[] { helloService });
252         scope.publishInterface(helloService, helloObject);
253         scope.publishInterface(floatService, 100.234f);
254         system.getRequiredInterfaces()[0].setProvider(helloService);
255         system.getRequiredInterfaces()[1].setProvider(floatService);
256
257         Scope runtime = system.start(scope);
258         system.stop(runtime);
259         
260         assertEquals(100.234f, ((Float)runtime.get("floatValue")).floatValue(), 0.0001f);
261     }
262
263 }