Проблемы тестирования сервисов, созданных с помощью maven-scr-plugin при использовании pax-exam

Я настроил очень простой сервис "HelloWorld", чтобы продемонстрировать свою проблему. Он использует maven-scr-plugin для генерации дескриптора службы и имеет тестовый модуль pax-exam. Но когда я пытаюсь запустить mvn clean test, он некоторое время блокируется, прежде чем выдает мне эту ошибку:

 org.ops4j.pax.swissbox.tracker.ServiceLookupException: gave up waiting for service com.liveops.examples.osgi.helloworld.HelloWorldService

Если я запускаю 'mvn -DskipTests=true package', а затем запускаю 'mvn test' (без очистки), это работает. Разница, кажется, заключается в добавлении этой строки в мой файл META-INF/M ANIFEST.MF:

 Service-Component: OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml

Кто-нибудь знает, есть ли способ убедиться, что эта строка добавлена ​​ранее в процессе сборки, чтобы пройти 'mvn clean test'? Или я что-то не так делаю?


Для справки: pom.xml, сервис и модульный тест.

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

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.liveops.examples</groupId>
    <artifactId>HelloWorldService</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <packaging>bundle</packaging>

    <dependencies>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>4.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
             <groupId>org.ops4j.pax.exam</groupId>
             <artifactId>pax-exam-container-native</artifactId>
             <version>3.3.0</version>
             <scope>test</scope>
         </dependency>
        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam-junit4</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.ops4j.pax.url</groupId>
            <artifactId>pax-url-aether</artifactId>
            <version>1.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.ops4j.pax.exam</groupId>
            <artifactId>pax-exam-link-mvn</artifactId>
            <version>3.3.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.framework</artifactId>
            <version>4.0.2</version>
            <scope>test</scope>
        </dependency>     
        <dependency>
            <groupId>org.apache.felix</groupId>
            <artifactId>org.apache.felix.scr.annotations</artifactId>
            <version>1.9.0</version>
        </dependency>
    </dependencies>

    <properties>
        <namespace>com.liveops.examples.osgi.helloworld</namespace>
    </properties>

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

            <plugin>
                <!--
                 | the following instructions build a simple set of public/private classes into an OSGi bundle
                -->
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.3.7</version>
                <extensions>true</extensions>                
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>${project.name}</Bundle-SymbolicName>
                        <Bundle-Version>${project.version}</Bundle-Version>
                        <!-- Bundle-Activator>${namespace}.internal.HelloActivator</Bundle-Activator -->
                        <!--
                         | assume public classes are in the top package, and private classes are under ".internal"
                        -->
                        <Export-Package>!${namespace}.internal.*,${namespace}.*;version="${project.version}"</Export-Package>
                        <Private-Package>${namespace}.internal.*</Private-Package>
                        <!--
                         | each module can override these defaults in their osgi.bnd file
     -->
                        <!--_include>-osgi.bnd</_include-->
                    </instructions>
                </configuration>   
                <executions>
                    <execution>
                        <id>generate-manifest</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>manifest</goal>
                        </goals>
                    </execution>
                </executions>          
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-scr-plugin</artifactId>
                <version>1.9.0</version>
                <configuration>
                    <supportedProjectTypes>
                        <supportedProjectType>jar</supportedProjectType>
                        <supportedProjectType>bundle</supportedProjectType>
                        <supportedProjectType>war</supportedProjectType>
                    </supportedProjectTypes>
                    <generateAccessors>true</generateAccessors>
                    <strictMode>true</strictMode>
                    <specVersion>1.1</specVersion>
                    <outputDirectory>target/classes</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <id>generate-scr-scrdescriptor</id>
                        <goals>
                            <goal>scr</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Класс реализации HelloWorld

package com.liveops.examples.osgi.helloworld.internal;

import com.liveops.examples.osgi.helloworld.HelloWorldService;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Component;


@Component
@Service(HelloWorldService.class)
public class HelloImpl implements HelloWorldService
{  
    public String helloWorld(String personalization)
    {
        return "Hello " + personalization + "!";
    }
}

Юнит тест

package com.liveops.examples.osgi.helloworld.internal;

import com.liveops.examples.osgi.helloworld.HelloWorldService;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.util.PathUtils;

import javax.inject.Inject;

import static org.ops4j.pax.exam.CoreOptions.*;

@RunWith(PaxExam.class)
public class HelloImplTest
{
    @Inject
    HelloWorldService hws;

    @Configuration

    public static Option[] configuration() throws Exception{
            return options(
                    systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("WARN"),
                    mavenBundle("org.apache.felix", "org.apache.felix.scr", "1.6.2"),
                    bundle("reference:file:" + PathUtils.getBaseDir() + "/target/classes"),
                           junitBundles());
    }

    @Test
    public void testInjection()
    {
        Assert.assertNotNull(hws);
    }

    @Test
    public void testHelloWorld() throws Exception
    {
        Assert.assertNotNull(hws);
        Assert.assertEquals("Hello UnitTest!", hws.helloWorld("UnitTest"));

    }
}

3 ответа

Решение

Используйте ProbeBuilder для улучшения вашего протестированного пакета:

    @ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
    probe.setHeader("Service-Component", "OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml");
    return probe;
}

Скорее всего, это все, чего не хватает.

РЕДАКТИРОВАТЬ:

на тот случай, если вы пытаетесь использовать pax-exam в одном и том же пакете, вам необходимо выполнить определенные действия в своем методе конфигурации:

streamBundle(bundle()
.add(SomceClass.class).add("OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml", new File("src/main/resources/OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml")
.toURL())
.set("Service-Component", "OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml")
.build()).start()

полный образец можно найти здесь

У меня может быть решение для этого, хотя это кажется немного не элегантным. Я могу явно добавить Service-Component в раздел maven-bundle-plugin файла pom.

    <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>2.3.7</version>
        <extensions>true</extensions>                
        <configuration>
            <instructions>
                <Bundle-SymbolicName>${project.name}</Bundle-SymbolicName>
                <Bundle-Version>${project.version}</Bundle-Version>
                <Export-Package>!${namespace}.internal.*,${namespace}.*;version="${project.version}"</Export-Package>
                <Private-Package>${namespace}.internal.*</Private-Package>

                <!--Explicitly add the components no that they can be found in the test phase -->
                <Service-Component>OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml</Service-Component>
            </instructions>
        </configuration>   
        <executions>
            <execution>
                <id>generate-manifest</id>
                <phase>process-classes</phase>
                <goals>
                    <goal>manifest</goal>
                </goals>
            </execution>
        </executions>          
    </plugin>

Пожалуйста, дайте мне знать, если кто-нибудь может придумать лучший способ.

Плагин Maven SCR генерирует только дескрипторы сервисных компонентов, но не включает их в манифест автоматически.

Так что нет ничего неуместного в том, чтобы включить <Service-Component> Инструкция по настройке Maven Bundle Plugin, это только документированное использование.

Поскольку заголовок манифеста отсутствует, SCR не регистрирует никаких служб от имени вашего пакета, и поэтому Pax Exam не может найти требуемую услугу.

Другие вопросы по тегам