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 SystemAssemblerTest 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 SystemAssembler.filterRequiredServices(prov1, Arrays
59 .asList(new RequiredInterface[] { req1 })));
60 AssertionUtils.assertEquals(new RequiredInterface[] { req1 },
61 SystemAssembler.filterRequiredServices(prov1, Arrays
62 .asList(new RequiredInterface[] { req1, req2 })));
63 AssertionUtils.assertEquals(new RequiredInterface[] { req1, req2 },
64 SystemAssembler.filterRequiredServices(prov3, Arrays
65 .asList(new RequiredInterface[] { req1, req2 })));
67 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
68 SystemAssembler.filterProvidedServices(req1, Arrays
69 .asList(new ProvidedInterface[] { prov1 })));
70 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
71 SystemAssembler.filterProvidedServices(req1, Arrays
72 .asList(new ProvidedInterface[] { prov1, prov2 })));
73 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1, prov3 },
74 SystemAssembler.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 SystemAssembler assembler = new SystemAssembler(new Component[] {
82 environment, application }, new ProvidedInterface[0]);
84 AssertionUtils.assertEquals(new String[] { "start.environment",
85 "start.application" }, _tracker.getEvents(
86 Thread.currentThread()).toArray(new String[0]));
87 ProvidedInterface[] envServices = environment.getRunningServices();
88 assertEquals(2, envServices.length);
89 ProvidedInterface[] appServices = environment.getRunningServices();
90 assertEquals(2, appServices.length);
94 public void testApplicationEnvironment() {
96 Component environment = new Environment();
97 Component application = new Application();
98 SystemAssembler assembler = new SystemAssembler(new Component[] {
99 application, environment }, new ProvidedInterface[0]);
101 } catch (SystemAssemblyException e) {
102 // e.printStackTrace();
108 public void testComposite() {
109 Component environment = new Environment(_tracker);
110 Component application = new Application(_tracker);
111 assertEquals(0, _tracker.getEventCount());
112 assertEquals(Status.NOT_STARTED, environment.getStatus());
113 assertEquals(Status.NOT_STARTED, application.getStatus());
115 Container system = new Container("all", new Component[] { environment,
116 application }, new ProvidedInterface[0],
117 new RequiredInterface[0]);
118 assertEquals(Status.NOT_STARTED, system.getStatus());
119 system.start("root");
120 RequiredInterface[] required = system.getRequiredServices();
121 assertEquals(0, required.length);
122 ProvidedInterface[] provided = system.getProvidedServices();
123 assertEquals(0, provided.length);
124 assertEquals(Status.RUNNING, environment.getStatus());
125 assertEquals(Status.RUNNING, application.getStatus());
126 assertEquals(Status.RUNNING, system.getStatus());
128 AssertionUtils.assertEquals(
129 new String[] { "start.environment", "start.application" },
130 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
134 assertEquals(Status.STOPPED, environment.getStatus());
135 assertEquals(Status.STOPPED, application.getStatus());
136 assertEquals(Status.STOPPED, system.getStatus());
138 AssertionUtils.assertEquals(
139 new String[] { "stop.application", "stop.environment" },
140 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
145 public void testCompositeWithWrongProvidedInfo() {
147 Component environment = new Environment();
148 Component application = new Application();
149 Container system = new Container("all", new Component[] {
150 environment, application },
151 new ProvidedInterface[] { new DefaultProvidedInterface(
152 "string", String.class) },
153 new DefaultRequiredInterface[0]);
154 } catch (SystemAssemblyException e) {
160 public void testCompositeRequiredInterfaceNotProvided() {
162 Component environment = new Environment();
163 Component application = new Application();
164 Container system = new Container("all", new Component[] {
165 environment, application }, new ProvidedInterface[0],
166 new RequiredInterface[] { new DefaultRequiredInterface(
167 "string", String.class) });
168 system.start("root");
169 } catch (SystemAssemblyException e) {
175 public void testCompositeWithSuperfluousRequiredInfo() {
176 Component environment = new Environment();
177 Component application = new Application();
178 Container system = new Container("all", new Component[] { environment,
179 application }, new ProvidedInterface[0],
180 new RequiredInterface[] { new DefaultRequiredInterface(
181 "string", String.class) });
182 system.getRequiredServices()[0]
183 .setProvider(new DefaultProvidedInterface("hallo", String.class));
184 system.start("root");
185 RequiredInterface[] required = system.getRequiredServices();
186 assertEquals(1, required.length);
187 ProvidedInterface[] provided = system.getProvidedServices();
188 assertEquals(0, provided.length);
191 public void testCompositeWithExternalDependencesNotProvided() {
193 Component environment = new Environment();
194 Component application = new Application();
195 Container system = new Container("all",
196 new Component[] { application }, new ProvidedInterface[0],
197 application.getRequiredServices());
198 system.start("root");
199 } catch (SystemAssemblyException e) {
206 public void testCompositeWithExternalDependencesProvided() {
208 Component environment = new Environment();
209 Component application = new Application();
210 Container system = new Container("all",
211 new Component[] { application }, new ProvidedInterface[0],
212 application.getRequiredServices());
213 environment.start("env");
214 system.getRequiredServices()[0].setProvider(environment
215 .getProvidedServices()[0]);
216 system.getRequiredServices()[1].setProvider(environment
217 .getProvidedServices()[1]);
219 system.start("root");
220 RequiredInterface[] required = system.getRequiredServices();
221 assertEquals(2, required.length);
222 ProvidedInterface[] provided = system.getProvidedServices();
223 assertEquals(0, provided.length);
227 public void testAmbiguousInterfaces() {
229 Component environment1 = new Environment();
230 Component environment2 = new Environment();
231 Component application = new Application();
232 SystemAssembler assembler = new SystemAssembler(new Component[] {
233 environment1, environment2, application },
234 new ProvidedInterface[0]);
237 } catch (SystemAssemblyException e) {
243 public void testIncompleteRequirements() {
245 Component application = new Application();
246 Container system = new Container("all",
247 new Component[] { application }, new ProvidedInterface[0],
248 new RequiredInterface[0]);
249 system.start("root");
250 } catch (SystemAssemblyException e) {