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;
10 import junit.framework.TestCase;
12 public class SystemAssemblerTest extends TestCase {
14 private ServiceRegistry _registry;
17 protected void setUp() throws Exception {
19 _registry = new DefaultServiceRegistry();
22 private static class MyMultiple implements Serializable, Runnable {
29 public void testFilterProvided() {
30 RequiredInterface req1 = new DefaultRequiredInterfaceDescriptor(
31 "name", Runnable.class);
32 RequiredInterface req2 = new DefaultRequiredInterfaceDescriptor(
33 "name", Serializable.class);
34 ProvidedInterface prov1 = new DefaultProvidedInterfaceDescriptor(
35 "name", Runnable.class);
36 ProvidedInterface prov2 = new DefaultProvidedInterfaceDescriptor(
37 "name", Serializable.class);
38 ProvidedInterface prov3 = new DefaultProvidedInterfaceDescriptor(
39 "name", MyMultiple.class);
41 AssertionUtils.assertEquals(new RequiredInterface[] { req1 },
42 SystemAssembler.filterRequiredServices(prov1, Arrays
43 .asList(new RequiredInterface[] { req1 })));
44 AssertionUtils.assertEquals(new RequiredInterface[] { req1 },
45 SystemAssembler.filterRequiredServices(prov1,
46 Arrays.asList(new RequiredInterface[] { req1,
48 AssertionUtils.assertEquals(new RequiredInterface[] { req1,
49 req2 }, SystemAssembler.filterRequiredServices(prov3, Arrays
50 .asList(new RequiredInterface[] { req1, req2 })));
52 AssertionUtils.assertEquals(
53 new ProvidedInterface[] { prov1 },
54 SystemAssembler.filterProvidedServices(req1, Arrays
55 .asList(new ProvidedInterface[] { prov1 })));
56 AssertionUtils.assertEquals(
57 new ProvidedInterface[] { prov1 }, SystemAssembler
58 .filterProvidedServices(req1, Arrays
59 .asList(new ProvidedInterface[] {
61 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1,
62 prov3 }, SystemAssembler.filterProvidedServices(req1, Arrays
63 .asList(new ProvidedInterface[] { prov1, prov3 })));
66 public void testEnvironmentApplication() {
67 Component environment = new Environment(_registry);
68 Component application = new Application(_registry);
69 SystemAssembler assembler = new SystemAssembler(new Component[] {
70 environment, application }, new ProvidedInterface[0]);
71 assembler.start(_registry, new Service[0]);
72 Service[] envServices = environment.getRunningServices();
73 assertEquals(2, envServices.length);
74 Service[] appServices = environment.getRunningServices();
75 assertEquals(2, appServices.length);
76 assertEquals(2, _registry.listAllServices().length);
79 assertEquals(0, _registry.listAllServices().length);
82 assertEquals(0, _registry.listAllServices().length);
85 public void testApplicationEnvironment() {
87 Component environment = new Environment(_registry);
88 Component application = new Application(_registry);
89 SystemAssembler assembler = new SystemAssembler(new Component[] {
90 application, environment },
91 new ProvidedInterface[0]);
92 assembler.start(_registry, new Service[0]);
93 } catch (SystemAssemblyException e) {
94 // e.printStackTrace();
100 public void testComposite() {
101 Component environment = new Environment(_registry);
102 Component application = new Application(_registry);
103 assertEquals(Status.NOT_STARTED, environment.getStatus());
104 assertEquals(Status.NOT_STARTED, application.getStatus());
105 Container system = new Container("all", _registry, new Component[] {
106 environment, application }, new ProvidedInterface[0],
107 new RequiredInterface[0]);
108 assertEquals(Status.NOT_STARTED, system.getStatus());
109 system.start("root", new Service[0]);
110 RequiredInterface[] required = system.getRequiredServices();
111 assertEquals(0, required.length);
112 ProvidedInterface[] provided = system.getProvidedServices();
113 assertEquals(0, provided.length);
114 assertEquals(Status.RUNNING, environment.getStatus());
115 assertEquals(Status.RUNNING, application.getStatus());
116 assertEquals(Status.RUNNING, system.getStatus());
119 assertEquals(Status.STOPPED, environment.getStatus());
120 assertEquals(Status.STOPPED, application.getStatus());
121 assertEquals(Status.STOPPED, system.getStatus());
124 public void testCompositeWithWrongProvidedInfo() {
126 Component environment = new Environment(_registry);
127 Component application = new Application(_registry);
128 Container system = new Container(
131 new Component[] { environment, application },
132 new ProvidedInterface[] { new DefaultProvidedInterfaceDescriptor(
133 "string", String.class) },
134 new DefaultRequiredInterfaceDescriptor[0]);
135 } catch (SystemAssemblyException e) {
141 public void testCompositeWithSuperfluousRequiredInfo() {
142 Component environment = new Environment(_registry);
143 Component application = new Application(_registry);
144 Container system = new Container(
147 new Component[] { environment, application },
148 new ProvidedInterface[0],
149 new RequiredInterface[] { new DefaultRequiredInterfaceDescriptor(
150 "string", String.class) });
151 system.start("root", new Service[0]);
152 RequiredInterface[] required = system.getRequiredServices();
153 assertEquals(1, required.length);
154 ProvidedInterface[] provided = system.getProvidedServices();
155 assertEquals(0, provided.length);
158 public void testCompositeWithExternalDependencesNotProvided() {
160 Component environment = new Environment(_registry);
161 Component application = new Application(_registry);
162 Container system = new Container("all", _registry,
163 new Component[] { application },
164 new ProvidedInterface[0], application
165 .getRequiredServices());
166 system.start("root", new Service[0]);
167 } catch (SystemAssemblyException e) {
174 public void testCompositeWithExternalDependencesProvided() {
176 Component environment = new Environment(_registry);
177 Component application = new Application(_registry);
178 Container system = new Container("all", _registry,
179 new Component[] { application },
180 new ProvidedInterface[0], application
181 .getRequiredServices());
182 Service[] envServices = environment.start("env", new Service[0]);
183 system.start("root", envServices);
184 RequiredInterface[] required = system.getRequiredServices();
185 assertEquals(2, required.length);
186 ProvidedInterface[] provided = system.getProvidedServices();
187 assertEquals(0, provided.length);
191 public void testAmbiguousInterfaces() {
193 Component environment1 = new Environment(_registry);
194 Component environment2 = new Environment(_registry);
195 Component application = new Application(_registry);
196 SystemAssembler assembler = new SystemAssembler(new Component[] {
197 environment1, environment2, application },
198 new ProvidedInterface[0]);
199 assembler.start(_registry, new Service[0]);
201 } catch (SystemAssemblyException e) {
207 public void testIncompleteRequirements() {
209 Component application = new Application(_registry);
210 Container system = new Container("all", _registry,
211 new Component[] { application },
212 new ProvidedInterface[0],
213 new RequiredInterface[0]);
214 system.start("root", new Service[0]);
215 } catch (SystemAssemblyException e) {