170297fa2c3f5ec0b3ce297e9920c979f6e5d7b3
[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 PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
41         
42         public static EventTracker<String> EVENT_TRACKER;
43         
44         private Scope _externalScope; 
45
46         @Override
47         protected void setUp() throws Exception {
48                 super.setUp();
49                 EVENT_TRACKER = new EventTracker<String>();
50                 _externalScope = new DefaultScope(new ProvidedInterface[0]);
51         }
52
53         public void testBlackboxSystem() {
54                 SpringComponent system = new SpringComponent("system",
55                                 new String[] { HELLO_SERVICE_SPRING_XML },
56                                 new HashMap<String, ProvidedInterface>(),
57                                 new HashMap<RequiredInterface, String>());
58                 
59                 Scope runtime = system.start(_externalScope);
60                 assertEquals(0, _externalScope.getProvidedInterfaces().length);
61
62                 system.stop(runtime);
63         }
64
65         public void testOneProvidedService() {
66                 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
67                 provided.put("helloService", new DefaultProvidedInterface("hello",
68                                 HelloService.class));
69
70                 SpringComponent system = new SpringComponent("system",
71                                 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
72                                 new HashMap<RequiredInterface, String>());
73                 Scope runtime = system.start(_externalScope);
74                 ProvidedInterface[] services = runtime.getProvidedInterfaces();
75         
76                 assertEquals(1, services.length);
77                 Object service = runtime.getInterfaceImplementation(services[0], Object.class);
78                 assertTrue(service instanceof HelloService);
79                 
80                 // BUG; Provided services should be made available in the external scope. 
81                 Object service2 = _externalScope.getInterfaceImplementation(provided.get("helloService"), Object.class);
82                 assertSame(service, service2);
83                 
84                 assertEquals("Hello world!", ((HelloService) service).say());
85                 system.stop(runtime);
86         }
87
88         public void testWithProperties() throws IOException {
89                 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
90                 provided.put("helloService", new DefaultProvidedInterface("hello",
91                                 HelloService.class));
92                 SpringComponent system = new SpringComponent("system",
93                                 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
94                                 provided, new HashMap<RequiredInterface, String>());
95                 Properties props = new Properties();
96                 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
97                 system.addProperties(props);
98
99                 Scope scope = system.start(_externalScope);
100                 // BUG: Hello service was constructed multiple times. Once with the unprocessed property
101                 // and another time with the processed property. 
102                 assertEquals(1, EVENT_TRACKER.getEventCount());
103                 ProvidedInterface[] services = scope.getProvidedInterfaces();
104                 assertEquals("Property Value", scope.getInterfaceImplementation(services[0], HelloService.class).say());
105         }
106
107         public void testWithMissingRequirement() {
108                 try {
109                         SpringComponent system = new SpringComponent("system",
110                                         new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
111                                         new HashMap<String, ProvidedInterface>(),
112                                         new HashMap<RequiredInterface, String>());
113                         system.start(_externalScope);
114                 } catch (SystemAssemblyException e) {
115                         // e.printStackTrace();
116                         return;
117                 }
118                 fail();
119         }
120
121         public void testWithRequirement() {
122                 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
123                 required.put(new DefaultRequiredInterface("hello", HelloService.class),
124                                 "helloService");
125                 SpringComponent system = new SpringComponent("system",
126                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
127                                 new HashMap<String, ProvidedInterface>(), required);
128
129                 HelloService helloObject = new HelloService("ladida");
130                 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
131                                 HelloService.class);
132                 Scope scope = new DefaultScope(new ProvidedInterface[]{ helloService });
133                 scope.publishInterface(helloService, helloObject);
134                 system.getRequiredInterfaces()[0].setProvider(helloService);
135
136                 Scope runtime = system.start(scope);
137                 system.stop(runtime);
138         }
139
140         public void testWithRequirementAndProvidedService() {
141                 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
142                 required.put(new DefaultRequiredInterface("hello", HelloService.class),
143                                 "helloService");
144                 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
145                 provided.put("blaService", new DefaultProvidedInterface("bla",
146                                 BlaService.class));
147
148                 SpringComponent system = new SpringComponent("system",
149                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, provided,
150                                 required);
151
152                 HelloService helloObject = new HelloService("ladida");
153                 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
154                                 HelloService.class);
155                 Scope scope = new DefaultScope(new ProvidedInterface[] {  helloService });
156                 scope.publishInterface(helloService, helloObject);
157                 system.getRequiredInterfaces()[0].setProvider(helloService);
158                 Scope runtime = system.start(scope);
159                 ProvidedInterface started = runtime.getProvidedInterfaces()[0];
160
161                 Object impl = runtime.getInterfaceImplementation(started, BlaService.class);
162                 assertNotNull(impl);
163                 assertTrue(impl instanceof BlaService);
164                 assertEquals("ladida", ((BlaService)impl).execute());
165                 system.stop(runtime);
166         }
167
168 }