Kohei Nozaki's blog 

Arquillian用ArchiveへクラスとMaven Dependencyを楽に追加する


Posted on Tuesday Jan 28, 2014 at 09:48PM in Technology


環境

  • shrinkwrap-resolver 2.1.0-alpha-1
  • wildfly-arquillian-container-remote 8.0.0.CR1
  • Arquillian 1.1.2.Final
  • WildFly 8.0.0.CR1
  • Eclipse Kepler SR1
  • Oracle JDK7u51

サブパッケージを再帰的に探索させてクラスを追加する

例えばこういう状況で

テストするのに必要なクラス群が複数のパッケージに分散している

クラスの配置

Hige.java

package org.example.hige;

import javax.inject.Named;

@Named
public class Hige {

    public String hige() {
        return "hige()";
    }
}

Hoge.java

package org.example.hoge;

import javax.inject.Inject;
import javax.inject.Named;

import org.example.hige.Hige;

@Named
public class Hoge {

    @Inject
    Hige hige;

    public String hoge() {
        return "hoge()" + hige.hige();
    }
}

こうするとよい

Archive作成時にaddPackages()メソッドをこういう感じで呼んでやればよい。これでいちいち全パッケージに対してaddPackage()呼んでやらずにすむ。第一引数がtrueだと、第二引数で与えたパッケージのサブパッケージを再帰的に探索してくれる。

テストクラス

package org.example.hoge;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class HogeTest {

    @Deployment
    public static Archive<?> createDeployment() {
        WebArchive a =
                ShrinkWrap.create(WebArchive.class, "test.war")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
                .addPackages(true, "org.example");
        return a;
    }

    @Inject
    Hoge hoge;

    @Test
    public void test() throws Exception {
        Assert.assertEquals("hoge()hige()", hoge.hoge());
    }
}

Maven Dependencyを楽に追加する

例えばこういう状況で

guavaをpom.xmlに設定して使っている。Archiveを作る際に、いちいち依存するjarファイルをaddLibraryしていくのが面倒くさい。

Hoge.java

package org.example.hoge;

import javax.inject.Named;

import com.google.common.collect.Lists;

@Named
public class Hoge {

    public void hoge() {
        Lists.newArrayList();
    }
}

こうするとよい

pom.xml

shrinkwrap-resolverへの依存を追加する(groupIdがorg.jboss.shrinkwrap.resolverの要素)。例えばこんな感じ

<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>
    <groupId>com.example</groupId>
    <artifactId>arquillian-prac</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jboss.shrinkwrap.resolver</groupId>
                <artifactId>shrinkwrap-resolver-bom</artifactId>
                <version>2.1.0-alpha-1</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>org.jboss.arquillian</groupId>
                <artifactId>arquillian-bom</artifactId>
                <version>1.1.2.Final</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-integration</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>net.avh4.util</groupId>
            <artifactId>imagecomparison</artifactId>
            <version>0.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.wildfly</groupId>
            <artifactId>wildfly-arquillian-container-remote</artifactId>
            <version>8.0.0.CR1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.extension</groupId>
            <artifactId>arquillian-persistence-impl</artifactId>
            <version>1.0.0.Alpha6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-impl-maven</artifactId>
            <version>2.1.0-alpha-1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-api-maven</artifactId>
            <version>2.1.0-alpha-1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>16.0</version>
        </dependency>
    </dependencies>
</project>

テストクラス

package org.example.hoge;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class HogeTest {

    @Deployment
    public static Archive<?> createDeployment() {
        WebArchive a =
                ShrinkWrap.create(WebArchive.class, "test.war")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
                .addPackages(true, "org.example");

        a.addAsLibraries(
                Maven.resolver()
                .loadPomFromFile("pom.xml")
                .importRuntimeDependencies()
                .resolve()
                .withTransitivity()
                .asFile());

        return a;
    }

    @Inject
    Hoge hoge;

    @Test
    public void test() throws Exception {
        hoge.hoge();
        Assert.assertTrue(true);
    }
}

importRuntimeDependencies()の他にもいろいろあるので必要に応じて使い分ける。ただ多くなりすぎるとデプロイが遅くなる。

  • importRuntimeAndCompileDependencies()
  • importDependencies(ScopeType…)
  • importRuntimeAndTestDependencies()
  • importRuntimeDependencies()
  • importTestDependencies()

こういう感じにすると依存性を個別に追加できる[3]

        File[] files = Maven.resolver()
                .loadPomFromFile("pom.xml")
                .resolve("org.apache.commons:commons-lang3:3.2.1")
                .withTransitivity()
                .asFile();

        WebArchive w = ShrinkWrap.create(WebArchive.class, "hoge.war").addClass(HogeBean.class)
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml").addAsLibraries(files);

バージョンの省略も可。省略するとpom.xmlに書いてあるバージョンを取ってきてくれる

        File[] files = Maven.resolver()
                .loadPomFromFile("pom.xml")
                .resolve("org.apache.commons:commons-lang3")
                .withTransitivity()
                .asFile();

参考文献

  1. Add all classes in src/main/java | Community
  2. java - Adding all Maven dependencies to Arquillian - Stack Overflow
  3. shrinkwrap/resolver · GitHub
  4. Using the ShrinkWrap Maven Resolver for Arquillian Tests | Develop In Java
  5. arquillian 1.1.0 final and shrinkwrap | Community
  6. Add directory recursively to arquillian using S… | Community
  7. Add src/main/java to webarchive | Community


Arquillian Persistence ExtensionでExcelデータを使ってみる


Posted on Tuesday Jan 28, 2014 at 04:21PM in Technology


Excelで作ったデータを投入したり検証に使ったりしてみます

環境

  • Arquillian Persistence Extension 1.0.0.Alpha6
  • wildfly-arquillian-container-remote 8.0.0.CR1
  • Arquillian 1.1.2.Final
  • Hibernate 4.3.0.Final
  • WildFly 8.0.0.CR1
  • Eclipse Kepler SR1
  • Oracle JDK7u51
  • postgresql-9.3-1100.jdbc41.jar
  • PostgreSQL 9.2.4
  • Excel 2011

前提条件

準備

資源の配置図

下図で選択した資源を作成または編集します

VariousTypes.java

package org.arquillian.example;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class VariousTypes implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long bigint1;
    @Column
    private String varchar1;
    @Column
    @Temporal(TemporalType.DATE)
    private Date date1;
    @Column
    @Temporal(TemporalType.TIME)
    private Date time1;
    @Column
    @Temporal(TemporalType.TIMESTAMP)
    private Date timestamp1;
    @Column(precision = 7, scale = 4)
    private BigDecimal numeric1;
    @Column
    private Double double1;

    public Long getBigint1() {
        return bigint1;
    }

    public void setBigint1(Long bigint1) {
        this.bigint1 = bigint1;
    }

    public String getVarchar1() {
        return varchar1;
    }

    public void setVarchar1(String varchar1) {
        this.varchar1 = varchar1;
    }

    public Date getDate1() {
        return date1;
    }

    public void setDate1(Date date1) {
        this.date1 = date1;
    }

    public Date getTime1() {
        return time1;
    }

    public void setTime1(Date time1) {
        this.time1 = time1;
    }

    public Date getTimestamp1() {
        return timestamp1;
    }

    public void setTimestamp1(Date timestamp1) {
        this.timestamp1 = timestamp1;
    }

    public BigDecimal getNumeric1() {
        return numeric1;
    }

    public void setNumeric1(BigDecimal numeric1) {
        this.numeric1 = numeric1;
    }

    public Double getDouble1() {
        return double1;
    }

    public void setDouble1(Double double1) {
        this.double1 = double1;
    }

    @Override
    public String toString() {
        return "VariousTypes [bigint1=" + bigint1 + ", varchar1=" + varchar1 + ", date1=" + date1 + ", time1=" + time1
                + ", timestamp1=" + timestamp1 + ", numeric1=" + numeric1 + ", double1=" + double1 + "]";
    }

}

orm.xml

以下のようなクエリを追加します

    <named-query name="findVariousTypes">
        <query><![CDATA[
            SELECT
                v
            FROM
                VariousTypes v
        ]]></query>
    </named-query>

XlsDataSetTest.java

package org.arquillian.example;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.persistence.ShouldMatchDataSet;
import org.jboss.arquillian.persistence.UsingDataSet;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;


@RunWith(Arquillian.class)
public class XlsDataSetTest {
   @Deployment
   public static Archive<?> createDeployment() {
       Archive<?> a = ShrinkWrap.create(WebArchive.class, "test.war")
           .addPackage(VariousTypes.class.getPackage())
           .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
           .addAsResource("META-INF/orm.xml", "META-INF/orm.xml")
           .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
           .addAsWebInfResource("jbossas-ds.xml");
       return a;
   }

   @PersistenceContext
   EntityManager em;

   @Test
   @Transactional
   @UsingDataSet("datasets/xlsDataSet/variousTypes.xls")
   @ShouldMatchDataSet(value = "datasets/xlsDataSet/variousTypesExpected.xls", orderBy = "bigint1")
   public void select() throws Exception {
       dumpEntityList(em.createNamedQuery("findVariousTypes", VariousTypes.class).getResultList());

       Calendar cal = Calendar.getInstance();
       cal.set(2014, 0, 2, 12, 13, 14);
       cal.set(Calendar.MILLISECOND, 0);
       Date newDate = cal.getTime();

       VariousTypes toModify = em.find(VariousTypes.class, 1l);
       toModify.setVarchar1("HogeHoge");
       toModify.setDate1(newDate);
       toModify.setTime1(newDate);
       toModify.setTimestamp1(newDate);
       toModify.setNumeric1(new BigDecimal("222.2222"));
       toModify.setDouble1(1.0);

       dumpEntityList(em.createNamedQuery("findVariousTypes", VariousTypes.class).getResultList());
   }

   protected void dumpEntityList(List<?> list){
       for(Object o : list){
           System.out.println(o + ", contains=" + em.contains(o));
       }
   }
}

variousTypes.xls

今回はMacのExcel2011で作りましたが、.xls形式(.xlsx形式でなく)で出力していれば、LibreOfficeやOpenOfficeで作ってもおそらく問題ありません。日付や時刻は文字列型で入れます。普通に入力するとシリアル値になって正常に認識してくれなくなります。こんな感じ。

blobは少し調べた範囲ではExcel形式では対応してなさそうなのでスルーします。XML形式なら対応しているそうですが試してません[2]

variousTypesExpected.xls

こんな感じ。IDが1のデータをテストクラス内で更新してその結果を検証してみます

テスト実行

投入データの確認

デバッガで45行目で止めて投入データを見てみます

テーブル

jpaprac=# \d varioustypes
             Table "public.varioustypes"
   Column   |            Type             | Modifiers 
------------+-----------------------------+-----------
 bigint1    | bigint                      | not null
 date1      | date                        | 
 double1    | double precision            | 
 numeric1   | numeric(7,4)                | 
 time1      | time without time zone      | 
 timestamp1 | timestamp without time zone | 
 varchar1   | character varying(255)      | 
Indexes:
    "varioustypes_pkey" PRIMARY KEY, btree (bigint1)

jpaprac=# 

データ

jpaprac=# select bigint1, varchar1, date1, time1, timestamp1, numeric1, double1 from varioustypes ;
 bigint1 | varchar1 |   date1    |  time1   |     timestamp1      | numeric1 | double1 
---------+----------+------------+----------+---------------------+----------+---------
       1 | Hoge     | 1980-01-01 | 12:00:00 | 1980-01-01 12:00:00 | 100.0001 |     0.5
       2 | Hige     | 1980-01-02 | 12:00:01 | 1980-01-02 12:00:01 | 100.0020 |    0.25
       3 | Fuge     | 1980-01-03 | 12:00:02 | 1980-01-03 12:00:02 | 100.0300 |   0.125
(3 rows)

jpaprac=# 

ちゃんと入ってますね

実行結果

JUnit窓

コンソール出力

18:21:16,667 INFO  [stdout] (pool-2-thread-20) Hibernate: select varioustyp0_.bigint1 as bigint1_2_, varioustyp0_.date1 as date2_2_, varioustyp0_.double1 as double3_2_, varioustyp0_.numeric1 as numeric4_2_, varioustyp0_.time1 as time5_2_, varioustyp0_.timestamp1 as timestam6_2_, varioustyp0_.varchar1 as varchar7_2_ from VariousTypes varioustyp0_
18:21:16,670 INFO  [stdout] (pool-2-thread-20) VariousTypes [bigint1=1, varchar1=Hoge, date1=1980-01-01, time1=12:00:00, timestamp1=1980-01-01 12:00:00.0, numeric1=100.0001, double1=0.5], contains=true
18:21:16,670 INFO  [stdout] (pool-2-thread-20) VariousTypes [bigint1=2, varchar1=Hige, date1=1980-01-02, time1=12:00:01, timestamp1=1980-01-02 12:00:01.0, numeric1=100.0020, double1=0.25], contains=true
18:21:16,671 INFO  [stdout] (pool-2-thread-20) VariousTypes [bigint1=3, varchar1=Fuge, date1=1980-01-03, time1=12:00:02, timestamp1=1980-01-03 12:00:02.0, numeric1=100.0300, double1=0.125], contains=true
18:21:16,676 INFO  [stdout] (pool-2-thread-20) Hibernate: update VariousTypes set date1=?, double1=?, numeric1=?, time1=?, timestamp1=?, varchar1=? where bigint1=?
18:21:16,677 INFO  [stdout] (pool-2-thread-20) Hibernate: select varioustyp0_.bigint1 as bigint1_2_, varioustyp0_.date1 as date2_2_, varioustyp0_.double1 as double3_2_, varioustyp0_.numeric1 as numeric4_2_, varioustyp0_.time1 as time5_2_, varioustyp0_.timestamp1 as timestam6_2_, varioustyp0_.varchar1 as varchar7_2_ from VariousTypes varioustyp0_
18:21:16,678 INFO  [stdout] (pool-2-thread-20) VariousTypes [bigint1=2, varchar1=Hige, date1=1980-01-02, time1=12:00:01, timestamp1=1980-01-02 12:00:01.0, numeric1=100.0020, double1=0.25], contains=true
18:21:16,678 INFO  [stdout] (pool-2-thread-20) VariousTypes [bigint1=3, varchar1=Fuge, date1=1980-01-03, time1=12:00:02, timestamp1=1980-01-03 12:00:02.0, numeric1=100.0300, double1=0.125], contains=true
18:21:16,681 INFO  [stdout] (pool-2-thread-20) VariousTypes [bigint1=1, varchar1=HogeHoge, date1=Thu Jan 02 12:13:14 JST 2014, time1=Thu Jan 02 12:13:14 JST 2014, timestamp1=Thu Jan 02 12:13:14 JST 2014, numeric1=222.2222, double1=1.0], contains=true

更新後のDBも覗いてみたいところですが、コンソールにJPQLから覗いた結果が出ているのでそれでよしとします

備考

  • 浮動小数点の検証が気になる。丸め誤差とか。最悪テスト実行後に走らせるSQL文にUPDATEかまして端数を切り捨てるとかすれば出来そうだけど気持ち悪いなあ
  • xlsのテストデータ修正後はEclipseでRefreshかけないとなぜか更新後のデータを読んでくれない

参考文献

  1. Java/DBUnit/ExcelシートでImport,Exportする - きのさいと
  2. 6. 便利な機能 | TECHSCORE(テックスコア)