Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add my own bundle sources to pax-exam when building with pax-maven-plugin

Tags:

java

maven

osgi

pax

I'm trying to build my OSGI bundle with pax-maven-build and in the same time test it with pax-exam. It have some bundle in provision than I can test with the following pax-exam test configuration:

@RunWith(JUnit4TestRunner.class)
@ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
public class OSGILoaderTest {

    @Inject
    protected BundleContext bundleContext;

    @Configuration
    public Option[] config() throws MalformedURLException {


        String projectRoot = // a path to my project

        return options(
                junitBundles(),
                equinox(),
                bundle(projectRoot + "libs/org.eclipse.core.variables_3.2.500.v20110511.jar"),
                bundle(projectRoot + "libs/org.eclipse.core.contenttype_3.4.100.v20110423-0524.jar"),
                bundle(projectRoot + "libs/org.eclipse.core.expressions_3.4.300.v20110228.jar"),
                // etc...
        );
    }

    @Test
    public void getBundleContext() throws RodinDBException {
        IRodinDB rodinDB = RodinCore.getRodinDB();
        assertNotNull(rodinDB);
    }
}

Here, I can see I can access to the IRodinDB instance from a jar I have provisonned.

Now I have code my own bundle, which is going to use all the jar provisionned. But I cannot even test my own code, for instance:

@Test
public void checkAccessToRodinDbTest() {
    VTGService service = null;
    assertTrue(true);
}

give an error at compilation time:

[ERROR] Failed to execute goal org.ops4j:maven-pax-plugin:1.5:testCompile (default-testCompile) : Compilation failure

[ERROR] cannot find symbol

[ERROR] symbol : class VTGService

It seems test compilation cannot see 'src/main/java', contrarly as expected by the default behavior of maven-compiler-plugin. But in my case, you can see than maven does not use the compiler plugin but instead maven-pax-plugin.

The question is: how can i test my own bundle with pax-exam ?

update1

It seems that this is a problem with recent version of maven-pax-plugin, as the basic example available in ops4j pax maven plugin (in section Using the Pax Plugin inside a POM) seems to suffer of the same problem.

update2

As requested by Dmytro, this is the pom.xml of my bundle:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <parent>
        <relativePath>../poms/compiled/</relativePath>
        <groupId>fr.xlim.ssd.vtg.build</groupId>
        <artifactId>compiled-bundle-settings</artifactId>
        <version>0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <bundle.symbolicName>fr.xlim.ssd.vtg.bundle</bundle.symbolicName>
        <bundle.namespace>fr.xlim.ssd.vtg.bundle</bundle.namespace>
    </properties>

    <modelVersion>4.0.0</modelVersion>
    <groupId>fr.xlim.ssd.vtg</groupId>
    <artifactId>fr.xlim.ssd.vtg.bundle</artifactId>
    <version>0.1-SNAPSHOT</version>

    <name>${bundle.symbolicName}</name>

    <packaging>bundle</packaging>

    <dependencies>

        <dependency>
            <type>pom</type>
            <groupId>${project.parent.groupId}</groupId>
            <artifactId>provision</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- not needed as equinox bundle are available in provision --> 
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>osgi_R4_core</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>osgi_R4_compendium</artifactId>
            <optional>true</optional>
        </dependency-->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam</artifactId>
            <version>2.3.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam-junit4</artifactId>
            <version>2.3.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam-inject</artifactId>
            <version>2.3.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.ops4j.pax.url</groupId>
            <artifactId>pax-url-mvn</artifactId>
            <version>1.3.5</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam-container-native</artifactId>
            <version>2.3.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam-link-mvn</artifactId>
            <version>2.3.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.0</version>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.0.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

I am not sure it is the most elegant solution, but I created a new maven project when I can import my own bundle like in the source code of my question.

Is there an elegant way to add my own java sources directly as new bundle for test in the same Maven project ? It could be not possible (as the bundle assembly operation is done after the compilation and tests)...

like image 838
Kartoch Avatar asked Apr 12 '12 08:04

Kartoch


2 Answers

I use the following setup to provision the bundle under test. When configuring the test, I provision the bundle using the reference-protocol (this is a non-standard feature of Equinox and Felix, see here):

@Configuration
public Option[] config() {

    return options(         
        bundle("reference:file:target/classes"),
        junitBundles(),
        felix()
        );
}

The test-cases also run when you specify knopplerfish() as the environment. I guess that is because the URL is resolved by Pax Exam, and not by the OSGi-runtime. I use the maven-bundle-plugin to build my bundles. To make this work as expected, you have to add the following configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>2.3.7</version>
            <extensions>true</extensions>
            <executions>
                <!-- This execution makes sure that the manifest is available 
                    when the tests are executed -->
                <execution>
                    <goals>
                        <goal>manifest</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Otherwise the manifest will not be available when the tests are run, since by default it is generated during the package-phase.

I hope I did not forget anything - please let me know if it worked for you!

like image 180
Björn Pollex Avatar answered Sep 20 '22 07:09

Björn Pollex


Check PaxExam docs how to configure your Maven POM with PaxExam.

Samples here

like image 33
Dmytro Pishchukhin Avatar answered Sep 22 '22 07:09

Dmytro Pishchukhin