SpringComponent now supports adding properties as beans instead of
[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], Object.class);
80                 assertTrue(service instanceof HelloService);
81                 
82                 // BUG; Provided services should be made available in the external scope. 
83                 Object service2 = _externalScope.getInterfaceImplementation(provided.get("helloService"), Object.class);
84                 assertSame(service, service2);
85                 
86                 assertEquals("Hello world!", ((HelloService) service).say());
87                 system.stop(runtime);
88         }
89
90         public void testWithProperties() throws IOException {
91                 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
92                 provided.put("helloService", new DefaultProvidedInterface("hello",
93                                 HelloService.class));
94                 SpringComponent system = new SpringComponent("system",
95                                 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
96                                 provided, new HashMap<RequiredInterface, String>());
97                 Properties props = new Properties();
98                 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
99                 system.addProperties(props);
100
101                 Scope scope = system.start(_externalScope);
102                 // BUG: Hello service was constructed multiple times. Once with the unprocessed property
103                 // and another time with the processed property. 
104                 assertEquals(1, EVENT_TRACKER.getEventCount());
105                 ProvidedInterface[] services = scope.getProvidedInterfaces();
106                 assertEquals("Property Value", scope.getInterfaceImplementation(services[0], HelloService.class).say());
107         }
108         
109         public void testWithPropertiesAsBean() throws IOException {
110         Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
111         provided.put("helloService", new DefaultProvidedInterface("hello",
112                 HelloService2.class));
113         SpringComponent system = new SpringComponent("system",
114                 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 },
115                 provided, new HashMap<RequiredInterface, String>());
116         Properties props = new Properties();
117         props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
118         system.addProperties("properties", props);
119
120         Scope scope = system.start(_externalScope);
121         
122         ProvidedInterface[] services = scope.getProvidedInterfaces();
123         
124         Properties props2 = scope.getInterfaceImplementation(services[0], HelloService2.class).getProperties();
125         assertEquals(props, props2);
126     }
127
128         public void testWithMissingRequirement() {
129                 try {
130                         SpringComponent system = new SpringComponent("system",
131                                         new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
132                                         new HashMap<String, ProvidedInterface>(),
133                                         new HashMap<RequiredInterface, String>());
134                         system.start(_externalScope);
135                 } catch (SystemAssemblyException e) {
136                         // e.printStackTrace();
137                         return;
138                 }
139                 fail();
140         }
141
142         public void testWithRequirement() {
143                 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
144                 required.put(new DefaultRequiredInterface("hello", HelloService.class),
145                                 "helloService");
146                 SpringComponent system = new SpringComponent("system",
147                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
148                                 new HashMap<String, ProvidedInterface>(), required);
149
150                 HelloService helloObject = new HelloService("ladida");
151                 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
152                                 HelloService.class);
153                 Scope scope = new DefaultScope(new ProvidedInterface[]{ helloService });
154                 scope.publishInterface(helloService, helloObject);
155                 system.getRequiredInterfaces()[0].setProvider(helloService);
156
157                 Scope runtime = system.start(scope);
158                 system.stop(runtime);
159         }
160
161         public void testWithRequirementAndProvidedService() {
162                 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
163                 required.put(new DefaultRequiredInterface("hello", HelloService.class),
164                                 "helloService");
165                 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
166                 provided.put("blaService", new DefaultProvidedInterface("bla",
167                                 BlaService.class));
168
169                 SpringComponent system = new SpringComponent("system",
170                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, provided,
171                                 required);
172
173                 HelloService helloObject = new HelloService("ladida");
174                 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
175                                 HelloService.class);
176                 Scope scope = new DefaultScope(new ProvidedInterface[] {  helloService });
177                 scope.publishInterface(helloService, helloObject);
178                 system.getRequiredInterfaces()[0].setProvider(helloService);
179                 Scope runtime = system.start(scope);
180                 ProvidedInterface started = runtime.getProvidedInterfaces()[0];
181
182                 Object impl = runtime.getInterfaceImplementation(started, BlaService.class);
183                 assertNotNull(impl);
184                 assertTrue(impl instanceof BlaService);
185                 assertEquals("ladida", ((BlaService)impl).execute());
186                 system.stop(runtime);
187         }
188
189 }