JBoss.orgCommunity Documentation
JBoss OSGi Husky is a OSGi Test Framework that allows you to run plain JUnit4 test cases from within an OSGi Framework. That the test is actually executed in the the OSGi Framework is transparent to your test case. There is no requirement to extend a specific base class nor do you need a special test runner. Your OSGi tests execute along side with all your other (non OSGi specific) test cases in Maven, Ant, or Eclipse.
Some time ago I was looking for ways to test bundles that are deployed to a remote instance of the JBoss OSGi Runtime. I wanted the solution to also work with an OSGi Framework that is bootstrapped from within a JUnit test case.
The basic problem is of course that you cannot access the artefacts that you deploy in a bundle directly from your test case, because they are loaded from different classloaders.
Former releases of JBoss OSGi used an approach which is documented in Non intrusive OSGi Bundle Testing and is still available. Although the remote logging approach worked for simple scenarios, it does not allow for fine grained interaction with the OSGi Framework (i.e. access to the registry). An additional problem was the asynchronous nature of LogEntry delivery.
For this release however, I revisited the problem and added a few more requirements.
The next section explains the solution that now comes as JBoss OSGi Husky
JBoss OSGi Husky has client side interceptor that fields the test request to an embedded/remote OSGi Framework where the test case is then actually executed.
Here is how it works
The JBoss OSGi jboss-osgi-husky.jar bundle registers the Connectors. The JMXConnector is always registered. The SocketConnector is registered when the appropriate configuration options are set. It then registers the HuskyExtender, which is a BundleListener that inspects every incomming bundle for the Test-Package manifest header. The Extender creates a PackageListener for every package in the 'Test-Package' manifest header and registers them with the available Connectors.
In the target OSGi Framework, which is the one that has the jboss-osgi-husky.jar bundle installed, you set these properties
Key | Value | Description |
---|---|---|
org.jboss.osgi.husky.runtime.connector.host | localhost | The Husky socket connector host poperty |
org.jboss.osgi.husky.runtime.connector.port | 5401 | The Husky socket connector port poperty |
Both properties must be set for the SocketConnector to be available.
On the client side, you must configure the Invoker you want to use.
Key | Value | Description |
---|---|---|
org.jboss.osgi.husky.Invoker | org.jboss.osgi.husky.internal.OSGiInvoker | The client side Husky Invoker |
This particular invoker will also look for the 'org.jboss.osgi.husky.runtime.connector.host' and 'org.jboss.osgi.husky.runtime.connector.port' properties and if available will use a socket invocation.
In a typical Husky test you have
For OSGi, the descriminator would be the BundleContext that gets injected by the 'in container' test Runner
The interceptor would be a call to one of the Bridge.run() variants.
public class SimpleHuskyTestCase
{
@ProvideContext
public BundleContext context;
...
@Test
public void testSimpleBundle() throws Exception
{
// Tell Husky to run this test method within the OSGi Runtime
if (context == null)
BridgeFactory.getBridge().run();
// Stop here if the context is not injected
assumeNotNull(context);
// Get the SimpleService reference
ServiceReference sref = context.getServiceReference(SimpleService.class.getName());
assertNotNull("SimpleService Not Null", sref);
// Access the SimpleService
SimpleService service = (SimpleService)context.getService(sref);
assertEquals("hello", service.echo("hello"));
}
}
The bundle that contains the test case must have the Test-Package manifest header configured. Here is the aQute Bnd Tool configuration for doing so.
Bundle-SymbolicName: example-simple-husky Bundle-Activator: org.jboss.test.osgi.example.simple.bundle.SimpleActivator Private-Package: org.jboss.test.osgi.example.simple.bundle # Export the package that contains tthe test case Export-Package: org.jboss.test.osgi.example.simple # Tell Husky that there are test cases in this package Test-Package: org.jboss.test.osgi.example.simple