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