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;
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Arrays;
22 import org.wamblee.system.Component.Status;
23 import org.wamblee.test.AssertionUtils;
24 import org.wamblee.test.EventTracker;
26 import junit.framework.TestCase;
28 public class ContainerTest extends TestCase {
30 private EventTracker<String> _tracker;
33 protected void setUp() throws Exception {
35 _tracker = new EventTracker<String>();
38 private static class MyMultiple implements Serializable, Runnable {
45 public void testFilterProvided() {
46 RequiredInterface req1 = new DefaultRequiredInterface("name",
48 RequiredInterface req2 = new DefaultRequiredInterface("name",
50 ProvidedInterface prov1 = new DefaultProvidedInterface("name",
52 ProvidedInterface prov2 = new DefaultProvidedInterface("name",
54 ProvidedInterface prov3 = new DefaultProvidedInterface("name",
57 AssertionUtils.assertEquals(new RequiredInterface[] { req1 },
58 Container.filterRequiredServices(prov1, Arrays
59 .asList(new RequiredInterface[] { req1 })));
60 AssertionUtils.assertEquals(new RequiredInterface[] { req1 },
61 Container.filterRequiredServices(prov1, Arrays
62 .asList(new RequiredInterface[] { req1, req2 })));
63 AssertionUtils.assertEquals(new RequiredInterface[] { req1, req2 },
64 Container.filterRequiredServices(prov3, Arrays
65 .asList(new RequiredInterface[] { req1, req2 })));
67 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
68 Container.filterProvidedServices(req1, Arrays
69 .asList(new ProvidedInterface[] { prov1 })));
70 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
71 Container.filterProvidedServices(req1, Arrays
72 .asList(new ProvidedInterface[] { prov1, prov2 })));
73 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1, prov3 },
74 Container.filterProvidedServices(req1, Arrays
75 .asList(new ProvidedInterface[] { prov1, prov3 })));
78 public void testEnvironmentApplication() {
79 Component environment = new Environment(_tracker);
80 Component application = new Application(_tracker);
81 Container container = new Container("root", new Component[] {
82 environment, application }, new ProvidedInterface[0],
83 new RequiredInterface[0]);
86 AssertionUtils.assertEquals(new String[] { "start.environment",
87 "start.application" }, _tracker.getEvents(
88 Thread.currentThread()).toArray(new String[0]));
89 ProvidedInterface[] envServices = environment.getRunningServices();
90 assertEquals(2, envServices.length);
91 ProvidedInterface[] appServices = environment.getRunningServices();
92 assertEquals(2, appServices.length);
96 public void testApplicationEnvironment() {
98 Component environment = new Environment();
99 Component application = new Application();
100 Container container = new Container(
103 application, environment },
104 new ProvidedInterface[0], new RequiredInterface[0]);
106 } catch (SystemAssemblyException e) {
107 // e.printStackTrace();
113 public void testComposite() {
114 Component environment = new Environment(_tracker);
115 Component application = new Application(_tracker);
116 assertEquals(0, _tracker.getEventCount());
117 assertEquals(Status.NOT_STARTED, environment.getStatus());
118 assertEquals(Status.NOT_STARTED, application.getStatus());
120 Container system = new Container("all", new Component[] { environment,
121 application }, new ProvidedInterface[0],
122 new RequiredInterface[0]);
123 assertEquals(Status.NOT_STARTED, system.getStatus());
125 RequiredInterface[] required = system.getRequiredServices();
126 assertEquals(0, required.length);
127 ProvidedInterface[] provided = system.getProvidedServices();
128 assertEquals(0, provided.length);
129 assertEquals(Status.RUNNING, environment.getStatus());
130 assertEquals(Status.RUNNING, application.getStatus());
131 assertEquals(Status.RUNNING, system.getStatus());
133 AssertionUtils.assertEquals(
134 new String[] { "start.environment", "start.application" },
135 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
139 assertEquals(Status.STOPPED, environment.getStatus());
140 assertEquals(Status.STOPPED, application.getStatus());
141 assertEquals(Status.STOPPED, system.getStatus());
143 AssertionUtils.assertEquals(
144 new String[] { "stop.application", "stop.environment" },
145 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
150 public void testCompositeWithWrongProvidedInfo() {
152 Component environment = new Environment();
153 Component application = new Application();
154 Container system = new Container("all", new Component[] {
155 environment, application },
156 new ProvidedInterface[] { new DefaultProvidedInterface(
157 "string", String.class) },
158 new DefaultRequiredInterface[0]);
159 } catch (SystemAssemblyException e) {
165 public void testCompositeRequiredInterfaceNotProvided() {
167 Component environment = new Environment();
168 Component application = new Application();
169 Container system = new Container("all", new Component[] {
170 environment, application }, new ProvidedInterface[0],
171 new RequiredInterface[] { new DefaultRequiredInterface(
172 "string", String.class) });
174 } catch (SystemAssemblyException e) {
180 public void testCompositeWithSuperfluousRequiredInfo() {
181 Component environment = new Environment();
182 Component application = new Application();
183 Container system = new Container("all", new Component[] { environment,
184 application }, new ProvidedInterface[0],
185 new RequiredInterface[] { new DefaultRequiredInterface(
186 "string", String.class) });
187 system.getRequiredServices()[0]
188 .setProvider(new DefaultProvidedInterface("hallo", String.class));
190 RequiredInterface[] required = system.getRequiredServices();
191 assertEquals(1, required.length);
192 ProvidedInterface[] provided = system.getProvidedServices();
193 assertEquals(0, provided.length);
196 public void testCompositeWithExternalDependencesNotProvided() {
198 Component environment = new Environment();
199 Component application = new Application();
200 Container system = new Container("all",
201 new Component[] { application }, new ProvidedInterface[0],
202 application.getRequiredServices());
204 } catch (SystemAssemblyException e) {
211 public void testCompositeWithExternalDependencesProvided() {
213 Component environment = new Environment();
214 Component application = new Application();
215 Container system = new Container("all",
216 new Component[] { application }, new ProvidedInterface[0],
217 application.getRequiredServices());
219 system.getRequiredServices()[0].setProvider(environment
220 .getProvidedServices()[0]);
221 system.getRequiredServices()[1].setProvider(environment
222 .getProvidedServices()[1]);
225 RequiredInterface[] required = system.getRequiredServices();
226 assertEquals(2, required.length);
227 ProvidedInterface[] provided = system.getProvidedServices();
228 assertEquals(0, provided.length);
232 public void testAmbiguousInterfaces() {
234 Component environment1 = new Environment();
235 Component environment2 = new Environment();
236 Component application = new Application();
237 Container container = new Container("root", new Component[] {
238 environment1, environment2, application },
239 new ProvidedInterface[0], new RequiredInterface[0]);
242 } catch (SystemAssemblyException e) {
248 public void testIncompleteRequirements() {
250 Component application = new Application();
251 Container system = new Container("all",
252 new Component[] { application }, new ProvidedInterface[0],
253 new RequiredInterface[0]);
255 } catch (SystemAssemblyException e) {