2017-05-23 6 views
1

テスト目的のために、Apache Camelアプリケーションの青写真でBeanのインスタンスを取得したいと思います。OSGIの設計図からプログラムで豆を取得するにはどうすればよいですか?

JUnitテストの中で、アプリケーションのOSGIコンテナに存在するので、以下のdaos.xmlでBeanのインスタンスを取得するにはどうすればよいですか?

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

} 
+0

テストはOSGi(pax examを使用して)または外部で実行できますか? –

+0

理想的には、Eclipse「Run as JUnit」(これが可能であれば)から実行したいと思います。コンテナはJBoss Fuseです。プロジェクトを再配置するのは本当に苦痛です。 –

+1

通常、Eclipseから直接pax試験のテストを実行できます。彼らはジュニットのために特別なランナーを使用しますが、他のジュニットのテストのように見えます。 –

答えて

2

で最後これは、あなたがテストを実行する方法によって異なります。

  • のOSGiコンテナを起動し、あなたのテストの親クラスとしてCamelBlueprintTestSupportクラスを使用してそこ
  • をあなたのバンドルを展開するPAXのEXAMを使用して、ちょうどキャメルを実行するために、小さな組み込みコンテナを起動します:2つの方法があります。

JUnitまたはTestNGの両方でテストを実行できます。テストフレームワークは関係ありません。 また、PAX EXAMはあなたの選択したJBoss Fuseバージョンを正確に実行できます。

PAX用EXAMを用い

PAX試験は、容器内 "プローブ・バンドル" を注入することによってテストを実行します。このプローブバンドルを実行中のテストクラスの「コピー」として想像してください。したがって、テストクラスでは、OSGiの内部構造に完全にアクセスできます。
その後、BundleContextから、登録したサービスの1つをOSGiに問い合わせることができます。
これはあなたのauditPageNavDao Beanを取得する方法について大まかなアイデアです:あなたは簡単に「

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

のように、私はドン、コンポーネントおよびエンドポイントと対話することができますCamelBlueprintTestSupport

を使用して

@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 

    } 
} 

おそらくCamelContextが青い豆を手に入れる方法を知っていることが良い出発点です。

1

pax試験では、サービスを直接注入することをおすすめします。 Alessandroのクラスをベースとして使用します。

@RunWith(PaxExam.class) 
class YourTest { 

    @javax.inject.Inject 
    protected AuditPageNavDao dao; 

    @Test 
    testMethod() { 
     // use DAO here 
    } 
} 

これは、サービスがシステムに存在するとすぐに@Testメソッドが実行されるという利点があります。 Alessandroの亜種では、サービスがそこにある前にテストが実行され、NPEが発生する可能性があります。

このアプローチはサービスに対してのみ機能することに注意してください。このようにしてブループリントビーンを手に入れることはできません。

個々の豆が必要な場合は、上記のようにBlueprintContainer サービスを注入することができます。青写真を使用する各バンドルには、そのようなサービスがあります。特定のバンドルのBlueprintContainerを取得するには、サービスプロパティーでフィルタリングすることができます。次に、これを使用してBeanを取得します。

dao = container.getComponentInstance("auditAuthDao"); 
関連する問題