blob: 1a1d3592b6445f03d1628a8f2f677b346594b5e1 [file] [log] [blame]
Clement Escoffierce0e1e52008-03-28 15:33:36 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.ipojo.test.scenarios.dependency.di;
20
21import java.util.Properties;
22
23import org.apache.felix.ipojo.ComponentInstance;
24import org.apache.felix.ipojo.architecture.Architecture;
25import org.apache.felix.ipojo.architecture.InstanceDescription;
26import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
27import org.apache.felix.ipojo.test.scenarios.service.CheckService;
28import org.apache.felix.ipojo.test.scenarios.util.Utils;
29import org.osgi.framework.ServiceReference;
30
31public class OptionalDependencies extends OSGiTestCase {
32
33 ComponentInstance instance1, instance2, instance3, instance4, instance5;
34 ComponentInstance fooProvider;
35
36 public void setUp() {
37 try {
38 Properties prov = new Properties();
39 prov.put("name", "FooProvider");
40 fooProvider = Utils.getFactoryByName(context, "FooProviderType-1").createComponentInstance(prov);
41 fooProvider.stop();
42
43 Properties i1 = new Properties();
44 i1.put("name", "Simple");
45 instance1 = Utils.getFactoryByName(context, "DISimpleOptionalCheckServiceProvider").createComponentInstance(i1);
46
47 Properties i2 = new Properties();
48 i2.put("name", "Void");
49 instance2 = Utils.getFactoryByName(context, "DIVoidOptionalCheckServiceProvider").createComponentInstance(i2);
50
51 Properties i3 = new Properties();
52 i3.put("name", "Object");
53 instance3 = Utils.getFactoryByName(context, "DIObjectOptionalCheckServiceProvider").createComponentInstance(i3);
54
55 Properties i4 = new Properties();
56 i4.put("name", "Ref");
57 instance4 = Utils.getFactoryByName(context, "DIRefOptionalCheckServiceProvider").createComponentInstance(i4);
58
59 Properties i5 = new Properties();
60 i5.put("name", "Both");
61 instance5 = Utils.getFactoryByName(context, "DIBothOptionalCheckServiceProvider").createComponentInstance(i5);
62 } catch(Exception e) { fail(e.getMessage()); }
63 }
64
65 public void tearDown() {
66 instance1.dispose();
67 instance2.dispose();
68 instance3.dispose();
69 instance4.dispose();
70 instance5.dispose();
71 fooProvider.dispose();
72 instance1 = null;
73 instance2 = null;
74 instance3 = null;
75 instance4 = null;
76 instance5 = null;
77 fooProvider = null;
78 }
79
80 public void testSimple() {
81 ServiceReference arch_ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), instance1.getInstanceName());
82 assertNotNull("Check architecture availability", arch_ref);
83 InstanceDescription id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
84 assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
85
86 ServiceReference cs_ref = Utils.getServiceReferenceByName(context, CheckService.class.getName(), instance1.getInstanceName());
87 assertNotNull("Check CheckService availability", cs_ref);
88 CheckService cs = (CheckService) context.getService(cs_ref);
89 Properties props = cs.getProps();
90
91 //Check properties
92 assertFalse("check CheckService invocation - 1", ((Boolean)props.get("result")).booleanValue()); // False is returned (nullable)
93 assertEquals("check void bind invocation - 1", ((Integer)props.get("voidB")).intValue(), 0);
94 assertEquals("check void unbind callback invocation - 1", ((Integer)props.get("voidU")).intValue(), 0);
95 assertEquals("check object bind callback invocation - 1", ((Integer)props.get("objectB")).intValue(), 0);
96 assertEquals("check object unbind callback invocation - 1", ((Integer)props.get("objectU")).intValue(), 0);
97 assertEquals("check ref bind callback invocation - 1", ((Integer)props.get("refB")).intValue(), 0);
98 assertEquals("check ref unbind callback invocation - 1", ((Integer)props.get("refU")).intValue(), 0);
99 assertNull("Check FS invocation (object) - 1", props.get("object"));
100 assertEquals("Check FS invocation (int) - 1", ((Integer)props.get("int")).intValue(), 5);
101 assertEquals("Check FS invocation (long) - 1", ((Long)props.get("long")).longValue(), 5);
102 assertEquals("Check FS invocation (double) - 1", ((Double)props.get("double")).doubleValue(), 5.0);
103
104 fooProvider.start();
105
106 id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
107 assertTrue("Check instance validity - 2", id.getState() == ComponentInstance.VALID);
108
109 assertNotNull("Check CheckService availability", cs_ref);
110 cs = (CheckService) context.getService(cs_ref);
111 props = cs.getProps();
112
113 //Check properties
114 assertTrue("check CheckService invocation - 2", ((Boolean)props.get("result")).booleanValue()); // True, a provider is there
115 assertEquals("check void bind invocation - 2", ((Integer)props.get("voidB")).intValue(), 0);
116 assertEquals("check void unbind callback invocation - 2", ((Integer)props.get("voidU")).intValue(), 0);
117 assertEquals("check object bind callback invocation - 2", ((Integer)props.get("objectB")).intValue(), 0);
118 assertEquals("check object unbind callback invocation - 2", ((Integer)props.get("objectU")).intValue(), 0);
119 assertEquals("check ref bind callback invocation - 2", ((Integer)props.get("refB")).intValue(), 0);
120 assertEquals("check ref unbind callback invocation - 2", ((Integer)props.get("refU")).intValue(), 0);
121 assertNotNull("Check FS invocation (object) - 2", props.get("object"));
122 assertEquals("Check FS invocation (int) - 2", ((Integer)props.get("int")).intValue(), 1);
123 assertEquals("Check FS invocation (long) - 2", ((Long)props.get("long")).longValue(), 1);
124 assertEquals("Check FS invocation (double) - 2", ((Double)props.get("double")).doubleValue(), 1.0);
125
126 fooProvider.stop();
127
128 id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
129 assertTrue("Check instance validity - 3", id.getState() == ComponentInstance.VALID);
130
131 id = null;
132 cs = null;
133 context.ungetService(arch_ref);
134 context.ungetService(cs_ref);
135 }
136
137 public void testVoid() {
138 ServiceReference arch_ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), instance2.getInstanceName());
139 assertNotNull("Check architecture availability", arch_ref);
140 InstanceDescription id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
141 assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
142
143 ServiceReference cs_ref = Utils.getServiceReferenceByName(context, CheckService.class.getName(), instance2.getInstanceName());
144 assertNotNull("Check CheckService availability", cs_ref);
145 CheckService cs = (CheckService) context.getService(cs_ref);
146 Properties props = cs.getProps();
147 //Check properties
148 assertFalse("check CheckService invocation - 1", ((Boolean)props.get("result")).booleanValue()); // False is returned (nullable)
149 assertEquals("check void bind invocation - 1", ((Integer)props.get("voidB")).intValue(), 0);
150 assertEquals("check void unbind callback invocation - 1", ((Integer)props.get("voidU")).intValue(), 0);
151 assertEquals("check object bind callback invocation - 1", ((Integer)props.get("objectB")).intValue(), 0);
152 assertEquals("check object unbind callback invocation - 1", ((Integer)props.get("objectU")).intValue(), 0);
153 assertEquals("check ref bind callback invocation - 1", ((Integer)props.get("refB")).intValue(), 0);
154 assertEquals("check ref unbind callback invocation - 1", ((Integer)props.get("refU")).intValue(), 0);
155 assertNull("Check FS invocation (object) - 1", props.get("object"));
156 assertEquals("Check FS invocation (int) - 1", ((Integer)props.get("int")).intValue(), 5);
157 assertEquals("Check FS invocation (long) - 1", ((Long)props.get("long")).longValue(), 5);
158 assertEquals("Check FS invocation (double) - 1", ((Double)props.get("double")).doubleValue(), 5.0);
159
160 fooProvider.start();
161
162 id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
163 assertTrue("Check instance validity - 2", id.getState() == ComponentInstance.VALID);
164
165 assertNotNull("Check CheckService availability", cs_ref);
166 cs = (CheckService) context.getService(cs_ref);
167 props = cs.getProps();
168 //Check properties
169 assertTrue("check CheckService invocation -2", ((Boolean)props.get("result")).booleanValue());
170 assertEquals("check void bind invocation -2", ((Integer)props.get("voidB")).intValue(), 1);
171 assertEquals("check void unbind callback invocation -2", ((Integer)props.get("voidU")).intValue(), 0);
172 assertEquals("check object bind callback invocation -2", ((Integer)props.get("objectB")).intValue(), 0);
173 assertEquals("check object unbind callback invocation -2", ((Integer)props.get("objectU")).intValue(), 0);
174 assertEquals("check ref bind callback invocation -2", ((Integer)props.get("refB")).intValue(), 0);
175 assertEquals("check ref unbind callback invocation -2", ((Integer)props.get("refU")).intValue(), 0);
176 assertNotNull("Check FS invocation (object) - 2", props.get("object"));
177 assertEquals("Check FS invocation (int) - 2", ((Integer)props.get("int")).intValue(), 1);
178 assertEquals("Check FS invocation (long) - 2", ((Long)props.get("long")).longValue(), 1);
179 assertEquals("Check FS invocation (double) - 2", ((Double)props.get("double")).doubleValue(), 1.0);
180
181 fooProvider.stop();
182
183 id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
184 assertTrue("Check instance validity - 3", id.getState() == ComponentInstance.VALID);
185
186 cs = (CheckService) context.getService(cs_ref);
187 props = cs.getProps();
188 //Check properties
189 assertFalse("check CheckService invocation -3", ((Boolean)props.get("result")).booleanValue());
190 assertEquals("check void bind invocation -3", ((Integer)props.get("voidB")).intValue(), 1);
191 assertEquals("check void unbind callback invocation -3 ("+((Integer)props.get("voidU")) + ")", ((Integer)props.get("voidU")).intValue(), 1);
192 assertEquals("check object bind callback invocation -3", ((Integer)props.get("objectB")).intValue(), 0);
193 assertEquals("check object unbind callback invocation -3", ((Integer)props.get("objectU")).intValue(), 0);
194 assertEquals("check ref bind callback invocation -3", ((Integer)props.get("refB")).intValue(), 0);
195 assertEquals("check ref unbind callback invocation -3", ((Integer)props.get("refU")).intValue(), 0);
196 assertNull("Check FS invocation (object) - 3", props.get("object"));
197 assertEquals("Check FS invocation (int) - 3", ((Integer)props.get("int")).intValue(), 5);
198 assertEquals("Check FS invocation (long) - 3", ((Long)props.get("long")).longValue(), 5);
199 assertEquals("Check FS invocation (double) - 3", ((Double)props.get("double")).doubleValue(), 5.0);
200
201 id = null;
202 cs = null;
203 context.ungetService(arch_ref);
204 context.ungetService(cs_ref);
205 }
206
207 public void testObject() {
208 ServiceReference arch_ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), instance3.getInstanceName());
209 assertNotNull("Check architecture availability", arch_ref);
210 InstanceDescription id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
211 assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
212
213 ServiceReference cs_ref = Utils.getServiceReferenceByName(context, CheckService.class.getName(), instance3.getInstanceName());
214 assertNotNull("Check CheckService availability", cs_ref);
215 CheckService cs = (CheckService) context.getService(cs_ref);
216 Properties props = cs.getProps();
217 //Check properties
218 assertFalse("check CheckService invocation -1", ((Boolean)props.get("result")).booleanValue()); // False is returned (nullable)
219 assertEquals("check void bind invocation -1", ((Integer)props.get("voidB")).intValue(), 0);
220 assertEquals("check void unbind callback invocation -1", ((Integer)props.get("voidU")).intValue(), 0);
221 assertEquals("check object bind callback invocation -1", ((Integer)props.get("objectB")).intValue(), 0);
222 assertEquals("check object unbind callback invocation -1", ((Integer)props.get("objectU")).intValue(), 0);
223 assertEquals("check ref bind callback invocation -1", ((Integer)props.get("refB")).intValue(), 0);
224 assertEquals("check ref unbind callback invocation -1", ((Integer)props.get("refU")).intValue(), 0);
225
226 fooProvider.start();
227
228 id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
229 assertTrue("Check instance validity - 2", id.getState() == ComponentInstance.VALID);
230
231 assertNotNull("Check CheckService availability", cs_ref);
232 cs = (CheckService) context.getService(cs_ref);
233 props = cs.getProps();
234 //Check properties
235 assertTrue("check CheckService invocation -2", ((Boolean)props.get("result")).booleanValue());
236 assertEquals("check void bind invocation -2", ((Integer)props.get("voidB")).intValue(), 0);
237 assertEquals("check void unbind callback invocation -2", ((Integer)props.get("voidU")).intValue(), 0);
238 assertEquals("check object bind callback invocation -2 (" + ((Integer)props.get("objectB")).intValue() + ")", ((Integer)props.get("objectB")).intValue(), 1);
239 assertEquals("check object unbind callback invocation -2", ((Integer)props.get("objectU")).intValue(), 0);
240 assertEquals("check ref bind callback invocation -2", ((Integer)props.get("refB")).intValue(), 0);
241 assertEquals("check ref unbind callback invocation -2", ((Integer)props.get("refU")).intValue(), 0);
242
243 fooProvider.stop();
244
245 id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
246 assertTrue("Check instance validity - 3", id.getState() == ComponentInstance.VALID);
247
248 cs = (CheckService) context.getService(cs_ref);
249 props = cs.getProps();
250 //Check properties
251 assertFalse("check CheckService invocation -3", ((Boolean)props.get("result")).booleanValue());
252 assertEquals("check void bind invocation -3", ((Integer)props.get("voidB")).intValue(), 0);
253 assertEquals("check void unbind callback invocation -3", ((Integer)props.get("voidU")).intValue(), 0);
254 assertEquals("check object bind callback invocation -3", ((Integer)props.get("objectB")).intValue(), 1);
255 assertEquals("check object unbind callback invocation -3", ((Integer)props.get("objectU")).intValue(), 1);
256 assertEquals("check ref bind callback invocation -3", ((Integer)props.get("refB")).intValue(), 0);
257 assertEquals("check ref unbind callback invocation -3", ((Integer)props.get("refU")).intValue(), 0);
258
259 id = null;
260 cs = null;
261 context.ungetService(arch_ref);
262 context.ungetService(cs_ref);
263 }
264
265 public void testRef() {
266 ServiceReference arch_ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), instance4.getInstanceName());
267 assertNotNull("Check architecture availability", arch_ref);
268 InstanceDescription id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
269 assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
270
271 ServiceReference cs_ref = Utils.getServiceReferenceByName(context, CheckService.class.getName(), instance4.getInstanceName());
272 assertNotNull("Check CheckService availability", cs_ref);
273 CheckService cs = (CheckService) context.getService(cs_ref);
274 Properties props = cs.getProps();
275 //Check properties
276 assertFalse("check CheckService invocation -1", ((Boolean)props.get("result")).booleanValue()); // False is returned (nullable)
277 assertEquals("check void bind invocation -1", ((Integer)props.get("voidB")).intValue(), 0);
278 assertEquals("check void unbind callback invocation -1", ((Integer)props.get("voidU")).intValue(), 0);
279 assertEquals("check object bind callback invocation -1", ((Integer)props.get("objectB")).intValue(), 0);
280 assertEquals("check object unbind callback invocation -1", ((Integer)props.get("objectU")).intValue(), 0);
281 assertEquals("check ref bind callback invocation -1", ((Integer)props.get("refB")).intValue(), 0);
282 assertEquals("check ref unbind callback invocation -1", ((Integer)props.get("refU")).intValue(), 0);
283
284 fooProvider.start();
285
286 id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
287 assertTrue("Check instance validity - 2", id.getState() == ComponentInstance.VALID);
288
289 assertNotNull("Check CheckService availability", cs_ref);
290 cs = (CheckService) context.getService(cs_ref);
291 props = cs.getProps();
292 //Check properties
293 assertTrue("check CheckService invocation -2", ((Boolean)props.get("result")).booleanValue());
294 assertEquals("check void bind invocation -2", ((Integer)props.get("voidB")).intValue(), 0);
295 assertEquals("check void unbind callback invocation -2", ((Integer)props.get("voidU")).intValue(), 0);
296 assertEquals("check object bind callback invocation -2", ((Integer)props.get("objectB")).intValue(), 0);
297 assertEquals("check object unbind callback invocation -2", ((Integer)props.get("objectU")).intValue(), 0);
298 assertEquals("check ref bind callback invocation -2", ((Integer)props.get("refB")).intValue(), 1);
299 assertEquals("check ref unbind callback invocation -2", ((Integer)props.get("refU")).intValue(), 0);
300
301 fooProvider.stop();
302
303 id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
304 assertTrue("Check instance validity - 3", id.getState() == ComponentInstance.VALID);
305
306 cs = (CheckService) context.getService(cs_ref);
307 props = cs.getProps();
308 //Check properties
309 assertFalse("check CheckService invocation -3", ((Boolean)props.get("result")).booleanValue());
310 assertEquals("check void bind invocation -3", ((Integer)props.get("voidB")).intValue(), 0);
311 assertEquals("check void unbind callback invocation -3", ((Integer)props.get("voidU")).intValue(), 0);
312 assertEquals("check object bind callback invocation -3", ((Integer)props.get("objectB")).intValue(), 0);
313 assertEquals("check object unbind callback invocation -3", ((Integer)props.get("objectU")).intValue(), 0);
314 assertEquals("check ref bind callback invocation -3", ((Integer)props.get("refB")).intValue(), 1);
315 assertEquals("check ref unbind callback invocation -3", ((Integer)props.get("refU")).intValue(), 1);
316
317 id = null;
318 cs = null;
319 context.ungetService(arch_ref);
320 context.ungetService(cs_ref);
321 }
322
323 public void testBoth() {
324 ServiceReference arch_ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), instance5.getInstanceName());
325 assertNotNull("Check architecture availability", arch_ref);
326 InstanceDescription id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
327 assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
328
329 ServiceReference cs_ref = Utils.getServiceReferenceByName(context, CheckService.class.getName(), instance5.getInstanceName());
330 assertNotNull("Check CheckService availability", cs_ref);
331 CheckService cs = (CheckService) context.getService(cs_ref);
332 Properties props = cs.getProps();
333 //Check properties
334 assertFalse("check CheckService invocation -1", ((Boolean)props.get("result")).booleanValue()); // False is returned (nullable)
335 assertEquals("check void bind invocation -1", ((Integer)props.get("voidB")).intValue(), 0);
336 assertEquals("check void unbind callback invocation -1", ((Integer)props.get("voidU")).intValue(), 0);
337 assertEquals("check object bind callback invocation -1", ((Integer)props.get("objectB")).intValue(), 0);
338 assertEquals("check object unbind callback invocation -1", ((Integer)props.get("objectU")).intValue(), 0);
339 assertEquals("check both bind callback invocation -1", ((Integer)props.get("bothB")).intValue(), 0);
340 assertEquals("check both unbind callback invocation -1", ((Integer)props.get("bothU")).intValue(), 0);
341
342 fooProvider.start();
343
344 id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
345 assertTrue("Check instance validity - 2", id.getState() == ComponentInstance.VALID);
346
347 assertNotNull("Check CheckService availability", cs_ref);
348 cs = (CheckService) context.getService(cs_ref);
349 props = cs.getProps();
350 //Check properties
351 assertTrue("check CheckService invocation -2", ((Boolean)props.get("result")).booleanValue());
352 assertEquals("check void bind invocation -2", ((Integer)props.get("voidB")).intValue(), 0);
353 assertEquals("check void unbind callback invocation -2", ((Integer)props.get("voidU")).intValue(), 0);
354 assertEquals("check object bind callback invocation -2", ((Integer)props.get("objectB")).intValue(), 0);
355 assertEquals("check object unbind callback invocation -2", ((Integer)props.get("objectU")).intValue(), 0);
356 assertEquals("check ref bind callback invocation -2", ((Integer)props.get("refB")).intValue(), 0);
357 assertEquals("check ref unbind callback invocation -2", ((Integer)props.get("refU")).intValue(), 0);
358 assertEquals("check both bind callback invocation -2", ((Integer)props.get("bothB")).intValue(), 1);
359 assertEquals("check both unbind callback invocation -2", ((Integer)props.get("bothU")).intValue(), 0);
360
361 fooProvider.stop();
362
363 id = ((Architecture) context.getService(arch_ref)).getInstanceDescription();
364 assertTrue("Check instance validity - 3", id.getState() == ComponentInstance.VALID);
365
366 cs = (CheckService) context.getService(cs_ref);
367 props = cs.getProps();
368 //Check properties
369 assertFalse("check CheckService invocation -3", ((Boolean)props.get("result")).booleanValue());
370 assertEquals("check void bind invocation -3", ((Integer)props.get("voidB")).intValue(), 0);
371 assertEquals("check void unbind callback invocation -3", ((Integer)props.get("voidU")).intValue(), 0);
372 assertEquals("check object bind callback invocation -3", ((Integer)props.get("objectB")).intValue(), 0);
373 assertEquals("check object unbind callback invocation -3", ((Integer)props.get("objectU")).intValue(), 0);
374 assertEquals("check ref bind callback invocation -3", ((Integer)props.get("refB")).intValue(), 0);
375 assertEquals("check ref unbind callback invocation -3", ((Integer)props.get("refU")).intValue(), 0);
376 assertEquals("check both bind callback invocation -3", ((Integer)props.get("bothB")).intValue(), 1);
377 assertEquals("check both unbind callback invocation -3", ((Integer)props.get("bothU")).intValue(), 1);
378
379 id = null;
380 cs = null;
381 context.ungetService(arch_ref);
382 context.ungetService(cs_ref);
383 }
384
385
386}