blob: 813267d143960444ab690d36f70e8e2991d08770 [file] [log] [blame]
Felix Meschberger47eb0372009-07-28 13:48:28 +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.scr.integration;
20
21
David Jencksb899d732014-06-15 01:10:18 +000022import java.util.Arrays;
David Jencksc4773f22014-06-15 01:09:31 +000023import java.util.Collection;
David Jencksb899d732014-06-15 01:10:18 +000024import java.util.Collections;
David Jencksc4773f22014-06-15 01:09:31 +000025
26import junit.framework.Assert;
Felix Meschberger47eb0372009-07-28 13:48:28 +000027import junit.framework.TestCase;
28
Felix Meschberger47eb0372009-07-28 13:48:28 +000029import org.apache.felix.scr.integration.components.SimpleComponent;
Felix Meschberger738751a2012-05-31 16:51:54 +000030import org.apache.felix.scr.integration.components.SimpleServiceImpl;
Felix Meschberger47eb0372009-07-28 13:48:28 +000031import org.junit.Test;
32import org.junit.runner.RunWith;
Felix Meschberger47eb0372009-07-28 13:48:28 +000033import org.ops4j.pax.exam.junit.JUnit4TestRunner;
Felix Meschberger47eb0372009-07-28 13:48:28 +000034import org.osgi.framework.Constants;
David Jencksc4773f22014-06-15 01:09:31 +000035import org.osgi.service.component.runtime.dto.ComponentConfigurationDTO;
Felix Meschberger47eb0372009-07-28 13:48:28 +000036
37
38@RunWith(JUnit4TestRunner.class)
Felix Meschbergera6ad9762009-08-19 13:38:56 +000039public class ComponentConfigurationTest extends ComponentTestBase
Felix Meschberger47eb0372009-07-28 13:48:28 +000040{
Felix Meschberger2e885422009-08-21 11:35:00 +000041 static
42 {
43 // uncomment to enable debugging of this test class
David Jencksa4af0eb2013-01-29 06:54:24 +000044// paxRunnerVmOption = DEBUG_VM_OPTION;
Felix Meschberger2e885422009-08-21 11:35:00 +000045 }
46
Felix Meschberger47eb0372009-07-28 13:48:28 +000047
Felix Meschberger47eb0372009-07-28 13:48:28 +000048 @Test
David Jencksbae44842014-06-21 20:15:24 +000049 public void test_SimpleComponent_configuration_ignore() throws Exception
Felix Meschberger47eb0372009-07-28 13:48:28 +000050 {
Felix Meschberger470a9752009-11-25 14:51:21 +000051 final String pid = "SimpleComponent.configuration.ignore";
David Jencksc4773f22014-06-15 01:09:31 +000052 TestCase.assertNull( SimpleComponent.INSTANCE );
Felix Meschberger470a9752009-11-25 14:51:21 +000053
54 deleteConfig( pid );
55 delay();
56
David Jencksc4773f22014-06-15 01:09:31 +000057 ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.ACTIVE);
58
Felix Meschberger470a9752009-11-25 14:51:21 +000059 TestCase.assertNotNull( SimpleComponent.INSTANCE );
60 TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
61
62 configure( pid );
63 delay();
64
David Jencksc4773f22014-06-15 01:09:31 +000065 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
Felix Meschberger470a9752009-11-25 14:51:21 +000066 TestCase.assertNotNull( SimpleComponent.INSTANCE );
67 TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
68
69 deleteConfig( pid );
70 delay();
71
David Jencksc4773f22014-06-15 01:09:31 +000072 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
Felix Meschberger470a9752009-11-25 14:51:21 +000073 TestCase.assertNotNull( SimpleComponent.INSTANCE );
74 TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
75
David Jencksc4773f22014-06-15 01:09:31 +000076 disableAndCheck( cc );
Felix Meschberger470a9752009-11-25 14:51:21 +000077 TestCase.assertNull( SimpleComponent.INSTANCE );
Felix Meschberger4dab8ea2009-07-31 14:44:12 +000078 }
Felix Meschberger47eb0372009-07-28 13:48:28 +000079
Felix Meschberger47eb0372009-07-28 13:48:28 +000080
Felix Meschberger4dab8ea2009-07-31 14:44:12 +000081 @Test
David Jencksbae44842014-06-21 20:15:24 +000082 public void test_SimpleComponent_configuration_optional() throws Exception
Felix Meschberger4dab8ea2009-07-31 14:44:12 +000083 {
Felix Meschberger470a9752009-11-25 14:51:21 +000084 final String pid = "SimpleComponent.configuration.optional";
David Jencksc4773f22014-06-15 01:09:31 +000085 ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.ACTIVE);
Felix Meschberger470a9752009-11-25 14:51:21 +000086
87 final SimpleComponent firstInstance = SimpleComponent.INSTANCE;
Felix Meschberger470a9752009-11-25 14:51:21 +000088 TestCase.assertNotNull( firstInstance );
89 TestCase.assertNull( firstInstance.getProperty( PROP_NAME ) );
90
91 configure( pid );
92 delay();
93
94 final SimpleComponent secondInstance = SimpleComponent.INSTANCE;
David Jencksc4773f22014-06-15 01:09:31 +000095 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
Felix Meschberger470a9752009-11-25 14:51:21 +000096 TestCase.assertNotNull( secondInstance );
97 TestCase.assertEquals( PROP_NAME, secondInstance.getProperty( PROP_NAME ) );
98
99 deleteConfig( pid );
100 delay();
101
102 final SimpleComponent thirdInstance = SimpleComponent.INSTANCE;
David Jencksc4773f22014-06-15 01:09:31 +0000103 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
Felix Meschberger470a9752009-11-25 14:51:21 +0000104 TestCase.assertNotNull( thirdInstance );
105 TestCase.assertNull( thirdInstance.getProperty( PROP_NAME ) );
106
107 TestCase.assertNotSame( "Expect new instance object after reconfiguration", firstInstance, secondInstance );
108 TestCase.assertNotSame( "Expect new instance object after configuration deletion (1)", firstInstance,
109 thirdInstance );
110 TestCase.assertNotSame( "Expect new instance object after configuration deletion (2)", secondInstance,
111 thirdInstance );
112
David Jencksc4773f22014-06-15 01:09:31 +0000113 disableAndCheck( cc );
Felix Meschberger470a9752009-11-25 14:51:21 +0000114 TestCase.assertNull( SimpleComponent.INSTANCE );
Felix Meschberger4dab8ea2009-07-31 14:44:12 +0000115 }
Felix Meschberger47eb0372009-07-28 13:48:28 +0000116
Felix Meschberger47eb0372009-07-28 13:48:28 +0000117
Felix Meschberger4dab8ea2009-07-31 14:44:12 +0000118 @Test
David Jencksbae44842014-06-21 20:15:24 +0000119 public void test_SimpleComponent_configuration_require() throws Exception
Felix Meschberger4dab8ea2009-07-31 14:44:12 +0000120 {
Felix Meschberger470a9752009-11-25 14:51:21 +0000121 final String pid = "SimpleComponent.configuration.require";
Felix Meschberger470a9752009-11-25 14:51:21 +0000122
123 deleteConfig( pid );
124 delay();
David Jencksc4773f22014-06-15 01:09:31 +0000125
Felix Meschberger470a9752009-11-25 14:51:21 +0000126 TestCase.assertNull( SimpleComponent.INSTANCE );
127
David Jencksc4773f22014-06-15 01:09:31 +0000128 getConfigurationsDisabledThenEnable(pid, 0, ComponentConfigurationDTO.UNSATISFIED);
Felix Meschberger470a9752009-11-25 14:51:21 +0000129 TestCase.assertNull( SimpleComponent.INSTANCE );
130
131 configure( pid );
132 delay();
133
David Jencksc4773f22014-06-15 01:09:31 +0000134 ComponentConfigurationDTO cc = findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
Felix Meschberger470a9752009-11-25 14:51:21 +0000135 TestCase.assertNotNull( SimpleComponent.INSTANCE );
136 TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
137
138 deleteConfig( pid );
139 delay();
140
David Jencksc4773f22014-06-15 01:09:31 +0000141 checkConfigurationCount(pid, 0, -1);
Felix Meschberger470a9752009-11-25 14:51:21 +0000142 TestCase.assertNull( SimpleComponent.INSTANCE );
143
David Jencksc4773f22014-06-15 01:09:31 +0000144 disableAndCheck( cc );
Felix Meschberger470a9752009-11-25 14:51:21 +0000145 TestCase.assertNull( SimpleComponent.INSTANCE );
Felix Meschberger4dab8ea2009-07-31 14:44:12 +0000146 }
Felix Meschberger47eb0372009-07-28 13:48:28 +0000147
David Jencksd02418a2014-06-15 01:10:02 +0000148 /**
149 * same as test_SimpleComponent_configuration_require except configuration is present when component is enabled.
150 */
151 @Test
David Jencksbae44842014-06-21 20:15:24 +0000152 public void test_SimpleComponent_configuration_require_initialize() throws Exception
David Jencksd02418a2014-06-15 01:10:02 +0000153 {
154 final String pid = "SimpleComponent.configuration.require";
155
156 deleteConfig( pid );
157 configure( pid );
158 delay();
159
160 TestCase.assertNull( SimpleComponent.INSTANCE );
161
162 ComponentConfigurationDTO cc = getConfigurationsDisabledThenEnable(pid, 1, ComponentConfigurationDTO.ACTIVE).iterator().next();
163
164 TestCase.assertNotNull( SimpleComponent.INSTANCE );
165 TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
166
167 deleteConfig( pid );
168 delay();
169
170 checkConfigurationCount(pid, 0, -1);
171 TestCase.assertNull( SimpleComponent.INSTANCE );
172
173 disableAndCheck( cc );
174 TestCase.assertNull( SimpleComponent.INSTANCE );
175 }
176
Felix Meschberger4dab8ea2009-07-31 14:44:12 +0000177
178 @Test
David Jencksbae44842014-06-21 20:15:24 +0000179 public void test_SimpleComponent_dynamic_configuration() throws Exception
Felix Meschberger4dab8ea2009-07-31 14:44:12 +0000180 {
Felix Meschberger470a9752009-11-25 14:51:21 +0000181 final String pid = "DynamicConfigurationComponent";
David Jencksacb83212014-04-30 18:01:44 +0000182 boolean pre13 = true;
David Jencksb899d732014-06-15 01:10:18 +0000183 boolean recreateOnDelete = true;
184 dynamicConfigTest(pid, pre13, recreateOnDelete);
David Jencksacb83212014-04-30 18:01:44 +0000185 }
186
187 @Test
David Jencksbae44842014-06-21 20:15:24 +0000188 public void test_SimpleComponent_dynamic_configuration_13() throws Exception
David Jencksacb83212014-04-30 18:01:44 +0000189 {
190 final String pid = "DynamicConfigurationComponent13";
191 boolean pre13 = false;
David Jencksb899d732014-06-15 01:10:18 +0000192 boolean recreateOnDelete = false;
193 dynamicConfigTest(pid, pre13, recreateOnDelete);
David Jencksacb83212014-04-30 18:01:44 +0000194 }
195
196 @Test
David Jencksbae44842014-06-21 20:15:24 +0000197 public void test_SimpleComponent_dynamic_configuration_flag() throws Exception
David Jencksacb83212014-04-30 18:01:44 +0000198 {
199 final String pid = "DynamicConfigurationComponentFlag";
David Jencksb899d732014-06-15 01:10:18 +0000200 boolean pre13 = true;
201 boolean recreateOnDelete = false;
202 dynamicConfigTest(pid, pre13, recreateOnDelete);
David Jencksacb83212014-04-30 18:01:44 +0000203 }
204
205
David Jencksbae44842014-06-21 20:15:24 +0000206 private void dynamicConfigTest(final String pid, boolean pre13, boolean recreateOnDelete) throws Exception
207 {
David Jencksb899d732014-06-15 01:10:18 +0000208 Object pidWithout;
209 Object pidWith;
210 if (pre13)
211 {
212 pidWithout = pid + ".description";
213 pidWith = pid;
214 }
215 else
216 {
217 pidWithout = Collections.singletonList(pid + ".description");
218 pidWith = Arrays.asList(new String[] {pid + ".description", pid});
219 }
Felix Meschberger470a9752009-11-25 14:51:21 +0000220 deleteConfig( pid );
221 delay();
222
David Jencksc4773f22014-06-15 01:09:31 +0000223 ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.ACTIVE);
Felix Meschberger470a9752009-11-25 14:51:21 +0000224
Felix Meschberger470a9752009-11-25 14:51:21 +0000225 TestCase.assertNotNull( SimpleComponent.INSTANCE );
226 TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
David Jencksb899d732014-06-15 01:10:18 +0000227 TestCase.assertEquals(pidWithout, SimpleComponent.INSTANCE.getProperty(Constants.SERVICE_PID));
Felix Meschberger470a9752009-11-25 14:51:21 +0000228
229 final SimpleComponent instance = SimpleComponent.INSTANCE;
230
231 configure( pid );
232 delay();
233
David Jencksc4773f22014-06-15 01:09:31 +0000234 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
Felix Meschberger470a9752009-11-25 14:51:21 +0000235 TestCase.assertEquals( instance, SimpleComponent.INSTANCE );
236 TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
David Jencksb899d732014-06-15 01:10:18 +0000237 TestCase.assertEquals(pidWith, SimpleComponent.INSTANCE.getProperty(Constants.SERVICE_PID));
Felix Meschberger470a9752009-11-25 14:51:21 +0000238
239 deleteConfig( pid );
240 delay();
241
David Jencksc4773f22014-06-15 01:09:31 +0000242 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
David Jencksb899d732014-06-15 01:10:18 +0000243 if (recreateOnDelete)
David Jencksacb83212014-04-30 18:01:44 +0000244 {
245 TestCase.assertNotSame( instance, SimpleComponent.INSTANCE );
246 }
247 else
248 {
249 TestCase.assertSame( instance, SimpleComponent.INSTANCE );
250 }
Felix Meschberger470a9752009-11-25 14:51:21 +0000251 TestCase.assertNull( SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
David Jencksb899d732014-06-15 01:10:18 +0000252 TestCase.assertEquals(pidWithout, SimpleComponent.INSTANCE.getProperty(Constants.SERVICE_PID));
Felix Meschberger470a9752009-11-25 14:51:21 +0000253
David Jencksc4773f22014-06-15 01:09:31 +0000254 disableAndCheck( cc );
Felix Meschberger470a9752009-11-25 14:51:21 +0000255 TestCase.assertNull( SimpleComponent.INSTANCE );
David Jencksacb83212014-04-30 18:01:44 +0000256 }
Felix Meschberger4dab8ea2009-07-31 14:44:12 +0000257
258
259 @Test
David Jencksbae44842014-06-21 20:15:24 +0000260 public void test_SimpleComponent_dynamic_optional_configuration_with_required_service() throws Exception
Felix Meschberger738751a2012-05-31 16:51:54 +0000261 {
262 final String targetProp = "ref.target";
263 final String filterProp = "required";
264 final SimpleServiceImpl service = SimpleServiceImpl.create( bundleContext, "sample" ).setFilterProperty( filterProp );
265 try
266 {
267 final String pid = "DynamicConfigurationComponentWithRequiredReference";
Felix Meschberger738751a2012-05-31 16:51:54 +0000268 deleteConfig( pid );
269 delay();
270
Felix Meschberger738751a2012-05-31 16:51:54 +0000271 // mandatory ref missing --> component unsatisfied
David Jencksc4773f22014-06-15 01:09:31 +0000272 ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.UNSATISFIED);
Felix Meschberger738751a2012-05-31 16:51:54 +0000273
274 // dynamically configure without the correct target
275 configure( pid );
276 delay();
277
278 // mandatory ref missing --> component unsatisfied
David Jencksc4773f22014-06-15 01:09:31 +0000279 findComponentConfigurationByName(pid, ComponentConfigurationDTO.UNSATISFIED);
Felix Meschberger738751a2012-05-31 16:51:54 +0000280
281 // dynamically configure with correct target
282 theConfig.put( targetProp, "(filterprop=" + filterProp + ")" );
283 configure( pid );
284 delay();
285
David Jencksc4773f22014-06-15 01:09:31 +0000286 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
Felix Meschberger738751a2012-05-31 16:51:54 +0000287 TestCase.assertNotNull( SimpleComponent.INSTANCE );
288 TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
289 TestCase.assertEquals( pid, SimpleComponent.INSTANCE.getProperty( Constants.SERVICE_PID ) );
290
291 final SimpleComponent instance = SimpleComponent.INSTANCE;
292
293 configure( pid );
294 delay();
295
296 // same instance after reconfiguration
David Jencksc4773f22014-06-15 01:09:31 +0000297 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
Felix Meschberger738751a2012-05-31 16:51:54 +0000298 TestCase.assertEquals( instance, SimpleComponent.INSTANCE );
299 TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
300 TestCase.assertEquals( pid, SimpleComponent.INSTANCE.getProperty( Constants.SERVICE_PID ) );
David Jencksa4af0eb2013-01-29 06:54:24 +0000301 TestCase.assertNotNull( SimpleComponent.INSTANCE.m_singleRef );
Felix Meschberger738751a2012-05-31 16:51:54 +0000302
303 // reconfigure without target --> unsatisifed
304 theConfig.remove( targetProp );
305 configure( pid );
306 delay();
307
308 // mandatory ref missing --> component unsatisfied
David Jencksc4773f22014-06-15 01:09:31 +0000309 findComponentConfigurationByName(pid, ComponentConfigurationDTO.UNSATISFIED);
Felix Meschberger738751a2012-05-31 16:51:54 +0000310
311 deleteConfig( pid );
312 delay();
313
314 // mandatory ref missing --> component unsatisfied
David Jencksc4773f22014-06-15 01:09:31 +0000315 findComponentConfigurationByName(pid, ComponentConfigurationDTO.UNSATISFIED);
Felix Meschberger738751a2012-05-31 16:51:54 +0000316
David Jencksc4773f22014-06-15 01:09:31 +0000317 disableAndCheck(cc);
Felix Meschberger738751a2012-05-31 16:51:54 +0000318 TestCase.assertNull( SimpleComponent.INSTANCE );
319 }
320 finally
321 {
322 theConfig.remove( targetProp );
323 if ( service != null )
324 {
325 service.drop();
326 }
327 }
328 }
329
David Jencksef3ee752013-02-15 02:34:58 +0000330 /**
331 * FELIX-3902. Start with filter matching two services, remove one, then change the filter
332 * to (still) match the other one. 2nd service should remain bound.
333 */
334 @Test
David Jencksbae44842014-06-21 20:15:24 +0000335 public void test_SimpleComponent_dynamic_optional_configuration_with_required_service2() throws Exception
David Jencksef3ee752013-02-15 02:34:58 +0000336 {
337 final String targetProp = "ref.target";
338 final String filterProp1 = "one";
339 final String filterProp2 = "two";
340 final SimpleServiceImpl service1 = SimpleServiceImpl.create( bundleContext, "one", 1 ).setFilterProperty( filterProp1 );
341 final SimpleServiceImpl service2 = SimpleServiceImpl.create( bundleContext, "two", 2 ).setFilterProperty( filterProp2 );
342 try
343 {
344 final String pid = "DynamicConfigurationComponentWithRequiredReference";
David Jencksef3ee752013-02-15 02:34:58 +0000345 deleteConfig( pid );
346 delay();
347
David Jencksef3ee752013-02-15 02:34:58 +0000348 // mandatory ref missing --> component unsatisfied
David Jencksc4773f22014-06-15 01:09:31 +0000349 ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.UNSATISFIED);
David Jencksef3ee752013-02-15 02:34:58 +0000350
351 // dynamically configure without the correct target
352 configure( pid );
353 delay();
354
355 // mandatory ref missing --> component unsatisfied
David Jencksc4773f22014-06-15 01:09:31 +0000356 findComponentConfigurationByName(pid, ComponentConfigurationDTO.UNSATISFIED);
David Jencksef3ee752013-02-15 02:34:58 +0000357
358 // dynamically configure with correct target
359 theConfig.put( targetProp, "(|(filterprop=" + filterProp1 + ")(filterprop=" + filterProp2 + "))" );
360 configure( pid );
361 delay();
362
David Jencksc4773f22014-06-15 01:09:31 +0000363 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
David Jencksef3ee752013-02-15 02:34:58 +0000364 TestCase.assertNotNull( SimpleComponent.INSTANCE );
365 TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
366 TestCase.assertEquals( pid, SimpleComponent.INSTANCE.getProperty( Constants.SERVICE_PID ) );
367
368 final SimpleComponent instance = SimpleComponent.INSTANCE;
369
370 configure( pid );
371 delay();
372
373 //remove higher ranked service
374 if (service2 != null)
375 {
376 service2.drop();
377 }
378 // same instance after reconfiguration
David Jencksc4773f22014-06-15 01:09:31 +0000379 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
David Jencksef3ee752013-02-15 02:34:58 +0000380 TestCase.assertEquals( instance, SimpleComponent.INSTANCE );
381 TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
382 TestCase.assertEquals( pid, SimpleComponent.INSTANCE.getProperty( Constants.SERVICE_PID ) );
383 TestCase.assertNotNull( SimpleComponent.INSTANCE.m_singleRef );
384
385 // reconfigure with new filter --> active
386 theConfig.put( targetProp, "(filterprop=" + filterProp1 + ")" );
387 configure( pid );
388 delay();
389
390 // same instance after reconfiguration
David Jencksc4773f22014-06-15 01:09:31 +0000391 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
David Jencksef3ee752013-02-15 02:34:58 +0000392 TestCase.assertEquals( instance, SimpleComponent.INSTANCE );
393 TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
394 TestCase.assertEquals( pid, SimpleComponent.INSTANCE.getProperty( Constants.SERVICE_PID ) );
395 TestCase.assertNotNull( SimpleComponent.INSTANCE.m_singleRef );
396
397 deleteConfig( pid );
398 delay();
399
400 // mandatory ref missing --> component unsatisfied
David Jencksc4773f22014-06-15 01:09:31 +0000401 findComponentConfigurationByName(pid, ComponentConfigurationDTO.UNSATISFIED);
David Jencksef3ee752013-02-15 02:34:58 +0000402
David Jencksc4773f22014-06-15 01:09:31 +0000403 disableAndCheck(cc);
David Jencksef3ee752013-02-15 02:34:58 +0000404 TestCase.assertNull( SimpleComponent.INSTANCE );
405 }
406 finally
407 {
408 theConfig.remove( targetProp );
409 if ( service1 != null )
410 {
411 service1.drop();
412 }
413 }
414 }
415
David Jencksa4af0eb2013-01-29 06:54:24 +0000416 @Test
David Jencksc4773f22014-06-15 01:09:31 +0000417 public void test_SimpleComponent_dynamic_optional_configuration_with_optional_service() throws Exception
David Jencksa4af0eb2013-01-29 06:54:24 +0000418 {
419 final String targetProp = "ref.target";
420 final String filterProp = "required";
421 final SimpleServiceImpl service = SimpleServiceImpl.create( bundleContext, "sample" ).setFilterProperty( filterProp );
422 try
423 {
424 final String pid = "DynamicConfigurationComponentWithOptionalReference";
David Jencksa4af0eb2013-01-29 06:54:24 +0000425 deleteConfig( pid );
426 delay();
427
David Jencksa4af0eb2013-01-29 06:54:24 +0000428 // optional ref missing --> component active
David Jencksc4773f22014-06-15 01:09:31 +0000429 ComponentConfigurationDTO cc = getDisabledConfigurationAndEnable(pid, ComponentConfigurationDTO.ACTIVE);
430
David Jencksa4af0eb2013-01-29 06:54:24 +0000431 TestCase.assertNotNull( SimpleComponent.INSTANCE );
432 final SimpleComponent instance = SimpleComponent.INSTANCE;
433
434 // dynamically configure without the correct target
435 configure( pid );
436 delay();
437
438 // optional ref missing --> component active
David Jencksc4773f22014-06-15 01:09:31 +0000439 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
David Jencksa4af0eb2013-01-29 06:54:24 +0000440 TestCase.assertEquals( instance, SimpleComponent.INSTANCE );
441 TestCase.assertNull( SimpleComponent.INSTANCE.m_singleRef );
442
443 // dynamically configure with correct target
444 theConfig.put( targetProp, "(filterprop=" + filterProp + ")" );
445 configure( pid );
446 delay();
447
David Jencksc4773f22014-06-15 01:09:31 +0000448 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
David Jencksa4af0eb2013-01-29 06:54:24 +0000449 TestCase.assertEquals( instance, SimpleComponent.INSTANCE );
450 TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
451 TestCase.assertEquals( pid, SimpleComponent.INSTANCE.getProperty( Constants.SERVICE_PID ) );
452 TestCase.assertNotNull( SimpleComponent.INSTANCE.m_singleRef );
453
454 configure( pid );
455 delay();
456
457 // same instance after reconfiguration
David Jencksc4773f22014-06-15 01:09:31 +0000458 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
David Jencksa4af0eb2013-01-29 06:54:24 +0000459 TestCase.assertEquals( instance, SimpleComponent.INSTANCE );
460 TestCase.assertEquals( PROP_NAME, SimpleComponent.INSTANCE.getProperty( PROP_NAME ) );
461 TestCase.assertEquals( pid, SimpleComponent.INSTANCE.getProperty( Constants.SERVICE_PID ) );
462 TestCase.assertNotNull( SimpleComponent.INSTANCE.m_singleRef );
463
464 // reconfigure without target --> active
465 theConfig.remove( targetProp );
466 configure( pid );
467 delay();
468
469 // optional ref missing --> component active
David Jencksc4773f22014-06-15 01:09:31 +0000470 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
David Jencksa4af0eb2013-01-29 06:54:24 +0000471 TestCase.assertEquals( instance, SimpleComponent.INSTANCE );
472 TestCase.assertNull( SimpleComponent.INSTANCE.m_singleRef );
473
474 deleteConfig( pid );
475 delay();
476
477 // optional ref missing --> component active
David Jencksc4773f22014-06-15 01:09:31 +0000478 findComponentConfigurationByName(pid, ComponentConfigurationDTO.ACTIVE);
David Jencksacb83212014-04-30 18:01:44 +0000479 TestCase.assertNotSame( instance, SimpleComponent.INSTANCE );
David Jencksa4af0eb2013-01-29 06:54:24 +0000480 TestCase.assertNull( SimpleComponent.INSTANCE.m_singleRef );
481
David Jencksc4773f22014-06-15 01:09:31 +0000482 disableAndCheck(cc);
David Jencksa4af0eb2013-01-29 06:54:24 +0000483 TestCase.assertNull( SimpleComponent.INSTANCE );
484 }
485 finally
486 {
487// Thread.sleep( 60000 );
488 theConfig.remove( targetProp );
489 if ( service != null )
490 {
491 service.drop();
492 }
493 }
494 }
495
Felix Meschberger738751a2012-05-31 16:51:54 +0000496
497 @Test
David Jencksbae44842014-06-21 20:15:24 +0000498 public void test_SimpleComponent_factory_configuration() throws Exception
Felix Meschberger4dab8ea2009-07-31 14:44:12 +0000499 {
Felix Meschberger2e885422009-08-21 11:35:00 +0000500 final String factoryPid = "FactoryConfigurationComponent";
501
502 deleteFactoryConfigurations( factoryPid );
503 delay();
504
David Jencksc4773f22014-06-15 01:09:31 +0000505 getConfigurationsDisabledThenEnable(factoryPid, 0, -1);
Felix Meschberger2e885422009-08-21 11:35:00 +0000506 TestCase.assertTrue( SimpleComponent.INSTANCES.isEmpty() );
507
508 // create two factory configurations expecting two components
509 final String pid0 = createFactoryConfiguration( factoryPid );
510 final String pid1 = createFactoryConfiguration( factoryPid );
511 delay();
512
David Jencksc4773f22014-06-15 01:09:31 +0000513 // expect two active components, //TODO WTF?? only first is active, second is disabled
514 checkConfigurationCount(factoryPid, 2, ComponentConfigurationDTO.ACTIVE);
David Jencksd02418a2014-06-15 01:10:02 +0000515 // delete a configuration
516 deleteConfig( pid0 );
517 delay();
518
519 // expect one component
520 checkConfigurationCount(factoryPid, 1, ComponentConfigurationDTO.ACTIVE);
521
522 // delete second configuration
523 deleteConfig( pid1 );
524 delay();
525
526 checkConfigurationCount(factoryPid, 0, ComponentConfigurationDTO.ACTIVE);
527 }
528
529 /**
530 * same as test_SimpleComponent_factory_configuration except configurations are present before
531 * component is enabled to test initialization.
532 */
533 @Test
David Jencksbae44842014-06-21 20:15:24 +0000534 public void test_SimpleComponent_factory_configuration_initialize() throws Exception
David Jencksd02418a2014-06-15 01:10:02 +0000535 {
536 final String factoryPid = "FactoryConfigurationComponent";
537
538 deleteFactoryConfigurations( factoryPid );
539
540 // create two factory configurations expecting two components
541 final String pid0 = createFactoryConfiguration( factoryPid );
542 final String pid1 = createFactoryConfiguration( factoryPid );
543 delay();
544
545 getConfigurationsDisabledThenEnable(factoryPid, 2, ComponentConfigurationDTO.ACTIVE);
Felix Meschberger2e885422009-08-21 11:35:00 +0000546
547 // delete a configuration
548 deleteConfig( pid0 );
549 delay();
550
551 // expect one component
David Jencksc4773f22014-06-15 01:09:31 +0000552 checkConfigurationCount(factoryPid, 1, ComponentConfigurationDTO.ACTIVE);
Felix Meschberger2e885422009-08-21 11:35:00 +0000553
554 // delete second configuration
555 deleteConfig( pid1 );
556 delay();
557
David Jencksc4773f22014-06-15 01:09:31 +0000558 checkConfigurationCount(factoryPid, 0, ComponentConfigurationDTO.ACTIVE);
Felix Meschberger4dab8ea2009-07-31 14:44:12 +0000559 }
560
Felix Meschbergerf96e10c2010-03-24 08:15:51 +0000561 @Test
David Jencksbae44842014-06-21 20:15:24 +0000562 public void test_SimpleComponent_factory_configuration_enabled() throws Exception
Felix Meschbergerf96e10c2010-03-24 08:15:51 +0000563 {
564 final String factoryPid = "FactoryConfigurationComponent_enabled";
565
566 deleteFactoryConfigurations( factoryPid );
567 delay();
568
David Jencksc4773f22014-06-15 01:09:31 +0000569 checkConfigurationCount(factoryPid, 0, ComponentConfigurationDTO.ACTIVE);
570 // no component config exists without configuration
Felix Meschbergerf96e10c2010-03-24 08:15:51 +0000571
572 // create two factory configurations expecting two components
573 final String pid0 = createFactoryConfiguration( factoryPid );
574 final String pid1 = createFactoryConfiguration( factoryPid );
575 delay();
576
577 // expect two components, all active
David Jencksc4773f22014-06-15 01:09:31 +0000578 checkConfigurationCount(factoryPid, 2, ComponentConfigurationDTO.ACTIVE);
Felix Meschbergerf96e10c2010-03-24 08:15:51 +0000579
580 // disable the name component
David Jencksc4773f22014-06-15 01:09:31 +0000581 disableAndCheck( factoryPid );
Felix Meschbergerf96e10c2010-03-24 08:15:51 +0000582 delay();
583
Felix Meschbergerf96e10c2010-03-24 08:15:51 +0000584
585 // create a configuration
586 final String pid3 = createFactoryConfiguration( factoryPid );
587 delay();
588
David Jencksd02418a2014-06-15 01:10:02 +0000589 getConfigurationsDisabledThenEnable(factoryPid, 3, ComponentConfigurationDTO.ACTIVE);
David Jencksc4773f22014-06-15 01:09:31 +0000590
Felix Meschbergerf96e10c2010-03-24 08:15:51 +0000591 }
592
David Jencksc4773f22014-06-15 01:09:31 +0000593
Felix Meschberger47eb0372009-07-28 13:48:28 +0000594}