blob: 2998375037d9f00460a8209dd832ed95bd550007 [file] [log] [blame]
Felix Meschberger42764b82009-08-18 07:51:00 +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.cm.integration;
20
21
22import java.io.IOException;
23import java.util.Hashtable;
24import junit.framework.TestCase;
25
Felix Meschberger6f5b69e2009-08-19 09:39:21 +000026import org.apache.felix.cm.integration.helper.ManagedServiceFactoryTestActivator;
27import org.apache.felix.cm.integration.helper.ManagedServiceFactoryTestActivator2;
Felix Meschberger8659e392009-08-19 05:46:49 +000028import org.apache.felix.cm.integration.helper.ManagedServiceTestActivator;
Felix Meschberger6f5b69e2009-08-19 09:39:21 +000029import org.apache.felix.cm.integration.helper.ManagedServiceTestActivator2;
Felix Meschberger42764b82009-08-18 07:51:00 +000030import org.junit.Test;
31import org.junit.runner.RunWith;
32import org.ops4j.pax.exam.junit.JUnit4TestRunner;
33import org.osgi.framework.Bundle;
34import org.osgi.framework.BundleException;
35import org.osgi.service.cm.Configuration;
Felix Meschberger42764b82009-08-18 07:51:00 +000036
37
38@RunWith(JUnit4TestRunner.class)
39public class ConfigurationBindingTest extends ConfigurationTestBase
40{
41
Felix Meschberger6f5b69e2009-08-19 09:39:21 +000042 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +000043 public void test_configuration_unbound_on_uninstall() throws BundleException
44 {
Felix Meschbergercefe5eb2009-08-19 12:37:32 +000045 String pid = "test_configuration_unbound_on_uninstall";
Felix Meschberger42764b82009-08-18 07:51:00 +000046 configure( pid );
47
48 // ensure configuration is unbound
49 final Configuration beforeInstall = getConfiguration( pid );
50 TestCase.assertNull( beforeInstall.getBundleLocation() );
51
52 bundle = installBundle( pid );
53
54 // ensure no configuration bound before start
55 final Configuration beforeStart = getConfiguration( pid );
56 TestCase.assertNull( beforeInstall.getBundleLocation() );
57 TestCase.assertNull( beforeStart.getBundleLocation() );
58
59 bundle.start();
Felix Meschberger8659e392009-08-19 05:46:49 +000060 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
Felix Meschberger42764b82009-08-18 07:51:00 +000061 TestCase.assertNotNull( "Activator not started !!", tester );
62
63 // give cm time for distribution
64 delay();
65
66 // assert activater has configuration
67 TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props );
Felix Meschberger8659e392009-08-19 05:46:49 +000068 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
Felix Meschberger42764b82009-08-18 07:51:00 +000069
70 // ensure a freshly retrieved object also has the location
71 final Configuration beforeStop = getConfiguration( pid );
72 TestCase.assertEquals( beforeStop.getBundleLocation(), bundle.getLocation() );
73
74 // check whether bundle context is set on first configuration
75 TestCase.assertEquals( beforeInstall.getBundleLocation(), bundle.getLocation() );
76 TestCase.assertEquals( beforeStart.getBundleLocation(), bundle.getLocation() );
77
78 bundle.stop();
79
80 delay();
81
82 // ensure configuration still bound
83 TestCase.assertEquals( beforeInstall.getBundleLocation(), bundle.getLocation() );
84 TestCase.assertEquals( beforeStart.getBundleLocation(), bundle.getLocation() );
85 TestCase.assertEquals( beforeStop.getBundleLocation(), bundle.getLocation() );
86
87 // ensure a freshly retrieved object also has the location
88 final Configuration beforeUninstall = getConfiguration( pid );
89 TestCase.assertEquals( beforeUninstall.getBundleLocation(), bundle.getLocation() );
90
91 bundle.uninstall();
92 bundle = null;
93
94 delay();
95
96 // ensure configuration is not bound any more
97 TestCase.assertNull( beforeInstall.getBundleLocation() );
98 TestCase.assertNull( beforeStart.getBundleLocation() );
99 TestCase.assertNull( beforeStop.getBundleLocation() );
100 TestCase.assertNull( beforeUninstall.getBundleLocation() );
101
102 // ensure a freshly retrieved object also does not have the location
103 final Configuration atEnd = getConfiguration( pid );
104 TestCase.assertNull( atEnd.getBundleLocation() );
105
106 // remove the configuration for good
107 deleteConfig( pid );
108 }
109
110
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000111 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +0000112 public void test_configuration_unbound_on_uninstall_with_cm_restart() throws BundleException
113 {
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000114 final String pid = "test_configuration_unbound_on_uninstall_with_cm_restart";
Felix Meschberger42764b82009-08-18 07:51:00 +0000115 configure( pid );
116 final Bundle cmBundle = getCmBundle();
117
118 // ensure configuration is unbound
119 final Configuration beforeInstall = getConfiguration( pid );
120 TestCase.assertNull( beforeInstall.getBundleLocation() );
121
122 bundle = installBundle( pid );
123
124 // ensure no configuration bound before start
125 final Configuration beforeStart = getConfiguration( pid );
126 TestCase.assertNull( beforeInstall.getBundleLocation() );
127 TestCase.assertNull( beforeStart.getBundleLocation() );
128
129 bundle.start();
Felix Meschberger8659e392009-08-19 05:46:49 +0000130 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
Felix Meschberger42764b82009-08-18 07:51:00 +0000131 TestCase.assertNotNull( "IOActivator not started !!", tester );
132
133 // give cm time for distribution
134 delay();
135
136 // assert activater has configuration
137 TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props );
Felix Meschberger8659e392009-08-19 05:46:49 +0000138 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
Felix Meschberger42764b82009-08-18 07:51:00 +0000139
140 // ensure a freshly retrieved object also has the location
141 final Configuration beforeStop = getConfiguration( pid );
142 TestCase.assertEquals( beforeStop.getBundleLocation(), bundle.getLocation() );
143
144 // check whether bundle context is set on first configuration
145 TestCase.assertEquals( beforeInstall.getBundleLocation(), bundle.getLocation() );
146 TestCase.assertEquals( beforeStart.getBundleLocation(), bundle.getLocation() );
147
148 bundle.stop();
149
150 // ensure configuration still bound
151 TestCase.assertEquals( beforeInstall.getBundleLocation(), bundle.getLocation() );
152 TestCase.assertEquals( beforeStart.getBundleLocation(), bundle.getLocation() );
153 TestCase.assertEquals( beforeStop.getBundleLocation(), bundle.getLocation() );
154
155 // ensure a freshly retrieved object also has the location
156 final Configuration beforeUninstall = getConfiguration( pid );
157 TestCase.assertEquals( beforeUninstall.getBundleLocation(), bundle.getLocation() );
158
159 // stop cm bundle now before uninstalling configured bundle
160 cmBundle.stop();
161 delay();
162
163 // assert configuration admin service is gone
164 TestCase.assertNull( configAdminTracker.getService() );
165
166 // uninstall bundle while configuration admin is stopped
167 bundle.uninstall();
168 bundle = null;
169
170 // start cm bundle again after uninstallation
171 cmBundle.start();
172 delay();
173
174 // ensure a freshly retrieved object also does not have the location
175 // FELIX-1484: this test fails due to bundle location not verified
176 // at first configuration access
177 final Configuration atEnd = getConfiguration( pid );
178 TestCase.assertNull( atEnd.getBundleLocation() );
179
180 // remove the configuration for good
181 deleteConfig( pid );
182 }
183
184
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000185 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +0000186 public void test_not_updated_new_configuration_not_bound_after_bundle_uninstall() throws IOException,
187 BundleException
188 {
189 final String pid = "test_not_updated_new_configuration_not_bound_after_bundle_uninstall";
190
191 // create a configuration but do not update with properties
Felix Meschberger8659e392009-08-19 05:46:49 +0000192 final Configuration newConfig = configure( pid, null, false );
Felix Meschberger42764b82009-08-18 07:51:00 +0000193 TestCase.assertNull( newConfig.getProperties() );
194 TestCase.assertNull( newConfig.getBundleLocation() );
195
196 // start and settle bundle
197 bundle = installBundle( pid );
198 bundle.start();
199 delay();
200
201 // ensure no properties provided to bundle
Felix Meschberger8659e392009-08-19 05:46:49 +0000202 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
Felix Meschberger42764b82009-08-18 07:51:00 +0000203 TestCase.assertNotNull( "Activator not started !!", tester );
204 TestCase.assertNull( "Expect no properties after Service Registration", tester.props );
Felix Meschberger8659e392009-08-19 05:46:49 +0000205 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
Felix Meschberger42764b82009-08-18 07:51:00 +0000206
207 // assert configuration is still unset but bound
208 TestCase.assertNull( newConfig.getProperties() );
209 TestCase.assertEquals( bundle.getLocation(), newConfig.getBundleLocation() );
210
211 // uninstall bundle, should unbind configuration
212 bundle.uninstall();
213 bundle = null;
214
215 delay();
216
217 // assert configuration is still unset and unbound
218 TestCase.assertNull( newConfig.getProperties() );
219 TestCase.assertNull( newConfig.getBundleLocation() );
220
221 // remove the configuration for good
222 deleteConfig( pid );
223 }
224
225
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000226 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +0000227 public void test_create_with_location_unbind_before_service_supply() throws BundleException, IOException
228 {
229
Felix Meschberger42764b82009-08-18 07:51:00 +0000230 final String pid = "test_create_with_location_unbind_before_service_supply";
231 final String dummyLocation = "http://some/dummy/location";
232
233 // 1. create and statically bind the configuration
Felix Meschberger8659e392009-08-19 05:46:49 +0000234 final Configuration config = configure( pid, dummyLocation, false );
Felix Meschberger42764b82009-08-18 07:51:00 +0000235 TestCase.assertEquals( pid, config.getPid() );
236 TestCase.assertEquals( dummyLocation, config.getBundleLocation() );
237
238 // 2. update configuration
239 Hashtable<String, String> props = new Hashtable<String, String>();
240 props.put( PROP_NAME, PROP_NAME );
241 config.update( props );
242 TestCase.assertEquals( PROP_NAME, config.getProperties().get( PROP_NAME ) );
243 TestCase.assertEquals( pid, config.getPid() );
244 TestCase.assertEquals( dummyLocation, config.getBundleLocation() );
245
246 // 3. (statically) set location to null
247 config.setBundleLocation( null );
248 TestCase.assertNull( config.getBundleLocation() );
249
250 // 4. install bundle with service
251 bundle = installBundle( pid );
252 bundle.start();
253 delay();
254
Felix Meschberger8659e392009-08-19 05:46:49 +0000255 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
Felix Meschberger42764b82009-08-18 07:51:00 +0000256 TestCase.assertNotNull( "Activator not started !!", tester );
257
258 // assert activater has configuration (two calls, one per pid)
259 TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props );
Felix Meschberger8659e392009-08-19 05:46:49 +0000260 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
Felix Meschberger42764b82009-08-18 07:51:00 +0000261
262 TestCase.assertEquals( bundle.getLocation(), config.getBundleLocation() );
263
264 bundle.uninstall();
265 bundle = null;
266
267 delay();
268
269 // statically bound configurations must remain bound after bundle
270 // uninstall
271 TestCase.assertNull( config.getBundleLocation() );
272
273 // remove the configuration for good
274 deleteConfig( pid );
275 }
276
277
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000278 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +0000279 public void test_statically_bound() throws BundleException
280 {
281 final String pid = "test_statically_bound";
282
283 // install the bundle (we need the location)
284 bundle = installBundle( pid );
285 final String location = bundle.getLocation();
286
287 // create and statically bind the configuration
288 configure( pid );
289 final Configuration config = getConfiguration( pid );
290 TestCase.assertEquals( pid, config.getPid() );
291 TestCase.assertNull( config.getBundleLocation() );
292 config.setBundleLocation( location );
293 TestCase.assertEquals( location, config.getBundleLocation() );
294
295 bundle.start();
296
297 // give cm time for distribution
298 delay();
299
Felix Meschberger8659e392009-08-19 05:46:49 +0000300 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
Felix Meschberger42764b82009-08-18 07:51:00 +0000301 TestCase.assertNotNull( "Activator not started !!", tester );
302
303 // assert activater has configuration (two calls, one per pid)
304 TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props );
Felix Meschberger8659e392009-08-19 05:46:49 +0000305 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
Felix Meschberger42764b82009-08-18 07:51:00 +0000306
307 TestCase.assertEquals( location, config.getBundleLocation() );
308
309 bundle.uninstall();
310 bundle = null;
311
312 delay();
313
314 // statically bound configurations must remain bound after bundle
315 // uninstall
316 TestCase.assertEquals( location, config.getBundleLocation() );
317
318 // remove the configuration for good
319 deleteConfig( pid );
320 }
321
322
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000323 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +0000324 public void test_static_binding_and_unbinding() throws BundleException
325 {
326 final String pid = "test_static_binding_and_unbinding";
327 final String location = bundleContext.getBundle().getLocation();
328
329 // create and statically bind the configuration
330 configure( pid );
331 final Configuration config = getConfiguration( pid );
332 TestCase.assertEquals( pid, config.getPid() );
333 TestCase.assertNull( config.getBundleLocation() );
334
335 // bind the configuration
336 config.setBundleLocation( location );
337 TestCase.assertEquals( location, config.getBundleLocation() );
338
339 // restart CM bundle
340 final Bundle cmBundle = getCmBundle();
341 cmBundle.stop();
342 delay();
343 cmBundle.start();
344
345 // assert configuration still bound
346 final Configuration configAfterRestart = getConfiguration( pid );
347 TestCase.assertEquals( pid, configAfterRestart.getPid() );
348 TestCase.assertEquals( location, configAfterRestart.getBundleLocation() );
349
350 // unbind the configuration
351 configAfterRestart.setBundleLocation( null );
352 TestCase.assertNull( configAfterRestart.getBundleLocation() );
353
354 // restart CM bundle
355 cmBundle.stop();
356 delay();
357 cmBundle.start();
358
359 // assert configuration unbound
360 final Configuration configUnboundAfterRestart = getConfiguration( pid );
361 TestCase.assertEquals( pid, configUnboundAfterRestart.getPid() );
362 TestCase.assertNull( configUnboundAfterRestart.getBundleLocation() );
363 }
364
365
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000366 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +0000367 public void test_dynamic_binding_and_unbinding() throws BundleException
368 {
369 final String pid = "test_dynamic_binding_and_unbinding";
370
371 // create and statically bind the configuration
372 configure( pid );
373 final Configuration config = getConfiguration( pid );
374 TestCase.assertEquals( pid, config.getPid() );
375 TestCase.assertNull( config.getBundleLocation() );
376
377 // dynamically bind the configuration
378 bundle = installBundle( pid );
379 final String location = bundle.getLocation();
380 bundle.start();
381 delay();
382 TestCase.assertEquals( location, config.getBundleLocation() );
383
384 // restart CM bundle
385 final Bundle cmBundle = getCmBundle();
386 cmBundle.stop();
387 delay();
388 cmBundle.start();
389
390 // assert configuration still bound
391 final Configuration configAfterRestart = getConfiguration( pid );
392 TestCase.assertEquals( pid, configAfterRestart.getPid() );
393 TestCase.assertEquals( location, configAfterRestart.getBundleLocation() );
394
395 // stop bundle (configuration remains bound !!)
396 bundle.stop();
397 delay();
398 TestCase.assertEquals( location, configAfterRestart.getBundleLocation() );
399
400 // restart CM bundle
401 cmBundle.stop();
402 delay();
403 cmBundle.start();
404
405 // assert configuration still bound
406 final Configuration configBoundAfterRestart = getConfiguration( pid );
407 TestCase.assertEquals( pid, configBoundAfterRestart.getPid() );
408 TestCase.assertEquals( location, configBoundAfterRestart.getBundleLocation() );
409 }
410
Felix Meschberger8659e392009-08-19 05:46:49 +0000411
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000412 @Test
Felix Meschberger8659e392009-08-19 05:46:49 +0000413 public void test_static_binding() throws BundleException
414 {
415 final String pid = "test_static_binding";
416
417 // install a bundle to get a location for binding
418 bundle = installBundle( pid );
419 final String location = bundle.getLocation();
420
421 // create and statically bind the configuration
422 configure( pid );
423 final Configuration config = getConfiguration( pid );
424 TestCase.assertEquals( pid, config.getPid() );
425 TestCase.assertNull( config.getBundleLocation() );
426 config.setBundleLocation( location );
427 TestCase.assertEquals( location, config.getBundleLocation() );
428
429 // start the bundle
430 bundle.start();
431 delay();
432 TestCase.assertEquals( location, config.getBundleLocation() );
433
434 // assert the configuration is supplied
435 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
436 TestCase.assertNotNull( "Activator not started !!", tester );
437 TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props );
438 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
439
440 // remove the static binding and assert still bound
441 config.setBundleLocation( null );
442 TestCase.assertEquals( location, config.getBundleLocation() );
443
444 // uninstall bundle and assert configuration unbound
445 bundle.uninstall();
446 bundle = null;
447 delay();
448 TestCase.assertNull( config.getBundleLocation() );
449 }
450
451
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000452 @Test
Felix Meschberger8659e392009-08-19 05:46:49 +0000453 public void test_two_bundles_one_pid() throws BundleException, IOException
454 {
455 // 1. Bundle registers service with pid1
456 final String pid = "test_two_bundles_one_pid";
457 final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class );
458 final String locationA = bundleA.getLocation();
459 bundleA.start();
460 delay();
461
462 // call back with null
463 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
464 TestCase.assertNull( tester.props );
465 TestCase.assertEquals( 1, tester.numManagedServiceUpdatedCalls );
466
467 // 2. create new Conf with pid1 and locationA.
468 final Configuration config = configure( pid, locationA, false );
469 delay();
470
471 // ==> No call back.
472 TestCase.assertNull( tester.props );
473 TestCase.assertEquals( 1, tester.numManagedServiceUpdatedCalls );
474
475 // 3. Configuration#update(prop) is called.
476 config.update( theConfig );
477 delay();
478
479 // ==> call back with the prop.
480 TestCase.assertNotNull( tester.props );
481 TestCase.assertEquals( 2, tester.numManagedServiceUpdatedCalls );
482
483 // 4. Stop BundleA
484 bundleA.stop();
485 delay();
486
487 // 5. Start BundleA
488 bundleA.start();
489 delay();
490
491 // ==> call back with the prop.
492 final ManagedServiceTestActivator tester2 = ManagedServiceTestActivator.INSTANCE;
493 TestCase.assertNotNull( tester2.props );
494 TestCase.assertEquals( 1, tester2.numManagedServiceUpdatedCalls );
495
496 // 6. Configuration#deleted() is called.
497 config.delete();
498 delay();
499
500 // ==> call back with null.
501 TestCase.assertNull( tester2.props );
502 TestCase.assertEquals( 2, tester2.numManagedServiceUpdatedCalls );
503
504 // 7. uninstall Bundle A for cleanup.
505 bundleA.uninstall();
506 delay();
507
508 // Test 2
509
510 // 8. BundleA registers ManagedService with pid1.
511 final Bundle bundleA2 = installBundle( pid, ManagedServiceTestActivator.class );
512 final String locationA2 = bundleA.getLocation();
513 bundleA2.start();
514 delay();
515
516 // call back with null
517 final ManagedServiceTestActivator tester21 = ManagedServiceTestActivator.INSTANCE;
518 TestCase.assertNull( tester21.props );
519 TestCase.assertEquals( 1, tester21.numManagedServiceUpdatedCalls );
520
521 // 9. create new Conf with pid1 and locationB.
522 final String locationB = "test:locationB/" + pid;
523 final Configuration configB = configure( pid, locationB, false );
524 delay();
525
526 // ==> No call back.
527 TestCase.assertNull( tester21.props );
528 TestCase.assertEquals( 1, tester21.numManagedServiceUpdatedCalls );
529
530 // 10. Configuration#update(prop) is called.
531 configB.update( theConfig );
532 delay();
533
534 // ==> No call back because the Conf is not bound to locationA.
535 TestCase.assertNull( tester21.props );
536 TestCase.assertEquals( 1, tester21.numManagedServiceUpdatedCalls );
537 }
538
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000539
540 @Test
541 public void test_switch_static_binding() throws BundleException
542 {
543 // 1. create config with pid and locationA
544 // 2. update config with properties
545 final String pid = "test_switch_static_binding";
546 final String locationA = "test:location/A/" + pid;
547 final Configuration config = configure( pid, locationA, true );
548
549 // 3. register ManagedService ms1 with pid from said locationA
550 final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class, locationA );
551 bundleA.start();
552 delay();
553
554 // ==> configuration supplied to the service ms1
555 final ManagedServiceTestActivator testerA1 = ManagedServiceTestActivator.INSTANCE;
556 TestCase.assertNotNull( testerA1.props );
557 TestCase.assertEquals( 1, testerA1.numManagedServiceUpdatedCalls );
558
559 // 4. register ManagedService ms2 with pid from locationB
560 final String locationB = "test:location/B/" + pid;
561 final Bundle bundleB = installBundle( pid, ManagedServiceTestActivator2.class, locationB );
562 bundleB.start();
563 delay();
564
565 // ==> configuration not supplied to service ms2
566 final ManagedServiceTestActivator2 testerB1 = ManagedServiceTestActivator2.INSTANCE;
567 TestCase.assertNull( testerB1.props );
568 TestCase.assertEquals( 0, testerB1.numManagedServiceUpdatedCalls );
569
570 // 5. Call Configuration.setBundleLocation( "locationB" )
571 config.setBundleLocation( locationB );
572 delay();
573
574 // ==> configuration is bound to locationB
575 TestCase.assertEquals( locationB, config.getBundleLocation() );
576
Felix Meschberger85bb47c2009-08-20 07:50:38 +0000577 /*
578 * According to BJ Hargrave configuration is not re-dispatched
579 * due to setting the bundle location.
580 * <p>
581 * Therefore, we have two sets one with re-dispatch expectation and
582 * one without re-dispatch expectation.
583 */
584 if ( REDISPATCH_CONFIGURATION_ON_SET_BUNDLE_LOCATION )
585 {
586 // ==> configuration removed from service ms1
587 TestCase.assertNull( testerA1.props );
588 TestCase.assertEquals( 2, testerA1.numManagedServiceUpdatedCalls );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000589
Felix Meschberger85bb47c2009-08-20 07:50:38 +0000590 // ==> configuration supplied to the service ms2
591 TestCase.assertNotNull( testerB1.props );
592 TestCase.assertEquals( 1, testerB1.numManagedServiceUpdatedCalls );
593 }
594 else
595 {
596 // ==> configuration remains for service ms1
597 TestCase.assertNotNull( testerA1.props );
598 TestCase.assertEquals( 1, testerA1.numManagedServiceUpdatedCalls );
599
600 // ==> configuration not supplied to the service ms2
601 TestCase.assertNull( testerB1.props );
602 TestCase.assertEquals( 0, testerB1.numManagedServiceUpdatedCalls );
603 }
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000604 }
605
606
607 @Test
608 public void test_switch_dynamic_binding() throws BundleException
609 {
610 // 1. create config with pid with null location
611 // 2. update config with properties
612 final String pid = "test_switch_dynamic_binding";
613 final String locationA = "test:location/A/" + pid;
614 final Configuration config = configure( pid, null, true );
615
616 // 3. register ManagedService ms1 with pid from locationA
617 final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class, locationA );
618 bundleA.start();
619 delay();
620
621 // ==> configuration supplied to the service ms1
622 final ManagedServiceTestActivator testerA1 = ManagedServiceTestActivator.INSTANCE;
623 TestCase.assertNotNull( testerA1.props );
624 TestCase.assertEquals( 1, testerA1.numManagedServiceUpdatedCalls );
625
626 // ==> configuration is dynamically bound to locationA
627 TestCase.assertEquals( locationA, config.getBundleLocation() );
628
629 // 4. register ManagedService ms2 with pid from locationB
630 final String locationB = "test:location/B/" + pid;
631 final Bundle bundleB = installBundle( pid, ManagedServiceTestActivator2.class, locationB );
632 bundleB.start();
633 delay();
634
635 // ==> configuration not supplied to service ms2
636 final ManagedServiceTestActivator2 testerB1 = ManagedServiceTestActivator2.INSTANCE;
637 TestCase.assertNull( testerB1.props );
638 TestCase.assertEquals( 0, testerB1.numManagedServiceUpdatedCalls );
639
640 // 5. Call Configuration.setBundleLocation( "locationB" )
641 config.setBundleLocation( locationB );
642 delay();
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000643 delay();
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000644
645 // ==> configuration is bound to locationB
646 TestCase.assertEquals( locationB, config.getBundleLocation() );
647
Felix Meschberger85bb47c2009-08-20 07:50:38 +0000648 /*
649 * According to BJ Hargrave configuration is not re-dispatched
650 * due to setting the bundle location.
651 * <p>
652 * Therefore, we have two sets one with re-dispatch expectation and
653 * one without re-dispatch expectation.
654 */
655 if ( REDISPATCH_CONFIGURATION_ON_SET_BUNDLE_LOCATION )
656 {
657 // ==> configuration removed from service ms1
658 TestCase.assertNull( testerA1.props );
659 TestCase.assertEquals( 2, testerA1.numManagedServiceUpdatedCalls );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000660
Felix Meschberger85bb47c2009-08-20 07:50:38 +0000661 // ==> configuration supplied to the service ms2
662 TestCase.assertNotNull( testerB1.props );
663 TestCase.assertEquals( 1, testerB1.numManagedServiceUpdatedCalls );
664 }
665 else
666 {
667 // ==> configuration remains for service ms1
668 TestCase.assertNotNull( testerA1.props );
669 TestCase.assertEquals( 1, testerA1.numManagedServiceUpdatedCalls );
670
671 // ==> configuration not supplied to the service ms2
672 TestCase.assertNull( testerB1.props );
673 TestCase.assertEquals( 0, testerB1.numManagedServiceUpdatedCalls );
674 }
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000675 }
676
677
678 @Test
679 public void test_switch_static_binding_factory() throws BundleException
680 {
681 // 1. create config with pid and locationA
682 // 2. update config with properties
683 final String factoryPid = "test_switch_static_binding_factory";
684 final String locationA = "test:location/A/" + factoryPid;
685 final Configuration config = createFactoryConfiguration( factoryPid, locationA, true );
686 final String pid = config.getPid();
687
688 // 3. register ManagedService ms1 with pid from said locationA
689 final Bundle bundleA = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class, locationA );
690 bundleA.start();
691 delay();
692
693 // ==> configuration supplied to the service ms1
694 final ManagedServiceFactoryTestActivator testerA1 = ManagedServiceFactoryTestActivator.INSTANCE;
695 TestCase.assertNotNull( testerA1.configs.get( pid ) );
696 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
697
698 // 4. register ManagedService ms2 with pid from locationB
699 final String locationB = "test:location/B/" + factoryPid;
700 final Bundle bundleB = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class, locationB );
701 bundleB.start();
702 delay();
703
704 // ==> configuration not supplied to service ms2
705 final ManagedServiceFactoryTestActivator2 testerB1 = ManagedServiceFactoryTestActivator2.INSTANCE;
706 TestCase.assertNull( testerB1.configs.get( pid ));
707 TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls );
708
709 // 5. Call Configuration.setBundleLocation( "locationB" )
710 config.setBundleLocation( locationB );
711 delay();
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000712 delay();
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000713
714 // ==> configuration is bound to locationB
715 TestCase.assertEquals( locationB, config.getBundleLocation() );
716
Felix Meschberger85bb47c2009-08-20 07:50:38 +0000717 /*
718 * According to BJ Hargrave configuration is not re-dispatched
719 * due to setting the bundle location.
720 * <p>
721 * Therefore, we have two sets one with re-dispatch expectation and
722 * one without re-dispatch expectation.
723 */
724 if ( REDISPATCH_CONFIGURATION_ON_SET_BUNDLE_LOCATION )
725 {
726 // ==> configuration removed from service ms1
727 TestCase.assertNull( testerA1.configs.get( pid ));
728 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
729 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryDeleteCalls );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000730
Felix Meschberger85bb47c2009-08-20 07:50:38 +0000731 // ==> configuration supplied to the service ms2
732 TestCase.assertNotNull( testerB1.configs.get( pid ) );
733 TestCase.assertEquals( 1, testerB1.numManagedServiceFactoryUpdatedCalls );
734 }
735 else
736 {
737 // ==> configuration not removed from service ms1
738 TestCase.assertNotNull( testerA1.configs.get( pid ));
739 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
740 TestCase.assertEquals( 0, testerA1.numManagedServiceFactoryDeleteCalls );
741
742 // ==> configuration not supplied to the service ms2
743 TestCase.assertNull( testerB1.configs.get( pid ) );
744 TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls );
745 }
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000746 }
747
748
749 @Test
750 public void test_switch_dynamic_binding_factory() throws BundleException
751 {
752 // 1. create config with pid and locationA
753 // 2. update config with properties
754 final String factoryPid = "test_switch_static_binding_factory";
755 final String locationA = "test:location/A/" + factoryPid;
756 final Configuration config = createFactoryConfiguration( factoryPid, null, true );
757 final String pid = config.getPid();
758
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000759 TestCase.assertNull( config.getBundleLocation() );
760
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000761 // 3. register ManagedService ms1 with pid from said locationA
762 final Bundle bundleA = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class, locationA );
763 bundleA.start();
764 delay();
765
766 // ==> configuration supplied to the service ms1
767 final ManagedServiceFactoryTestActivator testerA1 = ManagedServiceFactoryTestActivator.INSTANCE;
768 TestCase.assertNotNull( testerA1.configs.get( pid ) );
769 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000770 TestCase.assertEquals( locationA, config.getBundleLocation() );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000771
772 // 4. register ManagedService ms2 with pid from locationB
773 final String locationB = "test:location/B/" + factoryPid;
774 final Bundle bundleB = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class, locationB );
775 bundleB.start();
776 delay();
777
778 // ==> configuration not supplied to service ms2
779 final ManagedServiceFactoryTestActivator2 testerB1 = ManagedServiceFactoryTestActivator2.INSTANCE;
780 TestCase.assertNull( testerB1.configs.get( pid ));
781 TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls );
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000782 TestCase.assertEquals( locationA, config.getBundleLocation() );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000783
784 // 5. Call Configuration.setBundleLocation( "locationB" )
785 config.setBundleLocation( locationB );
786 delay();
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000787 delay();
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000788
789 // ==> configuration is bound to locationB
790 TestCase.assertEquals( locationB, config.getBundleLocation() );
791
Felix Meschberger85bb47c2009-08-20 07:50:38 +0000792 /*
793 * According to BJ Hargrave configuration is not re-dispatched
794 * due to setting the bundle location.
795 * <p>
796 * Therefore, we have two sets one with re-dispatch expectation and
797 * one without re-dispatch expectation.
798 */
799 if ( REDISPATCH_CONFIGURATION_ON_SET_BUNDLE_LOCATION )
800 {
801 // ==> configuration removed from service ms1
802 TestCase.assertNull( testerA1.configs.get( pid ));
803 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
804 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryDeleteCalls );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000805
Felix Meschberger85bb47c2009-08-20 07:50:38 +0000806 // ==> configuration supplied to the service ms2
807 TestCase.assertNotNull( testerB1.configs.get( pid ) );
808 TestCase.assertEquals( 1, testerB1.numManagedServiceFactoryUpdatedCalls );
809 }
810 else
811 {
812 // ==> configuration not removed from service ms1
813 TestCase.assertNotNull( testerA1.configs.get( pid ));
814 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
815 TestCase.assertEquals( 0, testerA1.numManagedServiceFactoryDeleteCalls );
816
817 // ==> configuration not supplied to the service ms2
818 TestCase.assertNull( testerB1.configs.get( pid ) );
819 TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls );
820 }
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000821 }
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000822
823
824 @Test
825 public void test_switch_dynamic_binding_after_uninstall() throws BundleException
826 {
827 // 1. create config with pid with null location
828 // 2. update config with properties
829 final String pid = "test_switch_dynamic_binding";
830 final String locationA = "test:location/A/" + pid;
831 final Configuration config = configure( pid, null, true );
832
833 TestCase.assertNull( config.getBundleLocation() );
834
835 // 3. register ManagedService ms1 with pid from locationA
836 final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class, locationA );
837 bundleA.start();
838 delay();
839
840 // ==> configuration supplied to the service ms1
841 final ManagedServiceTestActivator testerA1 = ManagedServiceTestActivator.INSTANCE;
842 TestCase.assertNotNull( testerA1.props );
843 TestCase.assertEquals( 1, testerA1.numManagedServiceUpdatedCalls );
844
845 // ==> configuration is dynamically bound to locationA
846 TestCase.assertEquals( locationA, config.getBundleLocation() );
847
848 // 4. register ManagedService ms2 with pid from locationB
849 final String locationB = "test:location/B/" + pid;
850 final Bundle bundleB = installBundle( pid, ManagedServiceTestActivator2.class, locationB );
851 bundleB.start();
852 delay();
853
854 // ==> configuration not supplied to service ms2
855 final ManagedServiceTestActivator2 testerB1 = ManagedServiceTestActivator2.INSTANCE;
856 TestCase.assertNull( testerB1.props );
857 TestCase.assertEquals( 0, testerB1.numManagedServiceUpdatedCalls );
858
859 // 5. Uninstall bundle A
860 bundleA.uninstall();
861 delay();
862 delay();
863
Felix Meschberger85bb47c2009-08-20 07:50:38 +0000864 /*
865 * According to BJ Hargrave configuration is not re-dispatched
866 * due to setting the bundle location.
867 * <p>
868 * Therefore, we have two sets one with re-dispatch expectation and
869 * one without re-dispatch expectation.
870 */
871 if ( REDISPATCH_CONFIGURATION_ON_SET_BUNDLE_LOCATION )
872 {
873 // ==> configuration is bound to locationB
874 TestCase.assertEquals( locationB, config.getBundleLocation() );
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000875
Felix Meschberger85bb47c2009-08-20 07:50:38 +0000876 // ==> configuration supplied to the service ms2
877 TestCase.assertNotNull( testerB1.props );
878 TestCase.assertEquals( 1, testerB1.numManagedServiceUpdatedCalls );
879 }
880 else
881 {
882 // ==> configuration is unbound
883 TestCase.assertNull( config.getBundleLocation() );
884
885 // ==> configuration not supplied to the service ms2
886 TestCase.assertNull( testerB1.props );
887 TestCase.assertEquals( 0, testerB1.numManagedServiceUpdatedCalls );
888 }
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000889 }
890
891
892 @Test
893 public void test_switch_dynamic_binding_factory_after_uninstall() throws BundleException
894 {
895 // 1. create config with pid and locationA
896 // 2. update config with properties
897 final String factoryPid = "test_switch_static_binding_factory";
898 final String locationA = "test:location/A/" + factoryPid;
899 final Configuration config = createFactoryConfiguration( factoryPid, null, true );
900 final String pid = config.getPid();
901
902 TestCase.assertNull( config.getBundleLocation() );
903
904 // 3. register ManagedService ms1 with pid from said locationA
905 final Bundle bundleA = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class, locationA );
906 bundleA.start();
907 delay();
908
909 // ==> configuration supplied to the service ms1
910 final ManagedServiceFactoryTestActivator testerA1 = ManagedServiceFactoryTestActivator.INSTANCE;
911 TestCase.assertNotNull( testerA1.configs.get( pid ) );
912 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
913 TestCase.assertEquals( locationA, config.getBundleLocation() );
914
915 // 4. register ManagedService ms2 with pid from locationB
916 final String locationB = "test:location/B/" + factoryPid;
917 final Bundle bundleB = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class, locationB );
918 bundleB.start();
919 delay();
920
921 // ==> configuration not supplied to service ms2
922 final ManagedServiceFactoryTestActivator2 testerB1 = ManagedServiceFactoryTestActivator2.INSTANCE;
923 TestCase.assertNull( testerB1.configs.get( pid ));
924 TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls );
925 TestCase.assertEquals( locationA, config.getBundleLocation() );
926
927 // 5. Uninstall bundle A
928 bundleA.uninstall();
929 delay();
930 delay();
931
Felix Meschberger85bb47c2009-08-20 07:50:38 +0000932 /*
933 * According to BJ Hargrave configuration is not re-dispatched
934 * due to setting the bundle location.
935 * <p>
936 * Therefore, we have two sets one with re-dispatch expectation and
937 * one without re-dispatch expectation.
938 */
939 if ( REDISPATCH_CONFIGURATION_ON_SET_BUNDLE_LOCATION )
940 {
941 // ==> configuration is bound to locationB
942 TestCase.assertEquals( locationB, config.getBundleLocation() );
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000943
Felix Meschberger85bb47c2009-08-20 07:50:38 +0000944 // ==> configuration supplied to the service ms2
945 TestCase.assertNotNull( testerB1.configs.get( pid ) );
946 TestCase.assertEquals( 1, testerB1.numManagedServiceFactoryUpdatedCalls );
947 }
948 else
949 {
950 // ==> configuration is unbound
951 TestCase.assertNull( config.getBundleLocation() );
952
953 // ==> configuration not supplied to the service ms2
954 TestCase.assertNull( testerB1.configs.get( pid ) );
955 TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls );
956 }
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000957 }
Felix Meschberger42764b82009-08-18 07:51:00 +0000958}