你如何让Pax Exam使用OBR来处理传递依赖?

时间:2012-06-12 01:03:49

标签: unit-testing osgi apache-karaf pax-exam

在Karaf中,我可以安装OBR功能并使用obr:addUrl添加repository.xml和obr:deploy来部署捆绑包以及所有传递依赖项。我在https://stackoverflow.com/a/10989017/242042

中记录了它

但是,现在我想用PaxExam创建一个JUnit测试,但我似乎无法模仿我在PaxExam上用Karaf做的事情。

任何代码片段都会显示如何指向OBR存储库并进行部署,并自动完成所有传递计算?

2 个答案:

答案 0 :(得分:1)

您可以使用Pax URL obr: protocol handler在Pax Exam测试中配置来自OBR存储库的各个捆绑包,但这不会引入任何传递依赖关系。

在Pax考试中,您始终需要自行配置每个分发包。但您可以通过复合选项对bundle进行分组,以支持重用测试配置。

答案 1 :(得分:1)

我实际上已经找到了答案。我不使用obr:协议处理程序,而是使用OBR实现(Apache Aries)。

这就是我配置测试用例的方法

@Configuration
public static Option[] configuration() throws Exception {
    return options(
            systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level")
                    .value("WARN"),
            frameworkProperty("obr.repository.url").value(
                    new File("target/dependency/repository.xml").toURI()
                            .toASCIIString()),
            bundle("mvn:org.apache.felix/org.osgi.service.obr/1.0.2"),
            bundle("mvn:org.apache.felix/org.apache.felix.bundlerepository/1.6.6"),
            bundle("mvn:org.apache.aries/org.apache.aries.util/0.4"),
            bundle("mvn:org.apache.aries.proxy/org.apache.aries.proxy/0.4"),
            junitBundles());
}

然后我在类中有一个方便的方法,使用OBR搜索字符串从OBR部署

private void obrDeploy(final String filter) throws Exception {
    final Resolver resolver = repositoryAdmin.resolver();
    final Resource[] discoverResources = repositoryAdmin
            .discoverResources(filter);
    for (final Resource r : discoverResources) {
        resolver.add(r);
    }
    assertTrue(resolver.resolve());
    resolver.deploy(true);
}

然后我的测试用例看起来像这样。这可以确保测试加载它正确公开的服务。

@Test
public void testBlueprintBundle() throws Exception {
    obrDeploy("(symbolicname=net.trajano.maven-jee6.blueprint.producer)");
    getService(bundleContext, MongoDbFactory.class);
    getService(bundleContext, BlockingQueue.class);
    getService(bundleContext, Executor.class);
}

请注意,这仅按照设计部署了具有传递链接的包。如果您有其他依赖项不像实现包那样存在,那么它们也需要部署。下面显示了如何使用通配符从OBR部署多个包以简化测试的一行。

obrDeploy("(|(symbolicname=*.blueprint.consumer)(symbolicname=*.blueprint.producer)(symbolicname=*.hello.osgi))");

完整来源位于https://github.com/trajano/maven-jee6/blob/emerging-technologies/osgi-sample/assembly/src/test/java/net/trajano/osgi/test/PaxTest.java