Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
{include:apache-felix-ipojo-header}
{html}
<div class="content">
{html}
h1. OSGi and friends methods

_Developing OSGi tests can be definitely boring. First, testing is generally not a very exciting experience, but imagine if you have to handle all the OSGi issues in your tests... Don't worry junit4osgi provides methods allowing to interact easily with OSGi!_

{div:class=toc}
{toc:maxLevel=4|minLevel=2}
{div}

h2. OSGi methods

junit4osgi test case extends the {{OSGiTestCase}} class. This class provides useful methods allowing to find services, get them, get the {{PackageAdmin}} service...  The description of these methods can be found afterwardsafterward.

The most part of the methods are available statically and non-statically. Static methods require a {{Bundle}}. Non static methods use the current bundle context, and track get services to release them when the test is done. So, we advise you to use the non-static methods. Static methods just allow you to check that a specific bundle can access to services / resources.

h3. Service interaction
* {{boolean isServiceAvailable(String svc)}} : returns {{true}} if the service {{svc}} is available.
* {{boolean isServiceAvailableByPID(String itf, String pid)}} : return {{true}} if the service {{svc}} is available and exposed with {{pid}} as {{service.pid}}.

* {{ServiceReference getServiceReference(String itf)}}: returns a service reference exposing the {{itf}} service specification, or {{null}} if not available.
* {{ServiceReference getServiceReference(String itf, String filter)}}: returns a service reference matching with the  \<service interface, filter\> request, or {{null}} is not available.
* {{ServiceReference getServiceReferenceByPID(String itf, String pid)}}: returns a service reference exposing the service {{itf}} and exposed with a {{service.pid}} equals to {{pid}}, or {{null}} if not available.
* {{ServiceReference[] getServiceReferences(String itf, String filter)}}: returns all the service references matching with the  \<service interface, filter\> request, or an empty array is not available.

* {{Object getServiceObject(String itf, String filter)}}: returns a service object matching with the \<service interface, filter\> request or {{null}} is not available.
* {{Object getServiceObject(ServiceReference ref)}}: returns the service object associated with the given {{service reference}} or {{null}} if not available.
* {{Object[] getServiceObjects(String itf, String filter)}}: gets all the service objects matching with the \<service interface, filter\> request or an empty array is no providers are available.

* {{void waitForService(String itf, String filter, long timeout)}}: waits for a service arrival matching with the \<service interface, filter\> request. If the timeout expires, this method fails. 

h3. Get the bundle context
* {{BundleContext getContext()}} : gives access to the OSGiTestCase bundle context.

h3. Install/Start/Uninstall bundles
* {{Bundle installBundle(String url)}}: installs a bundle from the given url. This method fails if the bundle cannot be installed.
* {{Bundle installBundle(String url, InputStream stream)}}: installs a bundle from the given input stream. This methods fails if the bundle cannot be installed.
* {{Bundle installAndStart(String url)}}: installs a bundle from the given url and starts it. This methods fails if the bundle cannot be installed and started correctly.
* {{Bundle installAndStart(String url, InputStream stream)}}: installs a bundle from the given input stream and starts it. This methods fails if the bundle cannot be installed and started correctly.
* {{Bundle getBundle(long bundleId)}}: gets an installed bundle by its bundle id, or {{null}} if not found.
* {{Bundle getBundle(String name)}}: gets an installed bundle by its symbolic name. Fails if not found.

h3. PackageAdmin
* {{PackageAdmin getPackageAdmin()}}: gives access to the {{Package Admin}} service exposed by the framework.
* {{refresh()}}: refresh package wires.

h2. Extensibility: Helper objects
junit4osgi provides an extensibility mechanism to reduce the pain of testing. So, if you're interacting with specific services or environment, you can use {{Helper}} objects. Those object have to be created in the {{setUp}} method and {{disposed}} in the {{tearDown}} method. 

So, for example, if you write iPOJO tests, you can use the iPOJO helper providing a lot of utility functions simplifying the development of tests.
{code:java}
public class MyTest extends OSGiTestCase {
	ComponentInstance fooProvider1, fooProvider2;
	
	IPOJOHelper helper; // Helper object

    public void setUp() {
        helper = new IPOJOHelper(this);	// Create the object.	
	String type2 = "PS-FooProviderType";
	
	fooProvider1 = helper.createComponentInstance(type2, p3);
        fooProvider2 = helper.createComponentInstance(type2, "FooProvider-4");
    }
	
	public void tearDown() {
		helper.dispose(); // Dispose it, instances will be disposed too.
    }
{code}

You can also implements your own helper by just implementing the {{Helper}} interface.
\\
\\
{include:apache-felix-ipojo-footer}