This page is an archive - the information on it is out of date, but is provided for archaeological use.
This sample demonstrates how to create a Spring-managed OSGi bundle, publish a service, and test the bundle in an OSGi container. You will find the sample in the svn tree under samples/simple-service.
We're using maven to build the sample, which comprises two separate maven projects: one containing the simple service OSGi bundle, and one for the integration tests. Let's look at the Simple Service bundle project first.
Simple Service Bundle
The project defines a simple java interface, org.springframework.osgi.samples.simpleservice.MyService, and an implementation of the interface org.springframework.osgi.samples.simpleservice.impl.MyServiceImpl. When packaging a project as an OSGi bundle, configuration files go in the META-INF/spring directory. This project has two configuration files, simpleservice.xml which defines the simpleService bean, and simpleservice-osgi.xml which includes the Spring OSGi namespace support and uses the osgi:service element to export the simpleService bean as an OSGi service. There is no requirement to split the definitions across two configuration files like this: we did it this way to make it easier to integration test the non-OSGi specific parts of the configuration outside of an OSGi container.
If you look at the pom.xml file for this project you'll see that it specifies packaging osgi-bundle. This causes the project to be packaged as an OSGi bundle when it is built. (The osgi-bundle packaging is supported by the parent pom.xml file which defines the maven-osgi-plugin and configures it to find the OSGi manifest for the bundle in META-INF/MANIFEST.MF.
The MANIFEST.MF file for this bundle gives the bundle a unique symbolic name, and exports the package org.springframework.osgi.samples.simpleservice. This is the package that defines the service interface type. The implementation type (in another package) will be completely hidden from clients of the bundle.
Under the test source root of the simple service bundle project we place tests that are intended to be run outside of any OSGi container. We've added two simple tests, a basic unit test for MyServiceImpl, and a simple integration test. Note how the integration test uses only the simpleservice.xml configuration file.
Simple Service Bundle Integration Tests
We want to test the simple service bundle inside a running OSGi container and verify that the service really is exported as we wish. An integration test like this depends on the packaged simple service bundle artifact, so we need to define the test in a separate project, simple-service-integration-test.
Let's start by looking at the pom.xml file for this project. The key part is the build section where we configure the surefire plugin to run during the integration-test phase and not during the test phase. The integration-test lifecycle phase comes after the packaging phase, when the packaged simple service bundle artifact will be available to us to use.
The SimpleServiceBundleTest test case is placed in the test source tree (where surefire can find it). It extends one of the Spring-OSGi JUnit test support classes, ConfigurableBundleCreatorTests. When this test case runs, an OSGi runtime is started up (equinox, felix, or knopflerfish, depending on the maven profile used to run the integration tests :- equinox is the default) and the test artifacts are packaged into a "virtual" bundle on the fly. This bundle is installed into the runtime, together with the other bundles the test depends on, and the test then executes inside the OSGi runtime, passing the results back out.
We override the getTestBundlesNames() method to define the set of bundles that we want to be installed for this test (certain bundles, such as the test support bundle, are always installed automatically). The helper method localMavenArtifact finds a packaged maven artifact produced by the current build. We also override the getManifestLocation operation to define the manifest to be used for the virtual bundle we are creating.
And that's it... we now know how to create a Spring-managed OSGi bundle, unit test, integration test out of the container, and integration test inside OSGi.
Please see the Testing chapter from the reference documentation for more information on the features supported by Spring-DM testing framework.