(no commit message)
[utils] / system / general / src / test / java / org / wamblee / system / SystemAssemblerTest.java
1 /*
2  * Copyright 2007 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */ 
16 package org.wamblee.system;
17
18 import java.io.Serializable;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21
22 import org.wamblee.system.Component.Status;
23 import org.wamblee.test.AssertionUtils;
24 import org.wamblee.test.EventTracker;
25
26 import junit.framework.TestCase;
27
28 public class SystemAssemblerTest extends TestCase {
29
30         private EventTracker<String> _tracker;
31
32         @Override
33         protected void setUp() throws Exception {
34                 super.setUp();
35                 _tracker = new EventTracker<String>();
36         }
37
38         private static class MyMultiple implements Serializable, Runnable {
39                 @Override
40                 public void run() {
41                         // Empty
42                 }
43         }
44
45         public void testFilterProvided() {
46                 RequiredInterface req1 = new DefaultRequiredInterface("name",
47                                 Runnable.class);
48                 RequiredInterface req2 = new DefaultRequiredInterface("name",
49                                 Serializable.class);
50                 ProvidedInterface prov1 = new DefaultProvidedInterface("name",
51                                 Runnable.class);
52                 ProvidedInterface prov2 = new DefaultProvidedInterface("name",
53                                 Serializable.class);
54                 ProvidedInterface prov3 = new DefaultProvidedInterface("name",
55                                 MyMultiple.class);
56
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 })));
66
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 })));
76         }
77
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]);
83                 assembler.start();
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);
91                 
92         }
93
94         public void testApplicationEnvironment() {
95                 try {
96                         Component environment = new Environment();
97                         Component application = new Application();
98                         SystemAssembler assembler = new SystemAssembler(new Component[] {
99                                         application, environment }, new ProvidedInterface[0]);
100                         assembler.start();
101                 } catch (SystemAssemblyException e) {
102                         // e.printStackTrace();
103                         return;
104                 }
105                 fail();
106         }
107
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());
114                 
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());
127                 
128                 AssertionUtils.assertEquals(
129                                 new String[] { "start.environment", "start.application" }, 
130                                 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
131         _tracker.clear();
132                 
133                 system.stop();
134                 assertEquals(Status.STOPPED, environment.getStatus());
135                 assertEquals(Status.STOPPED, application.getStatus());
136                 assertEquals(Status.STOPPED, system.getStatus());
137                 
138                 AssertionUtils.assertEquals(
139                                 new String[] { "stop.application", "stop.environment" }, 
140                                 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
141       
142                 
143         }
144
145         public void testCompositeWithWrongProvidedInfo() {
146                 try {
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) {
155                         return;
156                 }
157                 fail();
158         }
159
160         public void testCompositeRequiredInterfaceNotProvided() {
161                 try {
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) {
170                         return;
171                 }
172                 fail();
173         }
174
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);
189         }
190
191         public void testCompositeWithExternalDependencesNotProvided() {
192                 try {
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) {
200                         return;
201                 }
202                 fail();
203
204         }
205
206         public void testCompositeWithExternalDependencesProvided() {
207
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]);
218
219                 system.start("root");
220                 RequiredInterface[] required = system.getRequiredServices();
221                 assertEquals(2, required.length);
222                 ProvidedInterface[] provided = system.getProvidedServices();
223                 assertEquals(0, provided.length);
224
225         }
226
227         public void testAmbiguousInterfaces() {
228                 try {
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]);
235                         assembler.start();
236
237                 } catch (SystemAssemblyException e) {
238                         return;
239                 }
240                 fail();
241         }
242
243         public void testIncompleteRequirements() {
244                 try {
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) {
251                         return;
252                 }
253                 fail();
254         }
255
256 }