0914c9aa3d5e065c138ebb30387fe5f6ba377221
[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
34 public class SpringComponentTest extends TestCase {
35
36         private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
37         private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
38         private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
39         private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
40
41         @Override
42         protected void setUp() throws Exception {
43                 super.setUp();
44         }
45
46         public void testBlackboxSystem() {
47                 SpringComponent system = new SpringComponent("system",
48                                 new String[] { HELLO_SERVICE_SPRING_XML },
49                                 new HashMap<String, ProvidedInterface>(),
50                                 new HashMap<RequiredInterface, String>());
51                 Scope runtime = system.start();
52                 assertEquals(0, runtime.getProvidedInterfaces().length);
53
54                 system.stop(runtime);
55         }
56
57         public void testOneProvidedService() {
58                 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
59                 provided.put("helloService", new DefaultProvidedInterface("hello",
60                                 HelloService.class));
61
62                 SpringComponent system = new SpringComponent("system",
63                                 new String[] { HELLO_SERVICE_SPRING_XML }, provided,
64                                 new HashMap<RequiredInterface, String>());
65                 Scope runtime = system.start();
66                 ProvidedInterface[] services = runtime.getProvidedInterfaces();
67                 assertEquals(1, services.length);
68                 Object service = runtime.retrieveInterfaceImplementation(services[0], Object.class);
69                 assertTrue(service instanceof HelloService);
70                 assertEquals("Hello world!", ((HelloService) service).say());
71                 system.stop(runtime);
72         }
73
74         public void testWithProperties() throws IOException {
75                 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
76                 provided.put("helloService", new DefaultProvidedInterface("hello",
77                                 HelloService.class));
78                 SpringComponent system = new SpringComponent("system",
79                                 new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
80                                 provided, new HashMap<RequiredInterface, String>());
81                 Properties props = new Properties();
82                 props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
83                 system.addProperties(props);
84
85                 Scope scope = system.start();
86                 ProvidedInterface[] services = scope.getProvidedInterfaces();
87                 assertEquals("Property Value", scope.retrieveInterfaceImplementation(services[0], HelloService.class).say());
88         }
89
90         public void testWithMissingRequirement() {
91                 try {
92                         SpringComponent system = new SpringComponent("system",
93                                         new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
94                                         new HashMap<String, ProvidedInterface>(),
95                                         new HashMap<RequiredInterface, String>());
96                         system.start();
97                 } catch (SystemAssemblyException e) {
98                         // e.printStackTrace();
99                         return;
100                 }
101                 fail();
102         }
103
104         public void testWithRequirement() {
105                 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
106                 required.put(new DefaultRequiredInterface("hello", HelloService.class),
107                                 "helloService");
108                 SpringComponent system = new SpringComponent("system",
109                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
110                                 new HashMap<String, ProvidedInterface>(), required);
111
112                 HelloService helloObject = new HelloService("ladida");
113                 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
114                                 HelloService.class);
115                 Scope scope = new DefaultScope(new ProvidedInterface[]{ helloService });
116                 scope.publishInterface(helloService, helloObject);
117                 system.getRequiredInterfaces()[0].setProvider(helloService);
118
119                 Scope runtime = system.start(scope);
120                 system.stop(runtime);
121         }
122
123         public void testWithRequirementAndProvidedService() {
124                 Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
125                 required.put(new DefaultRequiredInterface("hello", HelloService.class),
126                                 "helloService");
127                 Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
128                 provided.put("blaService", new DefaultProvidedInterface("bla",
129                                 BlaService.class));
130
131                 SpringComponent system = new SpringComponent("system",
132                                 new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, provided,
133                                 required);
134
135                 HelloService helloObject = new HelloService("ladida");
136                 ProvidedInterface helloService = new DefaultProvidedInterface("hello",
137                                 HelloService.class);
138                 Scope scope = new DefaultScope(new ProvidedInterface[] {  helloService });
139                 scope.publishInterface(helloService, helloObject);
140                 system.getRequiredInterfaces()[0].setProvider(helloService);
141                 Scope runtime = system.start(scope);
142                 ProvidedInterface started = runtime.getProvidedInterfaces()[0];
143
144                 Object impl = runtime.retrieveInterfaceImplementation(started, BlaService.class);
145                 assertNotNull(impl);
146                 assertTrue(impl instanceof BlaService);
147                 assertEquals("ladida", ((BlaService)impl).execute());
148                 system.stop(runtime);
149         }
150
151 }