Skip to content

Commit

Permalink
SEAMREST-29
Browse files Browse the repository at this point in the history
  • Loading branch information
jharting committed Mar 7, 2011
1 parent 843d88c commit b7473f6
Show file tree
Hide file tree
Showing 13 changed files with 345 additions and 42 deletions.
85 changes: 69 additions & 16 deletions examples/tasks/pom.xml
Expand Up @@ -104,6 +104,11 @@
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -136,15 +141,6 @@
</build>

<profiles>
<profile>
<id>catch</id>
<dependencies>
<dependency>
<groupId>org.jboss.seam.catch</groupId>
<artifactId>seam-catch</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>jboss-remote-6</id>
<build>
Expand Down Expand Up @@ -244,16 +240,73 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>src/main/jboss</directory>
</resource>
</webResources>
</configuration>
<executions>
<execution>
<id>package-jboss</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<useCache>false</useCache>
<warSourceDirectory>src/main/jboss</warSourceDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>velocity</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<id>package-velocity</id>
<phase>package</phase>
<goals>
<goal>war</goal>
</goals>
<configuration>
<useCache>false</useCache>
<warSourceDirectory>src/main/velocity</warSourceDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
</dependency>
<!-- exclude freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>

<profile>
<id>catch</id>
<dependencies>
<dependency>
<groupId>org.jboss.seam.catch</groupId>
<artifactId>seam-catch</artifactId>
</dependency>
</dependencies>
</profile>

<profile>
Expand Down
11 changes: 7 additions & 4 deletions examples/tasks/readme.txt
Expand Up @@ -7,22 +7,25 @@ mvn clean package
cp target/seam-tasks.war $JBOSS_HOME/server/default/deploy
$JBOSS_HOME/bin/run.sh -Djboss.i18n.generate-proxies=true

To turn on Seam Catch integration, use -Pcatch (e.g. mvn clean package -Pcatch)

Deploying to Glassfish
======================
mvn clean package -Pglassfish
$GF_HOME/bin/asadmin start-database
$GF_HOME/bin/asadmin start-domain
$GF_HOME/bin/asadmin deploy target/seam-tasks.war


Deploying to Resin (not currently supported)
======================
mvn clean package -Presin
cp target/seam-tasks.war $RESIN_HOME/webapps
$RESIN_HOME/bin/resin.sh start

Build options
======================
To turn on Seam Catch integration, use -Pcatch (e.g. mvn clean package -Pcatch)
To use Apache Velocity instead of FreeMarker, use -Pvelociry (e.g. mvn clean package -Pvelocity)
Build options can be combined and used during functional testsuite execution.

Running functional test from command line
======================
build & deploy the application (using steps above)
Expand All @@ -42,4 +45,4 @@ arguments in the arguments tab:

-Dmethod=* -Dbrowser=*firefoxproxy -Dcontext.root=http://localhost:8080/ -Dcontext.path=/seam-tasks/ -Dselenium.host=localhost -Dselenium.port=14444 -Dselenium.debug=false -Dselenium.maximize=false -Dselenium.timeout.default=30000 -Dselenium.timeout.gui=5000 -Dselenium.timeout.ajax=15000 -Dselenium.timeout.model=30000 -Dselenium.speed=0 -Dselenium.timeout=3000 -Dbasedir=.

Note that you need to add the arguments for every test class.
Note that you need to add the arguments for every test class.
Expand Up @@ -12,6 +12,8 @@
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

import org.jboss.seam.rest.example.tasks.entity.Category;
import org.jboss.seam.rest.templating.ResponseTemplate;
Expand All @@ -38,6 +40,8 @@ public class CategoryCollectionResource
@Min(value = 0, message = "limit must be a non-negative number")
@Max(value = 100, message = "Cannot return more than 100 items")
protected int limit;
@Context
protected UriInfo uriInfo;

@GET
@ValidateRequest
Expand All @@ -50,4 +54,19 @@ public List<Category> getCategories()
{
return bean.getCategories(start, limit);
}

public int getStart()
{
return start;
}

public int getLimit()
{
return limit;
}

public UriInfo getUriInfo()
{
return uriInfo;
}
}
Expand Up @@ -73,4 +73,19 @@ public void setStatus(String status)
{
this.status = status;
}

public UriInfo getUriInfo()
{
return uriInfo;
}

public int getStart()
{
return start;
}

public int getLimit()
{
return limit;
}
}
2 changes: 1 addition & 1 deletion examples/tasks/src/main/resources/META-INF/seam-beans.xml
@@ -1,7 +1,7 @@
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:s="urn:java:ee" xmlns:rest="urn:java:org.jboss.seam.rest:org.jboss.seam.rest.exceptions">
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<rest:SeamRestConfiguration preferedTemplatingProvider="org.jboss.seam.rest.templating.freemarker.FreeMarkerProvider">
<rest:SeamRestConfiguration>
<rest:mappings>
<s:value>
<rest:Mapping exceptionType="javax.persistence.NoResultException" statusCode="404">
Expand Down
29 changes: 29 additions & 0 deletions examples/tasks/src/main/velocity/freemarker/categories-short.ftl
@@ -0,0 +1,29 @@
#*
This file contains an Apache Velocity template. It uses FreeMarker's file extension (.ftl)
since we need to switch between FreeMarker / Velocity without changing the Java code (annotations).
*#
<categories>
<link href="self" href="${categoryCollectionResource.uriInfo.baseUri}category?start=${categoryCollectionResource.start}&limit=${categoryCollectionResource.limit}"/>
#if ( $response.size() == $categoryCollectionResource.limit )
#set( $next = $categoryCollectionResource.start + $categoryCollectionResource.limit )
<link href="next" href="${categoryCollectionResource.uriInfo.baseUri}category?start=${next}&limit=${categoryCollectionResource.limit}"/>
#end
#if ( $categoryCollectionResource.start >= $categoryCollectionResource.limit )
#set( $previous = $categoryCollectionResource.start - $categoryCollectionResource.limit)
<link href="previous" href="${categoryCollectionResource.uriInfo.baseUri}category?start=${previous}&limit=${categoryCollectionResource.limit}"/>
#end
#foreach( ${category} in ${response} )
<category>
<name>${category.name}</name>
<link rel="self" href="${categoryCollectionResource.uriInfo.baseUri}category/${category.name}"/>
<tasks>
#foreach ( $task in $category.tasks )
<task>
<name>${task.name}</name>
<link rel="self" href="${categoryCollectionResource.uriInfo.baseUri}task/${task.id}"/>
</task>
#end
</tasks>
</category>
#end
<categories>
26 changes: 26 additions & 0 deletions examples/tasks/src/main/velocity/freemarker/categories.ftl
@@ -0,0 +1,26 @@
#*
This file contains an Apache Velocity template. It uses FreeMarker's file extension (.ftl)
since we need to switch between FreeMarker / Velocity without changing the Java code (annotations).
*#
<categories>
<link href="self" href="${categoryCollectionResource.uriInfo.baseUri}category?start=${categoryCollectionResource.start}&limit=${categoryCollectionResource.limit}"/>
#if ( $response.size() == $categoryCollectionResource.limit )
#set( $next = $categoryCollectionResource.start + $categoryCollectionResource.limit )
<link href="next" href="${categoryCollectionResource.uriInfo.baseUri}category?start=${next}&limit=${categoryCollectionResource.limit}"/>
#end
#if ( $categoryCollectionResource.start >= $categoryCollectionResource.limit )
#set( $previous = $categoryCollectionResource.start - $categoryCollectionResource.limit)
<link href="previous" href="${categoryCollectionResource.uriInfo.baseUri}category?start=${previous}&limit=${categoryCollectionResource.limit}"/>
#end
#foreach( ${category} in ${response} )
<category>
<name>${category.name}</name>
<link rel="self" href="${categoryCollectionResource.uriInfo.baseUri}category/${category.name}"/>
<tasks>
#foreach ( $task in $category.tasks )
#parse("/freemarker/task.ftl")
#end
</tasks>
</category>
#end
<categories>
13 changes: 13 additions & 0 deletions examples/tasks/src/main/velocity/freemarker/task.ftl
@@ -0,0 +1,13 @@
#*
This file contains an Apache Velocity template. It uses FreeMarker's file extension (.ftl)
since we need to switch between FreeMarker / Velocity without changing the Java code (annotations).
*#
<task>
<name>${task.name}</name>
<link rel="self" href="${taskCollectionResource.uriInfo.baseUri}task/${task.id}"/>
<link rel="http://sfwk.org/rest/tasks/move" href="${taskCollectionResource.uriInfo.baseUri}task/${task.id}/move?category={category-name}"/>
<link rel="edit" href="${taskCollectionResource.uriInfo.baseUri}task/${task.id}"/>
<resolved>${task.isResolved()}</resolved>
<created>${task.created}</created>
<updated>${task.updated}</updated>
</task>
18 changes: 18 additions & 0 deletions examples/tasks/src/main/velocity/freemarker/tasks.ftl
@@ -0,0 +1,18 @@
#*
This file contains an Apache Velocity template. It uses FreeMarker's file extension (.ftl)
since we need to switch between FreeMarker / Velocity without changing the Java code (annotations).
*#
<tasks>
<link href="self" href="${taskCollectionResource.uriInfo.absolutePath}?start=${taskCollectionResource.start}&limit=${taskCollectionResource.limit}"/>
#if ( $response.size() == $taskCollectionResource.limit)
#set( $next = $taskCollectionResource.start + $taskCollectionResource.limit )
<link href="next" href="${taskCollectionResource.uriInfo.absolutePath}?start=${next}&limit=${taskCollectionResource.limit}"/>
#end
#if ( $taskCollectionResource.start >= $taskCollectionResource.limit )
#set( $previous = $taskCollectionResource.start - $taskCollectionResource.limit)
<link href="previous" href="${taskCollectionResource.uriInfo.absolutePath}?start=${previous}&limit=${taskCollectionResource.limit}"/>
#end
#foreach( ${task} in ${response} )
#parse("/freemarker/task.ftl")
#end
</tasks>
2 changes: 1 addition & 1 deletion examples/tasks/src/main/webapp/freemarker/task.ftl
@@ -1,7 +1,7 @@
<task>
<name>${task.name}</name>
<link rel="self" href="${taskCollectionResource.uriInfo.baseUri}task/${task.id}"/>
<link rel="http://sfwk.org/rest/tasks/move" href="${categoryCollectionResource.uriInfo.baseUri}task/${task.id}/move?category={category-name}"/>
<link rel="http://sfwk.org/rest/tasks/move" href="${taskCollectionResource.uriInfo.baseUri}task/${task.id}/move?category={category-name}"/>
<link rel="edit" href="${taskCollectionResource.uriInfo.baseUri}task/${task.id}"/>
<resolved>${task.isResolved()?string}</resolved>
<created>${task.created}</created>
Expand Down
@@ -0,0 +1,80 @@
package org.jboss.seam.rest.example.tasks.test;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertTrue;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.testng.annotations.Test;

/**
* This test verifies XML representations created by FreeMarker or Apache Velocity.
* @author <a href="http://community.jboss.org/people/jharting">Jozef Hartinger</a>
*
*/
public class XmlRepresentationTest
{
private HttpClient client = new HttpClient();

private String getRepresentation(String url, String accept) throws Exception
{
GetMethod get = new GetMethod(url);
get.setRequestHeader("Accept", accept);
assertEquals(200, client.executeMethod(get));
return get.getResponseBodyAsString();
}

@Test
public void testTask() throws Exception
{
String representation = getRepresentation("http://localhost:8080/seam-tasks/api/task/2", "application/task+xml");
assertTrue(representation.contains("<name>Build the Turing machine</name>"));
assertTrue(representation.contains("<link rel=\"self\" href=\"http://localhost:8080/seam-tasks/api/task/2\"/>"));
assertTrue(representation.contains("<link rel=\"http://sfwk.org/rest/tasks/move\" href=\"http://localhost:8080/seam-tasks/api/task/2/move?category={category-name}\"/>"));
assertTrue(representation.contains("<link rel=\"edit\" href=\"http://localhost:8080/seam-tasks/api/task/2\"/>"));
assertTrue(representation.contains("<resolved>false</resolved>"));
}

@Test
public void testTasks() throws Exception
{
String representation = getRepresentation("http://localhost:8080/seam-tasks/api/task?start=4&limit=1", "application/tasks+xml");
assertTrue(representation.contains("<link href=\"self\" href=\"http://localhost:8080/seam-tasks/api/task?start=4&limit=1\"/>"));
assertTrue(representation.contains("<link href=\"next\" href=\"http://localhost:8080/seam-tasks/api/task?start=5&limit=1\"/>"));
assertTrue(representation.contains("<link href=\"previous\" href=\"http://localhost:8080/seam-tasks/api/task?start=3&limit=1\"/>"));
assertTrue(representation.contains("<name>Pick up meal tickets</name>"));
assertTrue(representation.contains("<link rel=\"self\" href=\"http://localhost:8080/seam-tasks/api/task/8\"/>"));
assertTrue(representation.contains("<link rel=\"http://sfwk.org/rest/tasks/move\" href=\"http://localhost:8080/seam-tasks/api/task/8/move?category={category-name}\"/>"));
assertTrue(representation.contains("<link rel=\"edit\" href=\"http://localhost:8080/seam-tasks/api/task/8\"/>"));
}

@Test
public void testCategories() throws Exception
{
String representation = getRepresentation("http://localhost:8080/seam-tasks/api/category?start=1&limit=1", "application/categories+xml");
assertTrue(representation.contains("<link href=\"self\" href=\"http://localhost:8080/seam-tasks/api/category?start=1&limit=1\"/>"));
assertTrue(representation.contains("<link href=\"next\" href=\"http://localhost:8080/seam-tasks/api/category?start=2&limit=1\"/>"));
assertTrue(representation.contains("<link href=\"previous\" href=\"http://localhost:8080/seam-tasks/api/category?start=0&limit=1\"/>"));
assertTrue(representation.contains("<name>Work</name>"));
assertTrue(representation.contains("<link rel=\"self\" href=\"http://localhost:8080/seam-tasks/api/category/Work\"/>"));
assertTrue(representation.contains("<name>Pick up meal tickets</name>"));
assertTrue(representation.contains("<link rel=\"self\" href=\"http://localhost:8080/seam-tasks/api/task/8\"/>"));
assertTrue(representation.contains("<link rel=\"http://sfwk.org/rest/tasks/move\" href=\"http://localhost:8080/seam-tasks/api/task/8/move?category={category-name}\"/>"));
assertTrue(representation.contains("<link rel=\"edit\" href=\"http://localhost:8080/seam-tasks/api/task/8\"/>"));
assertTrue(representation.contains("<resolved>false</resolved>"));
}

@Test
public void testCategoriesShort() throws Exception
{
String representation = getRepresentation("http://localhost:8080/seam-tasks/api/category?start=1&limit=1", "application/categories-short+xml");
assertTrue(representation.contains("<link href=\"self\" href=\"http://localhost:8080/seam-tasks/api/category?start=1&limit=1\"/>"));
assertTrue(representation.contains("<link href=\"next\" href=\"http://localhost:8080/seam-tasks/api/category?start=2&limit=1\"/>"));
assertTrue(representation.contains("<link href=\"previous\" href=\"http://localhost:8080/seam-tasks/api/category?start=0&limit=1\"/>"));
assertTrue(representation.contains("<name>Work</name>"));
assertTrue(representation.contains("<link rel=\"self\" href=\"http://localhost:8080/seam-tasks/api/category/Work\"/>"));
assertTrue(representation.contains("<name>Pick up meal tickets</name>"));
assertTrue(representation.contains("<link rel=\"self\" href=\"http://localhost:8080/seam-tasks/api/task/8\"/>"));
}

}

0 comments on commit b7473f6

Please sign in to comment.