【问题标题】:Problems testing services created with maven-scr-plugin when using pax-exam使用 pax-exam 时测试使用 maven-scr-plugin 创建的服务时出现问题
【发布时间】:2013-10-06 23:14:30
【问题描述】:

我设置了一个非常简单的“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"));

    }
}

【问题讨论】:

    标签: java maven osgi pax-exam maven-scr-plugin


    【解决方案1】:

    使用 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()
    

    可以在here找到完整的示例

    【讨论】:

    • 感谢您的回复,但这似乎不起作用。显然@ProbeBuilder 已被弃用,当我尝试此方法时,我收到此错误:ERROR: PAXEXAM-PROBE-c57393ab-7878-4dd3-b533-cbbfb90411d4 (18): Component descriptor entry 'OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml' not found
    • 这肯定不会被弃用,您是否将此方法添加到您的测试类中?看看我的一个示例:github.com/ANierbeck/Camel-Pax-Exam-Demo/blob/master/…
    • org.ops4j.pax.exam.junit.ProbeBuilder 已弃用并已被org.ops4j.pax.exam.ProbeBuilder 取代。
    • @hwellmann, @achim-nierbeck - 啊,谢谢你的澄清。我的错误,我试图使用junit.ProbeBuilder。但是,仍然收到文件未找到错误。
    • @peterc 很可能是它还没有生成?您是否使用 pax-exam 测试而不是单元测试?它们通常用于测试。
    【解决方案2】:

    我可能对此有一个修复,虽然它看起来有点不优雅。我可以将 Service-Component 显式添加到 pom 文件的 maven-bundle-plugin 部分。

        <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>
    

    如果有人能想到更好的方法,请告诉我。

    【讨论】:

      【解决方案3】:

      Maven SCR 插件仅生成服务组件描述符,但不会自动将它们包含在清单中。

      因此,在 Maven Bundle 插件配置中包含 &lt;Service-Component&gt; 指令并没有什么不雅之处,那就是 documented usage

      由于缺少清单标头,SCR 不会代表您的捆绑包注册任何服务,这就是 Pax Exam 找不到所需服务的原因。

      【讨论】:

      • 感谢您的回答。我认为这是我要走的路,但是您链接到的文档确实说&lt;Service-Component&gt; 将在您同时使用maven-scr-pluginmaven-bundle-plugin 时创建,因此“您不必自己配置”。事实上,它确实在 MANIFEST.MF 文件中生成了这个条目。它只是在测试阶段太晚了,正如你所说,该服务没有注册,所以 pax-exam 找不到它。
      猜你喜欢
      • 2012-04-24
      • 2015-09-13
      • 2013-11-19
      • 2012-08-06
      • 2016-03-19
      • 2020-09-02
      • 2011-02-26
      • 1970-01-01
      • 2012-04-07
      相关资源
      最近更新 更多