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 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 42 | static |
| 43 | { |
| 44 | // uncomment to enable debugging of this test class |
| 45 | // paxRunnerVmOption = DEBUG_VM_OPTION; |
| 46 | } |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 47 | |
| 48 | @Test |
| 49 | public void test_two_services_same_pid_in_same_bundle_configure_before_registration() throws BundleException |
| 50 | { |
| 51 | final String factoryPid = "test.pid"; |
| 52 | |
| 53 | final Configuration config = createFactoryConfiguration( factoryPid ); |
| 54 | final String pid = config.getPid(); |
| 55 | TestCase.assertEquals( factoryPid, config.getFactoryPid() ); |
| 56 | TestCase.assertNull( config.getBundleLocation() ); |
| 57 | |
| 58 | bundle = installBundle( factoryPid, MultiManagedServiceFactoryTestActivator.class ); |
| 59 | bundle.start(); |
| 60 | |
| 61 | // give cm time for distribution |
| 62 | delay(); |
| 63 | |
| 64 | final MultiManagedServiceFactoryTestActivator tester = MultiManagedServiceFactoryTestActivator.INSTANCE; |
| 65 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 66 | |
| 67 | // assert activater has configuration (two calls, one per pid) |
| 68 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.configs.get( pid ) ); |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 69 | TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceFactoryUpdatedCalls ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 70 | |
| 71 | TestCase.assertEquals( bundle.getLocation(), config.getBundleLocation() ); |
| 72 | |
| 73 | bundle.uninstall(); |
| 74 | bundle = null; |
| 75 | |
| 76 | delay(); |
| 77 | |
| 78 | TestCase.assertNull( config.getBundleLocation() ); |
| 79 | |
| 80 | // remove the configuration for good |
| 81 | deleteConfig( pid ); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | @Test |
| 86 | public void test_two_services_same_pid_in_same_bundle_configure_after_registration() throws BundleException |
| 87 | { |
| 88 | final String factoryPid = "test.pid"; |
| 89 | |
| 90 | bundle = installBundle( factoryPid, MultiManagedServiceFactoryTestActivator.class ); |
| 91 | bundle.start(); |
| 92 | |
| 93 | // give cm time for distribution |
| 94 | delay(); |
| 95 | |
| 96 | final MultiManagedServiceFactoryTestActivator tester = MultiManagedServiceFactoryTestActivator.INSTANCE; |
| 97 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 98 | |
| 99 | // no configuration yet |
| 100 | TestCase.assertTrue( "Expect Properties after Service Registration", tester.configs.isEmpty() ); |
| 101 | TestCase.assertEquals( "Expect two update calls", 0, tester.numManagedServiceFactoryUpdatedCalls ); |
| 102 | |
| 103 | final Configuration config = createFactoryConfiguration( factoryPid ); |
| 104 | final String pid = config.getPid(); |
| 105 | |
| 106 | delay(); |
| 107 | |
| 108 | TestCase.assertEquals( factoryPid, config.getFactoryPid() ); |
| 109 | TestCase.assertEquals( bundle.getLocation(), config.getBundleLocation() ); |
| 110 | |
| 111 | // assert activater has configuration (two calls, one per pid) |
| 112 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.configs.get( pid ) ); |
| 113 | TestCase.assertEquals( "Expect another two single update call", 2, tester.numManagedServiceFactoryUpdatedCalls ); |
| 114 | |
| 115 | bundle.uninstall(); |
| 116 | bundle = null; |
| 117 | |
| 118 | delay(); |
| 119 | |
| 120 | TestCase.assertNull( config.getBundleLocation() ); |
| 121 | |
| 122 | // remove the configuration for good |
| 123 | deleteConfig( pid ); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | @Test |
| 128 | public void test_two_services_same_pid_in_two_bundle_configure_before_registration() throws BundleException |
| 129 | { |
| 130 | Bundle bundle2 = null; |
| 131 | try |
| 132 | { |
| 133 | final String factoryPid = "test.pid"; |
| 134 | final Configuration config = createFactoryConfiguration( factoryPid ); |
| 135 | final String pid = config.getPid(); |
| 136 | |
| 137 | TestCase.assertEquals( factoryPid, config.getFactoryPid() ); |
| 138 | TestCase.assertNull( config.getBundleLocation() ); |
| 139 | |
| 140 | bundle = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class ); |
| 141 | bundle.start(); |
| 142 | |
| 143 | bundle2 = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class ); |
| 144 | bundle2.start(); |
| 145 | |
| 146 | // give cm time for distribution |
| 147 | delay(); |
| 148 | |
| 149 | final ManagedServiceFactoryTestActivator tester = ManagedServiceFactoryTestActivator.INSTANCE; |
| 150 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 151 | |
| 152 | final ManagedServiceFactoryTestActivator2 tester2 = ManagedServiceFactoryTestActivator2.INSTANCE; |
| 153 | TestCase.assertNotNull( "Activator 2 not started !!", tester2 ); |
| 154 | |
| 155 | // expect first activator to have received properties |
| 156 | |
| 157 | // assert first bundle has configuration (two calls, one per srv) |
| 158 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.configs.get( pid ) ); |
| 159 | TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceFactoryUpdatedCalls ); |
| 160 | |
| 161 | // assert second bundle has no configuration |
| 162 | TestCase.assertTrue( tester2.configs.isEmpty() ); |
| 163 | TestCase.assertEquals( 0, tester2.numManagedServiceFactoryUpdatedCalls ); |
| 164 | |
| 165 | // expect configuration bound to first bundle |
| 166 | TestCase.assertEquals( bundle.getLocation(), config.getBundleLocation() ); |
| 167 | |
| 168 | bundle.uninstall(); |
| 169 | bundle = null; |
| 170 | |
| 171 | delay(); |
| 172 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 173 | // expect configuration reassigned |
| 174 | TestCase.assertEquals( bundle2.getLocation(), config.getBundleLocation() ); |
| 175 | TestCase.assertNotNull( "Expect Properties after Configuration Redispatch", tester2.configs.get( pid ) ); |
| 176 | TestCase.assertEquals( "Expect update call after Configuration Redispatch", 1, tester2.numManagedServiceFactoryUpdatedCalls ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 177 | |
| 178 | // remove the configuration for good |
| 179 | deleteConfig( pid ); |
| 180 | } |
| 181 | finally |
| 182 | { |
| 183 | if ( bundle2 != null ) |
| 184 | { |
| 185 | bundle2.uninstall(); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | |
| 191 | @Test |
| 192 | public void test_two_services_same_pid_in_two_bundle_configure_after_registration() throws BundleException |
| 193 | { |
| 194 | Bundle bundle2 = null; |
| 195 | try |
| 196 | { |
| 197 | final String factoryPid = "test.pid"; |
| 198 | |
| 199 | bundle = installBundle( factoryPid, ManagedServiceFactoryTestActivator.class ); |
| 200 | bundle.start(); |
| 201 | |
| 202 | bundle2 = installBundle( factoryPid, ManagedServiceFactoryTestActivator2.class ); |
| 203 | bundle2.start(); |
| 204 | |
| 205 | final ManagedServiceFactoryTestActivator tester = ManagedServiceFactoryTestActivator.INSTANCE; |
| 206 | TestCase.assertNotNull( "Activator not started !!", tester ); |
| 207 | |
| 208 | final ManagedServiceFactoryTestActivator2 tester2 = ManagedServiceFactoryTestActivator2.INSTANCE; |
| 209 | TestCase.assertNotNull( "Activator not started !!", tester2 ); |
| 210 | |
| 211 | delay(); |
| 212 | |
| 213 | // expect no configuration but a call in each service |
| 214 | TestCase.assertTrue( "Expect Properties after Service Registration", tester.configs.isEmpty() ); |
| 215 | TestCase.assertEquals( "Expect a single update call", 0, tester.numManagedServiceFactoryUpdatedCalls ); |
| 216 | TestCase.assertTrue( "Expect Properties after Service Registration", tester2.configs.isEmpty() ); |
| 217 | TestCase.assertEquals( "Expect a single update call", 0, tester2.numManagedServiceFactoryUpdatedCalls ); |
| 218 | |
| 219 | final Configuration config = createFactoryConfiguration( factoryPid ); |
| 220 | final String pid = config.getPid(); |
| 221 | |
| 222 | delay(); |
| 223 | |
| 224 | TestCase.assertEquals( factoryPid, config.getFactoryPid() ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 225 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 226 | TestCase.assertEquals( |
| 227 | "Configuration must be bound to second bundle because the service has higher ranking", |
| 228 | bundle.getLocation(), config.getBundleLocation() ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 229 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 230 | // configuration assigned to the first bundle |
| 231 | TestCase.assertNotNull( "Expect Properties after Service Registration", tester.configs.get( pid ) ); |
| 232 | TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceFactoryUpdatedCalls ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 233 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 234 | TestCase.assertTrue( "Expect Properties after Service Registration", tester2.configs.isEmpty() ); |
| 235 | TestCase.assertEquals( "Expect a single update call", 0, tester2.numManagedServiceFactoryUpdatedCalls ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 236 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 237 | bundle.uninstall(); |
| 238 | bundle = null; |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 239 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 240 | delay(); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 241 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame^] | 242 | // expect configuration reassigned |
| 243 | TestCase.assertEquals( bundle2.getLocation(), config.getBundleLocation() ); |
| 244 | TestCase.assertNotNull( "Expect Properties after Configuration Redispatch", tester2.configs.get( pid ) ); |
| 245 | TestCase.assertEquals( "Expect a single update call after Configuration Redispatch", 1, tester2.numManagedServiceFactoryUpdatedCalls ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 246 | |
| 247 | // remove the configuration for good |
| 248 | deleteConfig( pid ); |
| 249 | } |
| 250 | finally |
| 251 | { |
| 252 | if ( bundle2 != null ) |
| 253 | { |
| 254 | bundle2.uninstall(); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | } |