blob: f1002f96aa5e031e49fe9ea6d10e837307c243de [file] [log] [blame]
Felix Meschberger42764b82009-08-18 07:51:00 +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 static org.ops4j.pax.exam.CoreOptions.mavenBundle;
23import static org.ops4j.pax.exam.CoreOptions.options;
24import static org.ops4j.pax.exam.CoreOptions.provision;
25import static org.ops4j.pax.exam.container.def.PaxRunnerOptions.scanDir;
26import static org.ops4j.pax.swissbox.tinybundles.core.TinyBundles.withBnd;
27
28import java.io.IOException;
29import java.io.InputStream;
30import java.util.Dictionary;
31import java.util.Hashtable;
32import junit.framework.TestCase;
33
Felix Meschberger8659e392009-08-19 05:46:49 +000034import org.apache.felix.cm.integration.helper.ManagedServiceTestActivator;
Felix Meschberger42764b82009-08-18 07:51:00 +000035import org.apache.felix.cm.integration.helper.MyTinyBundle;
Felix Meschberger8659e392009-08-19 05:46:49 +000036import org.apache.felix.cm.integration.helper.BaseTestActivator;
Felix Meschberger42764b82009-08-18 07:51:00 +000037import org.junit.After;
38import org.junit.Before;
39import org.ops4j.pax.exam.Inject;
40import org.ops4j.pax.exam.Option;
41import org.ops4j.pax.swissbox.tinybundles.core.TinyBundles;
42import org.osgi.framework.Bundle;
43import org.osgi.framework.BundleContext;
44import org.osgi.framework.BundleException;
45import org.osgi.framework.Constants;
46import org.osgi.framework.InvalidSyntaxException;
47import org.osgi.framework.ServiceReference;
48import org.osgi.service.cm.Configuration;
49import org.osgi.service.cm.ConfigurationAdmin;
50import org.osgi.util.tracker.ServiceTracker;
51
52
53public 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 Meschberger8659e392009-08-19 05:46:49 +000076 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 Meschberger42764b82009-08-18 07:51:00 +000080 );
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 Meschberger8659e392009-08-19 05:46:49 +0000107 return installBundle( pid, ManagedServiceTestActivator.class );
Felix Meschberger42764b82009-08-18 07:51:00 +0000108 }
109
110
111 protected Bundle installBundle( final String pid, final Class<?> activatorClass ) throws BundleException
112 {
Felix Meschberger8659e392009-08-19 05:46:49 +0000113 final String activatorClassName = activatorClass.getName();
Felix Meschberger42764b82009-08-18 07:51:00 +0000114 final InputStream bundleStream = new MyTinyBundle().prepare(
Felix Meschberger8659e392009-08-19 05:46:49 +0000115 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 Meschberger42764b82009-08-18 07:51:00 +0000118 TinyBundles.asStream() );
119
120 try
121 {
Felix Meschberger8659e392009-08-19 05:46:49 +0000122 return bundleContext.installBundle( "test:" + activatorClassName, bundleStream );
Felix Meschberger42764b82009-08-18 07:51:00 +0000123 }
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 Meschberger8659e392009-08-19 05:46:49 +0000168 protected Configuration configure( final String pid )
Felix Meschberger42764b82009-08-18 07:51:00 +0000169 {
Felix Meschberger8659e392009-08-19 05:46:49 +0000170 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 Meschberger42764b82009-08-18 07:51:00 +0000177 try
178 {
Felix Meschberger8659e392009-08-19 05:46:49 +0000179 final Configuration config = ca.getConfiguration( pid, location );
180 if ( withProps )
181 {
182 config.update( theConfig );
183 }
184 return config;
Felix Meschberger42764b82009-08-18 07:51:00 +0000185 }
186 catch ( IOException ioe )
187 {
188 TestCase.fail( "Failed updating configuration " + pid + ": " + ioe.toString() );
Felix Meschberger8659e392009-08-19 05:46:49 +0000189 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 Meschberger42764b82009-08-18 07:51:00 +0000217 }
218 }
219
220
221 protected Configuration getConfiguration( final String pid )
222 {
Felix Meschberger8659e392009-08-19 05:46:49 +0000223 final ConfigurationAdmin ca = getConfigurationAdmin();
Felix Meschberger42764b82009-08-18 07:51:00 +0000224 try
225 {
226 final String filter = "(" + Constants.SERVICE_PID + "=" + pid + ")";
Felix Meschberger8659e392009-08-19 05:46:49 +0000227 final Configuration[] configs = ca.listConfigurations( filter );
Felix Meschberger42764b82009-08-18 07:51:00 +0000228 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 Meschberger8659e392009-08-19 05:46:49 +0000247 protected void deleteConfig( final String pid )
Felix Meschberger42764b82009-08-18 07:51:00 +0000248 {
Felix Meschberger8659e392009-08-19 05:46:49 +0000249 final ConfigurationAdmin ca = getConfigurationAdmin();
Felix Meschberger42764b82009-08-18 07:51:00 +0000250 try
251 {
Felix Meschberger8659e392009-08-19 05:46:49 +0000252 final Configuration config = ca.getConfiguration( pid );
Felix Meschberger42764b82009-08-18 07:51:00 +0000253 config.delete();
254 }
255 catch ( IOException ioe )
256 {
257 TestCase.fail( "Failed deleting configuration " + pid + ": " + ioe.toString() );
258 }
259 }
260
261
Felix Meschberger42764b82009-08-18 07:51:00 +0000262 protected void deleteFactoryConfigurations( String factoryPid )
263 {
264 ConfigurationAdmin ca = getConfigurationAdmin();
265 try
266 {
267 final String filter = "(service.factoryPid=" + factoryPid + ")";
Felix Meschberger8659e392009-08-19 05:46:49 +0000268 Configuration[] configs = ca.listConfigurations( filter );
Felix Meschberger42764b82009-08-18 07:51:00 +0000269 if ( configs != null )
270 {
Felix Meschberger8659e392009-08-19 05:46:49 +0000271 for ( Configuration configuration : configs )
Felix Meschberger42764b82009-08-18 07:51:00 +0000272 {
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}