Description
Current state
OSGi-Test provides a very convenient and powerful way to (unit) test OSGi code, but the current setup is rather complex and hard to understand for anyone not familiar with OSGi+BND tooling, e.g. the maven example requires the following stuff:
- A non trivial test.bndrun file https://github.com/osgi/osgi-test/blob/bf9b049afb4bf3331cfa5a222d7566f7bc3ae29f/org.osgi.test.common/test.bndrun#L1-L62
bnd-resolver-maven-plugin
with non-trivial configuration https://github.com/osgi/osgi-test/blob/bf9b049afb4bf3331cfa5a222d7566f7bc3ae29f/examples/osgi-test-example-mvn/pom.xml#L134-L185bnd-testing-maven-plugin
with non trivial configuration https://github.com/osgi/osgi-test/blob/bf9b049afb4bf3331cfa5a222d7566f7bc3ae29f/examples/osgi-test-example-mvn/pom.xml#L162-L185- A disabling of the
maven-surefire-plugin
https://github.com/osgi/osgi-test/blob/bf9b049afb4bf3331cfa5a222d7566f7bc3ae29f/examples/osgi-test-example-mvn/org.osgi.test.example.player.test/pom.xml#L91-L96 bnd-maven-plugin
with non trivial configuration for the test-plugin itself https://github.com/osgi/osgi-test/blob/bf9b049afb4bf3331cfa5a222d7566f7bc3ae29f/examples/osgi-test-example-mvn/org.osgi.test.example.player.test/pom.xml#L100-L113
Problem with current aproach
While all of this is flexible and powerful in a larger OSGi application setup, it is simply an integration barrier for many other smaller use-cases
- I have a library project and I don't know much about OSGi but still people ask me to add OSGi support (e.g. Support for the Data Service Specification xerial/sqlite-jdbc#738) for a very limited part of OSGi. Suggesting such a complex setup that requires to restructure large parts of the project will most likely be rejected.
- I don't want to use an extra project for test but simply use Surefire plugin as-is, and I don't need OSGi for every test so I only selectively want to enable osgi-test it for some test but leave the others alone
- I don't need/want any special resolving as I can manage a small set of test-pugins and I don't like to afford the extra time required for resolving
Proposed Solution
I have created a proof-of-concept implementation on top of the OSGi Framework connect that drastically reduces the complexity but still enables usage of osgi-test in a unit test environment the following way:
- Just use a standard maven setup (but running as a plain Unit Test in the IDE e.g. from Eclipse with
Run As > JUnit Test
will also work) with maven-surefire test in the same module. - Add a dependency to
org.osgi.test.junit5
and an OSGi framework implementation (equinox in this case) that supports framework-connect:
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.test.junit5</artifactId>
<version>${osgi-test.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.osgi</artifactId>
<version>${equinoxVersion}</version>
</dependency>
- Create a plain JUnit test like this (based on what would be required for Support for the Data Service Specification xerial/sqlite-jdbc#738 as an example)
@ExtendWith(ServiceExtension.class)
public class OSGiTest {
@RegisterExtension
static FrameworkExtension framework = FrameworkExtension.builder()
.withBundle("org.xerial.sqlite-jdbc", true)
.withBundle("org.osgi.service.jdbc")
.build();
@InjectService(filter = "(osgi.jdbc.driver.class=org.sqlite.JDBC)")
ServiceAware<DataSourceFactory> datasourceFactory;
@BeforeEach
public void checkService() {
assertEquals(1, datasourceFactory.size(), "There should be exactly one DataSourceFactory for SQLite!");
}
@Test
public void testCreateDriver() throws SQLException {
DataSourceFactory service = datasourceFactory.getService();
assertNotNull(service);
Driver driver = service.createDriver(null);
assertNotNull(driver);
assertTrue(driver instanceof JDBC);
}
}
I can create a PR with a first implementation based on my proof-of-concept (needs some cleanup and better handling of bundle lookups), but first like to know if this seems valuable for the osgi-test
project and could be accepted.