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 "float", Float.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 "float", Float.class) });
194 system.getRequiredInterfaces()[0]
195 .setProvider(new DefaultProvidedInterface("hallo", Float.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);
293 assertNull(environment.getProvidedInterfaces()[0].getImplementation());
294 assertNull(environment.getProvidedInterfaces()[0].getImplementation());
300 public void testEnvironmentApplicationRollbackOnExceptionWithExceptionOnStop()
302 IMocksControl control = EasyMock.createControl();
304 Environment environment = new Environment(_tracker);
305 // Application 1 will throw an exception while stopping.
306 Application application1 = control.createMock(Application.class,
307 new ConstructorArgs(Application.class.getConstructor()),
308 Application.class.getDeclaredMethod("doStop"));
310 application1.doStop();
311 EasyMock.expectLastCall().andThrow(new RuntimeException());
313 // application 2 will throw an exception while starting
314 Application application2 = control.createMock(Application.class,
315 new ConstructorArgs(Application.class.getConstructor()),
316 Application.class.getDeclaredMethod("doStart"));
318 application2.doStart();
319 EasyMock.expectLastCall().andThrow(new RuntimeException());
324 Container container = new Container("root", new Component[] {
325 environment, application1, application2 }, new ProvidedInterface[0],
326 new RequiredInterface[0]);
329 } catch (RuntimeException e) {
330 AssertionUtils.assertEquals(new String[] { "start.environment",
331 "stop.environment" }, _tracker.getEvents(
332 Thread.currentThread()).toArray(new String[0]));
333 ProvidedInterface[] envServices = environment.getRunningInterfaces();
334 assertEquals(0, envServices.length);
335 assertNull(environment.getProvidedInterfaces()[0].getImplementation());
336 assertNull(environment.getProvidedInterfaces()[0].getImplementation());
342 public void testOptionalRequiredInterfaceProvidedOptionalInternal() {
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]);
349 container.getRequiredInterfaces()[1].setProvider(
350 env.getProvidedInterfaces()[1]);
352 assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
353 assertSame(env.getProvidedInterfaces()[1], container.getRequiredInterfaces()[1].getProvider());
354 assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
355 assertSame(env.getProvidedInterfaces()[1], application.getRequiredInterfaces()[1].getProvider());
358 public void testOptionalRequiredInterfaceNotProvidedOptionalInternal() {
359 Application application = new Application(true);
360 Container container = new Container("top", new Component[] { application },
361 new ProvidedInterface[0], Application.required(true));
362 Environment env = new Environment();
363 container.getRequiredInterfaces()[0].setProvider(
364 env.getProvidedInterfaces()[0]);
366 assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
367 assertNull(container.getRequiredInterfaces()[1].getProvider());
368 assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
369 assertNull(application.getRequiredInterfaces()[1].getProvider());
372 public void testOptionalRequiredInterfaceProvidedMandatoryInternal() {
373 Application application = new Application();
374 Container container = new Container("top", new Component[] { application },
375 new ProvidedInterface[0], Application.required(true));
376 Environment env = new Environment();
377 container.getRequiredInterfaces()[0].setProvider(
378 env.getProvidedInterfaces()[0]);
379 container.getRequiredInterfaces()[1].setProvider(
380 env.getProvidedInterfaces()[1]);
382 assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
383 assertSame(env.getProvidedInterfaces()[1], container.getRequiredInterfaces()[1].getProvider());
384 assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
385 assertSame(env.getProvidedInterfaces()[1], application.getRequiredInterfaces()[1].getProvider());