Commits

Micha Kops committed c08614e

arquillian-drone examples added.

Comments (0)

Files changed (8)

 	<groupId>com.hascode.tutorial</groupId>
 	<artifactId>arquillian-tutorial</artifactId>
 	<version>0.0.1</version>
-    <packaging>war</packaging>
-    <name>arquillian-tutorial</name>
+	<packaging>war</packaging>
+	<name>arquillian-tutorial</name>
 
-    <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>
+	<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>
+			<dependency>
+				<groupId>org.jboss.arquillian.extension</groupId>
+				<artifactId>arquillian-drone-bom</artifactId>
+				<version>1.0.0.CR3</version>
+				<type>pom</type>
+				<scope>import</scope>
+			</dependency>
+		</dependencies>
+	</dependencyManagement>
 
-    <dependencies>
-        <dependency>
-            <groupId>org.glassfish.main.extras</groupId>
-            <artifactId>glassfish-embedded-all</artifactId>
-            <version>3.1.2</version>
-            <scope>provided</scope>
-        </dependency>        
-        <dependency>
-            <groupId>org.jboss.arquillian.container</groupId>
-            <artifactId>arquillian-glassfish-embedded-3.1</artifactId>
-            <version>1.0.0.CR3</version>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.jboss.arquillian.junit</groupId>
-            <artifactId>arquillian-junit-container</artifactId>
-            <scope>test</scope>
-        </dependency> 
-                   
-        <dependency>
-            <groupId>junit</groupId>
-            <artifactId>junit</artifactId>
-            <version>4.10</version>
-            <scope>test</scope>
-        </dependency>
-    </dependencies>
+	<dependencies>
+		<dependency>
+			<groupId>org.glassfish.main.extras</groupId>
+			<artifactId>glassfish-embedded-all</artifactId>
+			<version>3.1.2</version>
+			<scope>provided</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.jboss.arquillian.container</groupId>
+			<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
+			<version>1.0.0.CR3</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.jboss.arquillian.junit</groupId>
+			<artifactId>arquillian-junit-container</artifactId>
+			<scope>test</scope>
+		</dependency>
+
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.10</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-drone-impl</artifactId>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.jboss.arquillian.extension</groupId>
+			<artifactId>arquillian-drone-selenium</artifactId>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.jboss.arquillian.extension</groupId>
+			<artifactId>arquillian-drone-selenium-server</artifactId>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.seleniumhq.selenium</groupId>
+			<artifactId>selenium-java</artifactId>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.seleniumhq.selenium</groupId>
+			<artifactId>selenium-server</artifactId>
+			<scope>test</scope>
+			<exclusions>
+				<exclusion>
+					<groupId>org.mortbay.jetty</groupId>
+					<artifactId>servlet-api-2.5</artifactId>
+				</exclusion>
+			</exclusions>
+		</dependency>
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-simple</artifactId>
+			<scope>test</scope>
+		</dependency>
+	</dependencies>
 </project>

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

 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
 import javax.validation.constraints.NotNull;
-import javax.validation.constraints.Size;
 
 @Entity
 public class Book {
 	private Long id;
 
 	@NotNull
-	@Size(min = 3, max = 40)
 	private String title;
 
 	@NotNull
 	public void setAuthor(final String author) {
 		this.author = author;
 	}
+
+	@Override
+	public String toString() {
+		return "Book [id=" + id + ", title=" + title + ", author=" + author
+				+ "]";
+	}
 }

src/main/java/com/hascode/tutorial/jee/BookControllerBean.java

+package com.hascode.tutorial.jee;
+
+import java.util.List;
+
+import javax.faces.bean.SessionScoped;
+import javax.inject.Inject;
+import javax.inject.Named;
+
+@Named("bookControllerBean")
+@SessionScoped
+public class BookControllerBean {
+	@Inject
+	private BookEJB bookEJB;
+
+	private Book book = new Book();
+
+	public String save() {
+		bookEJB.saveBook(book);
+		return "/books.xhtml";
+	}
+
+	public List<Book> getBooks() {
+		return bookEJB.findAllBooks(); // we're lazy here ;)
+	}
+
+	public Book getBook() {
+		return book;
+	}
+
+	public void setBook(final Book book) {
+		this.book = book;
+	}
+
+}

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

 
 	public List<Book> findAllBooks() {
 		final Query query = em
-				.createQuery("SELECT b FROM Book b ORDER BY b.title DESC");
+				.createQuery("SELECT b FROM Book b ORDER BY b.title ASC");
 		List<Book> entries = query.getResultList();
 		if (entries == null) {
 			entries = new ArrayList<Book>();

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

Empty file added.

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

+<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	version="3.0"
+	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
+	<display-name>hascode-arquillian</display-name>
+	<servlet>
+		<servlet-name>Faces Servlet</servlet-name>
+		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
+	</servlet>
+	<servlet-mapping>
+		<servlet-name>Faces Servlet</servlet-name>
+		<url-pattern>*.xhtml</url-pattern>
+	</servlet-mapping>
+	<welcome-file-list>
+		<welcome-file>/books.xhtml</welcome-file>
+	</welcome-file-list>
+</web-app>

src/main/webapp/books.xhtml

+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+	xmlns:h="http://java.sun.com/jsf/html"
+	xmlns:f="http://java.sun.com/jsf/core"
+	xmlns:ui="http://java.sun.com/jsf/facelets"
+	xmlns:p="http://primefaces.prime.com.tr/ui">
+<h:head>
+	<title><ui:insert name="title">hasCode.com - Book Management App</ui:insert></title>
+</h:head>
+<h:body>
+	<p:panel>
+		<h:panelGrid columns="2" cellpadding="10">
+			<h:panelGroup>
+				<h1>
+					<ui:insert name="heading">Java EE 6 Tutorial - Blog Application</ui:insert>
+				</h1>
+			</h:panelGroup>
+		</h:panelGrid>
+		<h2>#{bookControllerBean.books.size()} Books available:</h2>
+		<ul>
+			<ui:repeat value="#{bookControllerBean.books}" var="book">
+				<li><h:outputText value="#{book.title} by #{book.author}" /></li>
+			</ui:repeat>
+		</ul>
+		<h2>Create new book:</h2>
+		<h:messages/>
+		<h:form id="createBookForm">
+			<h:panelGrid columns="3" cellpadding="10">
+				<label>Title</label>
+				<h:inputText label="Title" id="title"
+					value="#{bookControllerBean.book.title}" required="true" />
+				<h:message for="title" class="errorMsg" />
+				<label>Author</label>
+				<h:inputText label="Author" id="author"
+					value="#{bookControllerBean.book.author}" required="true" />
+				<h:message for="author" class="errorMsg" />
+				<h:commandButton title="Save" value="Save" id="saveBook"
+					action="#{bookControllerBean.save}" />
+			</h:panelGrid>
+		</h:form>
+	</p:panel>
+</h:body>
+</html>

src/test/java/com/hascode/tutorial/jee/CreateBookSeleniumTest.java

+package com.hascode.tutorial.jee;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.net.URL;
+
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.drone.api.annotation.Drone;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.test.api.ArquillianResource;
+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;
+
+import com.thoughtworks.selenium.DefaultSelenium;
+
+@RunWith(Arquillian.class)
+public class CreateBookSeleniumTest {
+	private static final String WEBAPP_SRC = "src/main/webapp";
+
+	@Deployment(testable = false)
+	public static WebArchive createDeployment() {
+		return ShrinkWrap
+				.create(WebArchive.class, "books.war")
+				.addClasses(Book.class, BookEJB.class, BookControllerBean.class)
+				.addAsResource("META-INF/persistence.xml")
+				.addAsWebResource(new File(WEBAPP_SRC, "books.xhtml"))
+				.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
+				.addAsWebInfResource(EmptyAsset.INSTANCE, "faces-config.xml");
+	}
+
+	@Drone
+	DefaultSelenium browser;
+
+	@ArquillianResource
+	URL deploymentURL;
+
+	@Test
+	public void should_login_successfully() {
+		// this will be http://localhost:8181/books/books.xhtml
+		browser.open(deploymentURL + "books.xhtml");
+		browser.waitForPageToLoad("20000");
+		browser.type("id=createBookForm:title", "My book title");
+		browser.type("id=createBookForm:author", "The author");
+		browser.click("id=createBookForm:saveBook");
+		browser.waitForPageToLoad("20000");
+
+		assertTrue(
+				"Book title present",
+				browser.isElementPresent("xpath=//li[contains(text(), 'My book title by The author')]"));
+	}
+}