为了进行测试,我想在Apache Camel应用程序的蓝图中获得bean的实例。

在JUnit测试中,如何在daos.xml中获取Bean的实例,因为它们将存在于应用程序的OSGI容器中?

的种类

AuditPageNavDao aDao = Daos.getInstance("auditPageNavDao");


我有一个带有插件的Maven pom.xml:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Bundle-Name>DaoServicesCommon</Bundle-Name>
            <Bundle-Activator>com.acme.dao.bundle.Activator</Bundle-Activator>
            <Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
            <Import-Package>
                javax.persistence,
                org.hibernate.proxy,
                javassist.util.proxy,
                *
            </Import-Package>
        </instructions>
    </configuration>
</plugin>


META-INF / persistence.xml看起来像

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="appPU" transaction-type="JTA">
        <jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/appxadb)</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
            <property name="hibernate.enable_lazy_load_no_trans" value="true" />
            <property name="show_sql" value="true" />
        </properties>
    </persistence-unit>

</persistence>


和OSGI-INF / blueprint / daos.xml看起来像

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.1.0" xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.2.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
        http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint-2.8.0.xsd
        http://aries.apache.org/xmlns/transactions/v1.2.0 http://aries.apache.org/schemas/transaction/transactionv12.xsd
        http://aries.apache.org/xmlns/jpa/v1.1.0 http://aries.apache.org/schemas/jpa/jpa_110.xsd">

    <!-- AUDIT DAO SERVICES -->
    <bean id="auditAccountTxnDao" class="com.acme.dao.audit.jta.AuditAccountTxnDaoImpl">
        <argument index="0" ref="temporals" />
        <jpa:context index="1" unitname="appPU" />
    </bean>

    <service interface="com.acme.dao.audit.jta.AuditAccountTxnDao"
        ref="auditAccountTxnDao">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="AuditAccountTxnDao" />
        </service-properties>
    </service>

    <bean id="auditAuthDao" class="com.acme.dao.audit.jta.AuditAuthDaoImpl">
        <argument index="0" ref="temporals" />
        <jpa:context index="1" unitname="appPU" />
    </bean>

    <service interface="com.acme.dao.audit.jta.AuditAuthDao"
        ref="auditAuthDao">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="AuditAuthDao" />
        </service-properties>
    </service>

    <bean id="auditPageNavDao" class="com.acme.dao.audit.jta.AuditPageNavDaoImpl">
        <argument index="0" ref="temporals" />
        <jpa:context index="1" unitname="appPU" />
    </bean>

    <service interface="com.acme.dao.audit.jta.AuditPageNavDao"
        ref="auditPageNavDao">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="AuditPageNavDao" />
        </service-properties>
    </service>

    <bean id="auditTxnDao" class="com.acme.dao.audit.jta.AuditTxnDaoImpl">
        <argument index="0" ref="temporals" />
        <jpa:context index="1" unitname="appPU" />
    </bean>

    <service interface="com.acme.dao.audit.jta.AuditTxnDao"
        ref="auditTxnDao">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="AuditTxnDao" />
        </service-properties>
    </service>

    <bean id="auditEventCodeDao" class="com.acme.dao.audit.jta.AuditEventCodeDaoImpl">
        <argument index="0" ref="temporals" />
        <jpa:context index="1" unitname="appPU" />
    </bean>

    <service interface="com.acme.dao.audit.jta.AuditEventCodeDao"
        ref="auditEventCodeDao">
        <service-properties>
            <entry key="osgi.jndi.service.name" value="AuditEventCodeDao" />
        </service-properties>
    </service>

</blueprint>


最后一个激活器是

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator
{

    @Override
    public void start(final BundleContext context) throws Exception
    {
    }

    @Override
    public void stop(final BundleContext context) throws Exception
    {
    }

}

最佳答案

这取决于您如何运行测试。有两种方法:


使用PAX EXAM,它将启动OSGi容器并在其中部署捆绑软件
使用CamelBlueprintTestSupport类作为测试的父类,这将启动一个小的嵌入式容器来运行Camel


您可以使用JUnit或TestNG来运行测试,测试框架无关。
还要注意,PAX EXAM可以运行您选择的确切JBoss Fuse版本。

使用PAX EXAM

PAX EXAM通过在容器内注入“探针束”来运行测试。将该探针包想象为正在运行的测试类的“副本”。因此,在测试课程中,您可以完全访问OSGi内部。
然后从BundleContext您可以要求OSGi提供您注册的服务之一。
这是有关如何获取auditPageNavDao bean的粗略思路:

@RunWith(PaxExam.class)
class YourTest {
    @javax.inject.Inject
    protected BundleContext bundleContext_;

    @Test
    testMethod() {

        ServiceReference<AuditPageNavDao> daoServiceReference_ = bundleContext_.getServiceReference(AuditPageNavDao.class);
        AuditPageNavDao dao = bundleContext_.getService(daoServiceReference_);
        // use DAO here

    }
}


使用CamelBlueprintTestSupport

您可以轻松地与组件和端点进行交互,例如

MyBatisComponent mbc = context.getComponent("mydb", MyBatisComponent.class);
mbc.getSqlSessionFactory().openSession().getConnection();
// don't know if this will work
Object dao = context.getRegistry().lookupByName("auditPageNavDao");


我不知道如何获得Blueprint bean,可能CamelContext是一个很好的起点。

关于java - 如何以编程方式从OSGI蓝图中获取bean?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44133381/

10-14 08:49