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.Arrays;
21 import junit.framework.TestCase;
23 import org.easymock.classextension.ConstructorArgs;
24 import org.easymock.classextension.EasyMock;
25 import org.easymock.classextension.IMocksControl;
26 import org.wamblee.test.AssertionUtils;
27 import org.wamblee.test.EventTracker;
29 public class ContainerTest extends TestCase {
31 private EventTracker<String> _tracker;
34 protected void setUp() throws Exception {
36 _tracker = new EventTracker<String>();
39 private static class MyMultiple implements Serializable, Runnable {
46 public void testFilterProvided() {
47 RequiredInterface req1 = new DefaultRequiredInterface("name",
49 RequiredInterface req2 = new DefaultRequiredInterface("name",
51 ProvidedInterface prov1 = new DefaultProvidedInterface("name",
53 ProvidedInterface prov2 = new DefaultProvidedInterface("name",
55 ProvidedInterface prov3 = new DefaultProvidedInterface("name",
58 AssertionUtils.assertEquals(new RequiredInterface[] { req1 }, Container
59 .filterRequiredServices(prov1, Arrays
60 .asList(new RequiredInterface[] { req1 })));
61 AssertionUtils.assertEquals(new RequiredInterface[] { req1 }, Container
62 .filterRequiredServices(prov1, Arrays
63 .asList(new RequiredInterface[] { req1, req2 })));
64 AssertionUtils.assertEquals(new RequiredInterface[] { req1, req2 },
65 Container.filterRequiredServices(prov3, Arrays
66 .asList(new RequiredInterface[] { req1, req2 })));
68 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
69 Container.filterProvidedServices(req1, Arrays
70 .asList(new ProvidedInterface[] { prov1 })));
71 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
72 Container.filterProvidedServices(req1, Arrays
73 .asList(new ProvidedInterface[] { prov1, prov2 })));
74 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1, prov3 },
75 Container.filterProvidedServices(req1, Arrays
76 .asList(new ProvidedInterface[] { prov1, prov3 })));
79 public void testEnvironmentApplication() {
80 Environment environment = new Environment(_tracker);
81 Application application = new Application(_tracker);
82 Container container = new Container("root", new Component[] {
83 environment, application }, new ProvidedInterface[0],
84 new RequiredInterface[0]);
87 AssertionUtils.assertEquals(new String[] { "start.environment",
88 "start.application" }, _tracker.getEvents(
89 Thread.currentThread()).toArray(new String[0]));
90 ProvidedInterface[] envServices = environment.getRunningInterfaces();
91 assertEquals(2, envServices.length);
92 ProvidedInterface[] appServices = application.getRunningInterfaces();
93 assertEquals(0, appServices.length);
95 assertEquals(environment.getString(), application.getString());
96 assertEquals(environment.getInteger(), application.getInteger());
100 public void testApplicationEnvironment() {
102 Component environment = new Environment();
103 Component application = new Application();
104 Container container = new Container("root", new Component[] {
105 application, environment }, new ProvidedInterface[0],
106 new RequiredInterface[0]);
108 } catch (SystemAssemblyException e) {
109 // e.printStackTrace();
115 public void testComposite() {
116 Component environment = new Environment(_tracker);
117 Component application = new Application(_tracker);
118 assertEquals(0, _tracker.getEventCount());
120 Container system = new Container("all", new Component[] { environment,
121 application }, new ProvidedInterface[0],
122 new RequiredInterface[0]);
124 RequiredInterface[] required = system.getRequiredInterfaces();
125 assertEquals(0, required.length);
126 ProvidedInterface[] provided = system.getProvidedInterfaces();
127 assertEquals(0, provided.length);
129 AssertionUtils.assertEquals(new String[] { "start.environment",
130 "start.application" }, _tracker.getEvents(
131 Thread.currentThread()).toArray(new String[0]));
135 AssertionUtils.assertEquals(new String[] { "stop.application",
136 "stop.environment" }, _tracker
137 .getEvents(Thread.currentThread()).toArray(new String[0]));
141 public void testCompositeWithWrongProvidedInfo() {
143 Component environment = new Environment();
144 Component application = new Application();
145 Container system = new Container("all", new Component[] {
146 environment, application },
147 new ProvidedInterface[] { new DefaultProvidedInterface(
148 "float", Float.class) },
149 new DefaultRequiredInterface[0]);
150 } catch (SystemAssemblyException e) {
156 public void testCompositeRequiredInterfaceNotProvided() {
158 Component environment = new Environment();
159 Component application = new Application();
160 Container system = new Container("all", new Component[] {
161 environment, application }, new ProvidedInterface[0],
162 new RequiredInterface[] { new DefaultRequiredInterface(
163 "string", String.class) });
165 } catch (SystemAssemblyException e) {
171 public void testCompositeWithSuperfluousRequiredInfo() {
172 Component environment = new Environment();
173 Component application = new Application();
174 Container system = new Container("all", new Component[] { environment,
175 application }, new ProvidedInterface[0],
176 new RequiredInterface[] { new DefaultRequiredInterface(
177 "float", Float.class) });
178 system.getRequiredInterfaces()[0]
179 .setProvider(new DefaultProvidedInterface("hallo", Float.class));
181 RequiredInterface[] required = system.getRequiredInterfaces();
182 assertEquals(1, required.length);
183 ProvidedInterface[] provided = system.getProvidedInterfaces();
184 assertEquals(0, provided.length);
187 public void testCompositeWithExternalDependencesNotProvided() {
189 Component environment = new Environment();
190 Component application = new Application();
191 Container system = new Container("all",
192 new Component[] { application }, new ProvidedInterface[0],
193 application.getRequiredInterfaces());
195 } catch (SystemAssemblyException e) {
202 public void testCompositeWithExternalDependencesProvided() {
204 Component environment = new Environment();
205 Component application = new Application();
206 Container system = new Container("all",
207 new Component[] { application }, new ProvidedInterface[0],
208 application.getRequiredInterfaces());
210 system.getRequiredInterfaces()[0].setProvider(environment
211 .getProvidedInterfaces()[0]);
212 system.getRequiredInterfaces()[1].setProvider(environment
213 .getProvidedInterfaces()[1]);
216 RequiredInterface[] required = system.getRequiredInterfaces();
217 assertEquals(2, required.length);
218 ProvidedInterface[] provided = system.getProvidedInterfaces();
219 assertEquals(0, provided.length);
223 public void testAmbiguousInterfaces() {
225 Component environment1 = new Environment();
226 Component environment2 = new Environment();
227 Component application = new Application();
228 Container container = new Container("root", new Component[] {
229 environment1, environment2, application },
230 new ProvidedInterface[0], new RequiredInterface[0]);
233 } catch (SystemAssemblyException e) {
239 public void testIncompleteRequirements() {
241 Component application = new Application();
242 Container system = new Container("all",
243 new Component[] { application }, new ProvidedInterface[0],
244 new RequiredInterface[0]);
246 } catch (SystemAssemblyException e) {
252 public void testEnvironmentApplicationRollbackOnException()
254 IMocksControl control = EasyMock.createStrictControl();
256 Environment environment = new Environment(_tracker);
257 Application application = control.createMock(Application.class,
258 new ConstructorArgs(Application.class.getConstructor()),
259 Application.class.getDeclaredMethod("doStart"));
261 application.doStart();
262 EasyMock.expectLastCall().andThrow(new RuntimeException());
266 Container container = new Container("root", new Component[] {
267 environment, application }, new ProvidedInterface[0],
268 new RequiredInterface[0]);
271 } catch (RuntimeException e) {
272 AssertionUtils.assertEquals(new String[] { "start.environment",
273 "stop.environment" }, _tracker.getEvents(
274 Thread.currentThread()).toArray(new String[0]));
275 ProvidedInterface[] envServices = environment.getRunningInterfaces();
276 assertEquals(0, envServices.length);
277 assertNull(environment.getProvidedInterfaces()[0].getImplementation());
278 assertNull(environment.getProvidedInterfaces()[0].getImplementation());
284 public void testEnvironmentApplicationRollbackOnExceptionWithExceptionOnStop()
286 IMocksControl control = EasyMock.createControl();
288 Environment environment = new Environment(_tracker);
289 // Application 1 will throw an exception while stopping.
290 Application application1 = control.createMock(Application.class,
291 new ConstructorArgs(Application.class.getConstructor()),
292 Application.class.getDeclaredMethod("doStop"));
294 application1.doStop();
295 EasyMock.expectLastCall().andThrow(new RuntimeException());
297 // application 2 will throw an exception while starting
298 Application application2 = control.createMock(Application.class,
299 new ConstructorArgs(Application.class.getConstructor()),
300 Application.class.getDeclaredMethod("doStart"));
302 application2.doStart();
303 EasyMock.expectLastCall().andThrow(new RuntimeException());
308 Container container = new Container("root", new Component[] {
309 environment, application1, application2 }, new ProvidedInterface[0],
310 new RequiredInterface[0]);
313 } catch (RuntimeException e) {
314 AssertionUtils.assertEquals(new String[] { "start.environment",
315 "stop.environment" }, _tracker.getEvents(
316 Thread.currentThread()).toArray(new String[0]));
317 ProvidedInterface[] envServices = environment.getRunningInterfaces();
318 assertEquals(0, envServices.length);
319 assertNull(environment.getProvidedInterfaces()[0].getImplementation());
320 assertNull(environment.getProvidedInterfaces()[0].getImplementation());
326 public void testOptionalRequiredInterfaceProvidedOptionalInternal() {
327 Application application = new Application(true);
328 Container container = new Container("top", new Component[] { application },
329 new ProvidedInterface[0], Application.required(true));
330 Environment env = new Environment();
331 container.getRequiredInterfaces()[0].setProvider(
332 env.getProvidedInterfaces()[0]);
333 container.getRequiredInterfaces()[1].setProvider(
334 env.getProvidedInterfaces()[1]);
336 assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
337 assertSame(env.getProvidedInterfaces()[1], container.getRequiredInterfaces()[1].getProvider());
338 assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
339 assertSame(env.getProvidedInterfaces()[1], application.getRequiredInterfaces()[1].getProvider());
342 public void testOptionalRequiredInterfaceNotProvidedOptionalInternal() {
343 Application application = new Application(true);
344 Container container = new Container("top", new Component[] { application },
345 new ProvidedInterface[0], Application.required(true));
346 Environment env = new Environment();
347 container.getRequiredInterfaces()[0].setProvider(
348 env.getProvidedInterfaces()[0]);
350 assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
351 assertNull(container.getRequiredInterfaces()[1].getProvider());
352 assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
353 assertNull(application.getRequiredInterfaces()[1].getProvider());
356 public void testOptionalRequiredInterfaceProvidedMandatoryInternal() {
357 Application application = new Application();
358 Container container = new Container("top", new Component[] { application },
359 new ProvidedInterface[0], Application.required(true));
360 Environment env = new Environment();
361 container.getRequiredInterfaces()[0].setProvider(
362 env.getProvidedInterfaces()[0]);
363 container.getRequiredInterfaces()[1].setProvider(
364 env.getProvidedInterfaces()[1]);
366 assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
367 assertSame(env.getProvidedInterfaces()[1], container.getRequiredInterfaces()[1].getProvider());
368 assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
369 assertSame(env.getProvidedInterfaces()[1], application.getRequiredInterfaces()[1].getProvider());