cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1389328 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
Date Mon, 24 Sep 2012 12:23:03 GMT
Author: sergeyb
Date: Mon Sep 24 12:23:02 2012
New Revision: 1389328

URL: http://svn.apache.org/viewvc?rev=1389328&view=rev
Log:
Adding a test for post async call, and adding a shortcut to WebClient

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
    cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java?rev=1389328&r1=1389327&r2=1389328&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/WebClient.java Mon
Sep 24 12:23:02 2012
@@ -383,6 +383,16 @@ public class WebClient extends AbstractC
     }
     
     /**
+     * Does HTTP Async POST invocation and returns Future.
+     * Shortcut for async().get(InvocationCallback)
+     * @param callback invocation callback 
+     * @return the future
+     */
+    public <T> Future<T> post(Object body, InvocationCallback<T> callback)
{
+        return doInvokeAsyncCallback("POST", body, body.getClass(), getClass(), callback);
+    }
+    
+    /**
      * Does HTTP invocation and returns a collection of typed objects 
      * @param httpMethod HTTP method 
      * @param body request body, can be null

Modified: cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java?rev=1389328&r1=1389327&r2=1389328&view=diff
==============================================================================
--- cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
(original)
+++ cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRS20ClientServerBookTest.java
Mon Sep 24 12:23:02 2012
@@ -31,6 +31,7 @@ import javax.ws.rs.client.ClientRequestC
 import javax.ws.rs.client.ClientRequestFilter;
 import javax.ws.rs.client.ClientResponseContext;
 import javax.ws.rs.client.ClientResponseFilter;
+import javax.ws.rs.client.Entity;
 import javax.ws.rs.client.InvocationCallback;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
@@ -117,6 +118,17 @@ public class JAXRS20ClientServerBookTest
         return wc;
     }
     
+    private WebClient createWebClientPost(String address) {
+        List<Object> providers = new ArrayList<Object>();
+        providers.add(new ClientHeaderRequestFilter());
+        providers.add(new ClientHeaderResponseFilter());
+        providers.add(new ClientReaderInterceptor());
+        providers.add(new ClientWriterInterceptor());
+        WebClient wc = WebClient.create(address, providers);
+        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
+        return wc;
+    }
+    
     private void doTestGetBookAsync(String address, boolean asyncInvoker) 
         throws InterruptedException, ExecutionException {
         
@@ -138,6 +150,27 @@ public class JAXRS20ClientServerBookTest
         validateResponse(wc);   
     }
     
+    private void doTestPostBookAsyncHandler(String address) 
+        throws InterruptedException, ExecutionException {
+        
+        WebClient wc = createWebClientPost(address);
+        
+        final Holder<Book> holder = new Holder<Book>();
+        final InvocationCallback<Book> callback = new InvocationCallback<Book>()
{
+            public void completed(Book response) {
+                holder.value = response;
+            }
+            public void failed(ClientException error) {
+            }
+        };
+        
+        Future<Book> future = wc.post(new Book("async", 126L), callback);
+        Book book = future.get();
+        assertSame(book, holder.value);
+        assertEquals(124L, book.getId());
+        validatePostResponse(wc);   
+    }
+    
     private void doTestGetBookAsyncResponse(String address, boolean asyncInvoker) 
         throws InterruptedException, ExecutionException {
         
@@ -168,7 +201,13 @@ public class JAXRS20ClientServerBookTest
         assertEquals("http://localhost/redirect", response.getHeaderString(HttpHeaders.LOCATION));
     }
     
-    
+    private void validatePostResponse(WebClient wc) {
+        validateResponse(wc);
+        Response response = wc.getResponse();
+        assertEquals("serverRead", response.getHeaderString("ServerReaderInterceptor"));
+        assertEquals("clientWrite", response.getHeaderString("ClientWriterInterceptor"));
+        assertEquals("clientRead", response.getHeaderString("ClientReaderInterceptor"));
+    }
     
     @Test
     public void testClientFiltersLocalResponse() {
@@ -188,21 +227,25 @@ public class JAXRS20ClientServerBookTest
     @Test
     public void testPostBook() {
         String address = "http://localhost:" + PORT + "/bookstore/bookheaders/simple";
-        List<Object> providers = new ArrayList<Object>();
-        providers.add(new ClientHeaderRequestFilter());
-        providers.add(new ClientHeaderResponseFilter());
-        providers.add(new ClientReaderInterceptor());
-        providers.add(new ClientWriterInterceptor());
-        WebClient wc = WebClient.create(address, providers);
-        WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(1000000L);
+        WebClient wc = createWebClientPost(address);
         Book book = wc.post(new Book("Book", 126L), Book.class);
         assertEquals(124L, book.getId());
-        validateResponse(wc);
-        
-        Response response = wc.getResponse();
-        assertEquals("serverRead", response.getHeaderString("ServerReaderInterceptor"));
-        assertEquals("clientWrite", response.getHeaderString("ClientWriterInterceptor"));
-        assertEquals("clientRead", response.getHeaderString("ClientReaderInterceptor"));
+        validatePostResponse(wc);
+    }
+    
+    @Test
+    public void testPostBookAsync() throws Exception {
+        String address = "http://localhost:" + PORT + "/bookstore/bookheaders/simple";
+        WebClient wc = createWebClientPost(address);
+        Future<Book> future = wc.async().post(Entity.xml(new Book("Book", 126L)), Book.class);
+        assertEquals(124L, future.get().getId());
+        validatePostResponse(wc);
+    }
+    
+    @Test
+    public void testPostBookAsyncHandler() throws Exception {
+        String address = "http://localhost:" + PORT + "/bookstore/bookheaders/simple";
+        doTestPostBookAsyncHandler(address);
     }
     
     private static class ClientCacheRequestFilter implements ClientRequestFilter {



Mime
View raw message