cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject git commit: make websocket test utility to handle both string and byte[] transparently
Date Thu, 13 Mar 2014 13:34:01 GMT
Repository: cxf
Updated Branches:
  refs/heads/master bc91295f1 -> 0e8efba29


make websocket test utility to handle both string and byte[] transparently


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/0e8efba2
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/0e8efba2
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/0e8efba2

Branch: refs/heads/master
Commit: 0e8efba29ee1b2151645a3714436d9546007f382
Parents: bc91295
Author: Akitoshi Yoshida <ay@apache.org>
Authored: Thu Mar 13 14:32:57 2014 +0100
Committer: Akitoshi Yoshida <ay@apache.org>
Committed: Thu Mar 13 14:33:34 2014 +0100

----------------------------------------------------------------------
 .../JAXRSClientServerWebSocketTest.java         | 64 +++++++++++++-------
 .../jaxrs/websocket/WebSocketTestClient.java    | 55 ++++++++++++-----
 2 files changed, 82 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/0e8efba2/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/JAXRSClientServerWebSocketTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/JAXRSClientServerWebSocketTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/JAXRSClientServerWebSocketTest.java
index f53610b..0d1c404 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/JAXRSClientServerWebSocketTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/JAXRSClientServerWebSocketTest.java
@@ -48,23 +48,23 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
             // call the GET service
             wsclient.sendMessage("GET /websocket/web/bookstore/booknames".getBytes());
             assertTrue("one book must be returned", wsclient.await(3));
-            List<byte[]> received = wsclient.getReceivedBytes();
+            List<Object> received = wsclient.getReceived();
             assertEquals(1, received.size());
             Response resp = new Response(received.get(0));
             assertEquals(200, resp.getStatusCode());
             assertEquals("text/plain", resp.getContentType());
-            String value = new String(resp.getEntity());
+            String value = resp.getTextEntity();
             assertEquals("CXF in Action", value);
             
             // call another GET service
             wsclient.reset(1);
             wsclient.sendMessage("GET /websocket/web/bookstore/books/123".getBytes());
             assertTrue("response expected", wsclient.await(3));
-            received = wsclient.getReceivedBytes();
+            received = wsclient.getReceived();
             resp = new Response(received.get(0));
             assertEquals(200, resp.getStatusCode());
             assertEquals("application/xml", resp.getContentType());
-            value = new String(resp.getEntity());
+            value = resp.getTextEntity();
             assertTrue(value.startsWith("<?xml ") && value.endsWith("</Book>"));
             
             // call the POST service
@@ -73,29 +73,29 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
                 "POST /websocket/web/bookstore/booksplain\r\nContent-Type: text/plain\r\n\r\n123"
                     .getBytes());
             assertTrue("response expected", wsclient.await(3));
-            received = wsclient.getReceivedBytes();
+            received = wsclient.getReceived();
             resp = new Response(received.get(0));
             assertEquals(200, resp.getStatusCode());
             assertEquals("text/plain", resp.getContentType());
-            value = new String(resp.getEntity());
+            value = resp.getTextEntity();
             assertEquals("123", value);
             
             // call the GET service returning a continous stream output
             wsclient.reset(6);
             wsclient.sendMessage("GET /websocket/web/bookstore/bookbought".getBytes());
             assertTrue("response expected", wsclient.await(5));
-            received = wsclient.getReceivedBytes();
+            received = wsclient.getReceived();
             assertEquals(6, received.size());
             resp = new Response(received.get(0));
             assertEquals(200, resp.getStatusCode());
             assertEquals("application/octet-stream", resp.getContentType());
-            value = new String(resp.getEntity());
+            value = resp.getTextEntity();
             assertTrue(value.startsWith("Today:"));
             for (int r = 2, i = 1; i < 6; r *= 2, i++) {
                 // subsequent data should not carry the headers nor the status.
                 resp = new Response(received.get(i));
                 assertEquals(0, resp.getStatusCode());
-                assertEquals(r, Integer.parseInt(new String(resp.getEntity())));
+                assertEquals(r, Integer.parseInt(resp.getTextEntity()));
             }
         } finally {
             wsclient.close();
@@ -111,12 +111,12 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
         try {
             wsclient.sendMessage("GET /websocket/web/bookstore/booknames/servletstream".getBytes());
             assertTrue("one book must be returned", wsclient.await(3));
-            List<byte[]> received = wsclient.getReceivedBytes();
+            List<Object> received = wsclient.getReceived();
             assertEquals(1, received.size());
             Response resp = new Response(received.get(0));
             assertEquals(200, resp.getStatusCode());
             assertEquals("text/plain", resp.getContentType());
-            String value = new String(resp.getEntity());
+            String value = resp.getTextEntity();
             assertEquals("CXF in Action", value);
         } finally {
             wsclient.close();
@@ -133,7 +133,7 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
             // call the GET service using POST
             wsclient.sendMessage("POST /websocket/web/bookstore/booknames".getBytes());
             assertTrue("error response expected", wsclient.await(3));
-            List<byte[]> received = wsclient.getReceivedBytes();
+            List<Object> received = wsclient.getReceived();
             assertEquals(1, received.size());
             Response resp = new Response(received.get(0));
             assertEquals(405, resp.getStatusCode());
@@ -153,7 +153,7 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
             // call the GET service over the different path
             wsclient.sendMessage("GET /websocket/bookstore2".getBytes());
             assertTrue("error response expected", wsclient.await(3));
-            List<byte[]> received = wsclient.getReceivedBytes();
+            List<Object> received = wsclient.getReceived();
             assertEquals(1, received.size());
             Response resp = new Response(received.get(0));
             assertEquals(400, resp.getStatusCode());
@@ -164,13 +164,13 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
     
     //TODO this is a temporary way to verify the response; we should come up with something
better.
     private static class Response {
-        private byte[] data;
+        private Object data;
         private int pos; 
         private int statusCode;
         private String contentType;
-        private byte[] entity;
+        private Object entity;
         
-        public Response(byte[] data) {
+        public Response(Object data) {
             this.data = data;
             String line = readLine();
             if (line != null) {
@@ -186,8 +186,12 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
                     }
                 }
             }
-            entity = new byte[data.length - pos];
-            System.arraycopy(data, pos, entity, 0, entity.length);
+            if (data instanceof String) {
+                entity = ((String)data).substring(pos);
+            } else if (data instanceof byte[]) {
+                entity = new byte[((byte[])data).length - pos];
+                System.arraycopy((byte[])data, pos, (byte[])entity, 0, ((byte[])entity).length);
+            }
         }
                 
             
@@ -200,22 +204,26 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
             return contentType;
         }
         
-        public byte[] getEntity() {
+        public Object getEntity() {
             return entity;
         }
         
+        public String getTextEntity() {
+            return gettext(entity);
+        }
+
         public String toString() {
             StringBuffer sb = new StringBuffer();
             sb.append("Status: ").append(statusCode).append("\r\n");
             sb.append("Type: ").append(contentType).append("\r\n");
-            sb.append("Entity: ").append(new String(entity)).append("\r\n");
+            sb.append("Entity: ").append(gettext(entity)).append("\r\n");
             return sb.toString();
         }
         
         private String readLine() {
             StringBuilder sb = new StringBuilder();
-            while (pos < data.length) {
-                int c = 0xff & data[pos++];
+            while (pos < length(data)) {
+                int c = getchar(data, pos++);
                 if (c == '\n') {
                     break;
                 } else if (c == '\r') {
@@ -229,6 +237,18 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
             }
             return sb.toString();
         }
+
+        private int length(Object o) {
+            return o instanceof char[] ? ((String)o).length() : (o instanceof byte[] ? ((byte[])o).length
: 0);
+        }
+
+        private int getchar(Object o, int p) {
+            return 0xff & (o instanceof String ? ((String)o).charAt(p) : (o instanceof
byte[] ? ((byte[])o)[p] : -1));
+        }
+
+        private String gettext(Object o) {
+            return o instanceof String ? (String)o : (o instanceof byte[] ? new String((byte[])o)
: null);
+        }
     }
     
     protected String getPort() {

http://git-wip-us.apache.org/repos/asf/cxf/blob/0e8efba2/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/WebSocketTestClient.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/WebSocketTestClient.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/WebSocketTestClient.java
index 6dff1ac..d47b7eb 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/WebSocketTestClient.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/WebSocketTestClient.java
@@ -19,8 +19,10 @@
 
 package org.apache.cxf.systest.jaxrs.websocket;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
@@ -47,16 +49,16 @@ import org.apache.cxf.common.logging.LogUtils;
 class WebSocketTestClient {
     private static final Logger LOG = LogUtils.getL7dLogger(WebSocketTestClient.class);
 
-    private List<String> received;
-    private List<byte[]> receivedBytes;
+    private List<Object> received;
+    private List<Object> fragments;
     private CountDownLatch latch;
     private AsyncHttpClient client;
     private WebSocket websocket;
     private String url;
     
     public WebSocketTestClient(String url, int count) {
-        this.received = new ArrayList<String>();
-        this.receivedBytes = new ArrayList<byte[]>();
+        this.received = new ArrayList<Object>();
+        this.fragments = new ArrayList<Object>();
         this.latch = new CountDownLatch(count);
         this.client = new AsyncHttpClient();
         this.url = url;
@@ -82,16 +84,11 @@ class WebSocketTestClient {
     public void reset(int count) {
         latch = new CountDownLatch(count);
         received.clear();
-        receivedBytes.clear();
     }
 
-    public List<String> getReceived() {
+    public List<Object> getReceived() {
         return received;
     }
-    
-    public List<byte[]> getReceivedBytes() {
-        return receivedBytes;
-    }
 
     public void close() {
         websocket.close();
@@ -113,14 +110,13 @@ class WebSocketTestClient {
         }
 
         public void onMessage(byte[] message) {
-            receivedBytes.add(message);
+            received.add(message);
             LOG.info("[ws] received bytes --> " + makeString(message));
             latch.countDown();
         }
 
         public void onFragment(byte[] fragment, boolean last) {
-            // TODO Auto-generated method stub
-            LOG.info("TODO [ws] received fragment bytes --> " + makeString(fragment) +
"; last? " + last);
+            processFragments(fragment, last);
         }
 
         public void onMessage(String message) {
@@ -130,10 +126,39 @@ class WebSocketTestClient {
         }
 
         public void onFragment(String fragment, boolean last) {
-            // TODO Auto-generated method stub
-            LOG.info("TODO [ws] received fragment --> " + fragment + "; last? " + last);
+            processFragments(fragment, last);
         }
         
+        private void processFragments(Object f, boolean last) {
+            synchronized (fragments) {
+                fragments.add(f);
+                if (last) {
+                    if (f instanceof String) {
+                        // string
+                        StringBuilder sb = new StringBuilder();
+                        for (Iterator<Object> it = fragments.iterator(); it.hasNext();)
{
+                            Object o = it.next();
+                            if (o instanceof String) {
+                                sb.append((String)o);
+                                it.remove();
+                            }
+                        }
+                        received.add(sb.toString());
+                    } else {
+                        // byte[]
+                        ByteArrayOutputStream bao = new ByteArrayOutputStream();
+                        for (Iterator<Object> it = fragments.iterator(); it.hasNext();)
{
+                            Object o = it.next();
+                            if (o instanceof byte[]) {
+                                bao.write((byte[])o, 0, ((byte[])o).length);
+                                it.remove();
+                            }
+                        }
+                        received.add(bao.toString());
+                    }
+                }
+            }
+        }
     }
     
     private static String makeString(byte[] data) {


Mime
View raw message