Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +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 | |
Felix Meschberger | 13e9151 | 2009-08-20 11:23:32 +0000 | [diff] [blame] | 22 | import java.io.File; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 23 | import java.io.IOException; |
| 24 | import java.io.InputStream; |
Felix Meschberger | c67f9dc | 2009-08-31 13:03:11 +0000 | [diff] [blame] | 25 | import java.lang.reflect.Field; |
| 26 | import java.lang.reflect.Method; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 27 | import java.util.Dictionary; |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 28 | import java.util.HashSet; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 29 | import java.util.Hashtable; |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 30 | import java.util.Set; |
Felix Meschberger | 13e9151 | 2009-08-20 11:23:32 +0000 | [diff] [blame] | 31 | |
Felix Meschberger | c67f9dc | 2009-08-31 13:03:11 +0000 | [diff] [blame] | 32 | import junit.framework.AssertionFailedError; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 33 | import junit.framework.TestCase; |
| 34 | |
Felix Meschberger | 13e9151 | 2009-08-20 11:23:32 +0000 | [diff] [blame] | 35 | import org.apache.felix.cm.integration.helper.BaseTestActivator; |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 36 | import org.apache.felix.cm.integration.helper.ManagedServiceTestActivator; |
Felix Meschberger | c67f9dc | 2009-08-31 13:03:11 +0000 | [diff] [blame] | 37 | import org.apache.felix.cm.integration.helper.UpdateThreadSignalTask; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 38 | import org.junit.After; |
| 39 | import org.junit.Before; |
Guillaume Nodet | 3473093 | 2012-12-14 16:40:08 +0000 | [diff] [blame^] | 40 | |
| 41 | import javax.inject.Inject; |
| 42 | |
Felix Meschberger | 13e9151 | 2009-08-20 11:23:32 +0000 | [diff] [blame] | 43 | import org.ops4j.pax.exam.CoreOptions; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 44 | import org.ops4j.pax.exam.Option; |
Felix Meschberger | f2641a2 | 2010-08-25 08:45:50 +0000 | [diff] [blame] | 45 | import org.ops4j.pax.exam.OptionUtils; |
Guillaume Nodet | 3473093 | 2012-12-14 16:40:08 +0000 | [diff] [blame^] | 46 | import org.ops4j.pax.exam.TestProbeBuilder; |
| 47 | import org.ops4j.pax.exam.junit.ProbeBuilder; |
| 48 | import org.ops4j.pax.tinybundles.core.TinyBundles; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 49 | import org.osgi.framework.Bundle; |
| 50 | import org.osgi.framework.BundleContext; |
| 51 | import org.osgi.framework.BundleException; |
| 52 | import org.osgi.framework.Constants; |
| 53 | import org.osgi.framework.InvalidSyntaxException; |
| 54 | import org.osgi.framework.ServiceReference; |
| 55 | import org.osgi.service.cm.Configuration; |
| 56 | import org.osgi.service.cm.ConfigurationAdmin; |
| 57 | import org.osgi.util.tracker.ServiceTracker; |
| 58 | |
Guillaume Nodet | 3473093 | 2012-12-14 16:40:08 +0000 | [diff] [blame^] | 59 | import static org.ops4j.pax.exam.CoreOptions.*; |
| 60 | |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 61 | |
| 62 | public abstract class ConfigurationTestBase |
| 63 | { |
| 64 | |
Felix Meschberger | f2641a2 | 2010-08-25 08:45:50 +0000 | [diff] [blame] | 65 | // the name of the system property providing the bundle file to be installed and tested |
| 66 | protected static final String BUNDLE_JAR_SYS_PROP = "project.bundle.file"; |
| 67 | |
| 68 | // the default bundle jar file name |
| 69 | protected static final String BUNDLE_JAR_DEFAULT = "target/configadmin.jar"; |
| 70 | |
| 71 | // the JVM option to set to enable remote debugging |
| 72 | protected static final String DEBUG_VM_OPTION = "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=30303"; |
| 73 | |
| 74 | // the actual JVM option set, extensions may implement a static |
| 75 | // initializer overwriting this value to have the configuration() |
| 76 | // method include it when starting the OSGi framework JVM |
| 77 | protected static String paxRunnerVmOption = null; |
| 78 | |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 79 | @Inject |
| 80 | protected BundleContext bundleContext; |
| 81 | |
| 82 | protected Bundle bundle; |
| 83 | |
| 84 | protected ServiceTracker configAdminTracker; |
| 85 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 86 | private Set<String> configurations = new HashSet<String>(); |
| 87 | |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 88 | protected static final String PROP_NAME = "theValue"; |
| 89 | protected static final Dictionary<String, String> theConfig; |
| 90 | |
| 91 | static |
| 92 | { |
| 93 | theConfig = new Hashtable<String, String>(); |
| 94 | theConfig.put( PROP_NAME, PROP_NAME ); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | @org.ops4j.pax.exam.junit.Configuration |
| 99 | public static Option[] configuration() |
| 100 | { |
Felix Meschberger | f2641a2 | 2010-08-25 08:45:50 +0000 | [diff] [blame] | 101 | final String bundleFileName = System.getProperty( BUNDLE_JAR_SYS_PROP, BUNDLE_JAR_DEFAULT ); |
| 102 | final File bundleFile = new File( bundleFileName ); |
| 103 | if ( !bundleFile.canRead() ) |
| 104 | { |
| 105 | throw new IllegalArgumentException( "Cannot read from bundle file " + bundleFileName + " specified in the " |
| 106 | + BUNDLE_JAR_SYS_PROP + " system property" ); |
| 107 | } |
| 108 | |
| 109 | final Option[] base = options( |
Guillaume Nodet | 3473093 | 2012-12-14 16:40:08 +0000 | [diff] [blame^] | 110 | workingDirectory("target/paxexam/"), |
| 111 | cleanCaches(true), |
| 112 | junitBundles(), |
| 113 | mavenBundle("org.ops4j.pax.tinybundles", "tinybundles", "1.0.0"), |
| 114 | bundle(bundleFile.toURI().toString()) |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 115 | ); |
Guillaume Nodet | 3473093 | 2012-12-14 16:40:08 +0000 | [diff] [blame^] | 116 | final Option option = ( paxRunnerVmOption != null ) ? vmOption( paxRunnerVmOption ) : null; |
| 117 | return OptionUtils.combine( base, option ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
| 121 | @Before |
| 122 | public void setUp() |
| 123 | { |
| 124 | configAdminTracker = new ServiceTracker( bundleContext, ConfigurationAdmin.class.getName(), null ); |
| 125 | configAdminTracker.open(); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | @After |
| 130 | public void tearDown() throws BundleException |
| 131 | { |
| 132 | if ( bundle != null ) |
| 133 | { |
| 134 | bundle.uninstall(); |
| 135 | } |
| 136 | |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 137 | for ( String pid : configurations ) |
| 138 | { |
| 139 | deleteConfig( pid ); |
| 140 | } |
| 141 | |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 142 | configAdminTracker.close(); |
| 143 | configAdminTracker = null; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | protected Bundle installBundle( final String pid ) throws BundleException |
| 148 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 149 | return installBundle( pid, ManagedServiceTestActivator.class ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
| 153 | protected Bundle installBundle( final String pid, final Class<?> activatorClass ) throws BundleException |
| 154 | { |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 155 | return installBundle( pid, activatorClass, activatorClass.getName() ); |
| 156 | } |
| 157 | |
| 158 | |
Guillaume Nodet | 3473093 | 2012-12-14 16:40:08 +0000 | [diff] [blame^] | 159 | @ProbeBuilder |
| 160 | public TestProbeBuilder buildProbe( TestProbeBuilder builder ) { |
| 161 | return builder.setHeader(Constants.EXPORT_PACKAGE, "org.apache.felix.cm.integration.helper"); |
| 162 | } |
| 163 | |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 164 | protected Bundle installBundle( final String pid, final Class<?> activatorClass, final String location ) |
| 165 | throws BundleException |
| 166 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 167 | final String activatorClassName = activatorClass.getName(); |
Guillaume Nodet | 3473093 | 2012-12-14 16:40:08 +0000 | [diff] [blame^] | 168 | final InputStream bundleStream = TinyBundles.bundle() |
| 169 | .set(Constants.BUNDLE_SYMBOLICNAME, activatorClassName) |
Felix Meschberger | 13e9151 | 2009-08-20 11:23:32 +0000 | [diff] [blame] | 170 | .set( Constants.BUNDLE_VERSION, "0.0.11" ) |
| 171 | .set( Constants.IMPORT_PACKAGE, "org.apache.felix.cm.integration.helper" ) |
| 172 | .set( Constants.BUNDLE_ACTIVATOR, activatorClassName ) |
| 173 | .set( BaseTestActivator.HEADER_PID, pid ) |
Guillaume Nodet | 3473093 | 2012-12-14 16:40:08 +0000 | [diff] [blame^] | 174 | .build( TinyBundles.withBnd() ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 175 | |
| 176 | try |
| 177 | { |
Felix Meschberger | 6f5b69e | 2009-08-19 09:39:21 +0000 | [diff] [blame] | 178 | return bundleContext.installBundle( location, bundleStream ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 179 | } |
| 180 | finally |
| 181 | { |
| 182 | try |
| 183 | { |
| 184 | bundleStream.close(); |
| 185 | } |
| 186 | catch ( IOException ioe ) |
| 187 | { |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
Felix Meschberger | c67f9dc | 2009-08-31 13:03:11 +0000 | [diff] [blame] | 193 | protected void delay() |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 194 | { |
Felix Meschberger | c67f9dc | 2009-08-31 13:03:11 +0000 | [diff] [blame] | 195 | Object ca = configAdminTracker.getService(); |
| 196 | if ( ca != null ) |
| 197 | { |
| 198 | try |
| 199 | { |
| 200 | |
| 201 | Field caf = ca.getClass().getDeclaredField( "configurationManager" ); |
| 202 | caf.setAccessible( true ); |
| 203 | Object cm = caf.get( ca ); |
| 204 | |
| 205 | Field cmf = cm.getClass().getDeclaredField( "updateThread" ); |
| 206 | cmf.setAccessible( true ); |
| 207 | Object ut = cmf.get( cm ); |
| 208 | |
| 209 | Method utm = ut.getClass().getDeclaredMethod( "schedule" ); |
| 210 | utm.setAccessible( true ); |
| 211 | |
| 212 | UpdateThreadSignalTask signalTask = new UpdateThreadSignalTask(); |
| 213 | utm.invoke( ut, signalTask ); |
| 214 | signalTask.waitSignal(); |
| 215 | |
| 216 | return; |
| 217 | } |
| 218 | catch ( AssertionFailedError afe ) |
| 219 | { |
| 220 | throw afe; |
| 221 | } |
| 222 | catch ( Throwable t ) |
| 223 | { |
| 224 | // ignore any problem and revert to timed delay (might log this) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // no configadmin or failure while setting up task |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 229 | try |
| 230 | { |
| 231 | Thread.sleep( 300 ); |
| 232 | } |
| 233 | catch ( InterruptedException ie ) |
| 234 | { |
| 235 | // dont care |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | |
| 240 | protected Bundle getCmBundle() |
| 241 | { |
| 242 | final ServiceReference caref = configAdminTracker.getServiceReference(); |
| 243 | return ( caref == null ) ? null : caref.getBundle(); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | protected ConfigurationAdmin getConfigurationAdmin() |
| 248 | { |
| 249 | ConfigurationAdmin ca = ( ConfigurationAdmin ) configAdminTracker.getService(); |
| 250 | if ( ca == null ) |
| 251 | { |
| 252 | TestCase.fail( "Missing ConfigurationAdmin service" ); |
| 253 | } |
| 254 | return ca; |
| 255 | } |
| 256 | |
| 257 | |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 258 | protected Configuration configure( final String pid ) |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 259 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 260 | return configure( pid, null, true ); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | protected Configuration configure( final String pid, final String location, final boolean withProps ) |
| 265 | { |
| 266 | final ConfigurationAdmin ca = getConfigurationAdmin(); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 267 | try |
| 268 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 269 | final Configuration config = ca.getConfiguration( pid, location ); |
| 270 | if ( withProps ) |
| 271 | { |
| 272 | config.update( theConfig ); |
| 273 | } |
| 274 | return config; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 275 | } |
| 276 | catch ( IOException ioe ) |
| 277 | { |
| 278 | TestCase.fail( "Failed updating configuration " + pid + ": " + ioe.toString() ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 279 | return null; // keep the compiler quiet |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | |
| 284 | protected Configuration createFactoryConfiguration( final String factoryPid ) |
| 285 | { |
| 286 | return createFactoryConfiguration( factoryPid, null, true ); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | protected Configuration createFactoryConfiguration( final String factoryPid, final String location, |
| 291 | final boolean withProps ) |
| 292 | { |
| 293 | final ConfigurationAdmin ca = getConfigurationAdmin(); |
| 294 | try |
| 295 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 296 | final Configuration config = ca.createFactoryConfiguration( factoryPid, location ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 297 | if ( withProps ) |
| 298 | { |
| 299 | config.update( theConfig ); |
| 300 | } |
| 301 | return config; |
| 302 | } |
| 303 | catch ( IOException ioe ) |
| 304 | { |
| 305 | TestCase.fail( "Failed updating factory configuration " + factoryPid + ": " + ioe.toString() ); |
| 306 | return null; // keep the compiler quiet |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | |
| 311 | protected Configuration getConfiguration( final String pid ) |
| 312 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 313 | final ConfigurationAdmin ca = getConfigurationAdmin(); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 314 | try |
| 315 | { |
| 316 | final String filter = "(" + Constants.SERVICE_PID + "=" + pid + ")"; |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 317 | final Configuration[] configs = ca.listConfigurations( filter ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 318 | if ( configs != null && configs.length > 0 ) |
| 319 | { |
| 320 | return configs[0]; |
| 321 | } |
| 322 | } |
| 323 | catch ( InvalidSyntaxException ise ) |
| 324 | { |
| 325 | // unexpected |
| 326 | } |
| 327 | catch ( IOException ioe ) |
| 328 | { |
| 329 | TestCase.fail( "Failed listing configurations " + pid + ": " + ioe.toString() ); |
| 330 | } |
| 331 | |
| 332 | TestCase.fail( "No Configuration " + pid + " found" ); |
| 333 | return null; |
| 334 | } |
| 335 | |
| 336 | |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 337 | protected void deleteConfig( final String pid ) |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 338 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 339 | final ConfigurationAdmin ca = getConfigurationAdmin(); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 340 | try |
| 341 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 342 | configurations.remove( pid ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 343 | final Configuration config = ca.getConfiguration( pid ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 344 | config.delete(); |
| 345 | } |
| 346 | catch ( IOException ioe ) |
| 347 | { |
| 348 | TestCase.fail( "Failed deleting configuration " + pid + ": " + ioe.toString() ); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 353 | protected void deleteFactoryConfigurations( String factoryPid ) |
| 354 | { |
| 355 | ConfigurationAdmin ca = getConfigurationAdmin(); |
| 356 | try |
| 357 | { |
| 358 | final String filter = "(service.factoryPid=" + factoryPid + ")"; |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 359 | Configuration[] configs = ca.listConfigurations( filter ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 360 | if ( configs != null ) |
| 361 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 362 | for ( Configuration configuration : configs ) |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 363 | { |
Felix Meschberger | 007c50e | 2011-10-20 12:39:38 +0000 | [diff] [blame] | 364 | configurations.remove( configuration.getPid() ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 365 | configuration.delete(); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | catch ( InvalidSyntaxException ise ) |
| 370 | { |
| 371 | // unexpected |
| 372 | } |
| 373 | catch ( IOException ioe ) |
| 374 | { |
| 375 | TestCase.fail( "Failed deleting configurations " + factoryPid + ": " + ioe.toString() ); |
| 376 | } |
| 377 | } |
| 378 | } |