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; |
| 24 | import java.util.List; |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 25 | import java.util.Properties; |
| 26 | |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 27 | import junit.framework.TestCase; |
| 28 | |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 31 | import org.ops4j.pax.exam.junit.JUnit4TestRunner; |
| 32 | import org.osgi.framework.Bundle; |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 33 | import org.osgi.framework.BundleException; |
| 34 | import org.osgi.framework.Constants; |
| 35 | import org.osgi.framework.InvalidSyntaxException; |
| 36 | import org.osgi.framework.ServiceEvent; |
| 37 | import org.osgi.framework.ServiceListener; |
| 38 | import org.osgi.framework.ServiceReference; |
| 39 | import org.osgi.service.cm.ConfigurationAdmin; |
| 40 | import org.osgi.service.cm.ConfigurationEvent; |
| 41 | import org.osgi.service.cm.ConfigurationListener; |
| 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) |
| 45 | public class FELIX2813_ConfigurationAdminStartupTest extends ConfigurationTestBase implements ServiceListener, |
| 46 | ConfigurationListener |
| 47 | { |
| 48 | |
| 49 | private Object lock = new Object(); |
| 50 | private boolean eventSeen; |
| 51 | |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 52 | |
| 53 | @Test |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 54 | public void testAddConfigurationWhenConfigurationAdminStarts() throws InvalidSyntaxException, BundleException |
| 55 | { |
| 56 | |
| 57 | List<Bundle> bundles = new ArrayList<Bundle>(); |
| 58 | ServiceReference[] refs = configAdminTracker.getServiceReferences(); |
| 59 | if ( refs != null ) |
| 60 | { |
| 61 | for ( ServiceReference ref : refs ) |
| 62 | { |
| 63 | bundles.add( ref.getBundle() ); |
| 64 | ref.getBundle().stop(); |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 67 | |
| 68 | bundleContext.registerService( ConfigurationListener.class.getName(), this, null ); |
| 69 | bundleContext.addServiceListener( this, "(" + Constants.OBJECTCLASS + "=" + ConfigurationAdmin.class.getName() |
| 70 | + ")" ); |
| 71 | |
| 72 | // ensure we do not have a false positive below |
| 73 | eventSeen = false; |
| 74 | |
| 75 | for ( Bundle bundle : bundles ) |
| 76 | { |
| 77 | bundle.start(); |
| 78 | } |
| 79 | |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 80 | /* |
| 81 | * Look at the console output for the following exception: |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 82 | * |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 83 | * *ERROR* Unexpected problem executing task |
| 84 | * java.lang.NullPointerException: reference and pid must not be null |
| 85 | * at org.osgi.service.cm.ConfigurationEvent.<init>(ConfigurationEvent.java:120) |
| 86 | * at org.apache.felix.cm.impl.ConfigurationManager$FireConfigurationEvent.run(ConfigurationManager.java:1818) |
| 87 | * at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:104) |
| 88 | * at java.lang.Thread.run(Thread.java:680) |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 89 | * |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 90 | * It is in fact the service reference that is still null, because the service registration |
| 91 | * has not been 'set' yet. |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 92 | * |
| 93 | * This following code will ensure the situation did not occurr and the |
| 94 | * event has effectively been sent. The eventSeen flag is set by the |
| 95 | * configurationEvent method when the event for the test PID has been |
| 96 | * received. If the flag is not set, we wait at most 2 seconds for the |
| 97 | * event to arrive. If the event does not arrive by then, the test is |
| 98 | * assumed to have failed. This will rather generate false negatives |
| 99 | * (on slow machines) than false positives. |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 100 | */ |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 101 | synchronized ( lock ) |
| 102 | { |
| 103 | if ( !eventSeen ) |
| 104 | { |
| 105 | try |
| 106 | { |
| 107 | lock.wait( 2000 ); |
| 108 | } |
| 109 | catch ( InterruptedException ie ) |
| 110 | { |
| 111 | // don't care ... |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if ( !eventSeen ) |
| 116 | { |
| 117 | TestCase.fail( "ConfigurationEvent not received within 2 seconds since bundle start" ); |
| 118 | } |
| 119 | } |
| 120 | |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 123 | |
| 124 | public void serviceChanged( ServiceEvent event ) |
| 125 | { |
| 126 | if ( event.getType() == ServiceEvent.REGISTERED ) |
| 127 | { |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 128 | ServiceReference ref = event.getServiceReference(); |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 129 | ConfigurationAdmin ca = ( ConfigurationAdmin ) bundleContext.getService( ref ); |
| 130 | try |
| 131 | { |
| 132 | org.osgi.service.cm.Configuration config = ca.getConfiguration( "test" ); |
| 133 | Properties props = new Properties(); |
| 134 | props.put( "abc", "123" ); |
| 135 | config.update( props ); |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 136 | } |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 137 | catch ( IOException e ) |
| 138 | { |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Felix Meschberger | 4b26df9 | 2011-02-01 12:41:45 +0000 | [diff] [blame] | 143 | |
| 144 | public void configurationEvent( ConfigurationEvent event ) |
| 145 | { |
| 146 | if ( "test".equals( event.getPid() ) ) |
| 147 | { |
| 148 | synchronized ( lock ) |
| 149 | { |
| 150 | eventSeen = true; |
| 151 | lock.notifyAll(); |
| 152 | } |
| 153 | } |
Marcel Offermans | 33ad4a7 | 2011-01-31 19:59:18 +0000 | [diff] [blame] | 154 | } |
| 155 | } |