org.apache.felix.ipojo.junit4osgi
Class OSGiTestCase

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by org.apache.felix.ipojo.junit4osgi.OSGiTestCase
All Implemented Interfaces:
junit.framework.Test
Direct Known Subclasses:
TestOSGiTestCase

public class OSGiTestCase
extends junit.framework.TestCase

OSGi Test Case. Allows the injection of the bundle context.

Author:
Felix Project Team

Field Summary
protected  BundleContext context
          The bundle context.
 
Constructor Summary
OSGiTestCase()
           
 
Method Summary
 void addHelper(Helper helper)
          Add an helper.
static void assertContains(String message, byte[] array, int num)
          Checks that the given integer is contained in the given array.
static void assertContains(String message, char[] array, char character)
          Checks that the given character is contained in the given array.
static void assertContains(String message, double[] array, double num)
          Checks that the given double is contained in the given array.
static void assertContains(String message, float[] array, float num)
          Checks that the given float is contained in the given array.
static void assertContains(String message, int[] array, int num)
          Checks that the given integer is contained in the given array.
static void assertContains(String message, long[] array, long num)
          Checks that the given long is contained in the given array.
static void assertContains(String message, short[] array, int num)
          Checks that the given integer is contained in the given array.
static void assertContains(String message, String[] array, String txt)
          Checks that the given string is contained in the given array.
static void assertEquals(String message, double expected, double actual)
          Asserts that two doubles are equal.
static void assertNotEquals(String message, Object o1, Object o2)
          Asserts that two objects are not equal.
static boolean contains(int value, int[] array)
          Checks that the given integer is contained in the given array.
static boolean contains(String string, String[] array)
          Checks that the given string is contained in the given array.
 Bundle getBundle(long bundleId)
          Get the bundle by its id.
 Bundle getBundle(String name)
          Gets a bundle by its symbolic name.
 BundleContext getBundleContext()
           
 BundleContext getContext()
          Gets the Bundle Context.
 PackageAdmin getPackageAdmin()
          Gets the package admin exposed by the framework.
static Object getServiceObject(Bundle bundle, String itf, String filter)
          Returns the service object of a service provided by the specified bundle, offering the specified interface and matching the given filter.
 Object getServiceObject(ServiceReference ref)
          Returns the service object associated with this service reference.
 Object getServiceObject(String itf, String filter)
          Returns the service object of a service provided by the local bundle, offering the specified interface and matching the given filter.
static Object[] getServiceObjects(Bundle bundle, String itf, String filter)
          Returns the service objects of the services provided by the specified bundle, offering the specified interface and matching the given filter.
 Object[] getServiceObjects(String itf, String filter)
          Returns the service objects of the services provided by the local bundle, offering the specified interface and matching the given filter.
static ServiceReference getServiceReference(Bundle bundle, String itf, String filter)
          Returns the service reference of a service provided by the specified bundle, offering the specified interface and matching the given filter.
 ServiceReference getServiceReference(String itf)
          Returns the service reference of a service provided offering the specified interface.
 ServiceReference getServiceReference(String itf, String filter)
          Returns the service reference of a service provided by the local bundle, offering the specified interface and matching the given filter.
static ServiceReference getServiceReferenceByPID(Bundle bundle, String itf, String pid)
          Returns the service reference of the service provided by the specified bundle, offering the specified interface and having the given persistent ID.
 ServiceReference getServiceReferenceByPID(String itf, String pid)
          Returns the service reference of the service provided by the local bundle, offering the specified interface and having the given persistent ID.
static ServiceReference[] getServiceReferences(Bundle bundle, String itf, String filter)
          Returns the service reference of all the services provided in the specified bundle, offering the specified interface and matching the given filter.
 ServiceReference[] getServiceReferences(String itf, String filter)
          Returns the service reference of all the services provided in the local bundle, offering the specified interface and matching the given filter.
 Bundle installAndStart(String url)
          Installs and starts a bundle.
 Bundle installAndStart(String url, InputStream stream)
          Installs and starts a bundle.
 Bundle installBundle(String url)
          Installs a bundle.
 Bundle installBundle(String url, InputStream stream)
          Installs a bundle.
 boolean isServiceAvailable(String itf)
          Checks if the service is available.
 boolean isServiceAvailableByPID(String itf, String pid)
          Checks if the service is available.
 void refresh()
          Refresh the packages.
 void runBare()
          Extends runBare to release (unget) services after the teardown.
 void setBundleContext(BundleContext bc)
           
 void waitForService(String itf, String filter, long timeout)
          Waits for a service.
 
Methods inherited from class junit.framework.TestCase
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, countTestCases, createResult, fail, fail, failNotEquals, failNotSame, failSame, format, getName, run, run, runTest, setName, setUp, tearDown, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

context

protected BundleContext context
The bundle context.

Constructor Detail

OSGiTestCase

public OSGiTestCase()
Method Detail

getContext

public BundleContext getContext()
Gets the Bundle Context.

Returns:
the bundle context.

addHelper

public void addHelper(Helper helper)
Add an helper. This method is called by the Helper.Helper(OSGiTestCase) method.

Parameters:
helper - the helper object.

runBare

public void runBare()
             throws Throwable
Extends runBare to release (unget) services after the teardown.

Overrides:
runBare in class junit.framework.TestCase
Throws:
Throwable - when an error occurs.
See Also:
TestCase.runBare()

setBundleContext

public void setBundleContext(BundleContext bc)

getBundleContext

public BundleContext getBundleContext()

assertContains

public static void assertContains(String message,
                                  String[] array,
                                  String txt)
Checks that the given string is contained in the given array.

Parameters:
message - the assert point message
array - the String array
txt - the String to search

assertContains

public static void assertContains(String message,
                                  byte[] array,
                                  int num)
Checks that the given integer is contained in the given array.

Parameters:
message - the assert point message
array - the byte array
num - the number to search

assertContains

public static void assertContains(String message,
                                  short[] array,
                                  int num)
Checks that the given integer is contained in the given array.

Parameters:
message - the assert point message
array - the short array
num - the number to search

assertContains

public static void assertContains(String message,
                                  int[] array,
                                  int num)
Checks that the given integer is contained in the given array.

Parameters:
message - the assert point message
array - the integer array
num - the number to search

assertContains

public static void assertContains(String message,
                                  long[] array,
                                  long num)
Checks that the given long is contained in the given array.

Parameters:
message - the assert point message
array - the long array
num - the number to search

assertContains

public static void assertContains(String message,
                                  float[] array,
                                  float num)
Checks that the given float is contained in the given array.

Parameters:
message - the assert point message
array - the float array
num - the number to search

assertContains

public static void assertContains(String message,
                                  double[] array,
                                  double num)
Checks that the given double is contained in the given array.

Parameters:
message - the assert point message
array - the double array
num - the number to search

assertContains

public static void assertContains(String message,
                                  char[] array,
                                  char character)
Checks that the given character is contained in the given array.

Parameters:
message - the assert point message
array - the character array
character - the character to search

assertEquals

public static void assertEquals(String message,
                                double expected,
                                double actual)
Asserts that two doubles are equal. If they are not an AssertionFailedError is thrown with the given message.

Parameters:
message - the assert point message
expected - the expected double
actual - the received double

assertNotEquals

public static void assertNotEquals(String message,
                                   Object o1,
                                   Object o2)
Asserts that two objects are not equal. If they are an AssertionFailedError is thrown with the given message.

Parameters:
message - the assert point message
o1 - the unexpected object
o2 - the received object

contains

public static boolean contains(String string,
                               String[] array)
Checks that the given string is contained in the given array.

Parameters:
string - the String to search
array - the String array
Returns:
true if the array contains the string

contains

public static boolean contains(int value,
                               int[] array)
Checks that the given integer is contained in the given array.

Parameters:
value - the number to search
array - the integer array
Returns:
true if the array contains the value

getServiceObject

public static Object getServiceObject(Bundle bundle,
                                      String itf,
                                      String filter)
Returns the service object of a service provided by the specified bundle, offering the specified interface and matching the given filter.

Parameters:
bundle - the bundle from which the service is searched.
itf - the interface provided by the searched service.
filter - an additional filter (can be null).
Returns:
the service object provided by the specified bundle, offering the specified interface and matching the given filter.

getServiceObjects

public static Object[] getServiceObjects(Bundle bundle,
                                         String itf,
                                         String filter)
Returns the service objects of the services provided by the specified bundle, offering the specified interface and matching the given filter.

Parameters:
bundle - the bundle from which services are searched.
itf - the interface provided by the searched services.
filter - an additional filter (can be null).
Returns:
the service objects provided by the specified bundle, offering the specified interface and matching the given filter.

getServiceReference

public static ServiceReference getServiceReference(Bundle bundle,
                                                   String itf,
                                                   String filter)
Returns the service reference of a service provided by the specified bundle, offering the specified interface and matching the given filter.

Parameters:
bundle - the bundle from which the service is searched.
itf - the interface provided by the searched service.
filter - an additional filter (can be null).
Returns:
a service reference provided by the specified bundle, offering the specified interface and matching the given filter. If no service is found, null is returned.

isServiceAvailable

public boolean isServiceAvailable(String itf)
Checks if the service is available.

Parameters:
itf - the service interface
Returns:
true if the service is available, false otherwise.

isServiceAvailableByPID

public boolean isServiceAvailableByPID(String itf,
                                       String pid)
Checks if the service is available.

Parameters:
itf - the service interface
pid - the service pid
Returns:
true if the service is available, false otherwise.

getServiceReferenceByPID

public static ServiceReference getServiceReferenceByPID(Bundle bundle,
                                                        String itf,
                                                        String pid)
Returns the service reference of the service provided by the specified bundle, offering the specified interface and having the given persistent ID.

Parameters:
bundle - the bundle from which the service is searched.
itf - the interface provided by the searched service.
pid - the persistent ID of the searched service.
Returns:
a service provided by the specified bundle, offering the specified interface and having the given persistent ID.

getServiceReferences

public static ServiceReference[] getServiceReferences(Bundle bundle,
                                                      String itf,
                                                      String filter)
Returns the service reference of all the services provided in the specified bundle, offering the specified interface and matching the given filter.

Parameters:
bundle - the bundle from which services are searched.
itf - the interface provided by the searched services.
filter - an additional filter (can be null).
Returns:
all the service references provided in the specified bundle, offering the specified interface and matching the given filter. If no service matches, an empty array is returned.

getServiceObject

public Object getServiceObject(String itf,
                               String filter)
Returns the service object of a service provided by the local bundle, offering the specified interface and matching the given filter.

Parameters:
itf - the interface provided by the searched service.
filter - an additional filter (can be null).
Returns:
the service object provided by the local bundle, offering the specified interface and matching the given filter.

getServiceObject

public Object getServiceObject(ServiceReference ref)
Returns the service object associated with this service reference.

Parameters:
ref - service reference
Returns:
the service object.

getServiceObjects

public Object[] getServiceObjects(String itf,
                                  String filter)
Returns the service objects of the services provided by the local bundle, offering the specified interface and matching the given filter.

Parameters:
itf - the interface provided by the searched services.
filter - an additional filter (can be null).
Returns:
the service objects provided by the local bundle, offering the specified interface and matching the given filter.

getServiceReference

public ServiceReference getServiceReference(String itf,
                                            String filter)
Returns the service reference of a service provided by the local bundle, offering the specified interface and matching the given filter.

Parameters:
itf - the interface provided by the searched service.
filter - an additional filter (can be null).
Returns:
a service reference provided by the local bundle, offering the specified interface and matching the given filter. If no service is found, null is returned.

getServiceReference

public ServiceReference getServiceReference(String itf)
Returns the service reference of a service provided offering the specified interface.

Parameters:
itf - the interface provided by the searched service.
Returns:
a service reference provided by the local bundle, offering the specified interface and matching the given filter. If no service is found, null is returned.

getServiceReferenceByPID

public ServiceReference getServiceReferenceByPID(String itf,
                                                 String pid)
Returns the service reference of the service provided by the local bundle, offering the specified interface and having the given persistent ID.

Parameters:
itf - the interface provided by the searched service.
pid - the persistent ID of the searched service.
Returns:
a service provided by the local bundle, offering the specified interface and having the given persistent ID.

getServiceReferences

public ServiceReference[] getServiceReferences(String itf,
                                               String filter)
Returns the service reference of all the services provided in the local bundle, offering the specified interface and matching the given filter.

Parameters:
itf - the interface provided by the searched services.
filter - an additional filter (can be null).
Returns:
all the service references provided in the local bundle, offering the specified interface and matching the given filter. If no service matches, an empty array is returned.

getPackageAdmin

public PackageAdmin getPackageAdmin()
Gets the package admin exposed by the framework. Fails if the package admin is not available.

Returns:
the package admin service.

refresh

public void refresh()
Refresh the packages. Fails if the package admin service is not available.


waitForService

public void waitForService(String itf,
                           String filter,
                           long timeout)
Waits for a service. Fails on timeout. If timeout is set to 0, it sets the timeout to 10s.

Parameters:
itf - the service interface
filter - the filter
timeout - the timeout

installBundle

public Bundle installBundle(String url)
Installs a bundle. Fails if the bundle cannot be installed. Be aware that you have to uninstall the bundle yourself.

Parameters:
url - bundle url
Returns:
the installed bundle

installBundle

public Bundle installBundle(String url,
                            InputStream stream)
Installs a bundle. Fails if the bundle cannot be installed. Be aware that you have to uninstall the bundle yourself.

Parameters:
url - bundle url
stream - input stream containing the bundle
Returns:
the installed bundle

installAndStart

public Bundle installAndStart(String url)
Installs and starts a bundle. Fails if the bundle cannot be installed or an error occurs during startup. Be aware that you have to uninstall the bundle yourself.

Parameters:
url - the bundle url
Returns:
the Bundle object.

installAndStart

public Bundle installAndStart(String url,
                              InputStream stream)
Installs and starts a bundle. Fails if the bundle cannot be installed or an error occurs during startup. Be aware that you have to uninstall the bundle yourself.

Parameters:
url - the bundle url
stream - input stream containing the bundle
Returns:
the Bundle object.

getBundle

public Bundle getBundle(long bundleId)
Get the bundle by its id.

Parameters:
bundleId - the bundle id.
Returns:
the bundle with the given id.

getBundle

public Bundle getBundle(String name)
Gets a bundle by its symbolic name. Fails if no bundle matches.

Parameters:
name - the symbolic name of the bundle
Returns:
the bundle object.


Copyright © 2013 The Apache Software Foundation. All Rights Reserved.