【问题标题】:PaxExam and CmPropertyPlaceholderPaxExam 和 CmPropertyPlaceholder
【发布时间】:2014-03-02 05:00:53
【问题描述】:

我正在尝试实施 PaxExam 来测试蓝图骆驼路线:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
   xmlns:camel="http://camel.apache.org/schema/blueprint"
   xsi:schemaLocation="
   http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
   http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.0.0.xsd
   http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

<cm:property-placeholder persistent-id="paxexam.HelloBean">
  <cm:default-properties>
    <cm:property name="greeter" value="Hey"/>
  </cm:default-properties>
</cm:property-placeholder>

<bean id="helloBean" class="me.examples.paxexam.HelloBean">
  <property name="say" value="${greeter} from Camel"/>
</bean>

[...]

如果捆绑包部署在 Karaf 中它可以工作,但是当我尝试在 PaxExam 测试中运行它时,它会在初始化 CmPropertyPlaceholder 时失败并出现 NullPointerException:

Caused by: java.lang.NullPointerException
    at java.util.Hashtable.put(Hashtable.java:394)[:1.6.0_45]
    at org.apache.aries.blueprint.compendium.cm.CmPropertyPlaceholder.init(CmPropertyPlaceholder.java:102)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.6.0_45]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)[:1.6.0_45]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)[:1.6.0_45]
    at java.lang.reflect.Method.invoke(Method.java:597)[:1.6.0_45]
    at org.apache.aries.blueprint.utils.ReflectionUtils.invoke(ReflectionUtils.java:225)[10:org.apache.aries.blueprint:0.3.2]
    at org.apache.aries.blueprint.container.BeanRecipe.invoke(BeanRecipe.java:838)[10:org.apache.aries.blueprint:0.3.2]
    at org.apache.aries.blueprint.container.BeanRecipe.runBeanProcInit(BeanRecipe.java:638)[10:org.apache.aries.blueprint:0.3.2]  

这是我的 PaxExam 配置方法:

@Configuration
public static Option[] configure() throws Exception {
  return new Option[] {
    karafDistributionConfiguration().frameworkUrl(getFrameworkUrl("karaf"))
    .karafVersion("2.2.11")
    .useDeployFolder(false)
    .unpackDirectory(new File("target/exam/unpack")),
    logLevel(LogLevel.INFO),
    keepRuntimeFolder(),
    replaceConfigurationFile("etc/activemq-broker.xml", new File("src/test/resources/activemq-broker-test.xml")),

    features(getKarafStandardFeaturesUrl(), "config", "http"),
    features(getActiveMqFeaturesUrl(), "activemq-blueprint", "activemq-camel"),
    features(getCamelFeaturesUrl(), "camel-core", "camel-blueprint", "camel-jms", 
                    "camel-cxf", "camel-test", "camel-jackson", "camel-quartz"),
    features(getSmixFeaturesUrl(), "saaj", "activemq-broker"),

    streamBundle(bundle().add(Hello.class)
        .add(HelloBean.class)
        .add("OSGI-INF/blueprint/blueprint.xml",
            new File("src/main/resources/OSGI-INF/blueprint/blueprint.xml").toURL())
        .set(Constants.BUNDLE_SYMBOLICNAME, "me.examples.paxexam")
    .set(Constants.IMPORT_PACKAGE, "org.osgi.service.blueprint,org.slf4j")
        .set(Constants.DYNAMICIMPORT_PACKAGE, "*")
    .build()).start()
  };
}

我需要做一些特殊的事情才能将 cm:property-placeholder 与 PaxExam 一起使用吗? (去掉标签让测试运行正常)

提前谢谢, 干杯

【问题讨论】:

    标签: apache-camel apache-karaf blueprint-osgi pax-exam


    【解决方案1】:

    这是 Karaf Pax-Exam 测试吗? 如果是这样,您需要确保您拥有要使用配置管理服务设置的可用属性。 如果这是您所说的普通 Pax-Exam 测试,您需要确保您有可用的 ConfigurationAdmin 服务。稍后查看this sample

    【讨论】:

    • 这正是我所需要的,非常感谢!有趣的是,我意识到我的代码实际上已经基于您在 github 上的项目,但是您(再次)为我提供了解决方案……您是我的救星 :)
    猜你喜欢
    • 2016-03-20
    • 2016-05-20
    • 2012-12-02
    • 1970-01-01
    • 2012-07-29
    • 2014-02-04
    • 1970-01-01
    • 2016-05-22
    • 2016-03-23
    相关资源
    最近更新 更多