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 | |
| 22 | import static org.ops4j.pax.exam.CoreOptions.mavenBundle; |
| 23 | import static org.ops4j.pax.exam.CoreOptions.options; |
| 24 | import static org.ops4j.pax.exam.CoreOptions.provision; |
| 25 | import static org.ops4j.pax.exam.container.def.PaxRunnerOptions.scanDir; |
| 26 | import static org.ops4j.pax.swissbox.tinybundles.core.TinyBundles.withBnd; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.io.InputStream; |
| 30 | import java.util.Dictionary; |
| 31 | import java.util.Hashtable; |
| 32 | import junit.framework.TestCase; |
| 33 | |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 34 | import org.apache.felix.cm.integration.helper.ManagedServiceTestActivator; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 35 | import org.apache.felix.cm.integration.helper.MyTinyBundle; |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 36 | import org.apache.felix.cm.integration.helper.BaseTestActivator; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 37 | import org.junit.After; |
| 38 | import org.junit.Before; |
| 39 | import org.ops4j.pax.exam.Inject; |
| 40 | import org.ops4j.pax.exam.Option; |
| 41 | import org.ops4j.pax.swissbox.tinybundles.core.TinyBundles; |
| 42 | import org.osgi.framework.Bundle; |
| 43 | import org.osgi.framework.BundleContext; |
| 44 | import org.osgi.framework.BundleException; |
| 45 | import org.osgi.framework.Constants; |
| 46 | import org.osgi.framework.InvalidSyntaxException; |
| 47 | import org.osgi.framework.ServiceReference; |
| 48 | import org.osgi.service.cm.Configuration; |
| 49 | import org.osgi.service.cm.ConfigurationAdmin; |
| 50 | import org.osgi.util.tracker.ServiceTracker; |
| 51 | |
| 52 | |
| 53 | public abstract class ConfigurationTestBase |
| 54 | { |
| 55 | |
| 56 | @Inject |
| 57 | protected BundleContext bundleContext; |
| 58 | |
| 59 | protected Bundle bundle; |
| 60 | |
| 61 | protected ServiceTracker configAdminTracker; |
| 62 | |
| 63 | protected static final String PROP_NAME = "theValue"; |
| 64 | protected static final Dictionary<String, String> theConfig; |
| 65 | |
| 66 | static |
| 67 | { |
| 68 | theConfig = new Hashtable<String, String>(); |
| 69 | theConfig.put( PROP_NAME, PROP_NAME ); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | @org.ops4j.pax.exam.junit.Configuration |
| 74 | public static Option[] configuration() |
| 75 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 76 | return options( provision( scanDir( "target" ).filter( "*.jar" ), mavenBundle( "org.ops4j.pax.swissbox", |
| 77 | "pax-swissbox-tinybundles", "1.0.0" ) ) |
| 78 | // , PaxRunnerOptions.vmOption("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=30303" ) |
| 79 | // , PaxRunnerOptions.logProfile() |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 80 | ); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | @Before |
| 85 | public void setUp() |
| 86 | { |
| 87 | configAdminTracker = new ServiceTracker( bundleContext, ConfigurationAdmin.class.getName(), null ); |
| 88 | configAdminTracker.open(); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | @After |
| 93 | public void tearDown() throws BundleException |
| 94 | { |
| 95 | if ( bundle != null ) |
| 96 | { |
| 97 | bundle.uninstall(); |
| 98 | } |
| 99 | |
| 100 | configAdminTracker.close(); |
| 101 | configAdminTracker = null; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | protected Bundle installBundle( final String pid ) throws BundleException |
| 106 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 107 | return installBundle( pid, ManagedServiceTestActivator.class ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
| 111 | protected Bundle installBundle( final String pid, final Class<?> activatorClass ) throws BundleException |
| 112 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 113 | final String activatorClassName = activatorClass.getName(); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 114 | final InputStream bundleStream = new MyTinyBundle().prepare( |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 115 | withBnd().set( Constants.BUNDLE_SYMBOLICNAME, activatorClassName ).set( Constants.BUNDLE_VERSION, "0.0.11" ) |
| 116 | .set( Constants.IMPORT_PACKAGE, "org.apache.felix.cm.integration.helper" ).set( |
| 117 | Constants.BUNDLE_ACTIVATOR, activatorClassName ).set( BaseTestActivator.HEADER_PID, pid ) ).build( |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 118 | TinyBundles.asStream() ); |
| 119 | |
| 120 | try |
| 121 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 122 | return bundleContext.installBundle( "test:" + activatorClassName, bundleStream ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 123 | } |
| 124 | finally |
| 125 | { |
| 126 | try |
| 127 | { |
| 128 | bundleStream.close(); |
| 129 | } |
| 130 | catch ( IOException ioe ) |
| 131 | { |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | protected static void delay() |
| 138 | { |
| 139 | try |
| 140 | { |
| 141 | Thread.sleep( 300 ); |
| 142 | } |
| 143 | catch ( InterruptedException ie ) |
| 144 | { |
| 145 | // dont care |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | |
| 150 | protected Bundle getCmBundle() |
| 151 | { |
| 152 | final ServiceReference caref = configAdminTracker.getServiceReference(); |
| 153 | return ( caref == null ) ? null : caref.getBundle(); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | protected ConfigurationAdmin getConfigurationAdmin() |
| 158 | { |
| 159 | ConfigurationAdmin ca = ( ConfigurationAdmin ) configAdminTracker.getService(); |
| 160 | if ( ca == null ) |
| 161 | { |
| 162 | TestCase.fail( "Missing ConfigurationAdmin service" ); |
| 163 | } |
| 164 | return ca; |
| 165 | } |
| 166 | |
| 167 | |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 168 | protected Configuration configure( final String pid ) |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 169 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 170 | return configure( pid, null, true ); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | protected Configuration configure( final String pid, final String location, final boolean withProps ) |
| 175 | { |
| 176 | final ConfigurationAdmin ca = getConfigurationAdmin(); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 177 | try |
| 178 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 179 | final Configuration config = ca.getConfiguration( pid, location ); |
| 180 | if ( withProps ) |
| 181 | { |
| 182 | config.update( theConfig ); |
| 183 | } |
| 184 | return config; |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 185 | } |
| 186 | catch ( IOException ioe ) |
| 187 | { |
| 188 | TestCase.fail( "Failed updating configuration " + pid + ": " + ioe.toString() ); |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 189 | return null; // keep the compiler quiet |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | |
| 194 | protected Configuration createFactoryConfiguration( final String factoryPid ) |
| 195 | { |
| 196 | return createFactoryConfiguration( factoryPid, null, true ); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | protected Configuration createFactoryConfiguration( final String factoryPid, final String location, |
| 201 | final boolean withProps ) |
| 202 | { |
| 203 | final ConfigurationAdmin ca = getConfigurationAdmin(); |
| 204 | try |
| 205 | { |
| 206 | final Configuration config = ca.createFactoryConfiguration( factoryPid, null ); |
| 207 | if ( withProps ) |
| 208 | { |
| 209 | config.update( theConfig ); |
| 210 | } |
| 211 | return config; |
| 212 | } |
| 213 | catch ( IOException ioe ) |
| 214 | { |
| 215 | TestCase.fail( "Failed updating factory configuration " + factoryPid + ": " + ioe.toString() ); |
| 216 | return null; // keep the compiler quiet |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | |
| 221 | protected Configuration getConfiguration( final String pid ) |
| 222 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 223 | final ConfigurationAdmin ca = getConfigurationAdmin(); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 224 | try |
| 225 | { |
| 226 | final String filter = "(" + Constants.SERVICE_PID + "=" + pid + ")"; |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 227 | final Configuration[] configs = ca.listConfigurations( filter ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 228 | if ( configs != null && configs.length > 0 ) |
| 229 | { |
| 230 | return configs[0]; |
| 231 | } |
| 232 | } |
| 233 | catch ( InvalidSyntaxException ise ) |
| 234 | { |
| 235 | // unexpected |
| 236 | } |
| 237 | catch ( IOException ioe ) |
| 238 | { |
| 239 | TestCase.fail( "Failed listing configurations " + pid + ": " + ioe.toString() ); |
| 240 | } |
| 241 | |
| 242 | TestCase.fail( "No Configuration " + pid + " found" ); |
| 243 | return null; |
| 244 | } |
| 245 | |
| 246 | |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 247 | protected void deleteConfig( final String pid ) |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 248 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 249 | final ConfigurationAdmin ca = getConfigurationAdmin(); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 250 | try |
| 251 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 252 | final Configuration config = ca.getConfiguration( pid ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 253 | config.delete(); |
| 254 | } |
| 255 | catch ( IOException ioe ) |
| 256 | { |
| 257 | TestCase.fail( "Failed deleting configuration " + pid + ": " + ioe.toString() ); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 262 | protected void deleteFactoryConfigurations( String factoryPid ) |
| 263 | { |
| 264 | ConfigurationAdmin ca = getConfigurationAdmin(); |
| 265 | try |
| 266 | { |
| 267 | final String filter = "(service.factoryPid=" + factoryPid + ")"; |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 268 | Configuration[] configs = ca.listConfigurations( filter ); |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 269 | if ( configs != null ) |
| 270 | { |
Felix Meschberger | 8659e39 | 2009-08-19 05:46:49 +0000 | [diff] [blame] | 271 | for ( Configuration configuration : configs ) |
Felix Meschberger | 42764b8 | 2009-08-18 07:51:00 +0000 | [diff] [blame] | 272 | { |
| 273 | configuration.delete(); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | catch ( InvalidSyntaxException ise ) |
| 278 | { |
| 279 | // unexpected |
| 280 | } |
| 281 | catch ( IOException ioe ) |
| 282 | { |
| 283 | TestCase.fail( "Failed deleting configurations " + factoryPid + ": " + ioe.toString() ); |
| 284 | } |
| 285 | } |
| 286 | } |