c372bf045b13c236f7923c25f6afd4525ee02dae
[utils] / system / general / src / test / java / org / wamblee / system / SystemAssemblerTest.java
1 package org.wamblee.system;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6
7 import org.wamblee.system.Component.Status;
8 import org.wamblee.test.AssertionUtils;
9
10 import junit.framework.TestCase;
11
12 public class SystemAssemblerTest extends TestCase {
13
14         private ServiceRegistry _registry;
15
16         @Override
17         protected void setUp() throws Exception {
18                 super.setUp();
19                 _registry = new DefaultServiceRegistry();
20         }
21
22         private static class MyMultiple implements Serializable, Runnable {
23                 @Override
24                 public void run() {
25                         // Empty
26                 }
27         }
28
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);
40
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,
47                                                                 req2 })));
48                 AssertionUtils.assertEquals(new RequiredInterface[] { req1,
49                                 req2 }, SystemAssembler.filterRequiredServices(prov3, Arrays
50                                 .asList(new RequiredInterface[] { req1, req2 })));
51
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[] {
60                                                                                 prov1, prov2 })));
61                 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1,
62                                 prov3 }, SystemAssembler.filterProvidedServices(req1, Arrays
63                                 .asList(new ProvidedInterface[] { prov1, prov3 })));
64         }
65
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);
77
78                 environment.stop();
79                 assertEquals(0, _registry.listAllServices().length);
80
81                 application.stop();
82                 assertEquals(0, _registry.listAllServices().length);
83         }
84
85         public void testApplicationEnvironment() {
86                 try {
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();
95                         return;
96                 }
97                 fail();
98         }
99
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());
117
118                 system.stop();
119                 assertEquals(Status.STOPPED, environment.getStatus());
120                 assertEquals(Status.STOPPED, application.getStatus());
121                 assertEquals(Status.STOPPED, system.getStatus());
122         }
123
124         public void testCompositeWithWrongProvidedInfo() {
125                 try {
126                         Component environment = new Environment(_registry);
127                         Component application = new Application(_registry);
128                         Container system = new Container(
129                                         "all",
130                                         _registry,
131                                         new Component[] { environment, application },
132                                         new ProvidedInterface[] { new DefaultProvidedInterfaceDescriptor(
133                                                         "string", String.class) },
134                                         new DefaultRequiredInterfaceDescriptor[0]);
135                 } catch (SystemAssemblyException e) {
136                         return;
137                 }
138                 fail();
139         }
140
141         public void testCompositeWithSuperfluousRequiredInfo() {
142                 Component environment = new Environment(_registry);
143                 Component application = new Application(_registry);
144                 Container system = new Container(
145                                 "all",
146                                 _registry,
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);
156         }
157
158         public void testCompositeWithExternalDependencesNotProvided() {
159                 try {
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) {
168                         return;
169                 }
170                 fail();
171
172         }
173
174         public void testCompositeWithExternalDependencesProvided() {
175
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);
188
189         }
190
191         public void testAmbiguousInterfaces() {
192                 try {
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]);
200
201                 } catch (SystemAssemblyException e) {
202                         return;
203                 }
204                 fail();
205         }
206
207         public void testIncompleteRequirements() {
208                 try {
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) {
216                         return;
217                 }
218                 fail();
219         }
220
221 }