Felix Meschberger | 72f9507 | 2011-02-04 23:02:42 +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 | */ |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 19 | package org.apache.felix.cm.integration; |
| 20 | |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 21 | |
| 22 | import java.io.IOException; |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 23 | import java.util.ArrayList; |
Felix Meschberger | abb0bc2 | 2012-05-30 11:07:49 +0000 | [diff] [blame] | 24 | import java.util.Hashtable; |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 25 | import java.util.List; |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 26 | import org.apache.felix.cm.integration.helper.SynchronousTestListener; |
| 27 | import org.apache.felix.cm.integration.helper.TestListener; |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 30 | import org.ops4j.pax.exam.junit.JUnit4TestRunner; |
| 31 | import org.osgi.framework.Bundle; |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 32 | import org.osgi.framework.BundleException; |
| 33 | import org.osgi.framework.Constants; |
| 34 | import org.osgi.framework.InvalidSyntaxException; |
| 35 | import org.osgi.framework.ServiceEvent; |
| 36 | import org.osgi.framework.ServiceListener; |
| 37 | import org.osgi.framework.ServiceReference; |
| 38 | import org.osgi.service.cm.ConfigurationAdmin; |
| 39 | import org.osgi.service.cm.ConfigurationEvent; |
| 40 | import org.osgi.service.cm.ConfigurationListener; |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 41 | import org.osgi.service.cm.SynchronousConfigurationListener; |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 42 | |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 43 | |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 44 | @RunWith(JUnit4TestRunner.class) |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 45 | public class FELIX2813_ConfigurationAdminStartupTest extends ConfigurationTestBase implements ServiceListener |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 46 | { |
| 47 | |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 48 | @Test |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 49 | public void testAddConfigurationWhenConfigurationAdminStarts() throws InvalidSyntaxException, BundleException |
| 50 | { |
| 51 | |
| 52 | List<Bundle> bundles = new ArrayList<Bundle>(); |
| 53 | ServiceReference[] refs = configAdminTracker.getServiceReferences(); |
| 54 | if ( refs != null ) |
| 55 | { |
| 56 | for ( ServiceReference ref : refs ) |
| 57 | { |
| 58 | bundles.add( ref.getBundle() ); |
| 59 | ref.getBundle().stop(); |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 62 | |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 63 | final TestListener listener = new TestListener(); |
| 64 | bundleContext.registerService( ConfigurationListener.class.getName(), listener, null ); |
| 65 | final TestListener syncListener = new SynchronousTestListener(); |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 66 | bundleContext.registerService( SynchronousConfigurationListener.class.getName(), syncListener, null ); |
| 67 | final TestListener syncListenerAsync = new SynchronousTestListener(); |
| 68 | bundleContext.registerService( ConfigurationListener.class.getName(), syncListenerAsync, null ); |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 69 | bundleContext.addServiceListener( this, "(" + Constants.OBJECTCLASS + "=" + ConfigurationAdmin.class.getName() |
| 70 | + ")" ); |
| 71 | |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 72 | for ( Bundle bundle : bundles ) |
| 73 | { |
| 74 | bundle.start(); |
| 75 | } |
| 76 | |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 77 | /* |
| 78 | * Look at the console output for the following exception: |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 79 | * |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 80 | * *ERROR* Unexpected problem executing task |
| 81 | * java.lang.NullPointerException: reference and pid must not be null |
| 82 | * at org.osgi.service.cm.ConfigurationEvent.<init>(ConfigurationEvent.java:120) |
| 83 | * at org.apache.felix.cm.impl.ConfigurationManager$FireConfigurationEvent.run(ConfigurationManager.java:1818) |
| 84 | * at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:104) |
| 85 | * at java.lang.Thread.run(Thread.java:680) |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 86 | * |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 87 | * It is in fact the service reference that is still null, because the service registration |
| 88 | * has not been 'set' yet. |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 89 | * |
| 90 | * This following code will ensure the situation did not occurr and the |
| 91 | * event has effectively been sent. The eventSeen flag is set by the |
| 92 | * configurationEvent method when the event for the test PID has been |
| 93 | * received. If the flag is not set, we wait at most 2 seconds for the |
| 94 | * event to arrive. If the event does not arrive by then, the test is |
| 95 | * assumed to have failed. This will rather generate false negatives |
| 96 | * (on slow machines) than false positives. |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 97 | */ |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 98 | delay(); |
| 99 | listener.assertEvent( ConfigurationEvent.CM_UPDATED, "test", null, true, 1 ); |
| 100 | syncListener.assertEvent( ConfigurationEvent.CM_UPDATED, "test", null, false, 1 ); |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 101 | syncListenerAsync.assertEvent( ConfigurationEvent.CM_UPDATED, "test", null, true, 1 ); |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 104 | |
| 105 | public void serviceChanged( ServiceEvent event ) |
| 106 | { |
| 107 | if ( event.getType() == ServiceEvent.REGISTERED ) |
| 108 | { |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 109 | ServiceReference ref = event.getServiceReference(); |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 110 | ConfigurationAdmin ca = ( ConfigurationAdmin ) bundleContext.getService( ref ); |
| 111 | try |
| 112 | { |
| 113 | org.osgi.service.cm.Configuration config = ca.getConfiguration( "test" ); |
Felix Meschberger | abb0bc2 | 2012-05-30 11:07:49 +0000 | [diff] [blame] | 114 | Hashtable<String, Object> props = new Hashtable<String, Object>(); |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 115 | props.put( "abc", "123" ); |
| 116 | config.update( props ); |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 117 | } |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 118 | catch ( IOException e ) |
| 119 | { |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 123 | } |