【问题标题】:How do you make Pax Exam use an OBR to handle transitive dependencies?你如何让 Pax Exam 使用 OBR 来处理传递依赖?
【发布时间】:2012-06-14 21:43:45
【问题描述】:

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

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

是否有任何代码 sn-p 可以显示如何指向 OBR 存储库并在自动完成所有传递计算的情况下进行部署?

【问题讨论】:

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


    【解决方案1】:

    您可以在 Pax 考试测试中使用 Pax URL obr: protocol handler 从 OBR 存储库中配置单个包,但这不会引入任何传递依赖项。

    在 Pax Exam 中,您始终需要自行配置每个捆绑包。但是您可以按复合选项对捆绑包进行分组,以支持测试配置的重用。

    【讨论】:

      【解决方案2】:

      实际上,我不久前就找到了答案。我不使用 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

      【讨论】:

        猜你喜欢
        • 2018-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-14
        相关资源
        最近更新 更多