2 * Copyright 2007 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.system.core;
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Arrays;
22 import org.easymock.classextension.ConstructorArgs;
23 import org.easymock.classextension.EasyMock;
24 import org.easymock.classextension.IMocksControl;
25 import org.wamblee.system.core.Component;
26 import org.wamblee.system.core.Container;
27 import org.wamblee.system.core.DefaultProvidedInterface;
28 import org.wamblee.system.core.DefaultRequiredInterface;
29 import org.wamblee.system.core.ProvidedInterface;
30 import org.wamblee.system.core.RequiredInterface;
31 import org.wamblee.system.core.SystemAssemblyException;
32 import org.wamblee.system.core.Component.Status;
33 import org.wamblee.test.AssertionUtils;
34 import org.wamblee.test.EventTracker;
36 import junit.framework.TestCase;
38 public class ContainerTest extends TestCase {
40 private EventTracker<String> _tracker;
43 protected void setUp() throws Exception {
45 _tracker = new EventTracker<String>();
48 private static class MyMultiple implements Serializable, Runnable {
55 public void testFilterProvided() {
56 RequiredInterface req1 = new DefaultRequiredInterface("name",
58 RequiredInterface req2 = new DefaultRequiredInterface("name",
60 ProvidedInterface prov1 = new DefaultProvidedInterface("name",
62 ProvidedInterface prov2 = new DefaultProvidedInterface("name",
64 ProvidedInterface prov3 = new DefaultProvidedInterface("name",
67 AssertionUtils.assertEquals(new RequiredInterface[] { req1 }, Container
68 .filterRequiredServices(prov1, Arrays
69 .asList(new RequiredInterface[] { req1 })));
70 AssertionUtils.assertEquals(new RequiredInterface[] { req1 }, Container
71 .filterRequiredServices(prov1, Arrays
72 .asList(new RequiredInterface[] { req1, req2 })));
73 AssertionUtils.assertEquals(new RequiredInterface[] { req1, req2 },
74 Container.filterRequiredServices(prov3, Arrays
75 .asList(new RequiredInterface[] { req1, req2 })));
77 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
78 Container.filterProvidedServices(req1, Arrays
79 .asList(new ProvidedInterface[] { prov1 })));
80 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
81 Container.filterProvidedServices(req1, Arrays
82 .asList(new ProvidedInterface[] { prov1, prov2 })));
83 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1, prov3 },
84 Container.filterProvidedServices(req1, Arrays
85 .asList(new ProvidedInterface[] { prov1, prov3 })));
88 public void testEnvironmentApplication() {
89 Component environment = new Environment(_tracker);
90 Component application = new Application(_tracker);
91 Container container = new Container("root", new Component[] {
92 environment, application }, new ProvidedInterface[0],
93 new RequiredInterface[0]);
96 AssertionUtils.assertEquals(new String[] { "start.environment",
97 "start.application" }, _tracker.getEvents(
98 Thread.currentThread()).toArray(new String[0]));
99 ProvidedInterface[] envServices = environment.getRunningInterfaces();
100 assertEquals(2, envServices.length);
101 ProvidedInterface[] appServices = application.getRunningInterfaces();
102 assertEquals(0, appServices.length);
106 public void testApplicationEnvironment() {
108 Component environment = new Environment();
109 Component application = new Application();
110 Container container = new Container("root", new Component[] {
111 application, environment }, new ProvidedInterface[0],
112 new RequiredInterface[0]);
114 } catch (SystemAssemblyException e) {
115 // e.printStackTrace();
121 public void testComposite() {
122 Component environment = new Environment(_tracker);
123 Component application = new Application(_tracker);
124 assertEquals(0, _tracker.getEventCount());
125 assertEquals(Status.NOT_STARTED, environment.getStatus());
126 assertEquals(Status.NOT_STARTED, application.getStatus());
128 Container system = new Container("all", new Component[] { environment,
129 application }, new ProvidedInterface[0],
130 new RequiredInterface[0]);
131 assertEquals(Status.NOT_STARTED, system.getStatus());
133 RequiredInterface[] required = system.getRequiredInterfaces();
134 assertEquals(0, required.length);
135 ProvidedInterface[] provided = system.getProvidedInterfaces();
136 assertEquals(0, provided.length);
137 assertEquals(Status.RUNNING, environment.getStatus());
138 assertEquals(Status.RUNNING, application.getStatus());
139 assertEquals(Status.RUNNING, system.getStatus());
141 AssertionUtils.assertEquals(new String[] { "start.environment",
142 "start.application" }, _tracker.getEvents(
143 Thread.currentThread()).toArray(new String[0]));
147 assertEquals(Status.STOPPED, environment.getStatus());
148 assertEquals(Status.STOPPED, application.getStatus());
149 assertEquals(Status.STOPPED, system.getStatus());
151 AssertionUtils.assertEquals(new String[] { "stop.application",
152 "stop.environment" }, _tracker
153 .getEvents(Thread.currentThread()).toArray(new String[0]));
157 public void testCompositeWithWrongProvidedInfo() {
159 Component environment = new Environment();
160 Component application = new Application();
161 Container system = new Container("all", new Component[] {
162 environment, application },
163 new ProvidedInterface[] { new DefaultProvidedInterface(
164 "string", String.class) },
165 new DefaultRequiredInterface[0]);
166 } catch (SystemAssemblyException e) {
172 public void testCompositeRequiredInterfaceNotProvided() {
174 Component environment = new Environment();
175 Component application = new Application();
176 Container system = new Container("all", new Component[] {
177 environment, application }, new ProvidedInterface[0],
178 new RequiredInterface[] { new DefaultRequiredInterface(
179 "string", String.class) });
181 } catch (SystemAssemblyException e) {
187 public void testCompositeWithSuperfluousRequiredInfo() {
188 Component environment = new Environment();
189 Component application = new Application();
190 Container system = new Container("all", new Component[] { environment,
191 application }, new ProvidedInterface[0],
192 new RequiredInterface[] { new DefaultRequiredInterface(
193 "string", String.class) });
194 system.getRequiredInterfaces()[0]
195 .setProvider(new DefaultProvidedInterface("hallo", String.class));
197 RequiredInterface[] required = system.getRequiredInterfaces();
198 assertEquals(1, required.length);
199 ProvidedInterface[] provided = system.getProvidedInterfaces();
200 assertEquals(0, provided.length);
203 public void testCompositeWithExternalDependencesNotProvided() {
205 Component environment = new Environment();
206 Component application = new Application();
207 Container system = new Container("all",
208 new Component[] { application }, new ProvidedInterface[0],
209 application.getRequiredInterfaces());
211 } catch (SystemAssemblyException e) {
218 public void testCompositeWithExternalDependencesProvided() {
220 Component environment = new Environment();
221 Component application = new Application();
222 Container system = new Container("all",
223 new Component[] { application }, new ProvidedInterface[0],
224 application.getRequiredInterfaces());
226 system.getRequiredInterfaces()[0].setProvider(environment
227 .getProvidedInterfaces()[0]);
228 system.getRequiredInterfaces()[1].setProvider(environment
229 .getProvidedInterfaces()[1]);
232 RequiredInterface[] required = system.getRequiredInterfaces();
233 assertEquals(2, required.length);
234 ProvidedInterface[] provided = system.getProvidedInterfaces();
235 assertEquals(0, provided.length);
239 public void testAmbiguousInterfaces() {
241 Component environment1 = new Environment();
242 Component environment2 = new Environment();
243 Component application = new Application();
244 Container container = new Container("root", new Component[] {
245 environment1, environment2, application },
246 new ProvidedInterface[0], new RequiredInterface[0]);
249 } catch (SystemAssemblyException e) {
255 public void testIncompleteRequirements() {
257 Component application = new Application();
258 Container system = new Container("all",
259 new Component[] { application }, new ProvidedInterface[0],
260 new RequiredInterface[0]);
262 } catch (SystemAssemblyException e) {
268 public void testEnvironmentApplicationRollbackOnException()
270 IMocksControl control = EasyMock.createStrictControl();
272 Environment environment = new Environment(_tracker);
273 Application application = control.createMock(Application.class,
274 new ConstructorArgs(Application.class.getConstructor()),
275 Application.class.getDeclaredMethod("doStart"));
277 application.doStart();
278 EasyMock.expectLastCall().andThrow(new RuntimeException());
282 Container container = new Container("root", new Component[] {
283 environment, application }, new ProvidedInterface[0],
284 new RequiredInterface[0]);
287 } catch (RuntimeException e) {
288 AssertionUtils.assertEquals(new String[] { "start.environment",
289 "stop.environment" }, _tracker.getEvents(
290 Thread.currentThread()).toArray(new String[0]));
291 ProvidedInterface[] envServices = environment.getRunningInterfaces();
292 assertEquals(0, envServices.length);
298 public void testEnvironmentApplicationRollbackOnExceptionWithExceptionOnStop()
300 IMocksControl control = EasyMock.createControl();
302 Environment environment = new Environment(_tracker);
303 // Application 1 will throw an exception while stopping.
304 Application application1 = control.createMock(Application.class,
305 new ConstructorArgs(Application.class.getConstructor()),
306 Application.class.getDeclaredMethod("doStop"));
308 application1.doStop();
309 EasyMock.expectLastCall().andThrow(new RuntimeException());
311 // application 2 will throw an exception while starting
312 Application application2 = control.createMock(Application.class,
313 new ConstructorArgs(Application.class.getConstructor()),
314 Application.class.getDeclaredMethod("doStart"));
316 application2.doStart();
317 EasyMock.expectLastCall().andThrow(new RuntimeException());
322 Container container = new Container("root", new Component[] {
323 environment, application1, application2 }, new ProvidedInterface[0],
324 new RequiredInterface[0]);
327 } catch (RuntimeException e) {
328 AssertionUtils.assertEquals(new String[] { "start.environment",
329 "stop.environment" }, _tracker.getEvents(
330 Thread.currentThread()).toArray(new String[0]));
331 ProvidedInterface[] envServices = environment.getRunningInterfaces();
332 assertEquals(0, envServices.length);