(no commit message)
[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 import org.wamblee.test.EventTracker;
10
11 import junit.framework.TestCase;
12
13 public class SystemAssemblerTest extends TestCase {
14
15         private EventTracker<String> _tracker;
16
17         @Override
18         protected void setUp() throws Exception {
19                 super.setUp();
20                 _tracker = new EventTracker<String>();
21         }
22
23         private static class MyMultiple implements Serializable, Runnable {
24                 @Override
25                 public void run() {
26                         // Empty
27                 }
28         }
29
30         public void testFilterProvided() {
31                 RequiredInterface req1 = new DefaultRequiredInterface("name",
32                                 Runnable.class);
33                 RequiredInterface req2 = new DefaultRequiredInterface("name",
34                                 Serializable.class);
35                 ProvidedInterface prov1 = new DefaultProvidedInterface("name",
36                                 Runnable.class);
37                 ProvidedInterface prov2 = new DefaultProvidedInterface("name",
38                                 Serializable.class);
39                 ProvidedInterface prov3 = new DefaultProvidedInterface("name",
40                                 MyMultiple.class);
41
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 })));
51
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 })));
61         }
62
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]);
68                 assembler.start();
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);
76                 
77         }
78
79         public void testApplicationEnvironment() {
80                 try {
81                         Component environment = new Environment();
82                         Component application = new Application();
83                         SystemAssembler assembler = new SystemAssembler(new Component[] {
84                                         application, environment }, new ProvidedInterface[0]);
85                         assembler.start();
86                 } catch (SystemAssemblyException e) {
87                         // e.printStackTrace();
88                         return;
89                 }
90                 fail();
91         }
92
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());
99                 
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());
112                 
113                 AssertionUtils.assertEquals(
114                                 new String[] { "start.environment", "start.application" }, 
115                                 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
116         _tracker.clear();
117                 
118                 system.stop();
119                 assertEquals(Status.STOPPED, environment.getStatus());
120                 assertEquals(Status.STOPPED, application.getStatus());
121                 assertEquals(Status.STOPPED, system.getStatus());
122                 
123                 AssertionUtils.assertEquals(
124                                 new String[] { "stop.application", "stop.environment" }, 
125                                 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
126       
127                 
128         }
129
130         public void testCompositeWithWrongProvidedInfo() {
131                 try {
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) {
140                         return;
141                 }
142                 fail();
143         }
144
145         public void testCompositeRequiredInterfaceNotProvided() {
146                 try {
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) {
155                         return;
156                 }
157                 fail();
158         }
159
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);
174         }
175
176         public void testCompositeWithExternalDependencesNotProvided() {
177                 try {
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) {
185                         return;
186                 }
187                 fail();
188
189         }
190
191         public void testCompositeWithExternalDependencesProvided() {
192
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]);
203
204                 system.start("root");
205                 RequiredInterface[] required = system.getRequiredServices();
206                 assertEquals(2, required.length);
207                 ProvidedInterface[] provided = system.getProvidedServices();
208                 assertEquals(0, provided.length);
209
210         }
211
212         public void testAmbiguousInterfaces() {
213                 try {
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]);
220                         assembler.start();
221
222                 } catch (SystemAssemblyException e) {
223                         return;
224                 }
225                 fail();
226         }
227
228         public void testIncompleteRequirements() {
229                 try {
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) {
236                         return;
237                 }
238                 fail();
239         }
240
241 }