1 package org.wamblee.system;
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.Arrays;
7 import org.wamblee.system.Component.Status;
8 import org.wamblee.test.AssertionUtils;
9 import org.wamblee.test.EventTracker;
11 import junit.framework.TestCase;
13 public class SystemAssemblerTest extends TestCase {
15 private EventTracker<String> _tracker;
18 protected void setUp() throws Exception {
20 _tracker = new EventTracker<String>();
23 private static class MyMultiple implements Serializable, Runnable {
30 public void testFilterProvided() {
31 RequiredInterface req1 = new DefaultRequiredInterface("name",
33 RequiredInterface req2 = new DefaultRequiredInterface("name",
35 ProvidedInterface prov1 = new DefaultProvidedInterface("name",
37 ProvidedInterface prov2 = new DefaultProvidedInterface("name",
39 ProvidedInterface prov3 = new DefaultProvidedInterface("name",
42 AssertionUtils.assertEquals(new RequiredInterface[] { req1 },
43 SystemAssembler.filterRequiredServices(prov1, Arrays
44 .asList(new RequiredInterface[] { req1 })));
45 AssertionUtils.assertEquals(new RequiredInterface[] { req1 },
46 SystemAssembler.filterRequiredServices(prov1, Arrays
47 .asList(new RequiredInterface[] { req1, req2 })));
48 AssertionUtils.assertEquals(new RequiredInterface[] { req1, req2 },
49 SystemAssembler.filterRequiredServices(prov3, Arrays
50 .asList(new RequiredInterface[] { req1, req2 })));
52 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
53 SystemAssembler.filterProvidedServices(req1, Arrays
54 .asList(new ProvidedInterface[] { prov1 })));
55 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
56 SystemAssembler.filterProvidedServices(req1, Arrays
57 .asList(new ProvidedInterface[] { prov1, prov2 })));
58 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1, prov3 },
59 SystemAssembler.filterProvidedServices(req1, Arrays
60 .asList(new ProvidedInterface[] { prov1, prov3 })));
63 public void testEnvironmentApplication() {
64 Component environment = new Environment(_tracker);
65 Component application = new Application(_tracker);
66 SystemAssembler assembler = new SystemAssembler(new Component[] {
67 environment, application }, new ProvidedInterface[0]);
69 AssertionUtils.assertEquals(new String[] { "start.environment",
70 "start.application" }, _tracker.getEvents(
71 Thread.currentThread()).toArray(new String[0]));
72 ProvidedInterface[] envServices = environment.getRunningServices();
73 assertEquals(2, envServices.length);
74 ProvidedInterface[] appServices = environment.getRunningServices();
75 assertEquals(2, appServices.length);
79 public void testApplicationEnvironment() {
81 Component environment = new Environment();
82 Component application = new Application();
83 SystemAssembler assembler = new SystemAssembler(new Component[] {
84 application, environment }, new ProvidedInterface[0]);
86 } catch (SystemAssemblyException e) {
87 // e.printStackTrace();
93 public void testComposite() {
94 Component environment = new Environment(_tracker);
95 Component application = new Application(_tracker);
96 assertEquals(0, _tracker.getEventCount());
97 assertEquals(Status.NOT_STARTED, environment.getStatus());
98 assertEquals(Status.NOT_STARTED, application.getStatus());
100 Container system = new Container("all", new Component[] { environment,
101 application }, new ProvidedInterface[0],
102 new RequiredInterface[0]);
103 assertEquals(Status.NOT_STARTED, system.getStatus());
104 system.start("root");
105 RequiredInterface[] required = system.getRequiredServices();
106 assertEquals(0, required.length);
107 ProvidedInterface[] provided = system.getProvidedServices();
108 assertEquals(0, provided.length);
109 assertEquals(Status.RUNNING, environment.getStatus());
110 assertEquals(Status.RUNNING, application.getStatus());
111 assertEquals(Status.RUNNING, system.getStatus());
113 AssertionUtils.assertEquals(
114 new String[] { "start.environment", "start.application" },
115 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
119 assertEquals(Status.STOPPED, environment.getStatus());
120 assertEquals(Status.STOPPED, application.getStatus());
121 assertEquals(Status.STOPPED, system.getStatus());
123 AssertionUtils.assertEquals(
124 new String[] { "stop.application", "stop.environment" },
125 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
130 public void testCompositeWithWrongProvidedInfo() {
132 Component environment = new Environment();
133 Component application = new Application();
134 Container system = new Container("all", new Component[] {
135 environment, application },
136 new ProvidedInterface[] { new DefaultProvidedInterface(
137 "string", String.class) },
138 new DefaultRequiredInterface[0]);
139 } catch (SystemAssemblyException e) {
145 public void testCompositeRequiredInterfaceNotProvided() {
147 Component environment = new Environment();
148 Component application = new Application();
149 Container system = new Container("all", new Component[] {
150 environment, application }, new ProvidedInterface[0],
151 new RequiredInterface[] { new DefaultRequiredInterface(
152 "string", String.class) });
153 system.start("root");
154 } catch (SystemAssemblyException e) {
160 public void testCompositeWithSuperfluousRequiredInfo() {
161 Component environment = new Environment();
162 Component application = new Application();
163 Container system = new Container("all", new Component[] { environment,
164 application }, new ProvidedInterface[0],
165 new RequiredInterface[] { new DefaultRequiredInterface(
166 "string", String.class) });
167 system.getRequiredServices()[0]
168 .setProvider(new DefaultProvidedInterface("hallo", String.class));
169 system.start("root");
170 RequiredInterface[] required = system.getRequiredServices();
171 assertEquals(1, required.length);
172 ProvidedInterface[] provided = system.getProvidedServices();
173 assertEquals(0, provided.length);
176 public void testCompositeWithExternalDependencesNotProvided() {
178 Component environment = new Environment();
179 Component application = new Application();
180 Container system = new Container("all",
181 new Component[] { application }, new ProvidedInterface[0],
182 application.getRequiredServices());
183 system.start("root");
184 } catch (SystemAssemblyException e) {
191 public void testCompositeWithExternalDependencesProvided() {
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 environment.start("env");
199 system.getRequiredServices()[0].setProvider(environment
200 .getProvidedServices()[0]);
201 system.getRequiredServices()[1].setProvider(environment
202 .getProvidedServices()[1]);
204 system.start("root");
205 RequiredInterface[] required = system.getRequiredServices();
206 assertEquals(2, required.length);
207 ProvidedInterface[] provided = system.getProvidedServices();
208 assertEquals(0, provided.length);
212 public void testAmbiguousInterfaces() {
214 Component environment1 = new Environment();
215 Component environment2 = new Environment();
216 Component application = new Application();
217 SystemAssembler assembler = new SystemAssembler(new Component[] {
218 environment1, environment2, application },
219 new ProvidedInterface[0]);
222 } catch (SystemAssemblyException e) {
228 public void testIncompleteRequirements() {
230 Component application = new Application();
231 Container system = new Container("all",
232 new Component[] { application }, new ProvidedInterface[0],
233 new RequiredInterface[0]);
234 system.start("root");
235 } catch (SystemAssemblyException e) {