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.wamblee.system.core.Component;
23 import org.wamblee.system.core.Container;
24 import org.wamblee.system.core.DefaultProvidedInterface;
25 import org.wamblee.system.core.DefaultRequiredInterface;
26 import org.wamblee.system.core.ProvidedInterface;
27 import org.wamblee.system.core.RequiredInterface;
28 import org.wamblee.system.core.SystemAssemblyException;
29 import org.wamblee.system.core.Component.Status;
30 import org.wamblee.test.AssertionUtils;
31 import org.wamblee.test.EventTracker;
33 import junit.framework.TestCase;
35 public class ContainerTest extends TestCase {
37 private EventTracker<String> _tracker;
40 protected void setUp() throws Exception {
42 _tracker = new EventTracker<String>();
45 private static class MyMultiple implements Serializable, Runnable {
52 public void testFilterProvided() {
53 RequiredInterface req1 = new DefaultRequiredInterface("name",
55 RequiredInterface req2 = new DefaultRequiredInterface("name",
57 ProvidedInterface prov1 = new DefaultProvidedInterface("name",
59 ProvidedInterface prov2 = new DefaultProvidedInterface("name",
61 ProvidedInterface prov3 = new DefaultProvidedInterface("name",
64 AssertionUtils.assertEquals(new RequiredInterface[] { req1 },
65 Container.filterRequiredServices(prov1, Arrays
66 .asList(new RequiredInterface[] { req1 })));
67 AssertionUtils.assertEquals(new RequiredInterface[] { req1 },
68 Container.filterRequiredServices(prov1, Arrays
69 .asList(new RequiredInterface[] { req1, req2 })));
70 AssertionUtils.assertEquals(new RequiredInterface[] { req1, req2 },
71 Container.filterRequiredServices(prov3, Arrays
72 .asList(new RequiredInterface[] { req1, req2 })));
74 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
75 Container.filterProvidedServices(req1, Arrays
76 .asList(new ProvidedInterface[] { prov1 })));
77 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
78 Container.filterProvidedServices(req1, Arrays
79 .asList(new ProvidedInterface[] { prov1, prov2 })));
80 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1, prov3 },
81 Container.filterProvidedServices(req1, Arrays
82 .asList(new ProvidedInterface[] { prov1, prov3 })));
85 public void testEnvironmentApplication() {
86 Component environment = new Environment(_tracker);
87 Component application = new Application(_tracker);
88 Container container = new Container("root", new Component[] {
89 environment, application }, new ProvidedInterface[0],
90 new RequiredInterface[0]);
93 AssertionUtils.assertEquals(new String[] { "start.environment",
94 "start.application" }, _tracker.getEvents(
95 Thread.currentThread()).toArray(new String[0]));
96 ProvidedInterface[] envServices = environment.getRunningServices();
97 assertEquals(2, envServices.length);
98 ProvidedInterface[] appServices = environment.getRunningServices();
99 assertEquals(2, appServices.length);
103 public void testApplicationEnvironment() {
105 Component environment = new Environment();
106 Component application = new Application();
107 Container container = new Container(
110 application, environment },
111 new ProvidedInterface[0], new RequiredInterface[0]);
113 } catch (SystemAssemblyException e) {
114 // e.printStackTrace();
120 public void testComposite() {
121 Component environment = new Environment(_tracker);
122 Component application = new Application(_tracker);
123 assertEquals(0, _tracker.getEventCount());
124 assertEquals(Status.NOT_STARTED, environment.getStatus());
125 assertEquals(Status.NOT_STARTED, application.getStatus());
127 Container system = new Container("all", new Component[] { environment,
128 application }, new ProvidedInterface[0],
129 new RequiredInterface[0]);
130 assertEquals(Status.NOT_STARTED, system.getStatus());
132 RequiredInterface[] required = system.getRequiredServices();
133 assertEquals(0, required.length);
134 ProvidedInterface[] provided = system.getProvidedServices();
135 assertEquals(0, provided.length);
136 assertEquals(Status.RUNNING, environment.getStatus());
137 assertEquals(Status.RUNNING, application.getStatus());
138 assertEquals(Status.RUNNING, system.getStatus());
140 AssertionUtils.assertEquals(
141 new String[] { "start.environment", "start.application" },
142 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
146 assertEquals(Status.STOPPED, environment.getStatus());
147 assertEquals(Status.STOPPED, application.getStatus());
148 assertEquals(Status.STOPPED, system.getStatus());
150 AssertionUtils.assertEquals(
151 new String[] { "stop.application", "stop.environment" },
152 _tracker.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.getRequiredServices()[0]
195 .setProvider(new DefaultProvidedInterface("hallo", String.class));
197 RequiredInterface[] required = system.getRequiredServices();
198 assertEquals(1, required.length);
199 ProvidedInterface[] provided = system.getProvidedServices();
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.getRequiredServices());
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.getRequiredServices());
226 system.getRequiredServices()[0].setProvider(environment
227 .getProvidedServices()[0]);
228 system.getRequiredServices()[1].setProvider(environment
229 .getProvidedServices()[1]);
232 RequiredInterface[] required = system.getRequiredServices();
233 assertEquals(2, required.length);
234 ProvidedInterface[] provided = system.getProvidedServices();
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) {