blob: e269eff59a7477e21f613d0cda3cf673edf3651a [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
577 // ==> configuration removed from service ms1
578 TestCase.assertNull( testerA1.props );
579 TestCase.assertEquals( 2, testerA1.numManagedServiceUpdatedCalls );
580
581 // ==> configuration supplied to the service ms2
582 TestCase.assertNotNull( testerB1.props );
583 TestCase.assertEquals( 1, testerB1.numManagedServiceUpdatedCalls );
584 }
585
586
587 @Test
588 public void test_switch_dynamic_binding() throws BundleException
589 {
590 // 1. create config with pid with null location
591 // 2. update config with properties
592 final String pid = "test_switch_dynamic_binding";
593 final String locationA = "test:location/A/" + pid;
594 final Configuration config = configure( pid, null, true );
595
596 // 3. register ManagedService ms1 with pid from locationA
597 final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class, locationA );
598 bundleA.start();
599 delay();
600
601 // ==> configuration supplied to the service ms1
602 final ManagedServiceTestActivator testerA1 = ManagedServiceTestActivator.INSTANCE;
603 TestCase.assertNotNull( testerA1.props );
604 TestCase.assertEquals( 1, testerA1.numManagedServiceUpdatedCalls );
605
606 // ==> configuration is dynamically bound to locationA
607 TestCase.assertEquals( locationA, config.getBundleLocation() );
608
609 // 4. register ManagedService ms2 with pid from locationB
610 final String locationB = "test:location/B/" + pid;
611 final Bundle bundleB = installBundle( pid, ManagedServiceTestActivator2.class, locationB );
612 bundleB.start();
613 delay();
614
615 // ==> configuration not supplied to service ms2
616 final ManagedServiceTestActivator2 testerB1 = ManagedServiceTestActivator2.INSTANCE;
617 TestCase.assertNull( testerB1.props );
618 TestCase.assertEquals( 0, testerB1.numManagedServiceUpdatedCalls );
619
620 // 5. Call Configuration.setBundleLocation( "locationB" )
621 config.setBundleLocation( locationB );
622 delay();
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000623 delay();
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000624
625 // ==> configuration is bound to locationB
626 TestCase.assertEquals( locationB, config.getBundleLocation() );
627
628 // ==> configuration removed from service ms1
629 TestCase.assertNull( testerA1.props );
630 TestCase.assertEquals( 2, testerA1.numManagedServiceUpdatedCalls );
631
632 // ==> configuration supplied to the service ms2
633 TestCase.assertNotNull( testerB1.props );
634 TestCase.assertEquals( 1, testerB1.numManagedServiceUpdatedCalls );
635 }
636
637
638 @Test
639 public void test_switch_static_binding_factory() throws BundleException
640 {
641 // 1. create config with pid and locationA
642 // 2. update config with properties
643 final String factoryPid = "test_switch_static_binding_factory";
644 final String locationA = "test:location/A/" + factoryPid;
645 final Configuration config = createFactoryConfiguration( factoryPid, locationA, true );
646 final String pid = config.getPid();
647
648 // 3. register ManagedService ms1 with pid from said locationA
649 final Bundle bundleA = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class, locationA );
650 bundleA.start();
651 delay();
652
653 // ==> configuration supplied to the service ms1
654 final ManagedServiceFactoryTestActivator testerA1 = ManagedServiceFactoryTestActivator.INSTANCE;
655 TestCase.assertNotNull( testerA1.configs.get( pid ) );
656 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
657
658 // 4. register ManagedService ms2 with pid from locationB
659 final String locationB = "test:location/B/" + factoryPid;
660 final Bundle bundleB = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class, locationB );
661 bundleB.start();
662 delay();
663
664 // ==> configuration not supplied to service ms2
665 final ManagedServiceFactoryTestActivator2 testerB1 = ManagedServiceFactoryTestActivator2.INSTANCE;
666 TestCase.assertNull( testerB1.configs.get( pid ));
667 TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls );
668
669 // 5. Call Configuration.setBundleLocation( "locationB" )
670 config.setBundleLocation( locationB );
671 delay();
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000672 delay();
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000673
674 // ==> configuration is bound to locationB
675 TestCase.assertEquals( locationB, config.getBundleLocation() );
676
677 // ==> configuration removed from service ms1
678 TestCase.assertNull( testerA1.configs.get( pid ));
679 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
680 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryDeleteCalls );
681
682 // ==> configuration supplied to the service ms2
683 TestCase.assertNotNull( testerB1.configs.get( pid ) );
684 TestCase.assertEquals( 1, testerB1.numManagedServiceFactoryUpdatedCalls );
685 }
686
687
688 @Test
689 public void test_switch_dynamic_binding_factory() throws BundleException
690 {
691 // 1. create config with pid and locationA
692 // 2. update config with properties
693 final String factoryPid = "test_switch_static_binding_factory";
694 final String locationA = "test:location/A/" + factoryPid;
695 final Configuration config = createFactoryConfiguration( factoryPid, null, true );
696 final String pid = config.getPid();
697
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000698 TestCase.assertNull( config.getBundleLocation() );
699
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000700 // 3. register ManagedService ms1 with pid from said locationA
701 final Bundle bundleA = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class, locationA );
702 bundleA.start();
703 delay();
704
705 // ==> configuration supplied to the service ms1
706 final ManagedServiceFactoryTestActivator testerA1 = ManagedServiceFactoryTestActivator.INSTANCE;
707 TestCase.assertNotNull( testerA1.configs.get( pid ) );
708 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000709 TestCase.assertEquals( locationA, config.getBundleLocation() );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000710
711 // 4. register ManagedService ms2 with pid from locationB
712 final String locationB = "test:location/B/" + factoryPid;
713 final Bundle bundleB = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class, locationB );
714 bundleB.start();
715 delay();
716
717 // ==> configuration not supplied to service ms2
718 final ManagedServiceFactoryTestActivator2 testerB1 = ManagedServiceFactoryTestActivator2.INSTANCE;
719 TestCase.assertNull( testerB1.configs.get( pid ));
720 TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls );
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000721 TestCase.assertEquals( locationA, config.getBundleLocation() );
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000722
723 // 5. Call Configuration.setBundleLocation( "locationB" )
724 config.setBundleLocation( locationB );
725 delay();
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000726 delay();
Felix Meschberger6f5b69e2009-08-19 09:39:21 +0000727
728 // ==> configuration is bound to locationB
729 TestCase.assertEquals( locationB, config.getBundleLocation() );
730
731 // ==> configuration removed from service ms1
732 TestCase.assertNull( testerA1.configs.get( pid ));
733 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
734 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryDeleteCalls );
735
736 // ==> configuration supplied to the service ms2
737 TestCase.assertNotNull( testerB1.configs.get( pid ) );
738 TestCase.assertEquals( 1, testerB1.numManagedServiceFactoryUpdatedCalls );
739 }
Felix Meschbergercefe5eb2009-08-19 12:37:32 +0000740
741
742 @Test
743 public void test_switch_dynamic_binding_after_uninstall() throws BundleException
744 {
745 // 1. create config with pid with null location
746 // 2. update config with properties
747 final String pid = "test_switch_dynamic_binding";
748 final String locationA = "test:location/A/" + pid;
749 final Configuration config = configure( pid, null, true );
750
751 TestCase.assertNull( config.getBundleLocation() );
752
753 // 3. register ManagedService ms1 with pid from locationA
754 final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class, locationA );
755 bundleA.start();
756 delay();
757
758 // ==> configuration supplied to the service ms1
759 final ManagedServiceTestActivator testerA1 = ManagedServiceTestActivator.INSTANCE;
760 TestCase.assertNotNull( testerA1.props );
761 TestCase.assertEquals( 1, testerA1.numManagedServiceUpdatedCalls );
762
763 // ==> configuration is dynamically bound to locationA
764 TestCase.assertEquals( locationA, config.getBundleLocation() );
765
766 // 4. register ManagedService ms2 with pid from locationB
767 final String locationB = "test:location/B/" + pid;
768 final Bundle bundleB = installBundle( pid, ManagedServiceTestActivator2.class, locationB );
769 bundleB.start();
770 delay();
771
772 // ==> configuration not supplied to service ms2
773 final ManagedServiceTestActivator2 testerB1 = ManagedServiceTestActivator2.INSTANCE;
774 TestCase.assertNull( testerB1.props );
775 TestCase.assertEquals( 0, testerB1.numManagedServiceUpdatedCalls );
776
777 // 5. Uninstall bundle A
778 bundleA.uninstall();
779 delay();
780 delay();
781
782 // ==> configuration is bound to locationB
783 TestCase.assertEquals( locationB, config.getBundleLocation() );
784
785 // ==> configuration supplied to the service ms2
786 TestCase.assertNotNull( testerB1.props );
787 TestCase.assertEquals( 1, testerB1.numManagedServiceUpdatedCalls );
788 }
789
790
791 @Test
792 public void test_switch_dynamic_binding_factory_after_uninstall() throws BundleException
793 {
794 // 1. create config with pid and locationA
795 // 2. update config with properties
796 final String factoryPid = "test_switch_static_binding_factory";
797 final String locationA = "test:location/A/" + factoryPid;
798 final Configuration config = createFactoryConfiguration( factoryPid, null, true );
799 final String pid = config.getPid();
800
801 TestCase.assertNull( config.getBundleLocation() );
802
803 // 3. register ManagedService ms1 with pid from said locationA
804 final Bundle bundleA = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class, locationA );
805 bundleA.start();
806 delay();
807
808 // ==> configuration supplied to the service ms1
809 final ManagedServiceFactoryTestActivator testerA1 = ManagedServiceFactoryTestActivator.INSTANCE;
810 TestCase.assertNotNull( testerA1.configs.get( pid ) );
811 TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls );
812 TestCase.assertEquals( locationA, config.getBundleLocation() );
813
814 // 4. register ManagedService ms2 with pid from locationB
815 final String locationB = "test:location/B/" + factoryPid;
816 final Bundle bundleB = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class, locationB );
817 bundleB.start();
818 delay();
819
820 // ==> configuration not supplied to service ms2
821 final ManagedServiceFactoryTestActivator2 testerB1 = ManagedServiceFactoryTestActivator2.INSTANCE;
822 TestCase.assertNull( testerB1.configs.get( pid ));
823 TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls );
824 TestCase.assertEquals( locationA, config.getBundleLocation() );
825
826 // 5. Uninstall bundle A
827 bundleA.uninstall();
828 delay();
829 delay();
830
831 // ==> configuration is bound to locationB
832 TestCase.assertEquals( locationB, config.getBundleLocation() );
833
834 // ==> configuration supplied to the service ms2
835 TestCase.assertNotNull( testerB1.configs.get( pid ) );
836 TestCase.assertEquals( 1, testerB1.numManagedServiceFactoryUpdatedCalls );
837 }
Felix Meschberger42764b82009-08-18 07:51:00 +0000838}