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 | |
Felix Meschberger | c12db8c | 2009-08-19 06:43:59 +0000 | [diff] [blame] | 22 | import java.io.IOException; |
| 23 | import java.util.Dictionary; |
| 24 | |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 25 | import junit.framework.TestCase; |
| 26 | |
Felix Meschberger | c12db8c | 2009-08-19 06:43:59 +0000 | [diff] [blame] | 27 | import org.apache.felix.cm.integration.helper.ManagedServiceFactoryTestActivator; |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 28 | import org.apache.felix.cm.integration.helper.ManagedServiceTestActivator; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | import org.ops4j.pax.exam.junit.JUnit4TestRunner; |
Felix Meschberger | ca3cd98 | 2009-08-19 19:27:58 +0000 | [diff] [blame] | 32 | import org.osgi.framework.Bundle; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 33 | import org.osgi.framework.BundleException; |
Felix Meschberger | c12db8c | 2009-08-19 06:43:59 +0000 | [diff] [blame] | 34 | import org.osgi.framework.Constants; |
| 35 | import org.osgi.service.cm.Configuration; |
| 36 | import org.osgi.service.cm.ConfigurationAdmin; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | @RunWith(JUnit4TestRunner.class) |
| 40 | public class ConfigurationBaseTest extends ConfigurationTestBase |
| 41 | { |
| 42 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 43 | static |
| 44 | { |
| 45 | // uncomment to enable debugging of this test class |
| 46 | // paxRunnerVmOption = DEBUG_VM_OPTION; |
| 47 | } |
| 48 | |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 49 | @Test |
Felix Meschberger | c12db8c | 2009-08-19 06:43:59 +0000 | [diff] [blame] | 50 | public void test_basic_configuration_configure_then_start() throws BundleException, IOException |
| 51 | { |
| 52 | // 1. create config with pid and locationA |
| 53 | // 2. update config with properties |
| 54 | final String pid = "test_basic_configuration_configure_then_start"; |
| 55 | final Configuration config = configure( pid, null, true ); |
| 56 | |
| 57 | // 3. register ManagedService ms1 with pid from said locationA |
| 58 | bundle = installBundle( pid, ManagedServiceTestActivator.class ); |
| 59 | bundle.start(); |
| 60 | delay(); |
| 61 | |
| 62 | // ==> configuration supplied to the service ms1 |
| 63 | final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE; |
| 64 | TestCase.assertNotNull( tester.props ); |
| 65 | TestCase.assertEquals( pid, tester.props.get( Constants.SERVICE_PID ) ); |
| 66 | TestCase.assertNull( tester.props.get( ConfigurationAdmin.SERVICE_FACTORYPID ) ); |
| 67 | TestCase.assertNull( tester.props.get( ConfigurationAdmin.SERVICE_BUNDLELOCATION ) ); |
| 68 | TestCase.assertEquals( PROP_NAME, tester.props.get( PROP_NAME ) ); |
| 69 | TestCase.assertEquals( 1, tester.numManagedServiceUpdatedCalls ); |
| 70 | |
| 71 | // delete |
| 72 | config.delete(); |
| 73 | delay(); |
| 74 | |
| 75 | // ==> update with null |
| 76 | TestCase.assertNull( tester.props ); |
| 77 | TestCase.assertEquals( 2, tester.numManagedServiceUpdatedCalls ); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | @Test |
Felix Meschberger | e3c0e4b | 2010-09-08 12:07:47 +0000 | [diff] [blame] | 82 | public void test_basic_configuration_strange_pid() throws BundleException, IOException |
| 83 | { |
| 84 | // 1. create config with pid and locationA |
| 85 | // 2. update config with properties |
| 86 | final String pid = "pid with blanks and stuff %\"'"; |
| 87 | theConfig.put( pid, pid ); |
| 88 | final Configuration config = configure( pid, null, true ); |
| 89 | theConfig.remove( pid ); |
| 90 | |
| 91 | // 3. register ManagedService ms1 with pid from said locationA |
| 92 | bundle = installBundle( pid, ManagedServiceTestActivator.class ); |
| 93 | bundle.start(); |
| 94 | delay(); |
| 95 | |
| 96 | // ==> configuration supplied to the service ms1 |
| 97 | final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE; |
| 98 | TestCase.assertNotNull( tester.props ); |
| 99 | TestCase.assertEquals( pid, tester.props.get( Constants.SERVICE_PID ) ); |
| 100 | TestCase.assertNull( tester.props.get( ConfigurationAdmin.SERVICE_FACTORYPID ) ); |
| 101 | TestCase.assertNull( tester.props.get( ConfigurationAdmin.SERVICE_BUNDLELOCATION ) ); |
| 102 | TestCase.assertEquals( PROP_NAME, tester.props.get( PROP_NAME ) ); |
| 103 | TestCase.assertEquals( pid, tester.props.get( pid ) ); |
| 104 | TestCase.assertEquals( 1, tester.numManagedServiceUpdatedCalls ); |
| 105 | |
| 106 | // delete |
| 107 | config.delete(); |
| 108 | delay(); |
| 109 | |
| 110 | // ==> update with null |
| 111 | TestCase.assertNull( tester.props ); |
| 112 | TestCase.assertEquals( 2, tester.numManagedServiceUpdatedCalls ); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | @Test |
Felix Meschberger | c12db8c | 2009-08-19 06:43:59 +0000 | [diff] [blame] | 117 | public void test_basic_configuration_start_then_configure() throws BundleException, IOException |
| 118 | { |
| 119 | final String pid = "test_basic_configuration_start_then_configure"; |
| 120 | |
| 121 | // 1. register ManagedService ms1 with pid from said locationA |
| 122 | bundle = installBundle( pid, ManagedServiceTestActivator.class ); |
| 123 | bundle.start(); |
| 124 | delay(); |
| 125 | |
| 126 | // 1. create config with pid and locationA |
| 127 | // 2. update config with properties |
| 128 | final Configuration config = configure( pid, null, true ); |
| 129 | delay(); |
| 130 | |
| 131 | // ==> configuration supplied to the service ms1 |
| 132 | final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE; |
| 133 | TestCase.assertNotNull( tester.props ); |
| 134 | TestCase.assertEquals( pid, tester.props.get( Constants.SERVICE_PID ) ); |
| 135 | TestCase.assertNull( tester.props.get( ConfigurationAdmin.SERVICE_FACTORYPID ) ); |
| 136 | TestCase.assertNull( tester.props.get( ConfigurationAdmin.SERVICE_BUNDLELOCATION ) ); |
| 137 | TestCase.assertEquals( PROP_NAME, tester.props.get( PROP_NAME ) ); |
| 138 | TestCase.assertEquals( 2, tester.numManagedServiceUpdatedCalls ); |
| 139 | |
| 140 | // delete |
| 141 | config.delete(); |
| 142 | delay(); |
| 143 | |
| 144 | // ==> update with null |
| 145 | TestCase.assertNull( tester.props ); |
| 146 | TestCase.assertEquals( 3, tester.numManagedServiceUpdatedCalls ); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | @Test |
| 151 | public void test_basic_configuration_factory_configure_then_start() throws BundleException, IOException |
| 152 | { |
| 153 | final String factoryPid = "test_basic_configuration_factory_configure_then_start"; |
| 154 | bundle = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class ); |
| 155 | bundle.start(); |
| 156 | delay(); |
| 157 | |
| 158 | final Configuration config = createFactoryConfiguration( factoryPid, null, true ); |
| 159 | final String pid = config.getPid(); |
| 160 | delay(); |
| 161 | |
| 162 | // ==> configuration supplied to the service ms1 |
| 163 | final ManagedServiceFactoryTestActivator tester = ManagedServiceFactoryTestActivator.INSTANCE; |
| 164 | Dictionary<?, ?> props = tester.configs.get( pid ); |
| 165 | TestCase.assertNotNull( props ); |
| 166 | TestCase.assertEquals( pid, props.get( Constants.SERVICE_PID ) ); |
| 167 | TestCase.assertEquals( factoryPid, props.get( ConfigurationAdmin.SERVICE_FACTORYPID ) ); |
| 168 | TestCase.assertNull( props.get( ConfigurationAdmin.SERVICE_BUNDLELOCATION ) ); |
| 169 | TestCase.assertEquals( PROP_NAME, props.get( PROP_NAME ) ); |
| 170 | TestCase.assertEquals( 0, tester.numManagedServiceUpdatedCalls ); |
| 171 | TestCase.assertEquals( 1, tester.numManagedServiceFactoryUpdatedCalls ); |
| 172 | TestCase.assertEquals( 0, tester.numManagedServiceFactoryDeleteCalls ); |
| 173 | |
| 174 | // delete |
| 175 | config.delete(); |
| 176 | delay(); |
| 177 | |
| 178 | // ==> update with null |
Felix Meschberger | ca3cd98 | 2009-08-19 19:27:58 +0000 | [diff] [blame] | 179 | TestCase.assertNull( tester.configs.get( pid ) ); |
Felix Meschberger | c12db8c | 2009-08-19 06:43:59 +0000 | [diff] [blame] | 180 | TestCase.assertEquals( 0, tester.numManagedServiceUpdatedCalls ); |
| 181 | TestCase.assertEquals( 1, tester.numManagedServiceFactoryUpdatedCalls ); |
| 182 | TestCase.assertEquals( 1, tester.numManagedServiceFactoryDeleteCalls ); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | @Test |
| 187 | public void test_basic_configuration_factory_start_then_configure() throws BundleException, IOException |
| 188 | { |
| 189 | // 1. create config with pid and locationA |
| 190 | // 2. update config with properties |
| 191 | final String factoryPid = "test_basic_configuration_factory_start_then_configure"; |
| 192 | final Configuration config = createFactoryConfiguration( factoryPid, null, true ); |
| 193 | final String pid = config.getPid(); |
| 194 | |
| 195 | // 3. register ManagedService ms1 with pid from said locationA |
| 196 | bundle = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class ); |
| 197 | bundle.start(); |
| 198 | delay(); |
| 199 | |
| 200 | // ==> configuration supplied to the service ms1 |
| 201 | final ManagedServiceFactoryTestActivator tester = ManagedServiceFactoryTestActivator.INSTANCE; |
| 202 | Dictionary<?, ?> props = tester.configs.get( pid ); |
| 203 | TestCase.assertNotNull( props ); |
| 204 | TestCase.assertEquals( pid, props.get( Constants.SERVICE_PID ) ); |
| 205 | TestCase.assertEquals( factoryPid, props.get( ConfigurationAdmin.SERVICE_FACTORYPID ) ); |
| 206 | TestCase.assertNull( props.get( ConfigurationAdmin.SERVICE_BUNDLELOCATION ) ); |
| 207 | TestCase.assertEquals( PROP_NAME, props.get( PROP_NAME ) ); |
| 208 | TestCase.assertEquals( 0, tester.numManagedServiceUpdatedCalls ); |
| 209 | TestCase.assertEquals( 1, tester.numManagedServiceFactoryUpdatedCalls ); |
| 210 | TestCase.assertEquals( 0, tester.numManagedServiceFactoryDeleteCalls ); |
| 211 | |
| 212 | // delete |
| 213 | config.delete(); |
| 214 | delay(); |
| 215 | |
| 216 | // ==> update with null |
Felix Meschberger | ca3cd98 | 2009-08-19 19:27:58 +0000 | [diff] [blame] | 217 | TestCase.assertNull( tester.configs.get( pid ) ); |
Felix Meschberger | c12db8c | 2009-08-19 06:43:59 +0000 | [diff] [blame] | 218 | TestCase.assertEquals( 0, tester.numManagedServiceUpdatedCalls ); |
| 219 | TestCase.assertEquals( 1, tester.numManagedServiceFactoryUpdatedCalls ); |
| 220 | TestCase.assertEquals( 1, tester.numManagedServiceFactoryDeleteCalls ); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | @Test |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 225 | public void test_start_bundle_configure_stop_start_bundle() throws BundleException |
| 226 | { |
| 227 | String pid = "test_start_bundle_configure_stop_start_bundle"; |
| 228 | |
| 229 | // start the bundle and assert this |
| 230 | bundle = installBundle( pid ); |
| 231 | bundle.start(); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 232 | final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 233 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 234 | |
| 235 | // give cm time for distribution |
| 236 | delay(); |
| 237 | |
| 238 | // assert activater has no configuration |
| 239 | TestCase.assertNull( "Expect no Properties after Service Registration", tester.props ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 240 | TestCase.assertEquals( "Expect no update call", 1, tester.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 241 | |
| 242 | // configure after ManagedServiceRegistration --> configure via update |
| 243 | configure( pid ); |
| 244 | delay(); |
| 245 | |
| 246 | // assert activater has configuration |
| 247 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 248 | TestCase.assertEquals( "Expect a single update call", 2, tester.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 249 | |
| 250 | // stop the bundle now |
| 251 | bundle.stop(); |
| 252 | |
| 253 | // assert INSTANCE is null |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 254 | TestCase.assertNull( ManagedServiceTestActivator.INSTANCE ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 255 | |
| 256 | delay(); |
| 257 | |
| 258 | // start the bundle again (and check) |
| 259 | bundle.start(); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 260 | final ManagedServiceTestActivator tester2 = ManagedServiceTestActivator.INSTANCE; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 261 | TestCase.assertNotNull( "Activator not started the second time!!", tester2 ); |
| 262 | TestCase.assertNotSame( "Instances must not be the same", tester, tester2 ); |
| 263 | |
| 264 | // give cm time for distribution |
| 265 | delay(); |
| 266 | |
| 267 | // assert activater has configuration |
| 268 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester2.props ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 269 | TestCase.assertEquals( "Expect a second update call", 1, tester2.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 270 | |
| 271 | // cleanup |
| 272 | bundle.uninstall(); |
| 273 | bundle = null; |
| 274 | |
| 275 | // remove the configuration for good |
| 276 | deleteConfig( pid ); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | @Test |
| 281 | public void test_configure_start_bundle_stop_start_bundle() throws BundleException |
| 282 | { |
| 283 | String pid = "test_configure_start_bundle_stop_start_bundle"; |
| 284 | configure( pid ); |
| 285 | |
| 286 | // start the bundle and assert this |
| 287 | bundle = installBundle( pid ); |
| 288 | bundle.start(); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 289 | final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 290 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 291 | |
| 292 | // give cm time for distribution |
| 293 | delay(); |
| 294 | |
| 295 | // assert activater has configuration |
| 296 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 297 | TestCase.assertEquals( "Expect no update call", 1, tester.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 298 | |
| 299 | // stop the bundle now |
| 300 | bundle.stop(); |
| 301 | |
| 302 | // assert INSTANCE is null |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 303 | TestCase.assertNull( ManagedServiceTestActivator.INSTANCE ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 304 | |
| 305 | delay(); |
| 306 | |
| 307 | // start the bundle again (and check) |
| 308 | bundle.start(); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 309 | final ManagedServiceTestActivator tester2 = ManagedServiceTestActivator.INSTANCE; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 310 | TestCase.assertNotNull( "Activator not started the second time!!", tester2 ); |
| 311 | TestCase.assertNotSame( "Instances must not be the same", tester, tester2 ); |
| 312 | |
| 313 | // give cm time for distribution |
| 314 | delay(); |
| 315 | |
| 316 | // assert activater has configuration |
| 317 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester2.props ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 318 | TestCase.assertEquals( "Expect a second update call", 1, tester2.numManagedServiceUpdatedCalls ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 319 | |
| 320 | // cleanup |
| 321 | bundle.uninstall(); |
| 322 | bundle = null; |
| 323 | |
| 324 | // remove the configuration for good |
| 325 | deleteConfig( pid ); |
| 326 | } |
Felix Meschberger | ca3cd98 | 2009-08-19 19:27:58 +0000 | [diff] [blame] | 327 | |
| 328 | |
| 329 | @Test |
| 330 | public void test_listConfiguration() throws BundleException, IOException |
| 331 | { |
| 332 | // 1. create a new Conf1 with pid1 and null location. |
| 333 | // 2. Conf1#update(props) is called. |
| 334 | final String pid = "test_listConfiguration"; |
| 335 | final Configuration config = configure( pid, null, true ); |
| 336 | |
| 337 | // 3. bundleA will locationA registers ManagedServiceA with pid1. |
| 338 | bundle = installBundle( pid ); |
| 339 | bundle.start(); |
| 340 | delay(); |
| 341 | |
| 342 | // ==> ManagedServiceA is called back. |
| 343 | final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE; |
| 344 | TestCase.assertNotNull( tester ); |
| 345 | TestCase.assertNotNull( tester.props ); |
| 346 | TestCase.assertEquals( 1, tester.numManagedServiceUpdatedCalls ); |
| 347 | |
| 348 | // 4. bundleA is stopped but *NOT uninstalled*. |
| 349 | bundle.stop(); |
| 350 | delay(); |
| 351 | |
| 352 | // 5. test bundle calls cm.listConfigurations(null). |
| 353 | final Configuration listed = getConfiguration( pid ); |
| 354 | |
| 355 | // ==> Conf1 is included in the returned list and |
| 356 | // it has locationA. |
| 357 | // (In debug mode, dynamicBundleLocation==locationA |
| 358 | // and staticBundleLocation==null) |
| 359 | TestCase.assertNotNull( listed ); |
| 360 | TestCase.assertEquals( bundle.getLocation(), listed.getBundleLocation() ); |
| 361 | |
| 362 | // 6. test bundle calls cm.getConfiguration(pid1) |
| 363 | final Configuration get = getConfigurationAdmin().getConfiguration( pid ); |
| 364 | TestCase.assertEquals( bundle.getLocation(), get.getBundleLocation() ); |
| 365 | |
| 366 | final Bundle cmBundle = getCmBundle(); |
| 367 | cmBundle.stop(); |
| 368 | delay(); |
| 369 | cmBundle.start(); |
| 370 | delay(); |
| 371 | |
| 372 | // 5. test bundle calls cm.listConfigurations(null). |
| 373 | final Configuration listed2 = getConfiguration( pid ); |
| 374 | |
| 375 | // ==> Conf1 is included in the returned list and |
| 376 | // it has locationA. |
| 377 | // (In debug mode, dynamicBundleLocation==locationA |
| 378 | // and staticBundleLocation==null) |
| 379 | TestCase.assertNotNull( listed2 ); |
| 380 | TestCase.assertEquals( bundle.getLocation(), listed2.getBundleLocation() ); |
| 381 | |
| 382 | // 6. test bundle calls cm.getConfiguration(pid1) |
| 383 | final Configuration get2 = getConfigurationAdmin().getConfiguration( pid ); |
| 384 | TestCase.assertEquals( bundle.getLocation(), get2.getBundleLocation() ); |
| 385 | } |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 386 | } |