blob: d13a9ee9c8fa24ae6470417ffb0af17857fba393 [file] [log] [blame]
Felix Meschbergera6ad9762009-08-19 13:38:56 +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.scr.integration;
20
21
Felix Meschberger4f43c912012-03-08 04:37:56 +000022import static org.ops4j.pax.exam.CoreOptions.junitBundles;
Felix Meschbergera6ad9762009-08-19 13:38:56 +000023import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
24import static org.ops4j.pax.exam.CoreOptions.options;
25import static org.ops4j.pax.exam.CoreOptions.provision;
Felix Meschberger12d8c102009-12-18 12:47:44 +000026import static org.ops4j.pax.exam.CoreOptions.systemProperty;
Felix Meschberger4f43c912012-03-08 04:37:56 +000027import static org.ops4j.pax.tinybundles.core.TinyBundles.bundle;
28import static org.ops4j.pax.tinybundles.core.TinyBundles.withBnd;
Felix Meschbergera6ad9762009-08-19 13:38:56 +000029
Felix Meschberger2e885422009-08-21 11:35:00 +000030import java.io.File;
Felix Meschbergera6ad9762009-08-19 13:38:56 +000031import java.io.IOException;
32import java.io.InputStream;
David Jencks8584cf92012-06-16 03:56:11 +000033import java.io.PrintStream;
Felix Meschberger2e885422009-08-21 11:35:00 +000034import java.lang.reflect.Field;
David Jencks8584cf92012-06-16 03:56:11 +000035import java.util.Arrays;
36import java.util.Collections;
Felix Meschbergera6ad9762009-08-19 13:38:56 +000037import java.util.Dictionary;
38import java.util.Hashtable;
David Jencks8584cf92012-06-16 03:56:11 +000039import java.util.Iterator;
40import java.util.TreeSet;
Felix Meschberger4f43c912012-03-08 04:37:56 +000041
42import javax.inject.Inject;
Felix Meschbergera6ad9762009-08-19 13:38:56 +000043import junit.framework.TestCase;
44
45import org.apache.felix.scr.Component;
David Jencks8584cf92012-06-16 03:56:11 +000046import org.apache.felix.scr.Reference;
Felix Meschbergera6ad9762009-08-19 13:38:56 +000047import org.apache.felix.scr.ScrService;
Felix Meschbergera6ad9762009-08-19 13:38:56 +000048import org.junit.After;
49import org.junit.Before;
Felix Meschberger2e885422009-08-21 11:35:00 +000050import org.ops4j.pax.exam.CoreOptions;
Felix Meschbergera6ad9762009-08-19 13:38:56 +000051import org.ops4j.pax.exam.Option;
Felix Meschberger2e885422009-08-21 11:35:00 +000052import org.ops4j.pax.exam.OptionUtils;
Felix Meschberger4f43c912012-03-08 04:37:56 +000053import org.ops4j.pax.exam.TestProbeBuilder;
Felix Meschbergera6ad9762009-08-19 13:38:56 +000054import org.ops4j.pax.exam.junit.Configuration;
Felix Meschberger4f43c912012-03-08 04:37:56 +000055import org.ops4j.pax.exam.junit.ProbeBuilder;
Felix Meschbergera6ad9762009-08-19 13:38:56 +000056import org.osgi.framework.Bundle;
57import org.osgi.framework.BundleContext;
58import org.osgi.framework.BundleException;
59import org.osgi.framework.Constants;
60import org.osgi.framework.InvalidSyntaxException;
61import org.osgi.service.cm.ConfigurationAdmin;
62import org.osgi.util.tracker.ServiceTracker;
63
64
65public abstract class ComponentTestBase
66{
67
68 @Inject
69 protected BundleContext bundleContext;
70
71 protected Bundle bundle;
72
73 protected ServiceTracker scrTracker;
74
75 protected ServiceTracker configAdminTracker;
76
Felix Meschberger55e3b972009-10-12 07:16:08 +000077 // the name of the system property providing the bundle file to be installed and tested
78 protected static final String BUNDLE_JAR_SYS_PROP = "project.bundle.file";
79
80 // the default bundle jar file name
81 protected static final String BUNDLE_JAR_DEFAULT = "target/scr.jar";
82
Felix Meschbergera6ad9762009-08-19 13:38:56 +000083 protected static final String PROP_NAME = "theValue";
84 protected static final Dictionary<String, String> theConfig;
85
Felix Meschberger2e885422009-08-21 11:35:00 +000086 // the JVM option to set to enable remote debugging
87 protected static final String DEBUG_VM_OPTION = "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=30303";
88
89 // the actual JVM option set, extensions may implement a static
90 // initializer overwriting this value to have the configuration()
91 // method include it when starting the OSGi framework JVM
92 protected static String paxRunnerVmOption = null;
93
Felix Meschbergerf975aa52009-08-24 10:37:44 +000094 // the descriptor file to use for the installed test bundle
95 protected static String descriptorFile = "/integration_test_simple_components.xml";
96
David Jencksbad95fc2012-06-05 23:29:58 +000097 protected static boolean NONSTANDARD_COMPONENT_FACTORY_BEHAVIOR = false;
98
Felix Meschbergera6ad9762009-08-19 13:38:56 +000099 static
100 {
101 theConfig = new Hashtable<String, String>();
102 theConfig.put( PROP_NAME, PROP_NAME );
103 }
104
Felix Meschberger4f43c912012-03-08 04:37:56 +0000105 @ProbeBuilder
106 public TestProbeBuilder extendProbe(TestProbeBuilder builder) {
David Jencksa9cbe1b2012-06-15 21:37:30 +0000107 builder.setHeader("Export-Package", "org.apache.felix.scr.integration.components,org.apache.felix.scr.integration.components.activatesignature,org.apache.felix.scr.integration.components.circular");
Felix Meschberger8422a302012-03-08 05:55:06 +0000108 builder.setHeader("Import-Package", "org.apache.felix.scr,org.apache.felix.scr.component;mandatory:=\"status\"; status=\"provisional\"");
109 builder.setHeader("Bundle-ManifestVersion", "2");
Felix Meschberger4f43c912012-03-08 04:37:56 +0000110 return builder;
111 }
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000112
113 @Configuration
114 public static Option[] configuration()
115 {
Felix Meschberger55e3b972009-10-12 07:16:08 +0000116 final String bundleFileName = System.getProperty( BUNDLE_JAR_SYS_PROP, BUNDLE_JAR_DEFAULT );
117 final File bundleFile = new File( bundleFileName );
118 if ( !bundleFile.canRead() )
119 {
120 throw new IllegalArgumentException( "Cannot read from bundle file " + bundleFileName + " specified in the "
121 + BUNDLE_JAR_SYS_PROP + " system property" );
122 }
123
Felix Meschberger2e885422009-08-21 11:35:00 +0000124 final Option[] base = options(
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000125 provision(
Felix Meschberger55e3b972009-10-12 07:16:08 +0000126 CoreOptions.bundle( bundleFile.toURI().toString() ),
Felix Meschberger4f43c912012-03-08 04:37:56 +0000127 mavenBundle( "org.ops4j.pax.tinybundles", "tinybundles", "1.0.0" ),
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000128 mavenBundle( "org.apache.felix", "org.apache.felix.configadmin", "1.0.10" )
Felix Meschberger12d8c102009-12-18 12:47:44 +0000129 ),
Felix Meschberger4f43c912012-03-08 04:37:56 +0000130 junitBundles(),
David Jencksbad95fc2012-06-05 23:29:58 +0000131 systemProperty( "ds.factory.enabled" ).value( Boolean.toString( NONSTANDARD_COMPONENT_FACTORY_BEHAVIOR ) )
Felix Meschberger4f43c912012-03-08 04:37:56 +0000132
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000133 );
Felix Meschberger4f43c912012-03-08 04:37:56 +0000134 final Option vmOption = ( paxRunnerVmOption != null ) ? CoreOptions.vmOption( paxRunnerVmOption ) : null;
Felix Meschberger2e885422009-08-21 11:35:00 +0000135 return OptionUtils.combine( base, vmOption );
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000136 }
137
138
139 @Before
140 public void setUp() throws BundleException
141 {
Felix Meschbergerf873dbe2011-02-04 10:50:18 +0000142 scrTracker = new ServiceTracker( bundleContext, "org.apache.felix.scr.ScrService", null );
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000143 scrTracker.open();
Felix Meschbergerf873dbe2011-02-04 10:50:18 +0000144 configAdminTracker = new ServiceTracker( bundleContext, "org.osgi.service.cm.ConfigurationAdmin", null );
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000145 configAdminTracker.open();
146
Felix Meschbergerf975aa52009-08-24 10:37:44 +0000147 bundle = installBundle( descriptorFile );
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000148 bundle.start();
149 }
150
151
152 @After
153 public void tearDown() throws BundleException
154 {
Felix Meschbergerf975aa52009-08-24 10:37:44 +0000155 if ( bundle != null && bundle.getState() != Bundle.UNINSTALLED )
156 {
157 bundle.uninstall();
158 bundle = null;
159 }
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000160
161 configAdminTracker.close();
162 configAdminTracker = null;
163 scrTracker.close();
164 scrTracker = null;
165 }
166
167
Felix Meschbergered21c032010-01-27 09:58:17 +0000168 protected Component[] getComponents()
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000169 {
170 ScrService scr = ( ScrService ) scrTracker.getService();
171 if ( scr != null )
172 {
Felix Meschbergered21c032010-01-27 09:58:17 +0000173 return scr.getComponents();
174 }
175
176 return null;
177 }
178
179
180 protected Component findComponentByName( String name )
181 {
Felix Meschberger5fa8b0e2010-08-03 08:21:13 +0000182 Component[] components = findComponentsByName( name );
183 if ( components != null && components.length > 0 )
Felix Meschbergered21c032010-01-27 09:58:17 +0000184 {
Felix Meschberger5fa8b0e2010-08-03 08:21:13 +0000185 return components[0];
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000186 }
187
188 return null;
189 }
190
191
192 protected Component[] findComponentsByName( String name )
193 {
Felix Meschberger5fa8b0e2010-08-03 08:21:13 +0000194 ScrService scr = ( ScrService ) scrTracker.getService();
195 if ( scr != null )
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000196 {
Felix Meschberger5fa8b0e2010-08-03 08:21:13 +0000197 return scr.getComponents( name );
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000198 }
199
200 return null;
201 }
202
203
204 protected static void delay()
205 {
206 try
207 {
208 Thread.sleep( 300 );
209 }
210 catch ( InterruptedException ie )
211 {
212 // dont care
213 }
214 }
215
216
217 protected ConfigurationAdmin getConfigurationAdmin()
218 {
219 ConfigurationAdmin ca = ( ConfigurationAdmin ) configAdminTracker.getService();
220 if ( ca == null )
221 {
222 TestCase.fail( "Missing ConfigurationAdmin service" );
223 }
224 return ca;
225 }
226
227
228 protected void configure( String pid )
229 {
230 ConfigurationAdmin ca = getConfigurationAdmin();
231 try
232 {
233 org.osgi.service.cm.Configuration config = ca.getConfiguration( pid, null );
234 config.update( theConfig );
235 }
236 catch ( IOException ioe )
237 {
238 TestCase.fail( "Failed updating configuration " + pid + ": " + ioe.toString() );
239 }
240 }
241
242
243 protected void deleteConfig( String pid )
244 {
245 ConfigurationAdmin ca = getConfigurationAdmin();
246 try
247 {
248 org.osgi.service.cm.Configuration config = ca.getConfiguration( pid );
249 config.delete();
250 }
251 catch ( IOException ioe )
252 {
253 TestCase.fail( "Failed deleting configuration " + pid + ": " + ioe.toString() );
254 }
255 }
256
257
258 protected String createFactoryConfiguration( String factoryPid )
259 {
260 ConfigurationAdmin ca = getConfigurationAdmin();
261 try
262 {
263 org.osgi.service.cm.Configuration config = ca.createFactoryConfiguration( factoryPid, null );
264 config.update( theConfig );
265 return config.getPid();
266 }
267 catch ( IOException ioe )
268 {
269 TestCase.fail( "Failed updating factory configuration " + factoryPid + ": " + ioe.toString() );
270 return null;
271 }
272 }
273
274
275 protected void deleteFactoryConfigurations( String factoryPid )
276 {
277 ConfigurationAdmin ca = getConfigurationAdmin();
278 try
279 {
280 final String filter = "(service.factoryPid=" + factoryPid + ")";
281 org.osgi.service.cm.Configuration[] configs = ca.listConfigurations( filter );
282 if ( configs != null )
283 {
284 for ( org.osgi.service.cm.Configuration configuration : configs )
285 {
286 configuration.delete();
287 }
288 }
289 }
290 catch ( InvalidSyntaxException ise )
291 {
292 // unexpected
293 }
294 catch ( IOException ioe )
295 {
296 TestCase.fail( "Failed deleting configurations " + factoryPid + ": " + ioe.toString() );
297 }
298 }
Felix Meschberger2e885422009-08-21 11:35:00 +0000299
300
301 protected static Class<?> getType( Object object, String desiredName )
302 {
303 Class<?> ccImpl = object.getClass();
304 while ( ccImpl != null && !desiredName.equals( ccImpl.getSimpleName() ) )
305 {
306 ccImpl = ccImpl.getSuperclass();
307 }
308 if ( ccImpl == null )
309 {
310 TestCase.fail( "ComponentContext " + object + " is not a " + desiredName );
311 }
312
313 return ccImpl;
314 }
315
316
317 protected static Object getFieldValue( Object object, String fieldName )
318 {
319 try
320 {
321 final Field m_componentsField = getField( object.getClass(), fieldName );
322 return m_componentsField.get( object );
323 }
324 catch ( Throwable t )
325 {
326 TestCase.fail( "Cannot get " + fieldName + " from " + object + ": " + t );
327 return null; // keep the compiler happy
328 }
329 }
330
331
332 protected static Field getField( Class<?> type, String fieldName ) throws NoSuchFieldException
333 {
Pierre De Ropce7ca4d2012-06-15 16:24:54 +0000334 Class<?> clazz = type;
335 while (clazz != null)
336 {
337 Field[] fields = clazz.getDeclaredFields();
338 for (int i = 0; i < fields.length; i++)
339 {
340 Field field = fields[i];
341 if (field.getName().equals(fieldName))
342 {
343 field.setAccessible( true );
344 return field;
345 }
346 }
347 clazz = clazz.getSuperclass();
348 }
349 throw new NoSuchFieldException(fieldName);
Felix Meschberger2e885422009-08-21 11:35:00 +0000350 }
Felix Meschbergerf975aa52009-08-24 10:37:44 +0000351
352
353 protected Bundle installBundle( final String descriptorFile ) throws BundleException
354 {
Felix Meschberger4f43c912012-03-08 04:37:56 +0000355 final InputStream bundleStream = bundle()
356 .add("OSGI-INF/components.xml", getClass().getResource(descriptorFile))
357
358 .set(Constants.BUNDLE_SYMBOLICNAME, "simplecomponent")
359 .set(Constants.BUNDLE_VERSION, "0.0.11")
360 .set(Constants.IMPORT_PACKAGE,
David Jencksa9cbe1b2012-06-15 21:37:30 +0000361 "org.apache.felix.scr.integration.components,org.apache.felix.scr.integration.components.activatesignature,org.apache.felix.scr.integration.components.circular")
Felix Meschberger4f43c912012-03-08 04:37:56 +0000362 .set("Service-Component", "OSGI-INF/components.xml")
363 .build(withBnd());
Felix Meschbergerf975aa52009-08-24 10:37:44 +0000364
365 try
366 {
367 final String location = "test:SimpleComponent/" + System.currentTimeMillis();
368 return bundleContext.installBundle( location, bundleStream );
369 }
370 finally
371 {
372 try
373 {
374 bundleStream.close();
375 }
376 catch ( IOException ioe )
377 {
378 }
379 }
380 }
381
David Jencks8584cf92012-06-16 03:56:11 +0000382 //Code copied from ScrCommand to make it easier to find out what your test components are actually doing.
383 // @Test
384 public void testDescription()
385 {
386 PrintStream out = System.out;
387 info( out );
388 }
389
390 void info( PrintStream out )
391 {
392 Component[] components = getComponents();
393 if ( components == null )
394 {
395 return;
396 }
397
398 for ( int j = 0; j < components.length; j++ )
399 {
400 Component component = components[j];
401 out.print( "ID: " );
402 out.println( component.getId() );
403 out.print( "Name: " );
404 out.println( component.getName() );
405 out.print( "Bundle: " );
406 out.println( component.getBundle().getSymbolicName() + " (" + component.getBundle().getBundleId() + ")" );
407 out.print( "State: " );
408 out.println( toStateString( component.getState() ) );
409 out.print( "Default State: " );
410 out.println( component.isDefaultEnabled() ? "enabled" : "disabled" );
411 out.print( "Activation: " );
412 out.println( component.isImmediate() ? "immediate" : "delayed" );
413
414 // DS 1.1 new features
415 out.print( "Configuration Policy: " );
416 out.println( component.getConfigurationPolicy() );
417 out.print( "Activate Method: " );
418 out.print( component.getActivate() );
419 if ( component.isActivateDeclared() )
420 {
421 out.print( " (declared in the descriptor)" );
422 }
423 out.println();
424 out.print( "Deactivate Method: " );
425 out.print( component.getDeactivate() );
426 if ( component.isDeactivateDeclared() )
427 {
428 out.print( " (declared in the descriptor)" );
429 }
430 out.println();
431 out.print( "Modified Method: " );
432 if ( component.getModified() != null )
433 {
434 out.print( component.getModified() );
435 }
436 else
437 {
438 out.print( "-" );
439 }
440 out.println();
441
442 if ( component.getFactory() != null )
443 {
444 out.print( "Factory: " );
445 out.println( component.getFactory() );
446 }
447
448 String[] services = component.getServices();
449 if ( services != null )
450 {
451 out.print( "Services: " );
452 out.println( services[0] );
453 for ( int i = 1; i < services.length; i++ )
454 {
455 out.print( " " );
456 out.println( services[i] );
457 }
458 out.print( "Service Type: " );
459 out.println( component.isServiceFactory() ? "service factory" : "service" );
460 }
461
462 Reference[] refs = component.getReferences();
463 if ( refs != null )
464 {
465 for ( int i = 0; i < refs.length; i++ )
466 {
467 out.print( "Reference: " );
468 out.println( refs[i].getName() );
469 out.print( " Satisfied: " );
470 out.println( refs[i].isSatisfied() ? "satisfied" : "unsatisfied" );
471 out.print( " Service Name: " );
472 out.println( refs[i].getServiceName() );
473 if ( refs[i].getTarget() != null )
474 {
475 out.print( " Target Filter: " );
476 out.println( refs[i].getTarget() );
477 }
478 out.print( " Multiple: " );
479 out.println( refs[i].isMultiple() ? "multiple" : "single" );
480 out.print( " Optional: " );
481 out.println( refs[i].isOptional() ? "optional" : "mandatory" );
482 out.print( " Policy: " );
483 out.println( refs[i].isStatic() ? "static" : "dynamic" );
484 out.print( " Policy option: " );
485 out.println( refs[i].isReluctant() ? "reluctant" : "greedy" );
486 }
487 }
488
489 Dictionary props = component.getProperties();
490 if ( props != null )
491 {
492 out.println( "Properties:" );
493 TreeSet keys = new TreeSet( Collections.list( props.keys() ) );
494 for ( Iterator ki = keys.iterator(); ki.hasNext(); )
495 {
496 Object key = ki.next();
497 out.print( " " );
498 out.print( key );
499 out.print( " = " );
500
501 Object prop = props.get( key );
502 if ( prop.getClass().isArray() )
503 {
504 prop = Arrays.asList( ( Object[] ) prop );
505 }
506 out.print( prop );
507
508 out.println();
509 }
510 }
511 }
512 }
513
514 private String toStateString( int state )
515 {
516 switch ( state )
517 {
518 case Component.STATE_DISABLED:
519 return "disabled";
520 case Component.STATE_UNSATISFIED:
521 return "unsatisfied";
522 case Component.STATE_ACTIVE:
523 return "active";
524 case Component.STATE_REGISTERED:
525 return "registered";
526 case Component.STATE_FACTORY:
527 return "factory";
528 case Component.STATE_DISPOSED:
529 return "disposed";
530
531 case Component.STATE_ENABLING:
532 return "enabling";
533 case Component.STATE_ENABLED:
534 return "enabled";
535 case Component.STATE_ACTIVATING:
536 return "activating";
537 case Component.STATE_DEACTIVATING:
538 return "deactivating";
539 case Component.STATE_DISABLING:
540 return "disabling";
541 case Component.STATE_DISPOSING:
542 return "disposing";
543 default:
544 return String.valueOf( state );
545 }
546 }
547
Felix Meschbergera6ad9762009-08-19 13:38:56 +0000548}