blob: ccdf85628c4a1ae458f0668c703517cad7fa3418 [file] [log] [blame]
Felix Meschberger8659e392009-08-19 05:46:49 +00001/*
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 */
19package org.apache.felix.cm.integration;
20
21
22import junit.framework.TestCase;
23
24import org.apache.felix.cm.integration.helper.ManagedServiceTestActivator;
25import org.apache.felix.cm.integration.helper.ManagedServiceTestActivator2;
26import org.apache.felix.cm.integration.helper.MultiManagedServiceTestActivator;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.ops4j.pax.exam.junit.JUnit4TestRunner;
30import org.osgi.framework.Bundle;
31import org.osgi.framework.BundleException;
32import 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)
40public class MultiServicePIDTest extends ConfigurationTestBase
41{
Felix Meschberger007c50e2011-10-20 12:39:38 +000042 static
43 {
44 // uncomment to enable debugging of this test class
45 // paxRunnerVmOption = DEBUG_VM_OPTION;
46 }
Felix Meschberger8659e392009-08-19 05:46:49 +000047
48 @Test
49 public void test_two_services_same_pid_in_same_bundle_configure_before_registration() throws BundleException
50 {
51 final String pid = "test.pid";
52
53 configure( pid );
54
55 final Configuration config = getConfiguration( pid );
56 TestCase.assertEquals( pid, config.getPid() );
57 TestCase.assertNull( config.getBundleLocation() );
58
59 bundle = installBundle( pid, MultiManagedServiceTestActivator.class );
60 bundle.start();
61
62 // give cm time for distribution
63 delay();
64
65 final MultiManagedServiceTestActivator tester = MultiManagedServiceTestActivator.INSTANCE;
66 TestCase.assertNotNull( "Activator not started !!", tester );
67
68 // assert activater has configuration (two calls, one per pid)
69 TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props );
70 TestCase.assertEquals( "Expect a single update call", 2, tester.numManagedServiceUpdatedCalls );
71
72 TestCase.assertEquals( bundle.getLocation(), config.getBundleLocation() );
73
74 bundle.uninstall();
75 bundle = null;
76
77 delay();
78
79 TestCase.assertNull( config.getBundleLocation() );
80
81 // remove the configuration for good
82 deleteConfig( pid );
83 }
84
85
86 @Test
87 public void test_two_services_same_pid_in_same_bundle_configure_after_registration() throws BundleException
88 {
89 final String pid = "test.pid";
90
91 bundle = installBundle( pid, MultiManagedServiceTestActivator.class );
92 bundle.start();
93
94 // give cm time for distribution
95 delay();
96
97 final MultiManagedServiceTestActivator tester = MultiManagedServiceTestActivator.INSTANCE;
98 TestCase.assertNotNull( "Activator not started !!", tester );
99
100 // no configuration yet
101 TestCase.assertNull( "Expect Properties after Service Registration", tester.props );
102 TestCase.assertEquals( "Expect two update calls", 2, tester.numManagedServiceUpdatedCalls );
103
104 configure( pid );
105 delay();
106
107 final Configuration config = getConfiguration( pid );
108 TestCase.assertEquals( pid, config.getPid() );
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.props );
113 TestCase.assertEquals( "Expect another two single update call", 4, tester.numManagedServiceUpdatedCalls );
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 pid = "test.pid";
134
135 configure( pid );
136
137 final Configuration config = getConfiguration( pid );
138 TestCase.assertEquals( pid, config.getPid() );
139 TestCase.assertNull( config.getBundleLocation() );
140
141 bundle = installBundle( pid, ManagedServiceTestActivator.class );
142 bundle.start();
143
144 bundle2 = installBundle( pid, ManagedServiceTestActivator2.class );
145 bundle2.start();
146
147 // give cm time for distribution
148 delay();
149
150 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
151 TestCase.assertNotNull( "Activator not started !!", tester );
152
153 final ManagedServiceTestActivator2 tester2 = ManagedServiceTestActivator2.INSTANCE;
154 TestCase.assertNotNull( "Activator 2 not started !!", tester2 );
155
156 // expect first activator to have received properties
157
Felix Meschberger007c50e2011-10-20 12:39:38 +0000158 // assert first bundle has configuration (one calls, one per srv)
Felix Meschberger8659e392009-08-19 05:46:49 +0000159 TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props );
160 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
161
Felix Meschberger55d0a822011-11-16 12:07:17 +0000162 // assert second bundle has no configuration (but called with null)
Felix Meschberger8659e392009-08-19 05:46:49 +0000163 TestCase.assertNull( tester2.props );
Felix Meschberger55d0a822011-11-16 12:07:17 +0000164 TestCase.assertEquals( 1, tester2.numManagedServiceUpdatedCalls );
Felix Meschberger8659e392009-08-19 05:46:49 +0000165
166 // expect configuration bound to first bundle
167 TestCase.assertEquals( bundle.getLocation(), config.getBundleLocation() );
168
169 bundle.uninstall();
170 bundle = null;
171
172 delay();
173
Felix Meschberger007c50e2011-10-20 12:39:38 +0000174 // after uninstallation, the configuration is redispatched
175 // due to the dynamic binding being removed
176
177 // expect configuration reassigned
178 TestCase.assertEquals( bundle2.getLocation(), config.getBundleLocation() );
179
180 // assert second bundle now has the configuration
181 TestCase.assertNotNull( "Expect Properties after Configuration redispatch", tester2.props );
Felix Meschberger55d0a822011-11-16 12:07:17 +0000182 TestCase.assertEquals( "Expect a single update call after Configuration redispatch", 2,
Felix Meschberger007c50e2011-10-20 12:39:38 +0000183 tester2.numManagedServiceUpdatedCalls );
Felix Meschberger8659e392009-08-19 05:46:49 +0000184
185 // remove the configuration for good
186 deleteConfig( pid );
187 }
188 finally
189 {
190 if ( bundle2 != null )
191 {
192 bundle2.uninstall();
193 }
194 }
195 }
196
197
198 @Test
199 public void test_two_services_same_pid_in_two_bundle_configure_after_registration() throws BundleException
200 {
201 Bundle bundle2 = null;
202 try
203 {
204 final String pid = "test.pid";
205
206 bundle = installBundle( pid, ManagedServiceTestActivator.class );
207 bundle.start();
208
209 bundle2 = installBundle( pid, ManagedServiceTestActivator2.class );
210 bundle2.start();
211
212 final ManagedServiceTestActivator tester = ManagedServiceTestActivator.INSTANCE;
213 TestCase.assertNotNull( "Activator not started !!", tester );
214
215 final ManagedServiceTestActivator2 tester2 = ManagedServiceTestActivator2.INSTANCE;
216 TestCase.assertNotNull( "Activator not started !!", tester2 );
217
218 delay();
219
220 // expect no configuration but a call in each service
221 TestCase.assertNull( "Expect Properties after Service Registration", tester.props );
222 TestCase.assertEquals( "Expect a single update call", 1, tester.numManagedServiceUpdatedCalls );
223 TestCase.assertNull( "Expect Properties after Service Registration", tester2.props );
224 TestCase.assertEquals( "Expect a single update call", 1, tester2.numManagedServiceUpdatedCalls );
225
226 configure( pid );
227
228 delay();
229
230 final Configuration config = getConfiguration( pid );
231 TestCase.assertEquals( pid, config.getPid() );
Felix Meschberger8659e392009-08-19 05:46:49 +0000232
Felix Meschberger007c50e2011-10-20 12:39:38 +0000233 TestCase.assertEquals(
234 "Configuration must be bound to first bundle because the service has higher ranking",
235 bundle.getLocation(), config.getBundleLocation() );
Felix Meschberger8659e392009-08-19 05:46:49 +0000236
Felix Meschberger007c50e2011-10-20 12:39:38 +0000237 // configuration assigned to the first bundle
238 TestCase.assertNotNull( "Expect Properties after Service Registration", tester.props );
239 TestCase.assertEquals( "Expect a single update call", 2, tester.numManagedServiceUpdatedCalls );
Felix Meschberger8659e392009-08-19 05:46:49 +0000240
Felix Meschberger007c50e2011-10-20 12:39:38 +0000241 TestCase.assertNull( "Expect Properties after Service Registration", tester2.props );
242 TestCase.assertEquals( "Expect a single update call", 1, tester2.numManagedServiceUpdatedCalls );
Felix Meschberger8659e392009-08-19 05:46:49 +0000243
Felix Meschberger007c50e2011-10-20 12:39:38 +0000244 bundle.uninstall();
245 bundle = null;
Felix Meschberger8659e392009-08-19 05:46:49 +0000246
Felix Meschberger007c50e2011-10-20 12:39:38 +0000247 delay();
Felix Meschberger8659e392009-08-19 05:46:49 +0000248
Felix Meschberger007c50e2011-10-20 12:39:38 +0000249 // after uninstallation, the configuration is redispatched
250 // due to the dynamic binding being removed
Felix Meschberger8659e392009-08-19 05:46:49 +0000251
Felix Meschberger007c50e2011-10-20 12:39:38 +0000252 // expect configuration reassigned
253 TestCase.assertEquals( bundle2.getLocation(), config.getBundleLocation() );
Felix Meschberger8659e392009-08-19 05:46:49 +0000254
Felix Meschberger007c50e2011-10-20 12:39:38 +0000255 // assert second bundle now has the configuration
256 TestCase.assertNotNull( "Expect Properties after Configuration redispatch", tester2.props );
257 TestCase.assertEquals( "Expect a single update call after Configuration redispatch", 2,
258 tester2.numManagedServiceUpdatedCalls );
Felix Meschberger8659e392009-08-19 05:46:49 +0000259
260 // remove the configuration for good
261 deleteConfig( pid );
262 }
263 finally
264 {
265 if ( bundle2 != null )
266 {
267 bundle2.uninstall();
268 }
269 }
270 }
271
272}