775e80064af58f5e797d4e45c0285efb166964a8
[utils] / system / general / src / test / java / org / wamblee / system / 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;
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 ContainerTest 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                                 Container.filterRequiredServices(prov1, Arrays
59                                                 .asList(new RequiredInterface[] { req1 })));
60                 AssertionUtils.assertEquals(new RequiredInterface[] { req1 },
61                                 Container.filterRequiredServices(prov1, Arrays
62                                                 .asList(new RequiredInterface[] { req1, req2 })));
63                 AssertionUtils.assertEquals(new RequiredInterface[] { req1, req2 },
64                                 Container.filterRequiredServices(prov3, Arrays
65                                                 .asList(new RequiredInterface[] { req1, req2 })));
66
67                 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
68                                 Container.filterProvidedServices(req1, Arrays
69                                                 .asList(new ProvidedInterface[] { prov1 })));
70                 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 },
71                                 Container.filterProvidedServices(req1, Arrays
72                                                 .asList(new ProvidedInterface[] { prov1, prov2 })));
73                 AssertionUtils.assertEquals(new ProvidedInterface[] { prov1, prov3 },
74                                 Container.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                 Container container = new Container("root", new Component[] {
82                                 environment, application }, new ProvidedInterface[0],
83                                 new RequiredInterface[0]);
84                                 
85                 container.start();
86                 AssertionUtils.assertEquals(new String[] { "start.environment",
87                                 "start.application" }, _tracker.getEvents(
88                                 Thread.currentThread()).toArray(new String[0]));
89                 ProvidedInterface[] envServices = environment.getRunningServices();
90                 assertEquals(2, envServices.length);
91                 ProvidedInterface[] appServices = environment.getRunningServices();
92                 assertEquals(2, appServices.length);
93                 
94         }
95
96         public void testApplicationEnvironment() {
97                 try {
98                         Component environment = new Environment();
99                         Component application = new Application();
100                         Container container = new Container(
101                                         "root",
102                                         new Component[] {
103                                         application, environment }, 
104                                         new ProvidedInterface[0], new RequiredInterface[0]);
105                         container.start();
106                 } catch (SystemAssemblyException e) {
107                         // e.printStackTrace();
108                         return;
109                 }
110                 fail();
111         }
112
113         public void testComposite() {
114                 Component environment = new Environment(_tracker);
115                 Component application = new Application(_tracker);
116                 assertEquals(0, _tracker.getEventCount());
117                 assertEquals(Status.NOT_STARTED, environment.getStatus());
118                 assertEquals(Status.NOT_STARTED, application.getStatus());
119                 
120                 Container system = new Container("all", new Component[] { environment,
121                                 application }, new ProvidedInterface[0],
122                                 new RequiredInterface[0]);
123                 assertEquals(Status.NOT_STARTED, system.getStatus());
124                 system.start();
125                 RequiredInterface[] required = system.getRequiredServices();
126                 assertEquals(0, required.length);
127                 ProvidedInterface[] provided = system.getProvidedServices();
128                 assertEquals(0, provided.length);
129                 assertEquals(Status.RUNNING, environment.getStatus());
130                 assertEquals(Status.RUNNING, application.getStatus());
131                 assertEquals(Status.RUNNING, system.getStatus());
132                 
133                 AssertionUtils.assertEquals(
134                                 new String[] { "start.environment", "start.application" }, 
135                                 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
136         _tracker.clear();
137                 
138                 system.stop();
139                 assertEquals(Status.STOPPED, environment.getStatus());
140                 assertEquals(Status.STOPPED, application.getStatus());
141                 assertEquals(Status.STOPPED, system.getStatus());
142                 
143                 AssertionUtils.assertEquals(
144                                 new String[] { "stop.application", "stop.environment" }, 
145                                 _tracker.getEvents(Thread.currentThread()).toArray(new String[0]));
146       
147                 
148         }
149
150         public void testCompositeWithWrongProvidedInfo() {
151                 try {
152                         Component environment = new Environment();
153                         Component application = new Application();
154                         Container system = new Container("all", new Component[] {
155                                         environment, application },
156                                         new ProvidedInterface[] { new DefaultProvidedInterface(
157                                                         "string", String.class) },
158                                         new DefaultRequiredInterface[0]);
159                 } catch (SystemAssemblyException e) {
160                         return;
161                 }
162                 fail();
163         }
164
165         public void testCompositeRequiredInterfaceNotProvided() {
166                 try {
167                         Component environment = new Environment();
168                         Component application = new Application();
169                         Container system = new Container("all", new Component[] {
170                                         environment, application }, new ProvidedInterface[0],
171                                         new RequiredInterface[] { new DefaultRequiredInterface(
172                                                         "string", String.class) });
173                         system.start();
174                 } catch (SystemAssemblyException e) {
175                         return;
176                 }
177                 fail();
178         }
179
180         public void testCompositeWithSuperfluousRequiredInfo() {
181                 Component environment = new Environment();
182                 Component application = new Application();
183                 Container system = new Container("all", new Component[] { environment,
184                                 application }, new ProvidedInterface[0],
185                                 new RequiredInterface[] { new DefaultRequiredInterface(
186                                                 "string", String.class) });
187                 system.getRequiredServices()[0]
188                                 .setProvider(new DefaultProvidedInterface("hallo", String.class));
189                 system.start();
190                 RequiredInterface[] required = system.getRequiredServices();
191                 assertEquals(1, required.length);
192                 ProvidedInterface[] provided = system.getProvidedServices();
193                 assertEquals(0, provided.length);
194         }
195
196         public void testCompositeWithExternalDependencesNotProvided() {
197                 try {
198                         Component environment = new Environment();
199                         Component application = new Application();
200                         Container system = new Container("all",
201                                         new Component[] { application }, new ProvidedInterface[0],
202                                         application.getRequiredServices());
203                         system.start();
204                 } catch (SystemAssemblyException e) {
205                         return;
206                 }
207                 fail();
208
209         }
210
211         public void testCompositeWithExternalDependencesProvided() {
212
213                 Component environment = new Environment();
214                 Component application = new Application();
215                 Container system = new Container("all",
216                                 new Component[] { application }, new ProvidedInterface[0],
217                                 application.getRequiredServices());
218                 environment.start();
219                 system.getRequiredServices()[0].setProvider(environment
220                                 .getProvidedServices()[0]);
221                 system.getRequiredServices()[1].setProvider(environment
222                                 .getProvidedServices()[1]);
223
224                 system.start();
225                 RequiredInterface[] required = system.getRequiredServices();
226                 assertEquals(2, required.length);
227                 ProvidedInterface[] provided = system.getProvidedServices();
228                 assertEquals(0, provided.length);
229
230         }
231
232         public void testAmbiguousInterfaces() {
233                 try {
234                         Component environment1 = new Environment();
235                         Component environment2 = new Environment();
236                         Component application = new Application();
237                         Container container = new Container("root", new Component[] {
238                                         environment1, environment2, application },
239                                         new ProvidedInterface[0], new RequiredInterface[0]);
240                         container.start();
241
242                 } catch (SystemAssemblyException e) {
243                         return;
244                 }
245                 fail();
246         }
247
248         public void testIncompleteRequirements() {
249                 try {
250                         Component application = new Application();
251                         Container system = new Container("all",
252                                         new Component[] { application }, new ProvidedInterface[0],
253                                         new RequiredInterface[0]);
254                         system.start();
255                 } catch (SystemAssemblyException e) {
256                         return;
257                 }
258                 fail();
259         }
260
261 }