blob: 4961d008bd671b131964c0b944a564863cc4a7ca [file] [log] [blame]
Felix Meschberger0770cad2012-06-11 12:36:52 +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 java.io.IOException;
23import java.util.Hashtable;
24
25import org.apache.felix.cm.integration.helper.SynchronousTestListener;
26import org.apache.felix.cm.integration.helper.TestListener;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.ops4j.pax.exam.junit.JUnit4TestRunner;
30import org.osgi.framework.ServiceRegistration;
31import org.osgi.service.cm.Configuration;
32import org.osgi.service.cm.ConfigurationEvent;
33import org.osgi.service.cm.ConfigurationListener;
Felix Meschbergere94b1572012-07-14 11:59:29 +000034import org.osgi.service.cm.SynchronousConfigurationListener;
Felix Meschberger0770cad2012-06-11 12:36:52 +000035
36
37@RunWith(JUnit4TestRunner.class)
38public 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 Meschbergere94b1572012-07-14 11:59:29 +0000117
118 // Synchronous listener expecting synchronous events being
119 // registered as a SynchronousConfigurationListener
Felix Meschberger0770cad2012-06-11 12:36:52 +0000120 final TestListener testListener = new SynchronousTestListener();
Felix Meschbergere94b1572012-07-14 11:59:29 +0000121 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 Meschberger0770cad2012-06-11 12:36:52 +0000130 int eventCount = 0;
Felix Meschbergere94b1572012-07-14 11:59:29 +0000131 int eventCountAsync = 0;
132
Felix Meschberger0770cad2012-06-11 12:36:52 +0000133 try
134 {
135 delay();
136 testListener.assertNoEvent();
Felix Meschbergere94b1572012-07-14 11:59:29 +0000137 testListenerAsync.assertNoEvent();
Felix Meschberger0770cad2012-06-11 12:36:52 +0000138
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 Meschbergere94b1572012-07-14 11:59:29 +0000147 testListenerAsync.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCountAsync );
Felix Meschberger0770cad2012-06-11 12:36:52 +0000148
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 Meschbergere94b1572012-07-14 11:59:29 +0000157 testListenerAsync.assertEvent( ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCountAsync );
Felix Meschberger0770cad2012-06-11 12:36:52 +0000158
159 config.setBundleLocation( "new_Location" );
160 delay();
161 testListener.assertEvent( ConfigurationEvent.CM_LOCATION_CHANGED, pid, null, false, ++eventCount );
Felix Meschbergere94b1572012-07-14 11:59:29 +0000162 testListenerAsync.assertEvent( ConfigurationEvent.CM_LOCATION_CHANGED, pid, null, true, ++eventCountAsync );
Felix Meschberger0770cad2012-06-11 12:36:52 +0000163
164 config.update();
165 testListener.assertNoEvent();
Felix Meschbergere94b1572012-07-14 11:59:29 +0000166 testListenerAsync.assertNoEvent();
Felix Meschberger0770cad2012-06-11 12:36:52 +0000167
168 config.delete();
169 config = null;
170 delay();
171 testListener.assertEvent( ConfigurationEvent.CM_DELETED, pid, null, false, ++eventCount );
Felix Meschbergere94b1572012-07-14 11:59:29 +0000172 testListenerAsync.assertEvent( ConfigurationEvent.CM_DELETED, pid, null, true, ++eventCountAsync );
Felix Meschberger0770cad2012-06-11 12:36:52 +0000173 }
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 Meschbergere94b1572012-07-14 11:59:29 +0000189 listenerAsync.unregister();
Felix Meschberger0770cad2012-06-11 12:36:52 +0000190 }
191 }
192}