blob: 63f0c7070c50c72d7a075e370a7c650164a6d45d [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 Meschberger007c50e2011-10-20 12:39:38 +000042 static
43 {
44 // uncomment to enable debugging of this test class
45 // paxRunnerVmOption = DEBUG_VM_OPTION;
46 }
47
48
Felix Meschberger6f5b69e2009-08-19 09:39:21 +000049 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +000050 public void test_configuration_unbound_on_uninstall() throws BundleException
51 {
Felix Meschbergercefe5eb2009-08-19 12:37:32 +000052 String pid = "test_configuration_unbound_on_uninstall";
Felix Meschberger42764b82009-08-18 07:51:00 +000053 configure( pid );
54
55 // ensure configuration is unbound
56 final Configuration beforeInstall = getConfiguration( pid );
57 TestCase.assertNull( beforeInstall.getBundleLocation() );
58
59 bundle = installBundle( pid );
60
61 // ensure no configuration bound before start
62 final Configuration beforeStart = getConfiguration( pid );
63 TestCase.assertNull( beforeInstall.getBundleLocation() );
64 TestCase.assertNull( beforeStart.getBundleLocation() );
65
66 bundle.start();
Felix Meschberger8659e392009-08-19 05:46:49 +000067 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
Felix Meschberger42764b82009-08-18 07:51:00 +000068 TestCase.assertNotNull( "Activator not started !!", tester );
69
70 // give cm time for distribution
71 delay();
72
73 // assert activater has configuration
74 TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props );
Felix Meschberger8659e392009-08-19 05:46:49 +000075 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
Felix Meschberger42764b82009-08-18 07:51:00 +000076
77 // ensure a freshly retrieved object also has the location
78 final Configuration beforeStop = getConfiguration( pid );
79 TestCase.assertEquals( beforeStop.getBundleLocation(), bundle.getLocation() );
80
81 // check whether bundle context is set on first configuration
82 TestCase.assertEquals( beforeInstall.getBundleLocation(), bundle.getLocation() );
83 TestCase.assertEquals( beforeStart.getBundleLocation(), bundle.getLocation() );
84
85 bundle.stop();
86
87 delay();
88
89 // ensure configuration still bound
90 TestCase.assertEquals( beforeInstall.getBundleLocation(), bundle.getLocation() );
91 TestCase.assertEquals( beforeStart.getBundleLocation(), bundle.getLocation() );
92 TestCase.assertEquals( beforeStop.getBundleLocation(), bundle.getLocation() );
93
94 // ensure a freshly retrieved object also has the location
95 final Configuration beforeUninstall = getConfiguration( pid );
96 TestCase.assertEquals( beforeUninstall.getBundleLocation(), bundle.getLocation() );
97
98 bundle.uninstall();
99 bundle = null;
100
101 delay();
102
103 // ensure configuration is not bound any more
104 TestCase.assertNull( beforeInstall.getBundleLocation() );
105 TestCase.assertNull( beforeStart.getBundleLocation() );
106 TestCase.assertNull( beforeStop.getBundleLocation() );
107 TestCase.assertNull( beforeUninstall.getBundleLocation() );
108
109 // ensure a freshly retrieved object also does not have the location
110 final Configuration atEnd = getConfiguration( pid );
111 TestCase.assertNull( atEnd.getBundleLocation() );
112
113 // remove the configuration for good
114 deleteConfig( pid );
115 }
116
117
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000118 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +0000119 public void test_configuration_unbound_on_uninstall_with_cm_restart() throws BundleException
120 {
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000121 final String pid = "test_configuration_unbound_on_uninstall_with_cm_restart";
Felix Meschberger42764b82009-08-18 07:51:00 +0000122 configure( pid );
123 final Bundle cmBundle = getCmBundle();
124
125 // ensure configuration is unbound
126 final Configuration beforeInstall = getConfiguration( pid );
127 TestCase.assertNull( beforeInstall.getBundleLocation() );
128
129 bundle = installBundle( pid );
130
131 // ensure no configuration bound before start
132 final Configuration beforeStart = getConfiguration( pid );
133 TestCase.assertNull( beforeInstall.getBundleLocation() );
134 TestCase.assertNull( beforeStart.getBundleLocation() );
135
136 bundle.start();
Felix Meschberger8659e392009-08-19 05:46:49 +0000137 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
Felix Meschberger42764b82009-08-18 07:51:00 +0000138 TestCase.assertNotNull( "IOActivator not started !!", tester );
139
140 // give cm time for distribution
141 delay();
142
143 // assert activater has configuration
144 TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props );
Felix Meschberger8659e392009-08-19 05:46:49 +0000145 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
Felix Meschberger42764b82009-08-18 07:51:00 +0000146
147 // ensure a freshly retrieved object also has the location
148 final Configuration beforeStop = getConfiguration( pid );
149 TestCase.assertEquals( beforeStop.getBundleLocation(), bundle.getLocation() );
150
151 // check whether bundle context is set on first configuration
152 TestCase.assertEquals( beforeInstall.getBundleLocation(), bundle.getLocation() );
153 TestCase.assertEquals( beforeStart.getBundleLocation(), bundle.getLocation() );
154
155 bundle.stop();
156
157 // ensure configuration still bound
158 TestCase.assertEquals( beforeInstall.getBundleLocation(), bundle.getLocation() );
159 TestCase.assertEquals( beforeStart.getBundleLocation(), bundle.getLocation() );
160 TestCase.assertEquals( beforeStop.getBundleLocation(), bundle.getLocation() );
161
162 // ensure a freshly retrieved object also has the location
163 final Configuration beforeUninstall = getConfiguration( pid );
164 TestCase.assertEquals( beforeUninstall.getBundleLocation(), bundle.getLocation() );
165
166 // stop cm bundle now before uninstalling configured bundle
167 cmBundle.stop();
168 delay();
169
170 // assert configuration admin service is gone
171 TestCase.assertNull( configAdminTracker.getService() );
172
173 // uninstall bundle while configuration admin is stopped
174 bundle.uninstall();
175 bundle = null;
176
177 // start cm bundle again after uninstallation
178 cmBundle.start();
179 delay();
180
181 // ensure a freshly retrieved object also does not have the location
182 // FELIX-1484: this test fails due to bundle location not verified
183 // at first configuration access
184 final Configuration atEnd = getConfiguration( pid );
185 TestCase.assertNull( atEnd.getBundleLocation() );
186
187 // remove the configuration for good
188 deleteConfig( pid );
189 }
190
191
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000192 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +0000193 public void test_not_updated_new_configuration_not_bound_after_bundle_uninstall() throws IOException,
194 BundleException
195 {
196 final String pid = "test_not_updated_new_configuration_not_bound_after_bundle_uninstall";
197
198 // create a configuration but do not update with properties
Felix Meschberger8659e392009-08-19 05:46:49 +0000199 final Configuration newConfig = configure( pid, null, false );
Felix Meschberger42764b82009-08-18 07:51:00 +0000200 TestCase.assertNull( newConfig.getProperties() );
201 TestCase.assertNull( newConfig.getBundleLocation() );
202
203 // start and settle bundle
204 bundle = installBundle( pid );
205 bundle.start();
206 delay();
207
208 // ensure no properties provided to bundle
Felix Meschberger8659e392009-08-19 05:46:49 +0000209 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
Felix Meschberger42764b82009-08-18 07:51:00 +0000210 TestCase.assertNotNull( "Activator not started !!", tester );
211 TestCase.assertNull( "Expect no properties after Service Registration", tester.props );
Felix Meschberger8659e392009-08-19 05:46:49 +0000212 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
Felix Meschberger42764b82009-08-18 07:51:00 +0000213
214 // assert configuration is still unset but bound
215 TestCase.assertNull( newConfig.getProperties() );
216 TestCase.assertEquals( bundle.getLocation(), newConfig.getBundleLocation() );
217
218 // uninstall bundle, should unbind configuration
219 bundle.uninstall();
220 bundle = null;
221
222 delay();
223
224 // assert configuration is still unset and unbound
225 TestCase.assertNull( newConfig.getProperties() );
226 TestCase.assertNull( newConfig.getBundleLocation() );
227
228 // remove the configuration for good
229 deleteConfig( pid );
230 }
231
232
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000233 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +0000234 public void test_create_with_location_unbind_before_service_supply() throws BundleException, IOException
235 {
236
Felix Meschberger42764b82009-08-18 07:51:00 +0000237 final String pid = "test_create_with_location_unbind_before_service_supply";
238 final String dummyLocation = "http://some/dummy/location";
239
240 // 1. create and statically bind the configuration
Felix Meschberger8659e392009-08-19 05:46:49 +0000241 final Configuration config = configure( pid, dummyLocation, false );
Felix Meschberger42764b82009-08-18 07:51:00 +0000242 TestCase.assertEquals( pid, config.getPid() );
243 TestCase.assertEquals( dummyLocation, config.getBundleLocation() );
244
245 // 2. update configuration
246 Hashtable<String, String> props = new Hashtable<String, String>();
247 props.put( PROP_NAME, PROP_NAME );
248 config.update( props );
249 TestCase.assertEquals( PROP_NAME, config.getProperties().get( PROP_NAME ) );
250 TestCase.assertEquals( pid, config.getPid() );
251 TestCase.assertEquals( dummyLocation, config.getBundleLocation() );
252
253 // 3. (statically) set location to null
254 config.setBundleLocation( null );
255 TestCase.assertNull( config.getBundleLocation() );
256
257 // 4. install bundle with service
258 bundle = installBundle( pid );
259 bundle.start();
260 delay();
261
Felix Meschberger8659e392009-08-19 05:46:49 +0000262 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
Felix Meschberger42764b82009-08-18 07:51:00 +0000263 TestCase.assertNotNull( "Activator not started !!", tester );
264
265 // assert activater has configuration (two calls, one per pid)
266 TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props );
Felix Meschberger8659e392009-08-19 05:46:49 +0000267 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
Felix Meschberger42764b82009-08-18 07:51:00 +0000268
269 TestCase.assertEquals( bundle.getLocation(), config.getBundleLocation() );
270
271 bundle.uninstall();
272 bundle = null;
273
274 delay();
275
276 // statically bound configurations must remain bound after bundle
277 // uninstall
278 TestCase.assertNull( config.getBundleLocation() );
279
280 // remove the configuration for good
281 deleteConfig( pid );
282 }
283
284
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000285 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +0000286 public void test_statically_bound() throws BundleException
287 {
288 final String pid = "test_statically_bound";
289
290 // install the bundle (we need the location)
291 bundle = installBundle( pid );
292 final String location = bundle.getLocation();
293
294 // create and statically bind the configuration
295 configure( pid );
296 final Configuration config = getConfiguration( pid );
297 TestCase.assertEquals( pid, config.getPid() );
298 TestCase.assertNull( config.getBundleLocation() );
299 config.setBundleLocation( location );
300 TestCase.assertEquals( location, config.getBundleLocation() );
301
Felix Meschberger3b2b4152010-08-26 07:53:47 +0000302 // ensure configuration is settled before starting the bundle
303 delay();
304
Felix Meschberger42764b82009-08-18 07:51:00 +0000305 bundle.start();
306
307 // give cm time for distribution
308 delay();
309
Felix Meschberger8659e392009-08-19 05:46:49 +0000310 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
Felix Meschberger42764b82009-08-18 07:51:00 +0000311 TestCase.assertNotNull( "Activator not started !!", tester );
312
313 // assert activater has configuration (two calls, one per pid)
314 TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props );
Felix Meschberger8659e392009-08-19 05:46:49 +0000315 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
Felix Meschberger42764b82009-08-18 07:51:00 +0000316
317 TestCase.assertEquals( location, config.getBundleLocation() );
318
319 bundle.uninstall();
320 bundle = null;
321
322 delay();
323
324 // statically bound configurations must remain bound after bundle
325 // uninstall
326 TestCase.assertEquals( location, config.getBundleLocation() );
327
328 // remove the configuration for good
329 deleteConfig( pid );
330 }
331
332
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000333 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +0000334 public void test_static_binding_and_unbinding() throws BundleException
335 {
336 final String pid = "test_static_binding_and_unbinding";
337 final String location = bundleContext.getBundle().getLocation();
338
339 // create and statically bind the configuration
340 configure( pid );
341 final Configuration config = getConfiguration( pid );
342 TestCase.assertEquals( pid, config.getPid() );
343 TestCase.assertNull( config.getBundleLocation() );
344
345 // bind the configuration
346 config.setBundleLocation( location );
347 TestCase.assertEquals( location, config.getBundleLocation() );
348
349 // restart CM bundle
350 final Bundle cmBundle = getCmBundle();
351 cmBundle.stop();
352 delay();
353 cmBundle.start();
354
355 // assert configuration still bound
356 final Configuration configAfterRestart = getConfiguration( pid );
357 TestCase.assertEquals( pid, configAfterRestart.getPid() );
358 TestCase.assertEquals( location, configAfterRestart.getBundleLocation() );
359
360 // unbind the configuration
361 configAfterRestart.setBundleLocation( null );
362 TestCase.assertNull( configAfterRestart.getBundleLocation() );
363
364 // restart CM bundle
365 cmBundle.stop();
366 delay();
367 cmBundle.start();
368
369 // assert configuration unbound
370 final Configuration configUnboundAfterRestart = getConfiguration( pid );
371 TestCase.assertEquals( pid, configUnboundAfterRestart.getPid() );
372 TestCase.assertNull( configUnboundAfterRestart.getBundleLocation() );
373 }
374
375
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000376 @Test
Felix Meschberger42764b82009-08-18 07:51:00 +0000377 public void test_dynamic_binding_and_unbinding() throws BundleException
378 {
379 final String pid = "test_dynamic_binding_and_unbinding";
380
381 // create and statically bind the configuration
382 configure( pid );
383 final Configuration config = getConfiguration( pid );
384 TestCase.assertEquals( pid, config.getPid() );
385 TestCase.assertNull( config.getBundleLocation() );
386
387 // dynamically bind the configuration
388 bundle = installBundle( pid );
389 final String location = bundle.getLocation();
390 bundle.start();
391 delay();
392 TestCase.assertEquals( location, config.getBundleLocation() );
393
394 // restart CM bundle
395 final Bundle cmBundle = getCmBundle();
396 cmBundle.stop();
397 delay();
398 cmBundle.start();
399
400 // assert configuration still bound
401 final Configuration configAfterRestart = getConfiguration( pid );
402 TestCase.assertEquals( pid, configAfterRestart.getPid() );
403 TestCase.assertEquals( location, configAfterRestart.getBundleLocation() );
404
405 // stop bundle (configuration remains bound !!)
406 bundle.stop();
407 delay();
408 TestCase.assertEquals( location, configAfterRestart.getBundleLocation() );
409
410 // restart CM bundle
411 cmBundle.stop();
412 delay();
413 cmBundle.start();
414
415 // assert configuration still bound
416 final Configuration configBoundAfterRestart = getConfiguration( pid );
417 TestCase.assertEquals( pid, configBoundAfterRestart.getPid() );
418 TestCase.assertEquals( location, configBoundAfterRestart.getBundleLocation() );
419 }
420
Felix Meschberger8659e392009-08-19 05:46:49 +0000421
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000422 @Test
Felix Meschberger8659e392009-08-19 05:46:49 +0000423 public void test_static_binding() throws BundleException
424 {
425 final String pid = "test_static_binding";
426
427 // install a bundle to get a location for binding
428 bundle = installBundle( pid );
429 final String location = bundle.getLocation();
430
431 // create and statically bind the configuration
432 configure( pid );
433 final Configuration config = getConfiguration( pid );
434 TestCase.assertEquals( pid, config.getPid() );
435 TestCase.assertNull( config.getBundleLocation() );
436 config.setBundleLocation( location );
437 TestCase.assertEquals( location, config.getBundleLocation() );
438
Felix Meschberger3b2b4152010-08-26 07:53:47 +0000439 // ensure configuration is settled before starting the bundle
440 delay();
441
Felix Meschberger8659e392009-08-19 05:46:49 +0000442 // start the bundle
443 bundle.start();
444 delay();
445 TestCase.assertEquals( location, config.getBundleLocation() );
446
447 // assert the configuration is supplied
448 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
449 TestCase.assertNotNull( "Activator not started !!", tester );
450 TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props );
451 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
452
453 // remove the static binding and assert still bound
454 config.setBundleLocation( null );
Felix Meschberger007c50e2011-10-20 12:39:38 +0000455 delay();
Felix Meschberger8659e392009-08-19 05:46:49 +0000456 TestCase.assertEquals( location, config.getBundleLocation() );
457
458 // uninstall bundle and assert configuration unbound
459 bundle.uninstall();
460 bundle = null;
461 delay();
462 TestCase.assertNull( config.getBundleLocation() );
463 }
464
465
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000466 @Test
Felix Meschberger8659e392009-08-19 05:46:49 +0000467 public void test_two_bundles_one_pid() throws BundleException, IOException
468 {
469 // 1. Bundle registers service with pid1
470 final String pid = "test_two_bundles_one_pid";
471 final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class );
472 final String locationA = bundleA.getLocation();
473 bundleA.start();
474 delay();
475
476 // call back with null
477 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
478 TestCase.assertNull( tester.props );
479 TestCase.assertEquals( 1, tester.numManagedServiceUpdatedCalls );
480
481 // 2. create new Conf with pid1 and locationA.
482 final Configuration config = configure( pid, locationA, false );
483 delay();
484
485 // ==> No call back.
486 TestCase.assertNull( tester.props );
487 TestCase.assertEquals( 1, tester.numManagedServiceUpdatedCalls );
488
489 // 3. Configuration#update(prop) is called.
490 config.update( theConfig );
491 delay();
492
493 // ==> call back with the prop.
494 TestCase.assertNotNull( tester.props );
495 TestCase.assertEquals( 2, tester.numManagedServiceUpdatedCalls );
496
497 // 4. Stop BundleA
498 bundleA.stop();
499 delay();
500
501 // 5. Start BundleA
502 bundleA.start();
503 delay();
504
505 // ==> call back with the prop.
506 final ManagedServiceTestActivator tester2 = ManagedServiceTestActivator.INSTANCE;
507 TestCase.assertNotNull( tester2.props );
508 TestCase.assertEquals( 1, tester2.numManagedServiceUpdatedCalls );
509
510 // 6. Configuration#deleted() is called.
511 config.delete();
512 delay();
513
514 // ==> call back with null.
515 TestCase.assertNull( tester2.props );
516 TestCase.assertEquals( 2, tester2.numManagedServiceUpdatedCalls );
517
518 // 7. uninstall Bundle A for cleanup.
519 bundleA.uninstall();
520 delay();
521
522 // Test 2
523
524 // 8. BundleA registers ManagedService with pid1.
525 final Bundle bundleA2 = installBundle( pid, ManagedServiceTestActivator.class );
526 final String locationA2 = bundleA.getLocation();
527 bundleA2.start();
528 delay();
529
530 // call back with null
531 final ManagedServiceTestActivator tester21 = ManagedServiceTestActivator.INSTANCE;
532 TestCase.assertNull( tester21.props );
533 TestCase.assertEquals( 1, tester21.numManagedServiceUpdatedCalls );
534
535 // 9. create new Conf with pid1 and locationB.
536 final String locationB = "test:locationB/" + pid;
537 final Configuration configB = configure( pid, locationB, false );
538 delay();
539
540 // ==> No call back.
541 TestCase.assertNull( tester21.props );
542 TestCase.assertEquals( 1, tester21.numManagedServiceUpdatedCalls );
543
544 // 10. Configuration#update(prop) is called.
545 configB.update( theConfig );
546 delay();
547
548 // ==> No call back because the Conf is not bound to locationA.
549 TestCase.assertNull( tester21.props );
550 TestCase.assertEquals( 1, tester21.numManagedServiceUpdatedCalls );
551 }
552
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000553
554 @Test
555 public void test_switch_static_binding() throws BundleException
556 {
557 // 1. create config with pid and locationA
558 // 2. update config with properties
559 final String pid = "test_switch_static_binding";
560 final String locationA = "test:location/A/" + pid;
561 final Configuration config = configure( pid, locationA, true );
562
563 // 3. register ManagedService ms1 with pid from said locationA
564 final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class, locationA );
565 bundleA.start();
566 delay();
567
568 // ==> configuration supplied to the service ms1
569 final ManagedServiceTestActivator testerA1 = ManagedServiceTestActivator.INSTANCE;
570 TestCase.assertNotNull( testerA1.props );
571 TestCase.assertEquals( 1, testerA1.numManagedServiceUpdatedCalls );
572
573 // 4. register ManagedService ms2 with pid from locationB
574 final String locationB = "test:location/B/" + pid;
575 final Bundle bundleB = installBundle( pid, ManagedServiceTestActivator2.class, locationB );
576 bundleB.start();
577 delay();
578
579 // ==> configuration not supplied to service ms2
580 final ManagedServiceTestActivator2 testerB1 = ManagedServiceTestActivator2.INSTANCE;
581 TestCase.assertNull( testerB1.props );
582 TestCase.assertEquals( 0, testerB1.numManagedServiceUpdatedCalls );
583
584 // 5. Call Configuration.setBundleLocation( "locationB" )
585 config.setBundleLocation( locationB );
586 delay();
587
588 // ==> configuration is bound to locationB
589 TestCase.assertEquals( locationB, config.getBundleLocation() );
590
Felix Meschberger007c50e2011-10-20 12:39:38 +0000591 // ==> configuration removed from service ms1
592 TestCase.assertNull( testerA1.props );
593 TestCase.assertEquals( 2, testerA1.numManagedServiceUpdatedCalls );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000594
Felix Meschberger007c50e2011-10-20 12:39:38 +0000595 // ==> configuration supplied to the service ms2
596 TestCase.assertNotNull( testerB1.props );
597 TestCase.assertEquals( 1, testerB1.numManagedServiceUpdatedCalls );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000598 }
599
600
601 @Test
Felix Meschbergerf32700a2009-08-20 08:13:10 +0000602 public void test_switch_dynamic_binding() throws BundleException, IOException
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000603 {
604 // 1. create config with pid with null location
605 // 2. update config with properties
606 final String pid = "test_switch_dynamic_binding";
607 final String locationA = "test:location/A/" + pid;
608 final Configuration config = configure( pid, null, true );
609
610 // 3. register ManagedService ms1 with pid from locationA
611 final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class, locationA );
612 bundleA.start();
613 delay();
614
615 // ==> configuration supplied to the service ms1
616 final ManagedServiceTestActivator testerA1 = ManagedServiceTestActivator.INSTANCE;
617 TestCase.assertNotNull( testerA1.props );
618 TestCase.assertEquals( 1, testerA1.numManagedServiceUpdatedCalls );
619
620 // ==> configuration is dynamically bound to locationA
621 TestCase.assertEquals( locationA, config.getBundleLocation() );
622
623 // 4. register ManagedService ms2 with pid from locationB
624 final String locationB = "test:location/B/" + pid;
625 final Bundle bundleB = installBundle( pid, ManagedServiceTestActivator2.class, locationB );
626 bundleB.start();
627 delay();
628
629 // ==> configuration not supplied to service ms2
630 final ManagedServiceTestActivator2 testerB1 = ManagedServiceTestActivator2.INSTANCE;
631 TestCase.assertNull( testerB1.props );
632 TestCase.assertEquals( 0, testerB1.numManagedServiceUpdatedCalls );
633
634 // 5. Call Configuration.setBundleLocation( "locationB" )
635 config.setBundleLocation( locationB );
636 delay();
637
638 // ==> configuration is bound to locationB
639 TestCase.assertEquals( locationB, config.getBundleLocation() );
640
Felix Meschberger007c50e2011-10-20 12:39:38 +0000641 // ==> configuration removed from service ms1
642 TestCase.assertNull( testerA1.props );
643 TestCase.assertEquals( 2, testerA1.numManagedServiceUpdatedCalls );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000644
Felix Meschberger007c50e2011-10-20 12:39:38 +0000645 // ==> configuration supplied to the service ms2
646 TestCase.assertNotNull( testerB1.props );
647 TestCase.assertEquals( 1, testerB1.numManagedServiceUpdatedCalls );
Felix Meschbergerf32700a2009-08-20 08:13:10 +0000648
649 // 6. Update configuration now
650 config.update();
651 delay();
652
653 // ==> configuration supplied to the service ms2
654 TestCase.assertNotNull( testerB1.props );
Felix Meschberger007c50e2011-10-20 12:39:38 +0000655 TestCase.assertEquals( 2, testerB1.numManagedServiceUpdatedCalls );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000656 }
657
658
659 @Test
Felix Meschbergerf32700a2009-08-20 08:13:10 +0000660 public void test_switch_static_binding_factory() throws BundleException, IOException
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000661 {
662 // 1. create config with pid and locationA
663 // 2. update config with properties
664 final String factoryPid = "test_switch_static_binding_factory";
665 final String locationA = "test:location/A/" + factoryPid;
666 final Configuration config = createFactoryConfiguration( factoryPid, locationA, true );
667 final String pid = config.getPid();
668
669 // 3. register ManagedService ms1 with pid from said locationA
670 final Bundle bundleA = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class, locationA );
671 bundleA.start();
672 delay();
673
674 // ==> configuration supplied to the service ms1
675 final ManagedServiceFactoryTestActivator testerA1 = ManagedServiceFactoryTestActivator.INSTANCE;
676 TestCase.assertNotNull( testerA1.configs.get( pid ) );
677 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
678
679 // 4. register ManagedService ms2 with pid from locationB
680 final String locationB = "test:location/B/" + factoryPid;
681 final Bundle bundleB = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class, locationB );
682 bundleB.start();
683 delay();
684
685 // ==> configuration not supplied to service ms2
686 final ManagedServiceFactoryTestActivator2 testerB1 = ManagedServiceFactoryTestActivator2.INSTANCE;
687 TestCase.assertNull( testerB1.configs.get( pid ));
688 TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls );
689
690 // 5. Call Configuration.setBundleLocation( "locationB" )
691 config.setBundleLocation( locationB );
692 delay();
693
694 // ==> configuration is bound to locationB
695 TestCase.assertEquals( locationB, config.getBundleLocation() );
696
Felix Meschberger007c50e2011-10-20 12:39:38 +0000697 // ==> configuration removed from service ms1
698 TestCase.assertNull( testerA1.configs.get( pid ));
699 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
700 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryDeleteCalls );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000701
Felix Meschberger007c50e2011-10-20 12:39:38 +0000702 // ==> configuration supplied to the service ms2
703 TestCase.assertNotNull( testerB1.configs.get( pid ) );
704 TestCase.assertEquals( 1, testerB1.numManagedServiceFactoryUpdatedCalls );
Felix Meschbergerf32700a2009-08-20 08:13:10 +0000705
706 // 6. Update configuration now
707 config.update();
708 delay();
709
710 // ==> configuration supplied to the service ms2
711 TestCase.assertNotNull( testerB1.configs.get( pid ) );
Felix Meschberger007c50e2011-10-20 12:39:38 +0000712 TestCase.assertEquals( 2, testerB1.numManagedServiceFactoryUpdatedCalls );
Felix Meschbergerf32700a2009-08-20 08:13:10 +0000713 }
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000714
715
716 @Test
Felix Meschbergerf32700a2009-08-20 08:13:10 +0000717 public void test_switch_dynamic_binding_factory() throws BundleException, IOException
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000718 {
719 // 1. create config with pid and locationA
720 // 2. update config with properties
721 final String factoryPid = "test_switch_static_binding_factory";
722 final String locationA = "test:location/A/" + factoryPid;
723 final Configuration config = createFactoryConfiguration( factoryPid, null, true );
724 final String pid = config.getPid();
725
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000726 TestCase.assertNull( config.getBundleLocation() );
727
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000728 // 3. register ManagedService ms1 with pid from said locationA
729 final Bundle bundleA = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class, locationA );
730 bundleA.start();
731 delay();
732
733 // ==> configuration supplied to the service ms1
734 final ManagedServiceFactoryTestActivator testerA1 = ManagedServiceFactoryTestActivator.INSTANCE;
735 TestCase.assertNotNull( testerA1.configs.get( pid ) );
736 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000737 TestCase.assertEquals( locationA, config.getBundleLocation() );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000738
739 // 4. register ManagedService ms2 with pid from locationB
740 final String locationB = "test:location/B/" + factoryPid;
741 final Bundle bundleB = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class, locationB );
742 bundleB.start();
743 delay();
744
745 // ==> configuration not supplied to service ms2
746 final ManagedServiceFactoryTestActivator2 testerB1 = ManagedServiceFactoryTestActivator2.INSTANCE;
747 TestCase.assertNull( testerB1.configs.get( pid ));
748 TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls );
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000749 TestCase.assertEquals( locationA, config.getBundleLocation() );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000750
751 // 5. Call Configuration.setBundleLocation( "locationB" )
752 config.setBundleLocation( locationB );
753 delay();
754
755 // ==> configuration is bound to locationB
756 TestCase.assertEquals( locationB, config.getBundleLocation() );
757
Felix Meschberger007c50e2011-10-20 12:39:38 +0000758 // ==> configuration removed from service ms1
759 TestCase.assertNull( testerA1.configs.get( pid ));
760 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
761 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryDeleteCalls );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000762
Felix Meschberger007c50e2011-10-20 12:39:38 +0000763 // ==> configuration supplied to the service ms2
764 TestCase.assertNotNull( testerB1.configs.get( pid ) );
765 TestCase.assertEquals( 1, testerB1.numManagedServiceFactoryUpdatedCalls );
Felix Meschbergerf32700a2009-08-20 08:13:10 +0000766
767 // 6. Update configuration now
768 config.update();
769 delay();
770
771 // ==> configuration supplied to the service ms2
772 TestCase.assertNotNull( testerB1.configs.get( pid ) );
Felix Meschberger007c50e2011-10-20 12:39:38 +0000773 TestCase.assertEquals( 2, testerB1.numManagedServiceFactoryUpdatedCalls );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000774 }
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000775
776
777 @Test
Felix Meschbergerf32700a2009-08-20 08:13:10 +0000778 public void test_switch_dynamic_binding_after_uninstall() throws BundleException, IOException
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000779 {
780 // 1. create config with pid with null location
781 // 2. update config with properties
782 final String pid = "test_switch_dynamic_binding";
783 final String locationA = "test:location/A/" + pid;
784 final Configuration config = configure( pid, null, true );
785
786 TestCase.assertNull( config.getBundleLocation() );
787
788 // 3. register ManagedService ms1 with pid from locationA
789 final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class, locationA );
790 bundleA.start();
791 delay();
792
793 // ==> configuration supplied to the service ms1
794 final ManagedServiceTestActivator testerA1 = ManagedServiceTestActivator.INSTANCE;
795 TestCase.assertNotNull( testerA1.props );
796 TestCase.assertEquals( 1, testerA1.numManagedServiceUpdatedCalls );
797
798 // ==> configuration is dynamically bound to locationA
799 TestCase.assertEquals( locationA, config.getBundleLocation() );
800
801 // 4. register ManagedService ms2 with pid from locationB
802 final String locationB = "test:location/B/" + pid;
803 final Bundle bundleB = installBundle( pid, ManagedServiceTestActivator2.class, locationB );
804 bundleB.start();
805 delay();
806
807 // ==> configuration not supplied to service ms2
808 final ManagedServiceTestActivator2 testerB1 = ManagedServiceTestActivator2.INSTANCE;
809 TestCase.assertNull( testerB1.props );
810 TestCase.assertEquals( 0, testerB1.numManagedServiceUpdatedCalls );
811
812 // 5. Uninstall bundle A
813 bundleA.uninstall();
814 delay();
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000815
Felix Meschberger007c50e2011-10-20 12:39:38 +0000816 // ==> configuration is bound to locationB
817 TestCase.assertEquals( locationB, config.getBundleLocation() );
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000818
Felix Meschberger007c50e2011-10-20 12:39:38 +0000819 // ==> configuration supplied to the service ms2
820 TestCase.assertNotNull( testerB1.props );
821 TestCase.assertEquals( 1, testerB1.numManagedServiceUpdatedCalls );
Felix Meschbergerf32700a2009-08-20 08:13:10 +0000822
823 // 6. Update configuration now
824 config.update();
825 delay();
826
827 // ==> configuration supplied to the service ms2
828 TestCase.assertNotNull( testerB1.props );
Felix Meschberger007c50e2011-10-20 12:39:38 +0000829 TestCase.assertEquals( 2, testerB1.numManagedServiceUpdatedCalls );
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000830 }
831
832
833 @Test
Felix Meschbergerf32700a2009-08-20 08:13:10 +0000834 public void test_switch_dynamic_binding_factory_after_uninstall() throws BundleException, IOException
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000835 {
836 // 1. create config with pid and locationA
837 // 2. update config with properties
838 final String factoryPid = "test_switch_static_binding_factory";
839 final String locationA = "test:location/A/" + factoryPid;
840 final Configuration config = createFactoryConfiguration( factoryPid, null, true );
841 final String pid = config.getPid();
842
843 TestCase.assertNull( config.getBundleLocation() );
844
845 // 3. register ManagedService ms1 with pid from said locationA
846 final Bundle bundleA = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class, locationA );
847 bundleA.start();
848 delay();
849
850 // ==> configuration supplied to the service ms1
851 final ManagedServiceFactoryTestActivator testerA1 = ManagedServiceFactoryTestActivator.INSTANCE;
852 TestCase.assertNotNull( testerA1.configs.get( pid ) );
853 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
854 TestCase.assertEquals( locationA, config.getBundleLocation() );
855
856 // 4. register ManagedService ms2 with pid from locationB
857 final String locationB = "test:location/B/" + factoryPid;
858 final Bundle bundleB = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class, locationB );
859 bundleB.start();
860 delay();
861
862 // ==> configuration not supplied to service ms2
863 final ManagedServiceFactoryTestActivator2 testerB1 = ManagedServiceFactoryTestActivator2.INSTANCE;
864 TestCase.assertNull( testerB1.configs.get( pid ));
865 TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls );
866 TestCase.assertEquals( locationA, config.getBundleLocation() );
867
868 // 5. Uninstall bundle A
869 bundleA.uninstall();
870 delay();
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000871
Felix Meschberger007c50e2011-10-20 12:39:38 +0000872 // ==> configuration is bound to locationB
873 TestCase.assertEquals( locationB, config.getBundleLocation() );
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000874
Felix Meschberger007c50e2011-10-20 12:39:38 +0000875 // ==> configuration supplied to the service ms2
876 TestCase.assertNotNull( testerB1.configs.get( pid ) );
877 TestCase.assertEquals( 1, testerB1.numManagedServiceFactoryUpdatedCalls );
Felix Meschbergerf32700a2009-08-20 08:13:10 +0000878
879 // 6. Update configuration now
880 config.update();
881 delay();
882
883 // ==> configuration supplied to the service ms2
884 TestCase.assertNotNull( testerB1.configs.get( pid ) );
Felix Meschberger007c50e2011-10-20 12:39:38 +0000885 TestCase.assertEquals( 2, testerB1.numManagedServiceFactoryUpdatedCalls );
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000886 }
Felix Meschberger42764b82009-08-18 07:51:00 +0000887}