Commits

Micha Kops committed c5c1719

Initial import.

  • Participants

Comments (0)

Files changed (9)

+infinitest.filters
+.project
+.classpath
+target
+.settings
+derby.log
+*.orig
+# Arquillian Transactional Samples
+
+Some short examples using transaction management features in an Arquillian test.
+
+Please feel free to have a look at [my blog] for the full tutorial.
+
+## Starting the embedded GlassFish
+
+Simply run the following command from the project directory:
+
+```
+mvn
+```
+
+
+## Calling the RESTful Webservice
+
+**List all books**
+
+```
+curl -XGET http://localhost:8080/sample/ws/book/all
+```
+
+**Create new book**
+
+```
+curl -XPOST -H "Content-type: application/json" http://localhost:8080/sample/ws/book/all -d '{book:{title:"A book"}}'
+```
+
+----
+
+** 2013 Micha Kops /hasCode.com**
+
+   [my blog]:http://www.hascode.com
+<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.hascode.tutorial</groupId>
+	<artifactId>arquillian-transactional-sample</artifactId>
+	<version>1.0.0</version>
+	<packaging>war</packaging>
+
+	<prerequisites>
+		<maven>3.0.4</maven>
+	</prerequisites>
+
+	<properties>
+		<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<derby.version>10.9.1.0</derby.version>
+		<flyway.version>2.0.3</flyway.version>
+	</properties>
+
+	<dependencyManagement>
+		<dependencies>
+			<dependency>
+				<groupId>org.jboss.arquillian</groupId>
+				<artifactId>arquillian-bom</artifactId>
+				<version>1.0.3.Final</version>
+				<scope>import</scope>
+				<type>pom</type>
+			</dependency>
+		</dependencies>
+	</dependencyManagement>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.glassfish.main.extras</groupId>
+			<artifactId>glassfish-embedded-all</artifactId>
+			<version>3.1.2.2</version>
+			<scope>provided</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.jboss.arquillian.container</groupId>
+			<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
+			<version>1.0.0.CR4</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.hamcrest</groupId>
+			<artifactId>hamcrest-all</artifactId>
+			<version>1.3</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.11</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.jboss.arquillian.junit</groupId>
+			<artifactId>arquillian-junit-container</artifactId>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.jboss.arquillian.extension</groupId>
+			<artifactId>arquillian-transaction-jta</artifactId>
+			<version>1.0.0.Alpha3</version>
+		</dependency>
+		<dependency>
+			<groupId>org.eclipse.persistence</groupId>
+			<artifactId>eclipselink</artifactId>
+			<version>2.5.0</version>
+		</dependency>
+		<dependency>
+			<groupId>org.glassfish</groupId>
+			<artifactId>maven-embedded-glassfish-plugin</artifactId>
+			<version>3.1.1</version>
+			<type>maven-plugin</type>
+		</dependency>
+	</dependencies>
+
+	<repositories>
+		<repository>
+			<id>glassfish-repository</id>
+			<url>http://download.java.net/maven/glassfish</url>
+		</repository>
+		<repository>
+			<id>maven2-repository.dev.java.net</id>
+			<name>Java.net Repository for Maven</name>
+			<url>http://download.java.net/maven/2</url>
+		</repository>
+		<repository>
+			<name>jboss-public</name>
+			<id>jboss-public</id>
+			<url>https://repository.jboss.org/nexus/content/repositories/</url>
+		</repository>
+		<repository>
+			<id>EclipseLink Build Repo</id>
+			<url>http://maven.eclipse.org/nexus/content/groups/build/</url>
+		</repository>
+	</repositories>
+
+	<build>
+		<defaultGoal>clean package org.glassfish:maven-embedded-glassfish-plugin:3.1.1:run</defaultGoal>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>2.4</version>
+				<configuration>
+					<source>1.7</source>
+					<target>1.7</target>
+					<compilerArguments>
+						<endorseddirs>${endorsed.dir}</endorseddirs>
+					</compilerArguments>
+				</configuration>
+			</plugin>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-war-plugin</artifactId>
+				<version>2.1.1</version>
+				<configuration>
+					<failOnMissingWebXml>false</failOnMissingWebXml>
+				</configuration>
+			</plugin>
+			<plugin>
+				<groupId>org.glassfish</groupId>
+				<artifactId>maven-embedded-glassfish-plugin</artifactId>
+				<version>3.1.1</version>
+				<configuration>
+					<goalPrefix>glassfish</goalPrefix>
+					<app>${project.build.directory}/${project.build.finalName}</app>
+					<port>8080</port>
+					<contextRoot>sample</contextRoot>
+					<name>${project.name}</name>
+					<autoDelete>true</autoDelete>
+				</configuration>
+				<dependencies>
+					<dependency>
+						<groupId>org.apache.derby</groupId>
+						<artifactId>derby</artifactId>
+						<version>${derby.version}</version>
+					</dependency>
+					<dependency>
+						<groupId>org.apache.derby</groupId>
+						<artifactId>derbyclient</artifactId>
+						<version>${derby.version}</version>
+					</dependency>
+				</dependencies>
+			</plugin>
+		</plugins>
+	</build>
+</project>
+

src/main/java/com/hascode/tutorial/ejb/BookEJB.java

+package com.hascode.tutorial.ejb;
+
+import java.util.List;
+
+import javax.ejb.Stateless;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+import com.hascode.tutorial.entity.Book;
+
+@Stateless
+public class BookEJB {
+	@PersistenceContext
+	private EntityManager em;
+
+	public void save(final Book book) {
+		em.persist(book);
+	}
+
+	public List<Book> findAll() {
+		return em.createQuery("SELECT b FROM Book b ORDER BY b.title",
+				Book.class).getResultList();
+	}
+}

src/main/java/com/hascode/tutorial/entity/Book.java

+package com.hascode.tutorial.entity;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@Entity
+@XmlRootElement
+public class Book {
+	@Id
+	@GeneratedValue
+	private Long id;
+	private String title;
+
+	public final Long getId() {
+		return id;
+	}
+
+	public final void setId(final Long id) {
+		this.id = id;
+	}
+
+	public final String getTitle() {
+		return title;
+	}
+
+	public final void setTitle(final String title) {
+		this.title = title;
+	}
+
+	@Override
+	public String toString() {
+		return "Book [id=" + id + ", title=" + title + "]";
+	}
+}

src/main/java/com/hascode/tutorial/ws/BookService.java

+package com.hascode.tutorial.ws;
+
+import java.util.List;
+
+import javax.ejb.EJB;
+import javax.ejb.Stateless;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.GenericEntity;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import com.hascode.tutorial.ejb.BookEJB;
+import com.hascode.tutorial.entity.Book;
+
+@Path("/book")
+@Stateless
+public class BookService {
+	@EJB
+	private BookEJB bookEJB;
+
+	@POST
+	@Consumes(MediaType.APPLICATION_JSON)
+	public Response createBook(final Book book) {
+		bookEJB.save(book);
+		return Response.ok().build();
+	}
+
+	@Path("/all")
+	@GET
+	@Produces(MediaType.APPLICATION_JSON)
+	public Response findAllBooks() {
+		GenericEntity<List<Book>> books = new GenericEntity<List<Book>>(
+				bookEJB.findAll()) {
+		};
+		return Response.ok(books).build();
+	}
+}

src/main/resources/META-INF/persistence.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<persistence version="1.0"
+	xmlns="http://java.sun.com/xml/ns/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_1_0.xsd">
+	<persistence-unit name="default" transaction-type="JTA">
+		<provider>oracle.toplink.essentials.PersistenceProvider</provider>
+		<jta-data-source>jdbc/__default</jta-data-source>
+		<properties>
+			<property name="eclipselink.ddl-generation" value="create-tables" />
+		</properties>
+	</persistence-unit>
+</persistence>

src/main/resources/beans.xml

Empty file added.

src/main/webapp/WEB-INF/web.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
+	<servlet>
+		<servlet-name>rest-servlet-spi</servlet-name>
+		<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
+		<load-on-startup>1</load-on-startup>
+	</servlet>
+	<servlet-mapping>
+		<servlet-name>rest-servlet-spi</servlet-name>
+		<url-pattern>/ws/*</url-pattern>
+	</servlet-mapping>
+</web-app>