Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +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 java.io.IOException; |
| 23 | import java.util.Hashtable; |
| 24 | |
| 25 | import org.apache.felix.cm.integration.helper.SynchronousTestListener; |
| 26 | import org.apache.felix.cm.integration.helper.TestListener; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | import org.ops4j.pax.exam.junit.JUnit4TestRunner; |
| 30 | import org.osgi.framework.ServiceRegistration; |
| 31 | import org.osgi.service.cm.Configuration; |
| 32 | import org.osgi.service.cm.ConfigurationEvent; |
| 33 | import org.osgi.service.cm.ConfigurationListener; |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 34 | import org.osgi.service.cm.SynchronousConfigurationListener; |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | @RunWith(JUnit4TestRunner.class) |
| 38 | public class ConfigurationListenerTest extends ConfigurationTestBase |
| 39 | { |
| 40 | |
| 41 | static |
| 42 | { |
| 43 | // uncomment to enable debugging of this test class |
| 44 | // paxRunnerVmOption = DEBUG_VM_OPTION; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | @Test |
| 49 | public void test_async_listener() throws IOException |
| 50 | { |
| 51 | final String pid = "test_listener"; |
| 52 | final TestListener testListener = new TestListener(); |
| 53 | final ServiceRegistration listener = this.bundleContext.registerService( ConfigurationListener.class.getName(), |
| 54 | testListener, null ); |
| 55 | int eventCount = 0; |
| 56 | |
| 57 | Configuration config = configure( pid, null, false ); |
| 58 | try |
| 59 | { |
| 60 | delay(); |
| 61 | testListener.assertNoEvent(); |
| 62 | |
| 63 | config.update( new Hashtable<String, Object>() |
| 64 | { |
| 65 | { |
| 66 | put( "x", "x" ); |
| 67 | } |
| 68 | } ); |
| 69 | delay(); |
| 70 | testListener.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCount ); |
| 71 | |
| 72 | config.update( new Hashtable<String, Object>() |
| 73 | { |
| 74 | { |
| 75 | put( "x", "x" ); |
| 76 | } |
| 77 | } ); |
| 78 | delay(); |
| 79 | testListener.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCount ); |
| 80 | |
| 81 | config.setBundleLocation( "new_Location" ); |
| 82 | delay(); |
| 83 | testListener.assertEvent( ConfigurationEvent.CM_LOCATION_CHANGED, pid, null, true, ++eventCount ); |
| 84 | |
| 85 | config.update(); |
| 86 | testListener.assertNoEvent(); |
| 87 | |
| 88 | config.delete(); |
| 89 | config = null; |
| 90 | delay(); |
| 91 | testListener.assertEvent( ConfigurationEvent.CM_DELETED, pid, null, true, ++eventCount ); |
| 92 | } |
| 93 | finally |
| 94 | { |
| 95 | if ( config != null ) |
| 96 | { |
| 97 | try |
| 98 | { |
| 99 | config.delete(); |
| 100 | } |
| 101 | catch ( IOException ioe ) |
| 102 | { |
| 103 | // ignore |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | listener.unregister(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | @Test |
| 113 | public void test_sync_listener() throws IOException |
| 114 | { |
| 115 | final String pid = "test_listener"; |
| 116 | Configuration config = configure( pid, null, false ); |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 117 | |
| 118 | // Synchronous listener expecting synchronous events being |
| 119 | // registered as a SynchronousConfigurationListener |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 120 | final TestListener testListener = new SynchronousTestListener(); |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 121 | final ServiceRegistration listener = this.bundleContext.registerService( |
| 122 | SynchronousConfigurationListener.class.getName(), testListener, null ); |
| 123 | |
| 124 | // Synchronous listener expecting asynchronous events being |
| 125 | // registered as a regular ConfigurationListener |
| 126 | final TestListener testListenerAsync = new SynchronousTestListener(); |
| 127 | final ServiceRegistration listenerAsync = this.bundleContext.registerService( |
| 128 | ConfigurationListener.class.getName(), testListenerAsync, null ); |
| 129 | |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 130 | int eventCount = 0; |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 131 | int eventCountAsync = 0; |
| 132 | |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 133 | try |
| 134 | { |
| 135 | delay(); |
| 136 | testListener.assertNoEvent(); |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 137 | testListenerAsync.assertNoEvent(); |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 138 | |
| 139 | config.update( new Hashtable<String, Object>() |
| 140 | { |
| 141 | { |
| 142 | put( "x", "x" ); |
| 143 | } |
| 144 | } ); |
| 145 | delay(); |
| 146 | testListener.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, false, ++eventCount ); |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 147 | testListenerAsync.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCountAsync ); |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 148 | |
| 149 | config.update( new Hashtable<String, Object>() |
| 150 | { |
| 151 | { |
| 152 | put( "x", "x" ); |
| 153 | } |
| 154 | } ); |
| 155 | delay(); |
| 156 | testListener.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, false, ++eventCount ); |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 157 | testListenerAsync.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCountAsync ); |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 158 | |
| 159 | config.setBundleLocation( "new_Location" ); |
| 160 | delay(); |
| 161 | testListener.assertEvent( ConfigurationEvent.CM_LOCATION_CHANGED, pid, null, false, ++eventCount ); |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 162 | testListenerAsync.assertEvent( ConfigurationEvent.CM_LOCATION_CHANGED, pid, null, true, ++eventCountAsync ); |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 163 | |
| 164 | config.update(); |
| 165 | testListener.assertNoEvent(); |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 166 | testListenerAsync.assertNoEvent(); |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 167 | |
| 168 | config.delete(); |
| 169 | config = null; |
| 170 | delay(); |
| 171 | testListener.assertEvent( ConfigurationEvent.CM_DELETED, pid, null, false, ++eventCount ); |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 172 | testListenerAsync.assertEvent( ConfigurationEvent.CM_DELETED, pid, null, true, ++eventCountAsync ); |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 173 | } |
| 174 | finally |
| 175 | { |
| 176 | if ( config != null ) |
| 177 | { |
| 178 | try |
| 179 | { |
| 180 | config.delete(); |
| 181 | } |
| 182 | catch ( IOException ioe ) |
| 183 | { |
| 184 | // ignore |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | listener.unregister(); |
Felix Meschberger | e94b157 | 2012-07-14 11:59:29 +0000 | [diff] [blame^] | 189 | listenerAsync.unregister(); |
Felix Meschberger | 0770cad | 2012-06-11 12:36:52 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |