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 Environment environment = new Environment(_tracker);
90 Application 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);
104 assertEquals(environment.getString(), application.getString());
105 assertEquals(environment.getInteger(), application.getInteger());
109 public void testApplicationEnvironment() {
111 Component environment = new Environment();
112 Component application = new Application();
113 Container container = new Container("root", new Component[] {
114 application, environment }, new ProvidedInterface[0],
115 new RequiredInterface[0]);
117 } catch (SystemAssemblyException e) {
118 // e.printStackTrace();
124 public void testComposite() {
125 Component environment = new Environment(_tracker);
126 Component application = new Application(_tracker);
127 assertEquals(0, _tracker.getEventCount());
128 assertEquals(Status.NOT_STARTED, environment.getStatus());
129 assertEquals(Status.NOT_STARTED, application.getStatus());
131 Container system = new Container("all", new Component[] { environment,
132 application }, new ProvidedInterface[0],
133 new RequiredInterface[0]);
134 assertEquals(Status.NOT_STARTED, system.getStatus());
136 RequiredInterface[] required = system.getRequiredInterfaces();
137 assertEquals(0, required.length);
138 ProvidedInterface[] provided = system.getProvidedInterfaces();
139 assertEquals(0, provided.length);
140 assertEquals(Status.RUNNING, environment.getStatus());
141 assertEquals(Status.RUNNING, application.getStatus());
142 assertEquals(Status.RUNNING, system.getStatus());
144 AssertionUtils.assertEquals(new String[] { "start.environment",
145 "start.application" }, _tracker.getEvents(
146 Thread.currentThread()).toArray(new String[0]));
150 assertEquals(Status.STOPPED, environment.getStatus());
151 assertEquals(Status.STOPPED, application.getStatus());
152 assertEquals(Status.STOPPED, system.getStatus());
154 AssertionUtils.assertEquals(new String[] { "stop.application",
155 "stop.environment" }, _tracker
156 .getEvents(Thread.currentThread()).toArray(new String[0]));
160 public void testCompositeWithWrongProvidedInfo() {
162 Component environment = new Environment();
163 Component application = new Application();
164 Container system = new Container("all", new Component[] {
165 environment, application },
166 new ProvidedInterface[] { new DefaultProvidedInterface(
167 "float", Float.class) },
168 new DefaultRequiredInterface[0]);
169 } catch (SystemAssemblyException e) {
175 public void testCompositeRequiredInterfaceNotProvided() {
177 Component environment = new Environment();
178 Component application = new Application();
179 Container system = new Container("all", new Component[] {
180 environment, application }, new ProvidedInterface[0],
181 new RequiredInterface[] { new DefaultRequiredInterface(
182 "string", String.class) });
184 } catch (SystemAssemblyException e) {
190 public void testCompositeWithSuperfluousRequiredInfo() {
191 Component environment = new Environment();
192 Component application = new Application();
193 Container system = new Container("all", new Component[] { environment,
194 application }, new ProvidedInterface[0],
195 new RequiredInterface[] { new DefaultRequiredInterface(
196 "float", Float.class) });
197 system.getRequiredInterfaces()[0]
198 .setProvider(new DefaultProvidedInterface("hallo", Float.class));
200 RequiredInterface[] required = system.getRequiredInterfaces();
201 assertEquals(1, required.length);
202 ProvidedInterface[] provided = system.getProvidedInterfaces();
203 assertEquals(0, provided.length);
206 public void testCompositeWithExternalDependencesNotProvided() {
208 Component environment = new Environment();
209 Component application = new Application();
210 Container system = new Container("all",
211 new Component[] { application }, new ProvidedInterface[0],
212 application.getRequiredInterfaces());
214 } catch (SystemAssemblyException e) {
221 public void testCompositeWithExternalDependencesProvided() {
223 Component environment = new Environment();
224 Component application = new Application();
225 Container system = new Container("all",
226 new Component[] { application }, new ProvidedInterface[0],
227 application.getRequiredInterfaces());
229 system.getRequiredInterfaces()[0].setProvider(environment
230 .getProvidedInterfaces()[0]);
231 system.getRequiredInterfaces()[1].setProvider(environment
232 .getProvidedInterfaces()[1]);
235 RequiredInterface[] required = system.getRequiredInterfaces();
236 assertEquals(2, required.length);
237 ProvidedInterface[] provided = system.getProvidedInterfaces();
238 assertEquals(0, provided.length);
242 public void testAmbiguousInterfaces() {
244 Component environment1 = new Environment();
245 Component environment2 = new Environment();
246 Component application = new Application();
247 Container container = new Container("root", new Component[] {
248 environment1, environment2, application },
249 new ProvidedInterface[0], new RequiredInterface[0]);
252 } catch (SystemAssemblyException e) {
258 public void testIncompleteRequirements() {
260 Component application = new Application();
261 Container system = new Container("all",
262 new Component[] { application }, new ProvidedInterface[0],
263 new RequiredInterface[0]);
265 } catch (SystemAssemblyException e) {
271 public void testEnvironmentApplicationRollbackOnException()
273 IMocksControl control = EasyMock.createStrictControl();
275 Environment environment = new Environment(_tracker);
276 Application application = control.createMock(Application.class,
277 new ConstructorArgs(Application.class.getConstructor()),
278 Application.class.getDeclaredMethod("doStart"));
280 application.doStart();
281 EasyMock.expectLastCall().andThrow(new RuntimeException());
285 Container container = new Container("root", new Component[] {
286 environment, application }, new ProvidedInterface[0],
287 new RequiredInterface[0]);
290 } catch (RuntimeException e) {
291 AssertionUtils.assertEquals(new String[] { "start.environment",
292 "stop.environment" }, _tracker.getEvents(
293 Thread.currentThread()).toArray(new String[0]));
294 ProvidedInterface[] envServices = environment.getRunningInterfaces();
295 assertEquals(0, envServices.length);
296 assertNull(environment.getProvidedInterfaces()[0].getImplementation());
297 assertNull(environment.getProvidedInterfaces()[0].getImplementation());
303 public void testEnvironmentApplicationRollbackOnExceptionWithExceptionOnStop()
305 IMocksControl control = EasyMock.createControl();
307 Environment environment = new Environment(_tracker);
308 // Application 1 will throw an exception while stopping.
309 Application application1 = control.createMock(Application.class,
310 new ConstructorArgs(Application.class.getConstructor()),
311 Application.class.getDeclaredMethod("doStop"));
313 application1.doStop();
314 EasyMock.expectLastCall().andThrow(new RuntimeException());
316 // application 2 will throw an exception while starting
317 Application application2 = control.createMock(Application.class,
318 new ConstructorArgs(Application.class.getConstructor()),
319 Application.class.getDeclaredMethod("doStart"));
321 application2.doStart();
322 EasyMock.expectLastCall().andThrow(new RuntimeException());
327 Container container = new Container("root", new Component[] {
328 environment, application1, application2 }, new ProvidedInterface[0],
329 new RequiredInterface[0]);
332 } catch (RuntimeException e) {
333 AssertionUtils.assertEquals(new String[] { "start.environment",
334 "stop.environment" }, _tracker.getEvents(
335 Thread.currentThread()).toArray(new String[0]));
336 ProvidedInterface[] envServices = environment.getRunningInterfaces();
337 assertEquals(0, envServices.length);
338 assertNull(environment.getProvidedInterfaces()[0].getImplementation());
339 assertNull(environment.getProvidedInterfaces()[0].getImplementation());
345 public void testOptionalRequiredInterfaceProvidedOptionalInternal() {
346 Application application = new Application(true);
347 Container container = new Container("top", new Component[] { application },
348 new ProvidedInterface[0], Application.required(true));
349 Environment env = new Environment();
350 container.getRequiredInterfaces()[0].setProvider(
351 env.getProvidedInterfaces()[0]);
352 container.getRequiredInterfaces()[1].setProvider(
353 env.getProvidedInterfaces()[1]);
355 assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
356 assertSame(env.getProvidedInterfaces()[1], container.getRequiredInterfaces()[1].getProvider());
357 assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
358 assertSame(env.getProvidedInterfaces()[1], application.getRequiredInterfaces()[1].getProvider());
361 public void testOptionalRequiredInterfaceNotProvidedOptionalInternal() {
362 Application application = new Application(true);
363 Container container = new Container("top", new Component[] { application },
364 new ProvidedInterface[0], Application.required(true));
365 Environment env = new Environment();
366 container.getRequiredInterfaces()[0].setProvider(
367 env.getProvidedInterfaces()[0]);
369 assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
370 assertNull(container.getRequiredInterfaces()[1].getProvider());
371 assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
372 assertNull(application.getRequiredInterfaces()[1].getProvider());
375 public void testOptionalRequiredInterfaceProvidedMandatoryInternal() {
376 Application application = new Application();
377 Container container = new Container("top", new Component[] { application },
378 new ProvidedInterface[0], Application.required(true));
379 Environment env = new Environment();
380 container.getRequiredInterfaces()[0].setProvider(
381 env.getProvidedInterfaces()[0]);
382 container.getRequiredInterfaces()[1].setProvider(
383 env.getProvidedInterfaces()[1]);
385 assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
386 assertSame(env.getProvidedInterfaces()[1], container.getRequiredInterfaces()[1].getProvider());
387 assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
388 assertSame(env.getProvidedInterfaces()[1], application.getRequiredInterfaces()[1].getProvider());