Dienstag, 27. September 2011

Arquillian How to - Article

Some weeks ago I was asked to write an article about how to use Arquillian. The article was published in the german magazine called "Java aktuell". This magazine is produced for the ijug.eu (INTERESSENVERBUND DER JAVA USER GROUPS E.V.). You can find the article below including the source code examples. Have fun reading.

Vor einigen Wochen wurde ich gefragt, ob ich Lust hätte, einen Artikel über Arquillian zu schreiben. Der Artikel wurde im Magazin "Java aktuell" veröffentlicht. Dieses Magazin wird für die ijug.eu veröffentlicht, den Interessenverbund der Java User Groups e.V.). Anbei findet ihr den Artikel und den dazugehörigen Quellcode. Viel Spaß beim Lesen.

PDF Sonderdruck - Artikel zum Download

Quellcode-Beispiele zum Download

Beispiel 1 - HelloArquillian

IntCounter.java


package eu.ijug.helloarquillian;

public class IntCounter {

public int count(int a, int b) {
return (a + b);
}

}


IntCounterTest.java


package eu.ijug.helloarquillian;

import javax.inject.Inject;
import junit.framework.Assert;
import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class IntCounterTest {

@Inject
private IntCounter counter;

@Deployment
public static JavaArchive createTestArchive() {
return ShrinkWrap.create(JavaArchive.class, "test.jar")
.addClasses(IntCounter.class)
.addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
}

@Test
public void testCount() {
Assert.assertEquals(2, counter.count(1, 1));
}

}


jndi.properties


java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://localhost:1099


pom.xml


<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>eu.ijug</groupId>
<artifactId>HelloArquillian</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>ejb</packaging>
<name>HelloArquillian EJB</name>
<description>A short example using Arquillian on a remote JBoss AS</description>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<arquillian.version>1.0.0.Alpha5</arquillian.version>
</properties>

<repositories>

<repository>
<id>jboss_maven_repository</id>
<name>JBoss Maven repository</name>
<url>http://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>

<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>

<repository>
<id>jboss-deprecated-repository-group</id>
<name>JBoss Deprecated Maven Repository Group</name>
<url>https://repository.jboss.org/nexus/content/repositories/deprecated/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>

</repositories>

<dependencies>

<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.0-SP1</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-junit</artifactId>
<version>${arquillian.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-jbossas-remote-6</artifactId>
<version>${arquillian.version}</version>
</dependency>

<dependency>
<groupId>org.jboss.jbossas</groupId>
<artifactId>jboss-as-profileservice-client</artifactId>
<version>6.0.0.Final</version>
<type>pom</type>
</dependency>

</dependencies>

</project>


Beispiel 2 - HelloEmbedded

TimeServiceEJB.java


package eu.ijug.helloembedded;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.ejb.Stateless;

@Stateless
public class TimeServiceEJB {

public String getCurrentYear() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
return sdf.format(new Date());
}
}


TimeServiceTest.java


package eu.ijug.helloembedded;

import javax.ejb.EJB;

import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

@RunWith(Arquillian.class)
public class TimeServiceTest {

@EJB
private TimeServiceEJB timeService;

@Deployment
public static JavaArchive createTestArchive() {
return ShrinkWrap.create(JavaArchive.class, "timeServiceEJB.jar").addClasses(TimeServiceEJB.class);
}

@Test
public void testGetCurrentYear() {
String year = timeService.getCurrentYear();
assertEquals("2011", year);
}
}


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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>eu.ijug</groupId>
<artifactId>HelloEmbedded</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>ejb</packaging>
<name>Hello Embedded EJB</name>
<description>A short example using Arquillian with embedded Glassfish 3.1 and EJB3.1</description>

<repositories>

<repository>
<id>jboss_maven_repository</id>
<name>JBoss Maven repository</name>
<url>http://repository.jboss.org/nexus/content/groups/public-jboss/groups/</url>
</repository>

<repository>
<id>jboss_public</id>
<name>JBoss public maven repository</name>
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
</repository>

</repositories>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<arquillian.version>1.0.0.Alpha5</arquillian.version>
<glassfish.version>3.1</glassfish.version>
<junit.version>4.8.2</junit.version>
</properties>

<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-junit</artifactId>
<version>${arquillian.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
<version>${arquillian.version}</version>
</dependency>

<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>${glassfish.version}</version>
</dependency>

</dependencies>

</project>