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.IOException;
19 import java.io.Serializable;
20 import java.util.Arrays;
22 import junit.framework.TestCase;
24 import org.easymock.classextension.ConstructorArgs;
25 import org.easymock.classextension.EasyMock;
26 import org.easymock.classextension.IMocksControl;
27 import org.wamblee.test.AssertionUtils;
28 import org.wamblee.test.EasyMockMatchers;
29 import org.wamblee.test.EventTracker;
31 public class ContainerTest extends TestCase {
33 private EventTracker<String> _tracker;
36 protected void setUp() throws Exception {
38 _tracker = new EventTracker<String>();
41 private static class MyMultiple implements Serializable, Runnable {
48 public void testFilterProvided() {
49 RequiredInterface req1 = new DefaultRequiredInterface("name",
51 RequiredInterface req2 = new DefaultRequiredInterface("name",
53 ProvidedInterface prov1 = new DefaultProvidedInterface("name",
55 ProvidedInterface prov2 = new DefaultProvidedInterface("name",
57 ProvidedInterface prov3 = new DefaultProvidedInterface("name",
60 AssertionUtils.assertEquals(new RequiredInterface[] { req1 }, Container
61 .filterRequiredServices(prov1, Arrays
62 .asList(new RequiredInterface[] { req1 })));
63 AssertionUtils.assertEquals(new RequiredInterface[] { req1 }, Container
64 .filterRequiredServices(prov1, Arrays
65 .asList(new RequiredInterface[] { req1, req2 })));
66 AssertionUtils.assertEquals(new RequiredInterface[] { req1, req2 },
67 Container.filterRequiredServices(prov3, Arrays
68 .asList(new RequiredInterface[] { req1, req2 })));
70 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
71 Container.filterProvidedServices(req1, Arrays
72 .asList(new ProvidedInterface[] { prov1 })));
73 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
74 Container.filterProvidedServices(req1, Arrays
75 .asList(new ProvidedInterface[] { prov1, prov2 })));
76 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1, prov3 },
77 Container.filterProvidedServices(req1, Arrays
78 .asList(new ProvidedInterface[] { prov1, prov3 })));
81 public void testEnvironmentApplication() {
82 Environment environment = new Environment(_tracker);
83 Application application = new Application(_tracker);
84 Container container = new Container("root", new Component[] {
85 environment, application }, new ProvidedInterface[0],
86 new RequiredInterface[0]);
87 Scope scope = container.start();
88 assertTrue(container.isSealed());
89 AssertionUtils.assertEquals(new String[] { "start.environment",
90 "start.application" }, _tracker.getEvents(
91 Thread.currentThread()).toArray(new String[0]));
92 assertEquals(0, scope.getProvidedInterfaces().length);
94 assertEquals(environment.getString(), application.getString());
95 assertEquals(environment.getInteger(), application.getInteger());
99 public void testEnvironmentApplicationSimpleConstructor() {
100 Environment environment = new Environment(_tracker);
101 Application application = new Application(_tracker);
102 Container container = new Container("root").
103 addComponent(environment).addComponent(application);
105 Scope scope = container.start();
106 AssertionUtils.assertEquals(new String[] { "start.environment",
107 "start.application" }, _tracker.getEvents(
108 Thread.currentThread()).toArray(new String[0]));
109 assertEquals(0, scope.getProvidedInterfaces().length);
111 assertEquals(environment.getString(), application.getString());
112 assertEquals(environment.getInteger(), application.getInteger());
116 public void testApplicationEnvironment() {
118 Component environment = new Environment();
119 Component application = new Application();
120 Container container = new Container("root", new Component[] {
121 application, environment }, new ProvidedInterface[0],
122 new RequiredInterface[0]);
124 } catch (SystemAssemblyException e) {
125 // e.printStackTrace();
131 public void testComposite() {
132 Component environment = new Environment(_tracker);
133 Component application = new Application(_tracker);
134 assertEquals(0, _tracker.getEventCount());
136 Container system = new Container("all", new Component[] { environment,
137 application }, new ProvidedInterface[0],
138 new RequiredInterface[0]);
139 Scope runtime = system.start();
140 RequiredInterface[] required = system.getRequiredInterfaces();
141 assertEquals(0, required.length);
142 ProvidedInterface[] provided = system.getProvidedInterfaces();
143 assertEquals(0, provided.length);
145 AssertionUtils.assertEquals(new String[] { "start.environment",
146 "start.application" }, _tracker.getEvents(
147 Thread.currentThread()).toArray(new String[0]));
150 system.stop(runtime);
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 "float", Float.class) },
165 new DefaultRequiredInterface[0]);
167 } catch (SystemAssemblyException e) {
173 public void testCompositeRequiredInterfaceNotProvided() {
175 Component environment = new Environment();
176 Component application = new Application();
177 Container system = new Container("all", new Component[] {
178 environment, application }, new ProvidedInterface[0],
179 new RequiredInterface[] { new DefaultRequiredInterface(
180 "string", String.class) });
182 } catch (SystemAssemblyException e) {
188 public void testCompositeWithSuperfluousRequiredInfo() {
189 Component environment = new Environment();
190 Component application = new Application();
191 Container system = new Container("all", new Component[] { environment,
192 application }, new ProvidedInterface[0],
193 new RequiredInterface[] { new DefaultRequiredInterface(
194 "float", Float.class) });
195 system.getRequiredInterfaces()[0]
196 .setProvider(new DefaultProvidedInterface("hallo", Float.class));
198 RequiredInterface[] required = system.getRequiredInterfaces();
199 assertEquals(1, required.length);
200 ProvidedInterface[] provided = system.getProvidedInterfaces();
201 assertEquals(0, provided.length);
204 public void testCompositeWithExternalDependencesNotProvided() {
206 Component environment = new Environment();
207 Component application = new Application();
208 Container system = new Container("all",
209 new Component[] { application }, new ProvidedInterface[0],
210 application.getRequiredInterfaces());
212 } catch (SystemAssemblyException e) {
219 public void testDuplicateComponent() {
221 Component comp1 = new Application();
222 Component comp2 = new Application();
223 Container system = new Container("top");
224 system.addComponent(comp1).addComponent(comp2);
225 } catch (SystemAssemblyException e) {
232 public void testInconsistentHierarchy() {
234 Component comp = new Application();
235 Container system = new Container("top").addComponent(comp);
236 Container system2 = new Container("top2").addComponent(comp);
237 } catch (SystemAssemblyException e) {
243 public void testCompositeWithExternalDependencesProvided() {
245 Component environment = new Environment();
246 Component application = new Application();
247 Container system = new Container("all",
248 new Component[] { application }, new ProvidedInterface[0],
249 application.getRequiredInterfaces());
250 environment.start(new DefaultScope(new ProvidedInterface[0]));
251 system.getRequiredInterfaces()[0].setProvider(environment
252 .getProvidedInterfaces()[0]);
253 system.getRequiredInterfaces()[1].setProvider(environment
254 .getProvidedInterfaces()[1]);
257 RequiredInterface[] required = system.getRequiredInterfaces();
258 assertEquals(2, required.length);
259 ProvidedInterface[] provided = system.getProvidedInterfaces();
260 assertEquals(0, provided.length);
264 public void testAmbiguousInterfaces() {
266 Component environment1 = new Environment();
267 Component environment2 = new Environment();
268 Component application = new Application();
269 Container container = new Container("root", new Component[] {
270 environment1, environment2, application },
271 new ProvidedInterface[0], new RequiredInterface[0]);
274 } catch (SystemAssemblyException e) {
280 public void testIncompleteRequirements() {
282 Component application = new Application();
283 Container system = new Container("all",
284 new Component[] { application }, new ProvidedInterface[0],
285 new RequiredInterface[0]);
287 } catch (SystemAssemblyException e) {
293 public void testEnvironmentApplicationRollbackOnException()
295 IMocksControl control = EasyMock.createStrictControl();
297 Environment environment = new Environment(_tracker);
298 Application application = control.createMock(Application.class,
299 new ConstructorArgs(Application.class.getConstructor()),
300 Application.class.getDeclaredMethod("doStart", Scope.class));
302 application.doStart(EasyMockMatchers.anyObject(Scope.class));
303 EasyMock.expectLastCall().andThrow(new RuntimeException());
307 Container container = new Container("root", new Component[] {
308 environment, application }, new ProvidedInterface[0],
309 new RequiredInterface[0]);
312 } catch (RuntimeException e) {
313 AssertionUtils.assertEquals(new String[] { "start.environment",
314 "stop.environment" }, _tracker.getEvents(
315 Thread.currentThread()).toArray(new String[0]));
321 public void testEnvironmentApplicationRollbackOnExceptionWithExceptionOnStop()
323 IMocksControl control = EasyMock.createControl();
325 Environment environment = new Environment(_tracker);
326 // Application 1 will throw an exception while stopping.
327 Application application1 = control.createMock(Application.class,
328 new ConstructorArgs(Application.class.getConstructor()),
329 Application.class.getDeclaredMethod("doStop", Object.class));
331 application1.doStop(EasyMock.anyObject());
332 EasyMock.expectLastCall().andThrow(new RuntimeException());
334 // application 2 will throw an exception while starting
335 Application application2 = control.createMock(Application.class,
336 new ConstructorArgs(Application.class.getConstructor(String.class), "application2"),
337 Application.class.getDeclaredMethod("doStart", Scope.class));
339 application2.doStart(EasyMockMatchers.anyObject(Scope.class));
340 EasyMock.expectLastCall().andThrow(new RuntimeException());
345 Container container = new Container("root", new Component[] {
346 environment, application1, application2 }, new ProvidedInterface[0],
347 new RequiredInterface[0]);
350 } catch (RuntimeException e) {
351 AssertionUtils.assertEquals(new String[] { "start.environment",
352 "stop.environment" }, _tracker.getEvents(
353 Thread.currentThread()).toArray(new String[0]));
359 public void testOptionalRequiredInterfaceProvidedOptionalInternal() {
360 Application application = new Application(true);
361 Container container = new Container("top", new Component[] { application },
362 new ProvidedInterface[0], Application.required(true));
363 Environment env = new Environment();
364 container.getRequiredInterfaces()[0].setProvider(
365 env.getProvidedInterfaces()[0]);
366 container.getRequiredInterfaces()[1].setProvider(
367 env.getProvidedInterfaces()[1]);
368 Scope external = new DefaultScope(env.getProvidedInterfaces());
371 container.start(external);
372 assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
373 assertSame(env.getProvidedInterfaces()[1], container.getRequiredInterfaces()[1].getProvider());
374 assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
375 assertSame(env.getProvidedInterfaces()[1], application.getRequiredInterfaces()[1].getProvider());
378 public void testOptionalRequiredInterfaceNotProvidedOptionalInternal() {
379 Application application = new Application(true);
380 Container container = new Container("top", new Component[] { application },
381 new ProvidedInterface[0], Application.required(true));
382 Environment env = new Environment();
383 container.getRequiredInterfaces()[0].setProvider(
384 env.getProvidedInterfaces()[0]);
385 Scope external = new DefaultScope(new ProvidedInterface[0]);
386 external.publishInterface(env.getProvidedInterfaces()[0], env.getString());
387 container.start(external);
388 assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
389 assertNull(container.getRequiredInterfaces()[1].getProvider());
390 assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
391 assertNull(application.getRequiredInterfaces()[1].getProvider());
394 public void testOptionalRequiredInterfaceProvidedMandatoryInternal() {
395 Application application = new Application();
396 Container container = new Container("top", new Component[] { application },
397 new ProvidedInterface[0], Application.required(true));
398 Environment env = new Environment();
399 container.getRequiredInterfaces()[0].setProvider(
400 env.getProvidedInterfaces()[0]);
401 container.getRequiredInterfaces()[1].setProvider(
402 env.getProvidedInterfaces()[1]);
404 assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
405 assertSame(env.getProvidedInterfaces()[1], container.getRequiredInterfaces()[1].getProvider());
406 assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
407 assertSame(env.getProvidedInterfaces()[1], application.getRequiredInterfaces()[1].getProvider());
410 public void testSealed() {
411 final Container container = new Container("xx");
412 assertFalse(container.isSealed());
414 assertTrue(container.isSealed());
416 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
418 public void run() throws Exception {
419 container.addComponent(new Application());
421 }, SystemAssemblyException.class);
423 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
425 public void run() throws Exception {
426 container.addProvidedInterface(new DefaultProvidedInterface("xx", String.class));
428 }, SystemAssemblyException.class);
430 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
432 public void run() throws Exception {
433 container.addRequiredInterface(new DefaultRequiredInterface("xx", String.class));
435 }, SystemAssemblyException.class);