Author: jliu
Date: Mon Jul 16 01:46:44 2007
New Revision: 556553
URL: http://svn.apache.org/viewvc?view=rev&rev=556553
Log:
Added a system test for HTTP binding using JSON as content type. NOTE, I have
not figured out why RestClientServerBookTest.testAddBookJSON() does not work
yet.
Added:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/add_book_json.txt
(with props)
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_book123.txt
(with props)
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_books.txt
(with props)
Modified:
incubator/cxf/trunk/systests/pom.xml
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/BookServer.java
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerBookTest.java
Modified: incubator/cxf/trunk/systests/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/pom.xml?view=diff&rev=556553&r1=556552&r2=556553
==============================================================================
--- incubator/cxf/trunk/systests/pom.xml (original)
+++ incubator/cxf/trunk/systests/pom.xml Mon Jul 16 01:46:44 2007
@@ -373,6 +373,19 @@
<artifactId>junit</artifactId>
<version>4.3.1</version>
</dependency>
+ <dependency>
+ <groupId>org.codehaus.jettison</groupId>
+ <artifactId>jettison</artifactId>
+ <version>1.0-RC1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-httpclient</groupId>
+ <artifactId>commons-httpclient</artifactId>
+ <version>3.1-rc1</version>
+ <scope>test</scope>
+ </dependency>
+
</dependencies>
<properties>
<surefire.fork.mode>pertest</surefire.fork.mode>
Modified:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/BookServer.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/BookServer.java?view=diff&rev=556553&r1=556552&r2=556553
==============================================================================
---
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/BookServer.java
(original)
+++
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/BookServer.java
Mon Jul 16 01:46:44 2007
@@ -19,12 +19,20 @@
package org.apache.cxf.systest.rest;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+
import org.apache.cxf.binding.http.HttpBindingFactory;
import org.apache.cxf.customer.book.BookService;
import org.apache.cxf.customer.book.BookServiceImpl;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.service.invoker.BeanInvoker;
import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.codehaus.jettison.mapped.MappedXMLInputFactory;
+import org.codehaus.jettison.mapped.MappedXMLOutputFactory;
public class BookServer extends AbstractBusTestServerBase {
@@ -44,6 +52,39 @@
sf.getServiceFactory().setWrapped(false);
sf.create();
+
+ JaxWsServerFactoryBean sfJson = new JaxWsServerFactoryBean();
+ sfJson.setServiceClass(BookService.class);
+ // Use the HTTP Binding which understands the Java Rest Annotations
+ sfJson.setBindingId(HttpBindingFactory.HTTP_BINDING_ID);
+ sfJson.setAddress("http://localhost:9080/json");
+ sfJson.getServiceFactory().setInvoker(new BeanInvoker(serviceObj));
+
+ // Turn the "wrapped" style off. This means that CXF won't generate
+ // wrapper JSON elements and we'll have prettier JSON text. This
+ // means that we need to stick to one request and one response
+ // parameter though.
+ sfJson.getServiceFactory().setWrapped(false);
+
+ // Tell CXF to use a different Content-Type for the JSON endpoint
+ // This should probably be application/json, but text/plain allows
+ // us to view easily in a web browser.
+ Map<String, Object> properties = new HashMap<String, Object>();
+ properties.put("Content-Type", "text/plain");
+
+ // Set up the JSON StAX implementation
+ Map<String, String> nstojns = new HashMap<String, String>();
+ nstojns.put("http://book.acme.com", "acme");
+
+ MappedXMLInputFactory xif = new MappedXMLInputFactory(nstojns);
+ properties.put(XMLInputFactory.class.getName(), xif);
+
+ MappedXMLOutputFactory xof = new MappedXMLOutputFactory(nstojns);
+ properties.put(XMLOutputFactory.class.getName(), xof);
+
+ sfJson.setProperties(properties);
+
+ sfJson.create();
}
public static void main(String[] args) {
Modified:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerBookTest.java
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerBookTest.java?view=diff&rev=556553&r1=556552&r2=556553
==============================================================================
---
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerBookTest.java
(original)
+++
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/RestClientServerBookTest.java
Mon Jul 16 01:46:44 2007
@@ -19,16 +19,26 @@
package org.apache.cxf.systest.rest;
+import java.io.File;
+import java.io.InputStream;
+import java.net.URL;
import java.util.logging.Logger;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.FileRequestEntity;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.cxf.binding.http.HttpBindingFactory;
import org.apache.cxf.customer.book.Book;
import org.apache.cxf.customer.book.BookService;
import org.apache.cxf.customer.book.GetAnotherBook;
import org.apache.cxf.customer.book.GetBook;
+import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
import org.junit.BeforeClass;
+import org.junit.Ignore;
import org.junit.Test;
@@ -77,4 +87,71 @@
assertEquals(book.getId(), (long)123);
assertEquals(book.getName(), "CXF in Action");
}
+
+
+ @Test
+ public void testGetBooksJSON() throws Exception {
+ String endpointAddress =
+ "http://localhost:9080/json/books";
+ URL url = new URL(endpointAddress);
+ InputStream in = url.openStream();
+ assertNotNull(in);
+
+ InputStream expected =
getClass().getResourceAsStream("resources/expected_json_books.txt");
+
+ assertEquals(getStringFromInputStream(expected),
getStringFromInputStream(in));
+ }
+
+ @Test
+ public void testGetBookJSON() throws Exception {
+ String endpointAddress =
+ "http://localhost:9080/json/books/123";
+ URL url = new URL(endpointAddress);
+ InputStream in = url.openStream();
+ assertNotNull(in);
+
+ InputStream expected =
getClass().getResourceAsStream("resources/expected_json_book123.txt");
+
+ assertEquals(getStringFromInputStream(expected),
getStringFromInputStream(in));
+ }
+
+ @Test
+ @Ignore("Have not figured out yet why this does not work")
+ public void testAddBookJSON() throws Exception {
+ String endpointAddress =
+ "http://localhost:9080/json/books";
+
+ String inputFile =
getClass().getResource("resources/add_book_json.txt").getFile();
+ File input = new File(inputFile);
+ // Prepare HTTP post
+ PostMethod post = new PostMethod(endpointAddress);
+ // Request content will be retrieved directly
+ // from the input stream
+ RequestEntity entity = new FileRequestEntity(input, "text/plain;
charset=ISO-8859-1");
+ post.setRequestEntity(entity);
+ // Get HTTP client
+ HttpClient httpclient = new HttpClient();
+ // Execute request
+ try {
+ int result = httpclient.executeMethod(post);
+ // Display status code
+ System.out.println("Response status code: " + result);
+ // Display response
+ System.out.println("Response body: ");
+ System.out.println(post.getResponseBodyAsString());
+ } finally {
+ // Release current connection to the connection pool once you are
done
+ post.releaseConnection();
+ }
+ }
+
+ private String getStringFromInputStream(InputStream in) throws Exception {
+ CachedOutputStream bos = new CachedOutputStream();
+ IOUtils.copy(in, bos);
+ in.close();
+ bos.close();
+ System.out.println(bos.getOut().toString());
+ return bos.getOut().toString();
+ }
+
}
Added:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/add_book_json.txt
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/add_book_json.txt?view=auto&rev=556553
==============================================================================
---
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/add_book_json.txt
(added)
+++
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/add_book_json.txt
Mon Jul 16 01:46:44 2007
@@ -0,0 +1,2 @@
+{"acme.Book":{"acme.name":"CXF in Action - JSON"}}
+
\ No newline at end of file
Propchange:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/add_book_json.txt
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/add_book_json.txt
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_book123.txt
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_book123.txt?view=auto&rev=556553
==============================================================================
---
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_book123.txt
(added)
+++
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_book123.txt
Mon Jul 16 01:46:44 2007
@@ -0,0 +1 @@
+{"acme.Book":{"acme.id":"123","acme.name":"CXF in Action"}}
\ No newline at end of file
Propchange:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_book123.txt
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_book123.txt
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_books.txt
URL:
http://svn.apache.org/viewvc/incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_books.txt?view=auto&rev=556553
==============================================================================
---
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_books.txt
(added)
+++
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_books.txt
Mon Jul 16 01:46:44 2007
@@ -0,0 +1 @@
+{"acme.books":{"acme.books":{"acme.id":"123","acme.name":"CXF in Action"}}}
\ No newline at end of file
Propchange:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_books.txt
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/rest/resources/expected_json_books.txt
------------------------------------------------------------------------------
svn:mime-type = text/plain