Package rename org.wamblee.system to org.wamblee.system.core
[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.ArrayList;
20 import java.util.Arrays;
21
22 import org.wamblee.system.core.Component;
23 import org.wamblee.system.core.Container;
24 import org.wamblee.system.core.DefaultProvidedInterface;
25 import org.wamblee.system.core.DefaultRequiredInterface;
26 import org.wamblee.system.core.ProvidedInterface;
27 import org.wamblee.system.core.RequiredInterface;
28 import org.wamblee.system.core.SystemAssemblyException;
29 import org.wamblee.system.core.Component.Status;
30 import org.wamblee.test.AssertionUtils;
31 import org.wamblee.test.EventTracker;
32
33 import junit.framework.TestCase;
34
35 public class ContainerTest extends TestCase {
36
37         private EventTracker<String> _tracker;
38
39         @Override
40         protected void setUp() throws Exception {
41                 super.setUp();
42                 _tracker = new EventTracker<String>();
43         }
44
45         private static class MyMultiple implements Serializable, Runnable {
46                 @Override
47                 public void run() {
48                         // Empty
49                 }
50         }
51
52         public void testFilterProvided() {
53                 RequiredInterface req1 = new DefaultRequiredInterface("name",
54                                 Runnable.class);
55                 RequiredInterface req2 = new DefaultRequiredInterface("name",
56                                 Serializable.class);
57                 ProvidedInterface prov1 = new DefaultProvidedInterface("name",
58                                 Runnable.class);
59                 ProvidedInterface prov2 = new DefaultProvidedInterface("name",
60                                 Serializable.class);
61                 ProvidedInterface prov3 = new DefaultProvidedInterface("name",
62                                 MyMultiple.class);
63
64                 AssertionUtils.assertEquals(new RequiredInterface[] { req1 },
65                                 Container.filterRequiredServices(prov1, Arrays
66                                                 .asList(new RequiredInterface[] { req1 })));
67                 AssertionUtils.assertEquals(new RequiredInterface[] { req1 },
68                                 Container.filterRequiredServices(prov1, Arrays
69                                                 .asList(new RequiredInterface[] { req1, req2 })));
70                 AssertionUtils.assertEquals(new RequiredInterface[] { req1, req2 },
71                                 Container.filterRequiredServices(prov3, Arrays
72                                                 .asList(new RequiredInterface[] { req1, req2 })));
73
74                 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
75                                 Container.filterProvidedServices(req1, Arrays
76                                                 .asList(new ProvidedInterface[] { prov1 })));
77                 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
78                                 Container.filterProvidedServices(req1, Arrays
79                                                 .asList(new ProvidedInterface[] { prov1, prov2 })));
80                 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1, prov3 },
81                                 Container.filterProvidedServices(req1, Arrays
82                                                 .asList(new ProvidedInterface[] { prov1, prov3 })));
83         }
84
85         public void testEnvironmentApplication() {
86                 Component environment = new Environment(_tracker);
87                 Component application = new Application(_tracker);
88                 Container container = new Container("root", new Component[] {
89                                 environment, application }, new ProvidedInterface[0],
90                                 new RequiredInterface[0]);
91                                 
92                 container.start();
93                 AssertionUtils.assertEquals(new String[] { "start.environment",
94                                 "start.application" }, _tracker.getEvents(
95                                 Thread.currentThread()).toArray(new String[0]));
96                 ProvidedInterface[] envServices = environment.getRunningServices();
97                 assertEquals(2, envServices.length);
98                 ProvidedInterface[] appServices = environment.getRunningServices();
99                 assertEquals(2, appServices.length);
100                 
101         }
102
103         public void testApplicationEnvironment() {
104                 try {
105                         Component environment = new Environment();
106                         Component application = new Application();
107                         Container container = new Container(
108                                         "root",
109                                         new Component[] {
110                                         application, environment }, 
111                                         new ProvidedInterface[0], new RequiredInterface[0]);
112                         container.start();
113                 } catch (SystemAssemblyException e) {
114                         // e.printStackTrace();
115                         return;
116                 }
117                 fail();
118         }
119
120         public void testComposite() {
121                 Component environment = new Environment(_tracker);
122                 Component application = new Application(_tracker);
123                 assertEquals(0, _tracker.getEventCount());
124                 assertEquals(Status.NOT_STARTED, environment.getStatus());
125                 assertEquals(Status.NOT_STARTED, application.getStatus());
126                 
127                 Container system = new Container("all", new Component[] { environment,
128                                 application }, new ProvidedInterface[0],
129                                 new RequiredInterface[0]);
130                 assertEquals(Status.NOT_STARTED, system.getStatus());
131                 system.start();
132                 RequiredInterface[] required = system.getRequiredServices();
133                 assertEquals(0, required.length);
134                 ProvidedInterface[] provided = system.getProvidedServices();
135                 assertEquals(0, provided.length);
136                 assertEquals(Status.RUNNING, environment.getStatus());
137                 assertEquals(Status.RUNNING, application.getStatus());
138                 assertEquals(Status.RUNNING, system.getStatus());
139                 
140                 AssertionUtils.assertEquals(
141                                 new String[] { "start.environment", "start.application" }, 
142                                 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
143         _tracker.clear();
144                 
145                 system.stop();
146                 assertEquals(Status.STOPPED, environment.getStatus());
147                 assertEquals(Status.STOPPED, application.getStatus());
148                 assertEquals(Status.STOPPED, system.getStatus());
149                 
150                 AssertionUtils.assertEquals(
151                                 new String[] { "stop.application", "stop.environment" }, 
152                                 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
153       
154                 
155         }
156
157         public void testCompositeWithWrongProvidedInfo() {
158                 try {
159                         Component environment = new Environment();
160                         Component application = new Application();
161                         Container system = new Container("all", new Component[] {
162                                         environment, application },
163                                         new ProvidedInterface[] { new DefaultProvidedInterface(
164                                                         "string", String.class) },
165                                         new DefaultRequiredInterface[0]);
166                 } catch (SystemAssemblyException e) {
167                         return;
168                 }
169                 fail();
170         }
171
172         public void testCompositeRequiredInterfaceNotProvided() {
173                 try {
174                         Component environment = new Environment();
175                         Component application = new Application();
176                         Container system = new Container("all", new Component[] {
177                                         environment, application }, new ProvidedInterface[0],
178                                         new RequiredInterface[] { new DefaultRequiredInterface(
179                                                         "string", String.class) });
180                         system.start();
181                 } catch (SystemAssemblyException e) {
182                         return;
183                 }
184                 fail();
185         }
186
187         public void testCompositeWithSuperfluousRequiredInfo() {
188                 Component environment = new Environment();
189                 Component application = new Application();
190                 Container system = new Container("all", new Component[] { environment,
191                                 application }, new ProvidedInterface[0],
192                                 new RequiredInterface[] { new DefaultRequiredInterface(
193                                                 "string", String.class) });
194                 system.getRequiredServices()[0]
195                                 .setProvider(new DefaultProvidedInterface("hallo", String.class));
196                 system.start();
197                 RequiredInterface[] required = system.getRequiredServices();
198                 assertEquals(1, required.length);
199                 ProvidedInterface[] provided = system.getProvidedServices();
200                 assertEquals(0, provided.length);
201         }
202
203         public void testCompositeWithExternalDependencesNotProvided() {
204                 try {
205                         Component environment = new Environment();
206                         Component application = new Application();
207                         Container system = new Container("all",
208                                         new Component[] { application }, new ProvidedInterface[0],
209                                         application.getRequiredServices());
210                         system.start();
211                 } catch (SystemAssemblyException e) {
212                         return;
213                 }
214                 fail();
215
216         }
217
218         public void testCompositeWithExternalDependencesProvided() {
219
220                 Component environment = new Environment();
221                 Component application = new Application();
222                 Container system = new Container("all",
223                                 new Component[] { application }, new ProvidedInterface[0],
224                                 application.getRequiredServices());
225                 environment.start();
226                 system.getRequiredServices()[0].setProvider(environment
227                                 .getProvidedServices()[0]);
228                 system.getRequiredServices()[1].setProvider(environment
229                                 .getProvidedServices()[1]);
230
231                 system.start();
232                 RequiredInterface[] required = system.getRequiredServices();
233                 assertEquals(2, required.length);
234                 ProvidedInterface[] provided = system.getProvidedServices();
235                 assertEquals(0, provided.length);
236
237         }
238
239         public void testAmbiguousInterfaces() {
240                 try {
241                         Component environment1 = new Environment();
242                         Component environment2 = new Environment();
243                         Component application = new Application();
244                         Container container = new Container("root", new Component[] {
245                                         environment1, environment2, application },
246                                         new ProvidedInterface[0], new RequiredInterface[0]);
247                         container.start();
248
249                 } catch (SystemAssemblyException e) {
250                         return;
251                 }
252                 fail();
253         }
254
255         public void testIncompleteRequirements() {
256                 try {
257                         Component application = new Application();
258                         Container system = new Container("all",
259                                         new Component[] { application }, new ProvidedInterface[0],
260                                         new RequiredInterface[0]);
261                         system.start();
262                 } catch (SystemAssemblyException e) {
263                         return;
264                 }
265                 fail();
266         }
267
268 }