bcca8cc11bb823c37f01232e81dc78dc2767c76f
[utils] / system / general / src / test / java / org / wamblee / system / core / ContainerTest.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.core;
17
18 import java.io.Serializable;
19 import java.util.Arrays;
20
21 import junit.framework.TestCase;
22
23 import org.easymock.classextension.ConstructorArgs;
24 import org.easymock.classextension.EasyMock;
25 import org.easymock.classextension.IMocksControl;
26 import org.wamblee.test.AssertionUtils;
27 import org.wamblee.test.EventTracker;
28
29 public class ContainerTest extends TestCase {
30
31         private EventTracker<String> _tracker;
32
33         @Override
34         protected void setUp() throws Exception {
35                 super.setUp();
36                 _tracker = new EventTracker<String>();
37         }
38
39         private static class MyMultiple implements Serializable, Runnable {
40                 @Override
41                 public void run() {
42                         // Empty
43                 }
44         }
45
46         public void testFilterProvided() {
47                 RequiredInterface req1 = new DefaultRequiredInterface("name",
48                                 Runnable.class);
49                 RequiredInterface req2 = new DefaultRequiredInterface("name",
50                                 Serializable.class);
51                 ProvidedInterface prov1 = new DefaultProvidedInterface("name",
52                                 Runnable.class);
53                 ProvidedInterface prov2 = new DefaultProvidedInterface("name",
54                                 Serializable.class);
55                 ProvidedInterface prov3 = new DefaultProvidedInterface("name",
56                                 MyMultiple.class);
57
58                 AssertionUtils.assertEquals(new RequiredInterface[] { req1 }, Container
59                                 .filterRequiredServices(prov1, Arrays
60                                                 .asList(new RequiredInterface[] { req1 })));
61                 AssertionUtils.assertEquals(new RequiredInterface[] { req1 }, Container
62                                 .filterRequiredServices(prov1, Arrays
63                                                 .asList(new RequiredInterface[] { req1, req2 })));
64                 AssertionUtils.assertEquals(new RequiredInterface[] { req1, req2 },
65                                 Container.filterRequiredServices(prov3, Arrays
66                                                 .asList(new RequiredInterface[] { req1, req2 })));
67
68                 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
69                                 Container.filterProvidedServices(req1, Arrays
70                                                 .asList(new ProvidedInterface[] { prov1 })));
71                 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
72                                 Container.filterProvidedServices(req1, Arrays
73                                                 .asList(new ProvidedInterface[] { prov1, prov2 })));
74                 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1, prov3 },
75                                 Container.filterProvidedServices(req1, Arrays
76                                                 .asList(new ProvidedInterface[] { prov1, prov3 })));
77         }
78
79         public void testEnvironmentApplication() {
80                 Environment environment = new Environment(_tracker);
81                 Application application = new Application(_tracker);
82                 Container container = new Container("root", new Component[] {
83                                 environment, application }, new ProvidedInterface[0],
84                                 new RequiredInterface[0]);
85
86                 container.start();
87                 AssertionUtils.assertEquals(new String[] { "start.environment",
88                                 "start.application" }, _tracker.getEvents(
89                                 Thread.currentThread()).toArray(new String[0]));
90                 ProvidedInterface[] envServices = environment.getRunningInterfaces();
91                 assertEquals(2, envServices.length);
92                 ProvidedInterface[] appServices = application.getRunningInterfaces();
93                 assertEquals(0, appServices.length);
94                 
95                 assertEquals(environment.getString(), application.getString());
96                 assertEquals(environment.getInteger(), application.getInteger());
97
98         }
99
100         public void testApplicationEnvironment() {
101                 try {
102                         Component environment = new Environment();
103                         Component application = new Application();
104                         Container container = new Container("root", new Component[] {
105                                         application, environment }, new ProvidedInterface[0],
106                                         new RequiredInterface[0]);
107                         container.start();
108                 } catch (SystemAssemblyException e) {
109                         // e.printStackTrace();
110                         return;
111                 }
112                 fail();
113         }
114
115         public void testComposite() {
116                 Component environment = new Environment(_tracker);
117                 Component application = new Application(_tracker);
118                 assertEquals(0, _tracker.getEventCount());
119
120                 Container system = new Container("all", new Component[] { environment,
121                                 application }, new ProvidedInterface[0],
122                                 new RequiredInterface[0]);
123                 system.start();
124                 RequiredInterface[] required = system.getRequiredInterfaces();
125                 assertEquals(0, required.length);
126                 ProvidedInterface[] provided = system.getProvidedInterfaces();
127                 assertEquals(0, provided.length);
128
129                 AssertionUtils.assertEquals(new String[] { "start.environment",
130                                 "start.application" }, _tracker.getEvents(
131                                 Thread.currentThread()).toArray(new String[0]));
132                 _tracker.clear();
133
134                 system.stop();
135                 AssertionUtils.assertEquals(new String[] { "stop.application",
136                                 "stop.environment" }, _tracker
137                                 .getEvents(Thread.currentThread()).toArray(new String[0]));
138
139         }
140
141         public void testCompositeWithWrongProvidedInfo() {
142                 try {
143                         Component environment = new Environment();
144                         Component application = new Application();
145                         Container system = new Container("all", new Component[] {
146                                         environment, application },
147                                         new ProvidedInterface[] { new DefaultProvidedInterface(
148                                                         "float", Float.class) },
149                                         new DefaultRequiredInterface[0]);
150                 } catch (SystemAssemblyException e) {
151                         return;
152                 }
153                 fail();
154         }
155
156         public void testCompositeRequiredInterfaceNotProvided() {
157                 try {
158                         Component environment = new Environment();
159                         Component application = new Application();
160                         Container system = new Container("all", new Component[] {
161                                         environment, application }, new ProvidedInterface[0],
162                                         new RequiredInterface[] { new DefaultRequiredInterface(
163                                                         "string", String.class) });
164                         system.start();
165                 } catch (SystemAssemblyException e) {
166                         return;
167                 }
168                 fail();
169         }
170
171         public void testCompositeWithSuperfluousRequiredInfo() {
172                 Component environment = new Environment();
173                 Component application = new Application();
174                 Container system = new Container("all", new Component[] { environment,
175                                 application }, new ProvidedInterface[0],
176                                 new RequiredInterface[] { new DefaultRequiredInterface(
177                                                 "float", Float.class) });
178                 system.getRequiredInterfaces()[0]
179                                 .setProvider(new DefaultProvidedInterface("hallo", Float.class));
180                 system.start();
181                 RequiredInterface[] required = system.getRequiredInterfaces();
182                 assertEquals(1, required.length);
183                 ProvidedInterface[] provided = system.getProvidedInterfaces();
184                 assertEquals(0, provided.length);
185         }
186
187         public void testCompositeWithExternalDependencesNotProvided() {
188                 try {
189                         Component environment = new Environment();
190                         Component application = new Application();
191                         Container system = new Container("all",
192                                         new Component[] { application }, new ProvidedInterface[0],
193                                         application.getRequiredInterfaces());
194                         system.start();
195                 } catch (SystemAssemblyException e) {
196                         return;
197                 }
198                 fail();
199
200         }
201
202         public void testCompositeWithExternalDependencesProvided() {
203
204                 Component environment = new Environment();
205                 Component application = new Application();
206                 Container system = new Container("all",
207                                 new Component[] { application }, new ProvidedInterface[0],
208                                 application.getRequiredInterfaces());
209                 environment.start();
210                 system.getRequiredInterfaces()[0].setProvider(environment
211                                 .getProvidedInterfaces()[0]);
212                 system.getRequiredInterfaces()[1].setProvider(environment
213                                 .getProvidedInterfaces()[1]);
214
215                 system.start();
216                 RequiredInterface[] required = system.getRequiredInterfaces();
217                 assertEquals(2, required.length);
218                 ProvidedInterface[] provided = system.getProvidedInterfaces();
219                 assertEquals(0, provided.length);
220
221         }
222
223         public void testAmbiguousInterfaces() {
224                 try {
225                         Component environment1 = new Environment();
226                         Component environment2 = new Environment();
227                         Component application = new Application();
228                         Container container = new Container("root", new Component[] {
229                                         environment1, environment2, application },
230                                         new ProvidedInterface[0], new RequiredInterface[0]);
231                         container.start();
232
233                 } catch (SystemAssemblyException e) {
234                         return;
235                 }
236                 fail();
237         }
238
239         public void testIncompleteRequirements() {
240                 try {
241                         Component application = new Application();
242                         Container system = new Container("all",
243                                         new Component[] { application }, new ProvidedInterface[0],
244                                         new RequiredInterface[0]);
245                         system.start();
246                 } catch (SystemAssemblyException e) {
247                         return;
248                 }
249                 fail();
250         }
251
252         public void testEnvironmentApplicationRollbackOnException()
253                         throws Exception {
254                 IMocksControl control = EasyMock.createStrictControl();
255
256                 Environment environment = new Environment(_tracker);
257                 Application application = control.createMock(Application.class,
258                                 new ConstructorArgs(Application.class.getConstructor()),
259                                 Application.class.getDeclaredMethod("doStart"));
260
261                 application.doStart();
262                 EasyMock.expectLastCall().andThrow(new RuntimeException());
263                 control.replay();
264
265                 try {
266                         Container container = new Container("root", new Component[] {
267                                         environment, application }, new ProvidedInterface[0],
268                                         new RequiredInterface[0]);
269
270                         container.start();
271                 } catch (RuntimeException e) {
272                         AssertionUtils.assertEquals(new String[] { "start.environment",
273                                         "stop.environment" }, _tracker.getEvents(
274                                         Thread.currentThread()).toArray(new String[0]));
275                         ProvidedInterface[] envServices = environment.getRunningInterfaces();
276                         assertEquals(0, envServices.length);
277                         assertNull(environment.getProvidedInterfaces()[0].getImplementation());
278                         assertNull(environment.getProvidedInterfaces()[0].getImplementation());
279                         return;
280                 }
281                 fail();
282         }
283
284         public void testEnvironmentApplicationRollbackOnExceptionWithExceptionOnStop()
285                         throws Exception {
286                 IMocksControl control = EasyMock.createControl();
287
288                 Environment environment = new Environment(_tracker);
289                 // Application 1 will throw an exception while stopping.
290                 Application application1 = control.createMock(Application.class,
291                                 new ConstructorArgs(Application.class.getConstructor()),
292                                 Application.class.getDeclaredMethod("doStop"));
293
294                 application1.doStop();
295                 EasyMock.expectLastCall().andThrow(new RuntimeException());
296                 
297                 // application 2 will throw an exception while starting
298                 Application application2 = control.createMock(Application.class,
299                                 new ConstructorArgs(Application.class.getConstructor()),
300                                 Application.class.getDeclaredMethod("doStart"));
301
302                 application2.doStart();
303                 EasyMock.expectLastCall().andThrow(new RuntimeException());
304                 
305                 control.replay();
306
307                 try {
308                         Container container = new Container("root", new Component[] {
309                                         environment, application1, application2 }, new ProvidedInterface[0],
310                                         new RequiredInterface[0]);
311
312                         container.start();
313                 } catch (RuntimeException e) {
314                         AssertionUtils.assertEquals(new String[] { "start.environment", 
315                                         "stop.environment" }, _tracker.getEvents(
316                                         Thread.currentThread()).toArray(new String[0]));
317                         ProvidedInterface[] envServices = environment.getRunningInterfaces();
318                         assertEquals(0, envServices.length);
319                         assertNull(environment.getProvidedInterfaces()[0].getImplementation());
320                         assertNull(environment.getProvidedInterfaces()[0].getImplementation());
321                         return;
322                 }
323                 fail();
324         }
325         
326         public void testOptionalRequiredInterfaceProvidedOptionalInternal() {
327                 Application application = new Application(true);
328                 Container container = new Container("top", new Component[] { application }, 
329                                 new ProvidedInterface[0], Application.required(true));
330                 Environment env = new Environment();
331                 container.getRequiredInterfaces()[0].setProvider(
332                                 env.getProvidedInterfaces()[0]); 
333                 container.getRequiredInterfaces()[1].setProvider(
334                                 env.getProvidedInterfaces()[1]);
335             container.start();
336             assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
337             assertSame(env.getProvidedInterfaces()[1], container.getRequiredInterfaces()[1].getProvider());
338             assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
339             assertSame(env.getProvidedInterfaces()[1], application.getRequiredInterfaces()[1].getProvider());
340         }
341         
342         public void testOptionalRequiredInterfaceNotProvidedOptionalInternal() {
343                 Application application = new Application(true);
344                 Container container = new Container("top", new Component[] { application }, 
345                                 new ProvidedInterface[0], Application.required(true));
346                 Environment env = new Environment();
347                 container.getRequiredInterfaces()[0].setProvider(
348                                 env.getProvidedInterfaces()[0]); 
349             container.start();
350             assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
351             assertNull(container.getRequiredInterfaces()[1].getProvider());
352             assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
353             assertNull(application.getRequiredInterfaces()[1].getProvider());
354         }
355         
356         public void testOptionalRequiredInterfaceProvidedMandatoryInternal() { 
357                 Application application = new Application();
358                 Container container = new Container("top", new Component[] { application }, 
359                                 new ProvidedInterface[0], Application.required(true));
360                 Environment env = new Environment();
361                 container.getRequiredInterfaces()[0].setProvider(
362                                 env.getProvidedInterfaces()[0]); 
363                 container.getRequiredInterfaces()[1].setProvider(
364                                 env.getProvidedInterfaces()[1]);
365             container.start();
366             assertSame(env.getProvidedInterfaces()[0], container.getRequiredInterfaces()[0].getProvider());
367             assertSame(env.getProvidedInterfaces()[1], container.getRequiredInterfaces()[1].getProvider());
368             assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
369             assertSame(env.getProvidedInterfaces()[1], application.getRequiredInterfaces()[1].getProvider());   
370         }
371 }