Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 19 | package org.apache.felix.cm.integration; |
| 20 | |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.util.Hashtable; |
| 24 | import junit.framework.TestCase; |
| 25 | |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 26 | import org.apache.felix.cm.integration.helper.ManagedServiceFactoryTestActivator; |
| 27 | import org.apache.felix.cm.integration.helper.ManagedServiceFactoryTestActivator2; |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 28 | import org.apache.felix.cm.integration.helper.ManagedServiceTestActivator; |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 29 | import org.apache.felix.cm.integration.helper.ManagedServiceTestActivator2; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
| 32 | import org.ops4j.pax.exam.junit.JUnit4TestRunner; |
| 33 | import org.osgi.framework.Bundle; |
| 34 | import org.osgi.framework.BundleException; |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 35 | import org.osgi.framework.ServiceRegistration; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 36 | import org.osgi.service.cm.Configuration; |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 37 | import org.osgi.service.cm.ConfigurationEvent; |
| 38 | import org.osgi.service.cm.ConfigurationListener; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | @RunWith(JUnit4TestRunner.class) |
| 42 | public class ConfigurationBindingTest extends ConfigurationTestBase |
| 43 | { |
| 44 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 45 | static |
| 46 | { |
| 47 | // uncomment to enable debugging of this test class |
| 48 | // paxRunnerVmOption = DEBUG_VM_OPTION; |
| 49 | } |
| 50 | |
| 51 | |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 52 | private ConfigListener configListener; |
| 53 | private ServiceRegistration configListenerReg; |
| 54 | |
| 55 | |
| 56 | @Override |
| 57 | public void setUp() |
| 58 | { |
| 59 | super.setUp(); |
| 60 | |
| 61 | configListener = new ConfigListener(); |
| 62 | configListenerReg = bundleContext.registerService( ConfigurationListener.class.getName(), configListener, null ); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | @Override |
| 67 | public void tearDown() throws BundleException |
| 68 | { |
| 69 | if ( configListenerReg != null ) |
| 70 | { |
| 71 | configListenerReg.unregister(); |
| 72 | configListenerReg = null; |
| 73 | } |
| 74 | configListener = null; |
| 75 | |
| 76 | super.tearDown(); |
| 77 | } |
| 78 | |
| 79 | |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 80 | @Test |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 81 | public void test_configuration_unbound_on_uninstall() throws BundleException |
| 82 | { |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 83 | String pid = "test_configuration_unbound_on_uninstall"; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 84 | configure( pid ); |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 85 | configListener.assertEvents( ConfigurationEvent.CM_UPDATED, 1 ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 86 | |
| 87 | // ensure configuration is unbound |
| 88 | final Configuration beforeInstall = getConfiguration( pid ); |
| 89 | TestCase.assertNull( beforeInstall.getBundleLocation() ); |
| 90 | |
| 91 | bundle = installBundle( pid ); |
| 92 | |
| 93 | // ensure no configuration bound before start |
| 94 | final Configuration beforeStart = getConfiguration( pid ); |
| 95 | TestCase.assertNull( beforeInstall.getBundleLocation() ); |
| 96 | TestCase.assertNull( beforeStart.getBundleLocation() ); |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 97 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 0 ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 98 | |
| 99 | bundle.start(); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 100 | final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 101 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 102 | |
| 103 | // give cm time for distribution |
| 104 | delay(); |
| 105 | |
| 106 | // assert activater has configuration |
| 107 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 108 | TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 109 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 1 ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 110 | |
| 111 | // ensure a freshly retrieved object also has the location |
| 112 | final Configuration beforeStop = getConfiguration( pid ); |
| 113 | TestCase.assertEquals( beforeStop.getBundleLocation(), bundle.getLocation() ); |
| 114 | |
| 115 | // check whether bundle context is set on first configuration |
| 116 | TestCase.assertEquals( beforeInstall.getBundleLocation(), bundle.getLocation() ); |
| 117 | TestCase.assertEquals( beforeStart.getBundleLocation(), bundle.getLocation() ); |
| 118 | |
| 119 | bundle.stop(); |
| 120 | |
| 121 | delay(); |
| 122 | |
| 123 | // ensure configuration still bound |
| 124 | TestCase.assertEquals( beforeInstall.getBundleLocation(), bundle.getLocation() ); |
| 125 | TestCase.assertEquals( beforeStart.getBundleLocation(), bundle.getLocation() ); |
| 126 | TestCase.assertEquals( beforeStop.getBundleLocation(), bundle.getLocation() ); |
| 127 | |
| 128 | // ensure a freshly retrieved object also has the location |
| 129 | final Configuration beforeUninstall = getConfiguration( pid ); |
| 130 | TestCase.assertEquals( beforeUninstall.getBundleLocation(), bundle.getLocation() ); |
| 131 | |
| 132 | bundle.uninstall(); |
| 133 | bundle = null; |
| 134 | |
| 135 | delay(); |
| 136 | |
| 137 | // ensure configuration is not bound any more |
| 138 | TestCase.assertNull( beforeInstall.getBundleLocation() ); |
| 139 | TestCase.assertNull( beforeStart.getBundleLocation() ); |
| 140 | TestCase.assertNull( beforeStop.getBundleLocation() ); |
| 141 | TestCase.assertNull( beforeUninstall.getBundleLocation() ); |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 142 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 1 ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 143 | |
| 144 | // ensure a freshly retrieved object also does not have the location |
| 145 | final Configuration atEnd = getConfiguration( pid ); |
| 146 | TestCase.assertNull( atEnd.getBundleLocation() ); |
| 147 | |
| 148 | // remove the configuration for good |
| 149 | deleteConfig( pid ); |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 150 | delay(); |
| 151 | configListener.assertEvents( ConfigurationEvent.CM_DELETED, 1 ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 155 | @Test |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 156 | public void test_configuration_unbound_on_uninstall_with_cm_restart() throws BundleException |
| 157 | { |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 158 | final String pid = "test_configuration_unbound_on_uninstall_with_cm_restart"; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 159 | configure( pid ); |
| 160 | final Bundle cmBundle = getCmBundle(); |
| 161 | |
| 162 | // ensure configuration is unbound |
| 163 | final Configuration beforeInstall = getConfiguration( pid ); |
| 164 | TestCase.assertNull( beforeInstall.getBundleLocation() ); |
| 165 | |
| 166 | bundle = installBundle( pid ); |
| 167 | |
| 168 | // ensure no configuration bound before start |
| 169 | final Configuration beforeStart = getConfiguration( pid ); |
| 170 | TestCase.assertNull( beforeInstall.getBundleLocation() ); |
| 171 | TestCase.assertNull( beforeStart.getBundleLocation() ); |
| 172 | |
| 173 | bundle.start(); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 174 | final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 175 | TestCase.assertNotNull( "IOActivator not started !!", tester ); |
| 176 | |
| 177 | // give cm time for distribution |
| 178 | delay(); |
| 179 | |
| 180 | // assert activater has configuration |
| 181 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 182 | TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 183 | |
| 184 | // ensure a freshly retrieved object also has the location |
| 185 | final Configuration beforeStop = getConfiguration( pid ); |
| 186 | TestCase.assertEquals( beforeStop.getBundleLocation(), bundle.getLocation() ); |
| 187 | |
| 188 | // check whether bundle context is set on first configuration |
| 189 | TestCase.assertEquals( beforeInstall.getBundleLocation(), bundle.getLocation() ); |
| 190 | TestCase.assertEquals( beforeStart.getBundleLocation(), bundle.getLocation() ); |
| 191 | |
| 192 | bundle.stop(); |
| 193 | |
| 194 | // ensure configuration still bound |
| 195 | TestCase.assertEquals( beforeInstall.getBundleLocation(), bundle.getLocation() ); |
| 196 | TestCase.assertEquals( beforeStart.getBundleLocation(), bundle.getLocation() ); |
| 197 | TestCase.assertEquals( beforeStop.getBundleLocation(), bundle.getLocation() ); |
| 198 | |
| 199 | // ensure a freshly retrieved object also has the location |
| 200 | final Configuration beforeUninstall = getConfiguration( pid ); |
| 201 | TestCase.assertEquals( beforeUninstall.getBundleLocation(), bundle.getLocation() ); |
| 202 | |
| 203 | // stop cm bundle now before uninstalling configured bundle |
| 204 | cmBundle.stop(); |
| 205 | delay(); |
| 206 | |
| 207 | // assert configuration admin service is gone |
| 208 | TestCase.assertNull( configAdminTracker.getService() ); |
| 209 | |
| 210 | // uninstall bundle while configuration admin is stopped |
| 211 | bundle.uninstall(); |
| 212 | bundle = null; |
| 213 | |
| 214 | // start cm bundle again after uninstallation |
| 215 | cmBundle.start(); |
| 216 | delay(); |
| 217 | |
| 218 | // ensure a freshly retrieved object also does not have the location |
| 219 | // FELIX-1484: this test fails due to bundle location not verified |
| 220 | // at first configuration access |
| 221 | final Configuration atEnd = getConfiguration( pid ); |
| 222 | TestCase.assertNull( atEnd.getBundleLocation() ); |
| 223 | |
| 224 | // remove the configuration for good |
| 225 | deleteConfig( pid ); |
| 226 | } |
| 227 | |
| 228 | |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 229 | @Test |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 230 | public void test_not_updated_new_configuration_not_bound_after_bundle_uninstall() throws IOException, |
| 231 | BundleException |
| 232 | { |
| 233 | final String pid = "test_not_updated_new_configuration_not_bound_after_bundle_uninstall"; |
| 234 | |
| 235 | // create a configuration but do not update with properties |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 236 | final Configuration newConfig = configure( pid, null, false ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 237 | TestCase.assertNull( newConfig.getProperties() ); |
| 238 | TestCase.assertNull( newConfig.getBundleLocation() ); |
| 239 | |
| 240 | // start and settle bundle |
| 241 | bundle = installBundle( pid ); |
| 242 | bundle.start(); |
| 243 | delay(); |
| 244 | |
| 245 | // ensure no properties provided to bundle |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 246 | final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 247 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 248 | TestCase.assertNull( "Expect no properties after Service Registration", tester.props ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 249 | TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 250 | |
| 251 | // assert configuration is still unset but bound |
| 252 | TestCase.assertNull( newConfig.getProperties() ); |
| 253 | TestCase.assertEquals( bundle.getLocation(), newConfig.getBundleLocation() ); |
| 254 | |
| 255 | // uninstall bundle, should unbind configuration |
| 256 | bundle.uninstall(); |
| 257 | bundle = null; |
| 258 | |
| 259 | delay(); |
| 260 | |
| 261 | // assert configuration is still unset and unbound |
| 262 | TestCase.assertNull( newConfig.getProperties() ); |
| 263 | TestCase.assertNull( newConfig.getBundleLocation() ); |
| 264 | |
| 265 | // remove the configuration for good |
| 266 | deleteConfig( pid ); |
| 267 | } |
| 268 | |
| 269 | |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 270 | @Test |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 271 | public void test_create_with_location_unbind_before_service_supply() throws BundleException, IOException |
| 272 | { |
| 273 | |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 274 | final String pid = "test_create_with_location_unbind_before_service_supply"; |
| 275 | final String dummyLocation = "http://some/dummy/location"; |
| 276 | |
| 277 | // 1. create and statically bind the configuration |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 278 | final Configuration config = configure( pid, dummyLocation, false ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 279 | TestCase.assertEquals( pid, config.getPid() ); |
| 280 | TestCase.assertEquals( dummyLocation, config.getBundleLocation() ); |
| 281 | |
| 282 | // 2. update configuration |
| 283 | Hashtable<String, String> props = new Hashtable<String, String>(); |
| 284 | props.put( PROP_NAME, PROP_NAME ); |
| 285 | config.update( props ); |
| 286 | TestCase.assertEquals( PROP_NAME, config.getProperties().get( PROP_NAME ) ); |
| 287 | TestCase.assertEquals( pid, config.getPid() ); |
| 288 | TestCase.assertEquals( dummyLocation, config.getBundleLocation() ); |
| 289 | |
| 290 | // 3. (statically) set location to null |
| 291 | config.setBundleLocation( null ); |
| 292 | TestCase.assertNull( config.getBundleLocation() ); |
| 293 | |
| 294 | // 4. install bundle with service |
| 295 | bundle = installBundle( pid ); |
| 296 | bundle.start(); |
| 297 | delay(); |
| 298 | |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 299 | final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 300 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 301 | |
| 302 | // assert activater has configuration (two calls, one per pid) |
| 303 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 304 | TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 305 | |
| 306 | TestCase.assertEquals( bundle.getLocation(), config.getBundleLocation() ); |
| 307 | |
| 308 | bundle.uninstall(); |
| 309 | bundle = null; |
| 310 | |
| 311 | delay(); |
| 312 | |
| 313 | // statically bound configurations must remain bound after bundle |
| 314 | // uninstall |
| 315 | TestCase.assertNull( config.getBundleLocation() ); |
| 316 | |
| 317 | // remove the configuration for good |
| 318 | deleteConfig( pid ); |
| 319 | } |
| 320 | |
| 321 | |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 322 | @Test |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 323 | public void test_statically_bound() throws BundleException |
| 324 | { |
| 325 | final String pid = "test_statically_bound"; |
| 326 | |
| 327 | // install the bundle (we need the location) |
| 328 | bundle = installBundle( pid ); |
| 329 | final String location = bundle.getLocation(); |
| 330 | |
| 331 | // create and statically bind the configuration |
| 332 | configure( pid ); |
| 333 | final Configuration config = getConfiguration( pid ); |
| 334 | TestCase.assertEquals( pid, config.getPid() ); |
| 335 | TestCase.assertNull( config.getBundleLocation() ); |
| 336 | config.setBundleLocation( location ); |
| 337 | TestCase.assertEquals( location, config.getBundleLocation() ); |
| 338 | |
Felix Meschberger | 3b2b415 | 2010-08-26 07:53:47 +0000 | [diff] [blame] | 339 | // ensure configuration is settled before starting the bundle |
| 340 | delay(); |
| 341 | |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 342 | // expect single config update and location change |
| 343 | configListener.assertEvents( ConfigurationEvent.CM_UPDATED, 1 ); |
| 344 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 1 ); |
| 345 | |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 346 | bundle.start(); |
| 347 | |
| 348 | // give cm time for distribution |
| 349 | delay(); |
| 350 | |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 351 | final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 352 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 353 | |
| 354 | // assert activater has configuration (two calls, one per pid) |
| 355 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 356 | TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 357 | |
| 358 | TestCase.assertEquals( location, config.getBundleLocation() ); |
| 359 | |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 360 | // config already statically bound, no change event |
| 361 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 0 ); |
| 362 | |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 363 | bundle.uninstall(); |
| 364 | bundle = null; |
| 365 | |
| 366 | delay(); |
| 367 | |
| 368 | // statically bound configurations must remain bound after bundle |
| 369 | // uninstall |
| 370 | TestCase.assertEquals( location, config.getBundleLocation() ); |
| 371 | |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 372 | // configuration statically bound, no change event |
| 373 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 0 ); |
| 374 | |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 375 | // remove the configuration for good |
| 376 | deleteConfig( pid ); |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 377 | |
| 378 | delay(); |
| 379 | configListener.assertEvents( ConfigurationEvent.CM_DELETED, 1 ); |
| 380 | configListener.assertEvents( ConfigurationEvent.CM_UPDATED, 0 ); |
| 381 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 0 ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 385 | @Test |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 386 | public void test_static_binding_and_unbinding() throws BundleException |
| 387 | { |
| 388 | final String pid = "test_static_binding_and_unbinding"; |
| 389 | final String location = bundleContext.getBundle().getLocation(); |
| 390 | |
| 391 | // create and statically bind the configuration |
| 392 | configure( pid ); |
| 393 | final Configuration config = getConfiguration( pid ); |
| 394 | TestCase.assertEquals( pid, config.getPid() ); |
| 395 | TestCase.assertNull( config.getBundleLocation() ); |
| 396 | |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 397 | // first configuration updated event |
| 398 | delay(); |
| 399 | configListener.assertEvents( ConfigurationEvent.CM_UPDATED, 1 ); |
| 400 | |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 401 | // bind the configuration |
| 402 | config.setBundleLocation( location ); |
| 403 | TestCase.assertEquals( location, config.getBundleLocation() ); |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 404 | delay(); |
| 405 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 1 ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 406 | |
| 407 | // restart CM bundle |
| 408 | final Bundle cmBundle = getCmBundle(); |
| 409 | cmBundle.stop(); |
| 410 | delay(); |
| 411 | cmBundle.start(); |
| 412 | |
| 413 | // assert configuration still bound |
| 414 | final Configuration configAfterRestart = getConfiguration( pid ); |
| 415 | TestCase.assertEquals( pid, configAfterRestart.getPid() ); |
| 416 | TestCase.assertEquals( location, configAfterRestart.getBundleLocation() ); |
| 417 | |
| 418 | // unbind the configuration |
| 419 | configAfterRestart.setBundleLocation( null ); |
| 420 | TestCase.assertNull( configAfterRestart.getBundleLocation() ); |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 421 | delay(); |
| 422 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 1 ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 423 | |
| 424 | // restart CM bundle |
| 425 | cmBundle.stop(); |
| 426 | delay(); |
| 427 | cmBundle.start(); |
| 428 | |
| 429 | // assert configuration unbound |
| 430 | final Configuration configUnboundAfterRestart = getConfiguration( pid ); |
| 431 | TestCase.assertEquals( pid, configUnboundAfterRestart.getPid() ); |
| 432 | TestCase.assertNull( configUnboundAfterRestart.getBundleLocation() ); |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 433 | |
| 434 | configListener.assertEvents( ConfigurationEvent.CM_DELETED, 0 ); |
| 435 | configListener.assertEvents( ConfigurationEvent.CM_UPDATED, 0 ); |
| 436 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 0 ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 440 | @Test |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 441 | public void test_dynamic_binding_and_unbinding() throws BundleException |
| 442 | { |
| 443 | final String pid = "test_dynamic_binding_and_unbinding"; |
| 444 | |
| 445 | // create and statically bind the configuration |
| 446 | configure( pid ); |
| 447 | final Configuration config = getConfiguration( pid ); |
| 448 | TestCase.assertEquals( pid, config.getPid() ); |
| 449 | TestCase.assertNull( config.getBundleLocation() ); |
| 450 | |
| 451 | // dynamically bind the configuration |
| 452 | bundle = installBundle( pid ); |
| 453 | final String location = bundle.getLocation(); |
| 454 | bundle.start(); |
| 455 | delay(); |
| 456 | TestCase.assertEquals( location, config.getBundleLocation() ); |
| 457 | |
| 458 | // restart CM bundle |
| 459 | final Bundle cmBundle = getCmBundle(); |
| 460 | cmBundle.stop(); |
| 461 | delay(); |
| 462 | cmBundle.start(); |
| 463 | |
| 464 | // assert configuration still bound |
| 465 | final Configuration configAfterRestart = getConfiguration( pid ); |
| 466 | TestCase.assertEquals( pid, configAfterRestart.getPid() ); |
| 467 | TestCase.assertEquals( location, configAfterRestart.getBundleLocation() ); |
| 468 | |
| 469 | // stop bundle (configuration remains bound !!) |
| 470 | bundle.stop(); |
| 471 | delay(); |
| 472 | TestCase.assertEquals( location, configAfterRestart.getBundleLocation() ); |
| 473 | |
| 474 | // restart CM bundle |
| 475 | cmBundle.stop(); |
| 476 | delay(); |
| 477 | cmBundle.start(); |
| 478 | |
| 479 | // assert configuration still bound |
| 480 | final Configuration configBoundAfterRestart = getConfiguration( pid ); |
| 481 | TestCase.assertEquals( pid, configBoundAfterRestart.getPid() ); |
| 482 | TestCase.assertEquals( location, configBoundAfterRestart.getBundleLocation() ); |
| 483 | } |
| 484 | |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 485 | |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 486 | @Test |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 487 | public void test_static_binding() throws BundleException |
| 488 | { |
| 489 | final String pid = "test_static_binding"; |
| 490 | |
| 491 | // install a bundle to get a location for binding |
| 492 | bundle = installBundle( pid ); |
| 493 | final String location = bundle.getLocation(); |
| 494 | |
| 495 | // create and statically bind the configuration |
| 496 | configure( pid ); |
| 497 | final Configuration config = getConfiguration( pid ); |
| 498 | TestCase.assertEquals( pid, config.getPid() ); |
| 499 | TestCase.assertNull( config.getBundleLocation() ); |
| 500 | config.setBundleLocation( location ); |
| 501 | TestCase.assertEquals( location, config.getBundleLocation() ); |
| 502 | |
Felix Meschberger | 3b2b415 | 2010-08-26 07:53:47 +0000 | [diff] [blame] | 503 | // ensure configuration is settled before starting the bundle |
| 504 | delay(); |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 505 | configListener.assertEvents( ConfigurationEvent.CM_UPDATED, 1 ); |
Felix Meschberger | 3b2b415 | 2010-08-26 07:53:47 +0000 | [diff] [blame] | 506 | |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 507 | // start the bundle |
| 508 | bundle.start(); |
| 509 | delay(); |
| 510 | TestCase.assertEquals( location, config.getBundleLocation() ); |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 511 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 1 ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 512 | |
| 513 | // assert the configuration is supplied |
| 514 | final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE; |
| 515 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 516 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props ); |
| 517 | TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls ); |
| 518 | |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 519 | // remove the static binding and assert bound (again) |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 520 | config.setBundleLocation( null ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 521 | delay(); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 522 | TestCase.assertEquals( location, config.getBundleLocation() ); |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 523 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 2 ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 524 | |
| 525 | // uninstall bundle and assert configuration unbound |
| 526 | bundle.uninstall(); |
| 527 | bundle = null; |
| 528 | delay(); |
| 529 | TestCase.assertNull( config.getBundleLocation() ); |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 530 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 1 ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 534 | @Test |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 535 | public void test_two_bundles_one_pid() throws BundleException, IOException |
| 536 | { |
| 537 | // 1. Bundle registers service with pid1 |
| 538 | final String pid = "test_two_bundles_one_pid"; |
| 539 | final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class ); |
| 540 | final String locationA = bundleA.getLocation(); |
| 541 | bundleA.start(); |
| 542 | delay(); |
| 543 | |
| 544 | // call back with null |
| 545 | final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE; |
| 546 | TestCase.assertNull( tester.props ); |
| 547 | TestCase.assertEquals( 1, tester.numManagedServiceUpdatedCalls ); |
| 548 | |
| 549 | // 2. create new Conf with pid1 and locationA. |
| 550 | final Configuration config = configure( pid, locationA, false ); |
| 551 | delay(); |
| 552 | |
| 553 | // ==> No call back. |
| 554 | TestCase.assertNull( tester.props ); |
| 555 | TestCase.assertEquals( 1, tester.numManagedServiceUpdatedCalls ); |
| 556 | |
| 557 | // 3. Configuration#update(prop) is called. |
| 558 | config.update( theConfig ); |
| 559 | delay(); |
| 560 | |
| 561 | // ==> call back with the prop. |
| 562 | TestCase.assertNotNull( tester.props ); |
| 563 | TestCase.assertEquals( 2, tester.numManagedServiceUpdatedCalls ); |
| 564 | |
| 565 | // 4. Stop BundleA |
| 566 | bundleA.stop(); |
| 567 | delay(); |
| 568 | |
| 569 | // 5. Start BundleA |
| 570 | bundleA.start(); |
| 571 | delay(); |
| 572 | |
| 573 | // ==> call back with the prop. |
| 574 | final ManagedServiceTestActivator tester2 = ManagedServiceTestActivator.INSTANCE; |
| 575 | TestCase.assertNotNull( tester2.props ); |
| 576 | TestCase.assertEquals( 1, tester2.numManagedServiceUpdatedCalls ); |
| 577 | |
| 578 | // 6. Configuration#deleted() is called. |
| 579 | config.delete(); |
| 580 | delay(); |
| 581 | |
| 582 | // ==> call back with null. |
| 583 | TestCase.assertNull( tester2.props ); |
| 584 | TestCase.assertEquals( 2, tester2.numManagedServiceUpdatedCalls ); |
| 585 | |
| 586 | // 7. uninstall Bundle A for cleanup. |
| 587 | bundleA.uninstall(); |
| 588 | delay(); |
| 589 | |
| 590 | // Test 2 |
| 591 | |
| 592 | // 8. BundleA registers ManagedService with pid1. |
| 593 | final Bundle bundleA2 = installBundle( pid, ManagedServiceTestActivator.class ); |
| 594 | final String locationA2 = bundleA.getLocation(); |
| 595 | bundleA2.start(); |
| 596 | delay(); |
| 597 | |
| 598 | // call back with null |
| 599 | final ManagedServiceTestActivator tester21 = ManagedServiceTestActivator.INSTANCE; |
| 600 | TestCase.assertNull( tester21.props ); |
| 601 | TestCase.assertEquals( 1, tester21.numManagedServiceUpdatedCalls ); |
| 602 | |
| 603 | // 9. create new Conf with pid1 and locationB. |
| 604 | final String locationB = "test:locationB/" + pid; |
| 605 | final Configuration configB = configure( pid, locationB, false ); |
| 606 | delay(); |
| 607 | |
| 608 | // ==> No call back. |
| 609 | TestCase.assertNull( tester21.props ); |
| 610 | TestCase.assertEquals( 1, tester21.numManagedServiceUpdatedCalls ); |
| 611 | |
| 612 | // 10. Configuration#update(prop) is called. |
| 613 | configB.update( theConfig ); |
| 614 | delay(); |
| 615 | |
| 616 | // ==> No call back because the Conf is not bound to locationA. |
| 617 | TestCase.assertNull( tester21.props ); |
| 618 | TestCase.assertEquals( 1, tester21.numManagedServiceUpdatedCalls ); |
| 619 | } |
| 620 | |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 621 | |
| 622 | @Test |
| 623 | public void test_switch_static_binding() throws BundleException |
| 624 | { |
| 625 | // 1. create config with pid and locationA |
| 626 | // 2. update config with properties |
| 627 | final String pid = "test_switch_static_binding"; |
| 628 | final String locationA = "test:location/A/" + pid; |
| 629 | final Configuration config = configure( pid, locationA, true ); |
| 630 | |
| 631 | // 3. register ManagedService ms1 with pid from said locationA |
| 632 | final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class, locationA ); |
| 633 | bundleA.start(); |
| 634 | delay(); |
| 635 | |
| 636 | // ==> configuration supplied to the service ms1 |
| 637 | final ManagedServiceTestActivator testerA1 = ManagedServiceTestActivator.INSTANCE; |
| 638 | TestCase.assertNotNull( testerA1.props ); |
| 639 | TestCase.assertEquals( 1, testerA1.numManagedServiceUpdatedCalls ); |
| 640 | |
| 641 | // 4. register ManagedService ms2 with pid from locationB |
| 642 | final String locationB = "test:location/B/" + pid; |
| 643 | final Bundle bundleB = installBundle( pid, ManagedServiceTestActivator2.class, locationB ); |
| 644 | bundleB.start(); |
| 645 | delay(); |
| 646 | |
Felix Meschberger | 55d0a82 | 2011-11-16 12:07:17 +0000 | [diff] [blame^] | 647 | // ==> invisible configuration supplied as null to service ms2 |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 648 | final ManagedServiceTestActivator2 testerB1 = ManagedServiceTestActivator2.INSTANCE; |
| 649 | TestCase.assertNull( testerB1.props ); |
Felix Meschberger | 55d0a82 | 2011-11-16 12:07:17 +0000 | [diff] [blame^] | 650 | TestCase.assertEquals( 1, testerB1.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 651 | |
| 652 | // 5. Call Configuration.setBundleLocation( "locationB" ) |
| 653 | config.setBundleLocation( locationB ); |
| 654 | delay(); |
| 655 | |
| 656 | // ==> configuration is bound to locationB |
| 657 | TestCase.assertEquals( locationB, config.getBundleLocation() ); |
| 658 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 659 | // ==> configuration removed from service ms1 |
| 660 | TestCase.assertNull( testerA1.props ); |
| 661 | TestCase.assertEquals( 2, testerA1.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 662 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 663 | // ==> configuration supplied to the service ms2 |
| 664 | TestCase.assertNotNull( testerB1.props ); |
Felix Meschberger | 55d0a82 | 2011-11-16 12:07:17 +0000 | [diff] [blame^] | 665 | TestCase.assertEquals( 2, testerB1.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | |
| 669 | @Test |
Felix Meschberger | f32700a | 2009-08-20 08:13:10 +0000 | [diff] [blame] | 670 | public void test_switch_dynamic_binding() throws BundleException, IOException |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 671 | { |
| 672 | // 1. create config with pid with null location |
| 673 | // 2. update config with properties |
| 674 | final String pid = "test_switch_dynamic_binding"; |
| 675 | final String locationA = "test:location/A/" + pid; |
| 676 | final Configuration config = configure( pid, null, true ); |
| 677 | |
| 678 | // 3. register ManagedService ms1 with pid from locationA |
| 679 | final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class, locationA ); |
| 680 | bundleA.start(); |
| 681 | delay(); |
| 682 | |
| 683 | // ==> configuration supplied to the service ms1 |
| 684 | final ManagedServiceTestActivator testerA1 = ManagedServiceTestActivator.INSTANCE; |
| 685 | TestCase.assertNotNull( testerA1.props ); |
| 686 | TestCase.assertEquals( 1, testerA1.numManagedServiceUpdatedCalls ); |
| 687 | |
| 688 | // ==> configuration is dynamically bound to locationA |
| 689 | TestCase.assertEquals( locationA, config.getBundleLocation() ); |
| 690 | |
| 691 | // 4. register ManagedService ms2 with pid from locationB |
| 692 | final String locationB = "test:location/B/" + pid; |
| 693 | final Bundle bundleB = installBundle( pid, ManagedServiceTestActivator2.class, locationB ); |
| 694 | bundleB.start(); |
| 695 | delay(); |
| 696 | |
Felix Meschberger | 55d0a82 | 2011-11-16 12:07:17 +0000 | [diff] [blame^] | 697 | // ==> invisible configuration supplied as null to service ms2 |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 698 | final ManagedServiceTestActivator2 testerB1 = ManagedServiceTestActivator2.INSTANCE; |
| 699 | TestCase.assertNull( testerB1.props ); |
Felix Meschberger | 55d0a82 | 2011-11-16 12:07:17 +0000 | [diff] [blame^] | 700 | TestCase.assertEquals( 1, testerB1.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 701 | |
| 702 | // 5. Call Configuration.setBundleLocation( "locationB" ) |
| 703 | config.setBundleLocation( locationB ); |
| 704 | delay(); |
| 705 | |
| 706 | // ==> configuration is bound to locationB |
| 707 | TestCase.assertEquals( locationB, config.getBundleLocation() ); |
| 708 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 709 | // ==> configuration removed from service ms1 |
| 710 | TestCase.assertNull( testerA1.props ); |
| 711 | TestCase.assertEquals( 2, testerA1.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 712 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 713 | // ==> configuration supplied to the service ms2 |
| 714 | TestCase.assertNotNull( testerB1.props ); |
Felix Meschberger | 55d0a82 | 2011-11-16 12:07:17 +0000 | [diff] [blame^] | 715 | TestCase.assertEquals( 2, testerB1.numManagedServiceUpdatedCalls ); |
Felix Meschberger | f32700a | 2009-08-20 08:13:10 +0000 | [diff] [blame] | 716 | |
| 717 | // 6. Update configuration now |
| 718 | config.update(); |
| 719 | delay(); |
| 720 | |
| 721 | // ==> configuration supplied to the service ms2 |
| 722 | TestCase.assertNotNull( testerB1.props ); |
Felix Meschberger | 55d0a82 | 2011-11-16 12:07:17 +0000 | [diff] [blame^] | 723 | TestCase.assertEquals( 3, testerB1.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | |
| 727 | @Test |
Felix Meschberger | f32700a | 2009-08-20 08:13:10 +0000 | [diff] [blame] | 728 | public void test_switch_static_binding_factory() throws BundleException, IOException |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 729 | { |
| 730 | // 1. create config with pid and locationA |
| 731 | // 2. update config with properties |
| 732 | final String factoryPid = "test_switch_static_binding_factory"; |
| 733 | final String locationA = "test:location/A/" + factoryPid; |
| 734 | final Configuration config = createFactoryConfiguration( factoryPid, locationA, true ); |
| 735 | final String pid = config.getPid(); |
| 736 | |
| 737 | // 3. register ManagedService ms1 with pid from said locationA |
| 738 | final Bundle bundleA = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class, locationA ); |
| 739 | bundleA.start(); |
| 740 | delay(); |
| 741 | |
| 742 | // ==> configuration supplied to the service ms1 |
| 743 | final ManagedServiceFactoryTestActivator testerA1 = ManagedServiceFactoryTestActivator.INSTANCE; |
| 744 | TestCase.assertNotNull( testerA1.configs.get( pid ) ); |
| 745 | TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls ); |
| 746 | |
| 747 | // 4. register ManagedService ms2 with pid from locationB |
| 748 | final String locationB = "test:location/B/" + factoryPid; |
| 749 | final Bundle bundleB = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class, locationB ); |
| 750 | bundleB.start(); |
| 751 | delay(); |
| 752 | |
| 753 | // ==> configuration not supplied to service ms2 |
| 754 | final ManagedServiceFactoryTestActivator2 testerB1 = ManagedServiceFactoryTestActivator2.INSTANCE; |
| 755 | TestCase.assertNull( testerB1.configs.get( pid )); |
| 756 | TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls ); |
| 757 | |
| 758 | // 5. Call Configuration.setBundleLocation( "locationB" ) |
| 759 | config.setBundleLocation( locationB ); |
| 760 | delay(); |
| 761 | |
| 762 | // ==> configuration is bound to locationB |
| 763 | TestCase.assertEquals( locationB, config.getBundleLocation() ); |
| 764 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 765 | // ==> configuration removed from service ms1 |
| 766 | TestCase.assertNull( testerA1.configs.get( pid )); |
| 767 | TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls ); |
| 768 | TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryDeleteCalls ); |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 769 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 770 | // ==> configuration supplied to the service ms2 |
| 771 | TestCase.assertNotNull( testerB1.configs.get( pid ) ); |
| 772 | TestCase.assertEquals( 1, testerB1.numManagedServiceFactoryUpdatedCalls ); |
Felix Meschberger | f32700a | 2009-08-20 08:13:10 +0000 | [diff] [blame] | 773 | |
| 774 | // 6. Update configuration now |
| 775 | config.update(); |
| 776 | delay(); |
| 777 | |
| 778 | // ==> configuration supplied to the service ms2 |
| 779 | TestCase.assertNotNull( testerB1.configs.get( pid ) ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 780 | TestCase.assertEquals( 2, testerB1.numManagedServiceFactoryUpdatedCalls ); |
Felix Meschberger | f32700a | 2009-08-20 08:13:10 +0000 | [diff] [blame] | 781 | } |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 782 | |
| 783 | |
| 784 | @Test |
Felix Meschberger | f32700a | 2009-08-20 08:13:10 +0000 | [diff] [blame] | 785 | public void test_switch_dynamic_binding_factory() throws BundleException, IOException |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 786 | { |
| 787 | // 1. create config with pid and locationA |
| 788 | // 2. update config with properties |
| 789 | final String factoryPid = "test_switch_static_binding_factory"; |
| 790 | final String locationA = "test:location/A/" + factoryPid; |
| 791 | final Configuration config = createFactoryConfiguration( factoryPid, null, true ); |
| 792 | final String pid = config.getPid(); |
| 793 | |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 794 | TestCase.assertNull( config.getBundleLocation() ); |
| 795 | |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 796 | // 3. register ManagedService ms1 with pid from said locationA |
| 797 | final Bundle bundleA = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class, locationA ); |
| 798 | bundleA.start(); |
| 799 | delay(); |
| 800 | |
| 801 | // ==> configuration supplied to the service ms1 |
| 802 | final ManagedServiceFactoryTestActivator testerA1 = ManagedServiceFactoryTestActivator.INSTANCE; |
| 803 | TestCase.assertNotNull( testerA1.configs.get( pid ) ); |
| 804 | TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls ); |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 805 | TestCase.assertEquals( locationA, config.getBundleLocation() ); |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 806 | |
| 807 | // 4. register ManagedService ms2 with pid from locationB |
| 808 | final String locationB = "test:location/B/" + factoryPid; |
| 809 | final Bundle bundleB = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class, locationB ); |
| 810 | bundleB.start(); |
| 811 | delay(); |
| 812 | |
| 813 | // ==> configuration not supplied to service ms2 |
| 814 | final ManagedServiceFactoryTestActivator2 testerB1 = ManagedServiceFactoryTestActivator2.INSTANCE; |
| 815 | TestCase.assertNull( testerB1.configs.get( pid )); |
| 816 | TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls ); |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 817 | TestCase.assertEquals( locationA, config.getBundleLocation() ); |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 818 | |
| 819 | // 5. Call Configuration.setBundleLocation( "locationB" ) |
| 820 | config.setBundleLocation( locationB ); |
| 821 | delay(); |
| 822 | |
| 823 | // ==> configuration is bound to locationB |
| 824 | TestCase.assertEquals( locationB, config.getBundleLocation() ); |
| 825 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 826 | // ==> configuration removed from service ms1 |
| 827 | TestCase.assertNull( testerA1.configs.get( pid )); |
| 828 | TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls ); |
| 829 | TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryDeleteCalls ); |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 830 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 831 | // ==> configuration supplied to the service ms2 |
| 832 | TestCase.assertNotNull( testerB1.configs.get( pid ) ); |
| 833 | TestCase.assertEquals( 1, testerB1.numManagedServiceFactoryUpdatedCalls ); |
Felix Meschberger | f32700a | 2009-08-20 08:13:10 +0000 | [diff] [blame] | 834 | |
| 835 | // 6. Update configuration now |
| 836 | config.update(); |
| 837 | delay(); |
| 838 | |
| 839 | // ==> configuration supplied to the service ms2 |
| 840 | TestCase.assertNotNull( testerB1.configs.get( pid ) ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 841 | TestCase.assertEquals( 2, testerB1.numManagedServiceFactoryUpdatedCalls ); |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 842 | } |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 843 | |
| 844 | |
| 845 | @Test |
Felix Meschberger | f32700a | 2009-08-20 08:13:10 +0000 | [diff] [blame] | 846 | public void test_switch_dynamic_binding_after_uninstall() throws BundleException, IOException |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 847 | { |
| 848 | // 1. create config with pid with null location |
| 849 | // 2. update config with properties |
| 850 | final String pid = "test_switch_dynamic_binding"; |
| 851 | final String locationA = "test:location/A/" + pid; |
| 852 | final Configuration config = configure( pid, null, true ); |
| 853 | |
| 854 | TestCase.assertNull( config.getBundleLocation() ); |
| 855 | |
| 856 | // 3. register ManagedService ms1 with pid from locationA |
| 857 | final Bundle bundleA = installBundle( pid, ManagedServiceTestActivator.class, locationA ); |
| 858 | bundleA.start(); |
| 859 | delay(); |
| 860 | |
| 861 | // ==> configuration supplied to the service ms1 |
| 862 | final ManagedServiceTestActivator testerA1 = ManagedServiceTestActivator.INSTANCE; |
| 863 | TestCase.assertNotNull( testerA1.props ); |
| 864 | TestCase.assertEquals( 1, testerA1.numManagedServiceUpdatedCalls ); |
| 865 | |
| 866 | // ==> configuration is dynamically bound to locationA |
| 867 | TestCase.assertEquals( locationA, config.getBundleLocation() ); |
| 868 | |
| 869 | // 4. register ManagedService ms2 with pid from locationB |
| 870 | final String locationB = "test:location/B/" + pid; |
| 871 | final Bundle bundleB = installBundle( pid, ManagedServiceTestActivator2.class, locationB ); |
| 872 | bundleB.start(); |
| 873 | delay(); |
| 874 | |
Felix Meschberger | 55d0a82 | 2011-11-16 12:07:17 +0000 | [diff] [blame^] | 875 | // ==> invisible configuration supplied as null to service ms2 |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 876 | final ManagedServiceTestActivator2 testerB1 = ManagedServiceTestActivator2.INSTANCE; |
| 877 | TestCase.assertNull( testerB1.props ); |
Felix Meschberger | 55d0a82 | 2011-11-16 12:07:17 +0000 | [diff] [blame^] | 878 | TestCase.assertEquals( 1, testerB1.numManagedServiceUpdatedCalls ); |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 879 | |
| 880 | // 5. Uninstall bundle A |
| 881 | bundleA.uninstall(); |
| 882 | delay(); |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 883 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 884 | // ==> configuration is bound to locationB |
| 885 | TestCase.assertEquals( locationB, config.getBundleLocation() ); |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 886 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 887 | // ==> configuration supplied to the service ms2 |
| 888 | TestCase.assertNotNull( testerB1.props ); |
Felix Meschberger | 55d0a82 | 2011-11-16 12:07:17 +0000 | [diff] [blame^] | 889 | TestCase.assertEquals( 2, testerB1.numManagedServiceUpdatedCalls ); |
Felix Meschberger | f32700a | 2009-08-20 08:13:10 +0000 | [diff] [blame] | 890 | |
| 891 | // 6. Update configuration now |
| 892 | config.update(); |
| 893 | delay(); |
| 894 | |
| 895 | // ==> configuration supplied to the service ms2 |
| 896 | TestCase.assertNotNull( testerB1.props ); |
Felix Meschberger | 55d0a82 | 2011-11-16 12:07:17 +0000 | [diff] [blame^] | 897 | TestCase.assertEquals( 3, testerB1.numManagedServiceUpdatedCalls ); |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | |
| 901 | @Test |
Felix Meschberger | f32700a | 2009-08-20 08:13:10 +0000 | [diff] [blame] | 902 | public void test_switch_dynamic_binding_factory_after_uninstall() throws BundleException, IOException |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 903 | { |
| 904 | // 1. create config with pid and locationA |
| 905 | // 2. update config with properties |
| 906 | final String factoryPid = "test_switch_static_binding_factory"; |
| 907 | final String locationA = "test:location/A/" + factoryPid; |
| 908 | final Configuration config = createFactoryConfiguration( factoryPid, null, true ); |
| 909 | final String pid = config.getPid(); |
| 910 | |
| 911 | TestCase.assertNull( config.getBundleLocation() ); |
| 912 | |
| 913 | // 3. register ManagedService ms1 with pid from said locationA |
| 914 | final Bundle bundleA = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class, locationA ); |
| 915 | bundleA.start(); |
| 916 | delay(); |
| 917 | |
| 918 | // ==> configuration supplied to the service ms1 |
| 919 | final ManagedServiceFactoryTestActivator testerA1 = ManagedServiceFactoryTestActivator.INSTANCE; |
| 920 | TestCase.assertNotNull( testerA1.configs.get( pid ) ); |
| 921 | TestCase.assertEquals( 1, testerA1.numManagedServiceFactoryUpdatedCalls ); |
| 922 | TestCase.assertEquals( locationA, config.getBundleLocation() ); |
| 923 | |
| 924 | // 4. register ManagedService ms2 with pid from locationB |
| 925 | final String locationB = "test:location/B/" + factoryPid; |
| 926 | final Bundle bundleB = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class, locationB ); |
| 927 | bundleB.start(); |
| 928 | delay(); |
| 929 | |
| 930 | // ==> configuration not supplied to service ms2 |
| 931 | final ManagedServiceFactoryTestActivator2 testerB1 = ManagedServiceFactoryTestActivator2.INSTANCE; |
| 932 | TestCase.assertNull( testerB1.configs.get( pid )); |
| 933 | TestCase.assertEquals( 0, testerB1.numManagedServiceFactoryUpdatedCalls ); |
| 934 | TestCase.assertEquals( locationA, config.getBundleLocation() ); |
| 935 | |
| 936 | // 5. Uninstall bundle A |
| 937 | bundleA.uninstall(); |
| 938 | delay(); |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 939 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 940 | // ==> configuration is bound to locationB |
| 941 | TestCase.assertEquals( locationB, config.getBundleLocation() ); |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 942 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 943 | // ==> configuration supplied to the service ms2 |
| 944 | TestCase.assertNotNull( testerB1.configs.get( pid ) ); |
| 945 | TestCase.assertEquals( 1, testerB1.numManagedServiceFactoryUpdatedCalls ); |
Felix Meschberger | f32700a | 2009-08-20 08:13:10 +0000 | [diff] [blame] | 946 | |
| 947 | // 6. Update configuration now |
| 948 | config.update(); |
| 949 | delay(); |
| 950 | |
| 951 | // ==> configuration supplied to the service ms2 |
| 952 | TestCase.assertNotNull( testerB1.configs.get( pid ) ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 953 | TestCase.assertEquals( 2, testerB1.numManagedServiceFactoryUpdatedCalls ); |
Felix Meschberger | cefe5eb | 2009-08-19 12:37:32 +0000 | [diff] [blame] | 954 | } |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 955 | |
Felix Meschberger | 2c04dcf | 2011-10-20 13:52:48 +0000 | [diff] [blame] | 956 | |
| 957 | @Test |
| 958 | public void test_location_changed_events() throws BundleException, IOException |
| 959 | { |
| 960 | String pid = "test_location_changed_events"; |
| 961 | configure( pid ); |
| 962 | delay(); |
| 963 | configListener.assertEvents( ConfigurationEvent.CM_UPDATED, 1 ); |
| 964 | |
| 965 | // ensure configuration is unbound |
| 966 | final Configuration config = getConfiguration( pid ); |
| 967 | TestCase.assertNull( config.getBundleLocation() ); |
| 968 | |
| 969 | bundle = installBundle( pid ); |
| 970 | bundle.start(); |
| 971 | delay(); |
| 972 | |
| 973 | // ensure no configuration bound before start |
| 974 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 1 ); |
| 975 | |
| 976 | // uninstall the bundle, dynamic location changed |
| 977 | bundle.uninstall(); |
| 978 | bundle = null; |
| 979 | delay(); |
| 980 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 1 ); |
| 981 | |
| 982 | // change the location |
| 983 | config.setBundleLocation( "some_location_1" ); |
| 984 | delay(); |
| 985 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 1 ); |
| 986 | |
| 987 | // change the location |
| 988 | config.setBundleLocation( "some_location_2" ); |
| 989 | delay(); |
| 990 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 1 ); |
| 991 | |
| 992 | // remove configuration, delete event |
| 993 | config.delete(); |
| 994 | delay(); |
| 995 | configListener.assertEvents( ConfigurationEvent.CM_DELETED, 1 ); |
| 996 | |
| 997 | // no more events |
| 998 | delay(); |
| 999 | configListener.assertEvents( ConfigurationEvent.CM_DELETED, 0 ); |
| 1000 | configListener.assertEvents( ConfigurationEvent.CM_UPDATED, 0 ); |
| 1001 | configListener.assertEvents( ConfigurationEvent.CM_LOCATION_CHANGED, 0 ); |
| 1002 | } |
| 1003 | |
Felix Meschberger | 2f8f3c0 | 2011-10-20 13:45:30 +0000 | [diff] [blame] | 1004 | private static class ConfigListener implements ConfigurationListener { |
| 1005 | |
| 1006 | private int[] events = new int[3]; |
| 1007 | |
| 1008 | public void configurationEvent( ConfigurationEvent event ) |
| 1009 | { |
| 1010 | events[event.getType()-1]++; |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | void assertEvents( final int type, final int numEvents ) |
| 1015 | { |
| 1016 | TestCase.assertEquals( "Events of type " + type, numEvents, events[type - 1] ); |
| 1017 | events[type - 1] = 0; |
| 1018 | } |
| 1019 | } |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 1020 | } |