Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +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 junit.framework.TestCase; |
| 23 | |
| 24 | import org.apache.felix.cm.integration.helper.ManagedServiceFactoryTestActivator; |
| 25 | import org.apache.felix.cm.integration.helper.ManagedServiceFactoryTestActivator2; |
| 26 | import org.apache.felix.cm.integration.helper.MultiManagedServiceFactoryTestActivator; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | import org.ops4j.pax.exam.junit.JUnit4TestRunner; |
| 30 | import org.osgi.framework.Bundle; |
| 31 | import org.osgi.framework.BundleException; |
| 32 | import org.osgi.service.cm.Configuration; |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * The <code>MultiServicePIDTest</code> tests the case of multiple services |
| 37 | * bound with the same PID |
| 38 | */ |
| 39 | @RunWith(JUnit4TestRunner.class) |
| 40 | public class MultiServiceFactoryPIDTest extends ConfigurationTestBase |
| 41 | { |
| 42 | |
| 43 | @Test |
| 44 | public void test_two_services_same_pid_in_same_bundle_configure_before_registration() throws BundleException |
| 45 | { |
| 46 | final String factoryPid = "test.pid"; |
| 47 | |
| 48 | final Configuration config = createFactoryConfiguration( factoryPid ); |
| 49 | final String pid = config.getPid(); |
| 50 | TestCase.assertEquals( factoryPid, config.getFactoryPid() ); |
| 51 | TestCase.assertNull( config.getBundleLocation() ); |
| 52 | |
| 53 | bundle = installBundle( factoryPid, MultiManagedServiceFactoryTestActivator.class ); |
| 54 | bundle.start(); |
| 55 | |
| 56 | // give cm time for distribution |
| 57 | delay(); |
| 58 | |
| 59 | final MultiManagedServiceFactoryTestActivator tester = MultiManagedServiceFactoryTestActivator.INSTANCE; |
| 60 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 61 | |
| 62 | // assert activater has configuration (two calls, one per pid) |
| 63 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.configs.get( pid ) ); |
| 64 | TestCase.assertEquals( "Expect a single update call", 2, tester.numManagedServiceFactoryUpdatedCalls ); |
| 65 | |
| 66 | TestCase.assertEquals( bundle.getLocation(), config.getBundleLocation() ); |
| 67 | |
| 68 | bundle.uninstall(); |
| 69 | bundle = null; |
| 70 | |
| 71 | delay(); |
| 72 | |
| 73 | TestCase.assertNull( config.getBundleLocation() ); |
| 74 | |
| 75 | // remove the configuration for good |
| 76 | deleteConfig( pid ); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | @Test |
| 81 | public void test_two_services_same_pid_in_same_bundle_configure_after_registration() throws BundleException |
| 82 | { |
| 83 | final String factoryPid = "test.pid"; |
| 84 | |
| 85 | bundle = installBundle( factoryPid, MultiManagedServiceFactoryTestActivator.class ); |
| 86 | bundle.start(); |
| 87 | |
| 88 | // give cm time for distribution |
| 89 | delay(); |
| 90 | |
| 91 | final MultiManagedServiceFactoryTestActivator tester = MultiManagedServiceFactoryTestActivator.INSTANCE; |
| 92 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 93 | |
| 94 | // no configuration yet |
| 95 | TestCase.assertTrue( "Expect Properties after Service Registration", tester.configs.isEmpty() ); |
| 96 | TestCase.assertEquals( "Expect two update calls", 0, tester.numManagedServiceFactoryUpdatedCalls ); |
| 97 | |
| 98 | final Configuration config = createFactoryConfiguration( factoryPid ); |
| 99 | final String pid = config.getPid(); |
| 100 | |
| 101 | delay(); |
| 102 | |
| 103 | TestCase.assertEquals( factoryPid, config.getFactoryPid() ); |
| 104 | TestCase.assertEquals( bundle.getLocation(), config.getBundleLocation() ); |
| 105 | |
| 106 | // assert activater has configuration (two calls, one per pid) |
| 107 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.configs.get( pid ) ); |
| 108 | TestCase.assertEquals( "Expect another two single update call", 2, tester.numManagedServiceFactoryUpdatedCalls ); |
| 109 | |
| 110 | bundle.uninstall(); |
| 111 | bundle = null; |
| 112 | |
| 113 | delay(); |
| 114 | |
| 115 | TestCase.assertNull( config.getBundleLocation() ); |
| 116 | |
| 117 | // remove the configuration for good |
| 118 | deleteConfig( pid ); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | @Test |
| 123 | public void test_two_services_same_pid_in_two_bundle_configure_before_registration() throws BundleException |
| 124 | { |
| 125 | Bundle bundle2 = null; |
| 126 | try |
| 127 | { |
| 128 | final String factoryPid = "test.pid"; |
| 129 | final Configuration config = createFactoryConfiguration( factoryPid ); |
| 130 | final String pid = config.getPid(); |
| 131 | |
| 132 | TestCase.assertEquals( factoryPid, config.getFactoryPid() ); |
| 133 | TestCase.assertNull( config.getBundleLocation() ); |
| 134 | |
| 135 | bundle = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class ); |
| 136 | bundle.start(); |
| 137 | |
| 138 | bundle2 = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class ); |
| 139 | bundle2.start(); |
| 140 | |
| 141 | // give cm time for distribution |
| 142 | delay(); |
| 143 | |
| 144 | final ManagedServiceFactoryTestActivator tester = ManagedServiceFactoryTestActivator.INSTANCE; |
| 145 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 146 | |
| 147 | final ManagedServiceFactoryTestActivator2 tester2 = ManagedServiceFactoryTestActivator2.INSTANCE; |
| 148 | TestCase.assertNotNull( "Activator 2 not started !!", tester2 ); |
| 149 | |
| 150 | // expect first activator to have received properties |
| 151 | |
| 152 | // assert first bundle has configuration (two calls, one per srv) |
| 153 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.configs.get( pid ) ); |
| 154 | TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceFactoryUpdatedCalls ); |
| 155 | |
| 156 | // assert second bundle has no configuration |
| 157 | TestCase.assertTrue( tester2.configs.isEmpty() ); |
| 158 | TestCase.assertEquals( 0, tester2.numManagedServiceFactoryUpdatedCalls ); |
| 159 | |
| 160 | // expect configuration bound to first bundle |
| 161 | TestCase.assertEquals( bundle.getLocation(), config.getBundleLocation() ); |
| 162 | |
| 163 | bundle.uninstall(); |
| 164 | bundle = null; |
| 165 | |
| 166 | delay(); |
| 167 | |
Felix Meschberger | 85bb47c | 2009-08-20 07:50:38 +0000 | [diff] [blame] | 168 | /* |
| 169 | * According to BJ Hargrave configuration is not re-dispatched |
| 170 | * due to setting the bundle location. |
| 171 | * <p> |
| 172 | * Therefore, we have two sets one with re-dispatch expectation and |
| 173 | * one without re-dispatch expectation. |
| 174 | */ |
| 175 | if ( REDISPATCH_CONFIGURATION_ON_SET_BUNDLE_LOCATION ) |
| 176 | { |
| 177 | // expect configuration reassigned |
| 178 | TestCase.assertEquals( bundle2.getLocation(), config.getBundleLocation() ); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | // expected configuration unbound |
| 183 | TestCase.assertNull( config.getBundleLocation() ); |
| 184 | } |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 185 | |
| 186 | // remove the configuration for good |
| 187 | deleteConfig( pid ); |
| 188 | } |
| 189 | finally |
| 190 | { |
| 191 | if ( bundle2 != null ) |
| 192 | { |
| 193 | bundle2.uninstall(); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | |
| 199 | @Test |
| 200 | public void test_two_services_same_pid_in_two_bundle_configure_after_registration() throws BundleException |
| 201 | { |
| 202 | Bundle bundle2 = null; |
| 203 | try |
| 204 | { |
| 205 | final String factoryPid = "test.pid"; |
| 206 | |
| 207 | bundle = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class ); |
| 208 | bundle.start(); |
| 209 | |
| 210 | bundle2 = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class ); |
| 211 | bundle2.start(); |
| 212 | |
| 213 | final ManagedServiceFactoryTestActivator tester = ManagedServiceFactoryTestActivator.INSTANCE; |
| 214 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 215 | |
| 216 | final ManagedServiceFactoryTestActivator2 tester2 = ManagedServiceFactoryTestActivator2.INSTANCE; |
| 217 | TestCase.assertNotNull( "Activator not started !!", tester2 ); |
| 218 | |
| 219 | delay(); |
| 220 | |
| 221 | // expect no configuration but a call in each service |
| 222 | TestCase.assertTrue( "Expect Properties after Service Registration", tester.configs.isEmpty() ); |
| 223 | TestCase.assertEquals( "Expect a single update call", 0, tester.numManagedServiceFactoryUpdatedCalls ); |
| 224 | TestCase.assertTrue( "Expect Properties after Service Registration", tester2.configs.isEmpty() ); |
| 225 | TestCase.assertEquals( "Expect a single update call", 0, tester2.numManagedServiceFactoryUpdatedCalls ); |
| 226 | |
| 227 | final Configuration config = createFactoryConfiguration( factoryPid ); |
| 228 | final String pid = config.getPid(); |
| 229 | |
| 230 | delay(); |
| 231 | |
| 232 | TestCase.assertEquals( factoryPid, config.getFactoryPid() ); |
| 233 | TestCase.assertNotNull( config.getBundleLocation() ); |
| 234 | |
| 235 | if ( bundle.getLocation().equals( config.getBundleLocation() ) ) |
| 236 | { |
| 237 | // configuration assigned to the first bundle |
| 238 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.configs.get( pid ) ); |
| 239 | TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceFactoryUpdatedCalls ); |
| 240 | |
| 241 | TestCase.assertTrue( "Expect Properties after Service Registration", tester2.configs.isEmpty() ); |
| 242 | TestCase.assertEquals( "Expect a single update call", 0, tester2.numManagedServiceFactoryUpdatedCalls ); |
| 243 | |
| 244 | bundle.uninstall(); |
| 245 | bundle = null; |
| 246 | |
| 247 | delay(); |
| 248 | |
Felix Meschberger | 85bb47c | 2009-08-20 07:50:38 +0000 | [diff] [blame] | 249 | /* |
| 250 | * According to BJ Hargrave configuration is not re-dispatched |
| 251 | * due to setting the bundle location. |
| 252 | * <p> |
| 253 | * Therefore, we have two sets one with re-dispatch expectation and |
| 254 | * one without re-dispatch expectation. |
| 255 | */ |
| 256 | if ( REDISPATCH_CONFIGURATION_ON_SET_BUNDLE_LOCATION ) |
| 257 | { |
| 258 | // expect configuration reassigned |
| 259 | TestCase.assertEquals( bundle2.getLocation(), config.getBundleLocation() ); |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | // expected configuration unbound |
| 264 | TestCase.assertNull( config.getBundleLocation() ); |
| 265 | } |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 266 | } |
| 267 | else if ( bundle2.getLocation().equals( config.getBundleLocation() ) ) |
| 268 | { |
| 269 | // configuration assigned to the second bundle |
| 270 | // assert activater has configuration (two calls, one per pid) |
| 271 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester2.configs.get( pid ) ); |
| 272 | TestCase.assertEquals( "Expect a single update call", 1, tester2.numManagedServiceFactoryUpdatedCalls ); |
| 273 | |
| 274 | TestCase.assertTrue( "Expect Properties after Service Registration", tester.configs.isEmpty() ); |
| 275 | TestCase.assertEquals( "Expect a single update call", 0, tester.numManagedServiceFactoryUpdatedCalls ); |
| 276 | |
| 277 | bundle2.uninstall(); |
| 278 | bundle2 = null; |
| 279 | |
| 280 | delay(); |
| 281 | |
Felix Meschberger | 85bb47c | 2009-08-20 07:50:38 +0000 | [diff] [blame] | 282 | /* |
| 283 | * According to BJ Hargrave configuration is not re-dispatched |
| 284 | * due to setting the bundle location. |
| 285 | * <p> |
| 286 | * Therefore, we have two sets one with re-dispatch expectation and |
| 287 | * one without re-dispatch expectation. |
| 288 | */ |
| 289 | if ( REDISPATCH_CONFIGURATION_ON_SET_BUNDLE_LOCATION ) |
| 290 | { |
| 291 | // expect configuration reassigned |
| 292 | TestCase.assertEquals( bundle.getLocation(), config.getBundleLocation() ); |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | // expected configuration unbound |
| 297 | TestCase.assertNull( config.getBundleLocation() ); |
| 298 | } |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 299 | } |
| 300 | else |
| 301 | { |
| 302 | // configuration assigned to some other bundle ... fail |
| 303 | TestCase.fail( "Configuration assigned to unexpected bundle " + config.getBundleLocation() ); |
| 304 | } |
| 305 | |
| 306 | // remove the configuration for good |
| 307 | deleteConfig( pid ); |
| 308 | } |
| 309 | finally |
| 310 | { |
| 311 | if ( bundle2 != null ) |
| 312 | { |
| 313 | bundle2.uninstall(); |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | } |