2017-10-07 10 views
1

私はArquillianを使い始めましたが、サーバ固有のファイル(jboss-ds.xmlglassfish-resources.xmlなど)のJPAでJTAで使用するデータソースを指定する必要があることに気付きましたが、Java EE> 6では可能ですweb.xml(またはejb-jar.xml,application.xmlまたはapplication-client.xml)で指定します。web.xmlやArquillianのどこかでポータブルデータソースを指定する方法は?

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
    [...] 
    <data-source> 
     <name>jdbc/project1</name> 
     <class-name>org.apache.derby.jdbc.EmbeddedDataSource</class-name> 
     <server-name>localhost</server-name> 
     <database-name>project1</database-name> 
     <user>project1</user> 
     <password>project1</password> 
     <property> 
      <name>connectionAttributes</name> 
      <value>create=true</value> 
     </property> 
     <transactional>true</transactional> 
     <isolation-level>TRANSACTION_READ_COMMITTED</isolation-level> 
     <initial-pool-size>2</initial-pool-size> 
     <max-pool-size>10</max-pool-size> 
     <min-pool-size>5</min-pool-size> 
     <max-statements>0</max-statements> 
    </data-source> 
</web-app> 

と、次のようにそれを使用する:

@Deployment 
public static Archive<?> createDeployment() { 
    WebArchive retValue = ShrinkWrap.create(WebArchive.class) 
      .addClasses(MyManagedBean.class, SaveController.class, DefaultSaveController.class) 
      .setWebXML("web.xml") 
      .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml"); 
    Stream.of(Maven.resolver().loadPomFromFile("pom.xml").importRuntimeDependencies().resolve().withTransitivity().as(JavaArchive.class)).forEach(archive -> retValue.addAsLibrary(archive)); 
    return retValue; 
} 

重複を避けることになります。ただし、データソースは利用できません。たとえばGlassFishはCaused by: com.sun.appserv.connectors.internal.api.ConnectorRuntimeException: Invalid resource : jdbc/project1__pmのため失敗します。

MCVEはhttps://github.com/krichter722/arquillian-data-source-in-web-xmlにあります。

Arquillianは使用可能なデータソースを作成しているようですが、実際には既に統合に近いため、生産環境の正確なタイプをテストしたいと思います。

答えて

0

Arquillian JPA tutorialに従うことをお勧めします。glassfish-resourcess.xmlを使用してください。次のようにすると、mavenプロジェクトの次のファイル構造が表示されます。

あなたは、このパッケージとファイル構造を持っていたら、その後<testResources>タグを追加することにより、pom.xmlにテストリソースを指します。

pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
 
    <modelVersion>4.0.0</modelVersion> 
 
    <parent> 
 
     <groupId>org.example</groupId> 
 
     <artifactId>service</artifactId> 
 
     <version>1.0-SNAPSHOT</version> 
 
    </parent> 
 
    <artifactId>project-service</artifactId> 
 
    <packaging>jar</packaging> 
 
    
 
    <dependencyManagement> 
 
     <dependencies> 
 
      <dependency> 
 
       <groupId>org.jboss.arquillian</groupId> 
 
       <artifactId>arquillian-bom</artifactId> 
 
       <version>1.0.0.Final</version> 
 
       <scope>import</scope> 
 
       <type>pom</type> 
 
      </dependency> 
 
     </dependencies> 
 
    </dependencyManagement> 
 
    <dependencies> 
 
     <dependency> 
 
      <groupId>${project.groupId}</groupId> 
 
      <artifactId>infohub-entities</artifactId> 
 
      <version>${project.version}</version> 
 
     </dependency> 
 
     <dependency> 
 
      <groupId>junit</groupId> 
 
      <artifactId>junit</artifactId> 
 
      <version>4.8.1</version> 
 
     </dependency> 
 
     <dependency> 
 
      <groupId>org.jboss.arquillian.junit</groupId> 
 
      <artifactId>arquillian-junit-container</artifactId> 
 
      <scope>test</scope> 
 
     </dependency> 
 

 
     <dependency> 
 
      <groupId>org.jboss.arquillian.container</groupId> 
 
      <artifactId>arquillian-glassfish-embedded-3.1</artifactId> 
 
      <version>1.0.2</version> 
 
     </dependency> 
 
     <dependency> 
 
      <groupId>org.glassfish.main.extras</groupId> 
 
      <artifactId>glassfish-embedded-web</artifactId> 
 
      <version>5.0</version> 
 
     </dependency> 
 
     
 
     <dependency> 
 
      <groupId>org.hamcrest</groupId> 
 
      <artifactId>hamcrest-core</artifactId> 
 
      <version>1.3</version> 
 
      <scope>test</scope> 
 
     </dependency> 
 
     
 
    </dependencies> 
 
    <properties> 
 
     <maven.compiler.source>1.8</maven.compiler.source> 
 
     <maven.compiler.target>1.8</maven.compiler.target> 
 
    </properties> 
 
    <name>project-service</name> 
 
    
 
    <build> 
 
     <testResources> 
 
      <testResource> 
 
       <directory>src/test/resources</directory> 
 
      </testResource> 
 
      <testResource> 
 
       <directory>src/test/resources-glassfish-embedded</directory> 
 
      </testResource> 
 
     </testResources> 
 
    </build> 
 
</project>

次にglassfish-resources.xmlに、あなたのポータブルデータ・ソースの構成を作成することができます。

<?xml version="1.0" encoding="UTF-8"?> 
 
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"> 
 
<resources> 
 
    <jdbc-resource enabled="true" jndi-name="jdbc/datasource" object-type="user" pool-name="connectionPool"> 
 
    <description/> 
 
    </jdbc-resource> 
 
    <jdbc-connection-pool allow-non-component-callers="false" 
 
        associate-with-thread="false" 
 
        connection-creation-retry-attempts="0" 
 
        connection-creation-retry-interval-in-seconds="10" 
 
        connection-leak-reclaim="false" 
 
        connection-leak-timeout-in-seconds="0" 
 
        connection-validation-method="auto-commit" 
 
        datasource-classname="oracle.jdbc.pool.OracleDataSource" 
 
        fail-all-connections="false" 
 
        idle-timeout-in-seconds="300" 
 
        is-connection-validation-required="false" 
 
        is-isolation-level-guaranteed="true" 
 
        lazy-connection-association="false" 
 
        lazy-connection-enlistment="false" 
 
        match-connections="false" 
 
        max-connection-usage-count="0" 
 
        max-pool-size="32" max-wait-time-in-millis="60000" 
 
        name="connectionPool" 
 
        non-transactional-connections="false" 
 
        pool-resize-quantity="2" 
 
        res-type="javax.sql.DataSource" 
 
        statement-timeout-in-seconds="-1" 
 
        steady-pool-size="8" 
 
        validate-atmost-once-period-in-seconds="0" 
 
        wrap-jdbc-objects="false"> 
 
    <property name="URL" value="jdbc:oracle:thin:@//url:port"/> 
 
    <property name="User" value="username"/> 
 
    <property name="Password" value="password"/> 
 
    </jdbc-connection-pool> 
 
</resources>

そしてarquillian.xml

<?xml version="1.0" encoding="UTF-8"?> 
 
<arquillian xmlns="http://jboss.org/schema/arquillian" 
 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
    xsi:schemaLocation=" 
 
     http://jboss.org/schema/arquillian 
 
     http://jboss.org/schema/arquillian/arquillian_1_0.xsd"> 
 
    <container qualifier="glassfish-embedded" default="true"> 
 
     <configuration> 
 
      <property name="resourcesXml"> 
 
       src/test/resources-glassfish-embedded/glassfish-resources.xml 
 
      </property> 
 
     </configuration> 
 
    </container> 
 
</arquillian>

にデータ・ソースを指すように、データ・ソースは、各時間の試験開始に利用できるようになるポータブルであります。 完全な解決方法はArquillian JPA Tutorialに従ってください。またはArquillian Blog

関連する問題