cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject git commit: some clean up in systests/jaxrs' websocket tests
Date Mon, 24 Mar 2014 09:12:38 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 869b0807b -> c17955382


some clean up in systests/jaxrs' websocket tests


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

Branch: refs/heads/master
Commit: c179553822e8de276d14abc2e74c5184de5515a3
Parents: 869b080
Author: Akitoshi Yoshida <ay@apache.org>
Authored: Fri Mar 21 22:57:29 2014 +0100
Committer: Akitoshi Yoshida <ay@apache.org>
Committed: Mon Mar 24 10:11:50 2014 +0100

----------------------------------------------------------------------
 .../JAXRSClientServerWebSocketTest.java         | 140 ++++---------------
 .../jaxrs/websocket/WebSocketTestClient.java    | 103 +++++++++++++-
 2 files changed, 127 insertions(+), 116 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/c1795538/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 2859c6b..df0b5aa 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
@@ -43,15 +43,15 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
     public void testBookWithWebSocket() throws Exception {
         String address = "ws://localhost:" + getPort() + "/websocket/web/bookstore";
 
-        WebSocketTestClient wsclient = new WebSocketTestClient(address, 1);
+        WebSocketTestClient wsclient = new WebSocketTestClient(address);
         wsclient.connect();
         try {
             // call the GET service
             wsclient.sendMessage("GET /websocket/web/bookstore/booknames".getBytes());
             assertTrue("one book must be returned", wsclient.await(3));
-            List<Object> received = wsclient.getReceived();
+            List<WebSocketTestClient.Response> received = wsclient.getReceivedResponses();
             assertEquals(1, received.size());
-            Response resp = new Response(received.get(0));
+            WebSocketTestClient.Response resp = received.get(0);
             assertEquals(200, resp.getStatusCode());
             assertEquals("text/plain", resp.getContentType());
             String value = resp.getTextEntity();
@@ -61,9 +61,9 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
             wsclient.reset(1);
             wsclient.sendTextMessage("GET /websocket/web/bookstore/booknames");
             assertTrue("one book must be returned", wsclient.await(3));
-            received = wsclient.getReceived();
+            received = wsclient.getReceivedResponses();
             assertEquals(1, received.size());
-            resp = new Response(received.get(0));
+            resp = received.get(0);
             assertEquals(200, resp.getStatusCode());
             assertEquals("text/plain", resp.getContentType());
             value = resp.getTextEntity();
@@ -73,8 +73,8 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
             wsclient.reset(1);
             wsclient.sendMessage("GET /websocket/web/bookstore/books/123".getBytes());
             assertTrue("response expected", wsclient.await(3));
-            received = wsclient.getReceived();
-            resp = new Response(received.get(0));
+            received = wsclient.getReceivedResponses();
+            resp = received.get(0);
             assertEquals(200, resp.getStatusCode());
             assertEquals("application/xml", resp.getContentType());
             value = resp.getTextEntity();
@@ -86,8 +86,8 @@ 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.getReceived();
-            resp = new Response(received.get(0));
+            received = wsclient.getReceivedResponses();
+            resp = received.get(0);
             assertEquals(200, resp.getStatusCode());
             assertEquals("text/plain", resp.getContentType());
             value = resp.getTextEntity();
@@ -98,8 +98,8 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
             wsclient.sendTextMessage(
                 "POST /websocket/web/bookstore/booksplain\r\nContent-Type: text/plain\r\n\r\n123");
             assertTrue("response expected", wsclient.await(3));
-            received = wsclient.getReceived();
-            resp = new Response(received.get(0));
+            received = wsclient.getReceivedResponses();
+            resp = received.get(0);
             assertEquals(200, resp.getStatusCode());
             assertEquals("text/plain", resp.getContentType());
             value = resp.getTextEntity();
@@ -109,16 +109,16 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
             wsclient.reset(6);
             wsclient.sendMessage("GET /websocket/web/bookstore/bookbought".getBytes());
             assertTrue("response expected", wsclient.await(5));
-            received = wsclient.getReceived();
+            received = wsclient.getReceivedResponses();
             assertEquals(6, received.size());
-            resp = new Response(received.get(0));
+            resp = received.get(0);
             assertEquals(200, resp.getStatusCode());
             assertEquals("application/octet-stream", resp.getContentType());
             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));
+                resp = received.get(i);
                 assertEquals(0, resp.getStatusCode());
                 assertEquals(r, Integer.parseInt(resp.getTextEntity()));
             }
@@ -131,7 +131,7 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
     public void testBookWithWebSocketAndHTTP() throws Exception {
         String address = "ws://localhost:" + getPort() + "/websocket/web/bookstore";
 
-        WebSocketTestClient wsclient = new WebSocketTestClient(address, 1);
+        WebSocketTestClient wsclient = new WebSocketTestClient(address);
         wsclient.connect();
         try {
             // call the GET service
@@ -139,7 +139,7 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
             assertTrue("one book must be returned", wsclient.await(3));
             List<Object> received = wsclient.getReceived();
             assertEquals(1, received.size());
-            Response resp = new Response(received.get(0));
+            WebSocketTestClient.Response resp = new WebSocketTestClient.Response(received.get(0));
             assertEquals(200, resp.getStatusCode());
             assertEquals("text/plain", resp.getContentType());
             String value = resp.getTextEntity();
@@ -165,14 +165,14 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
     public void testBookWithWebSocketServletStream() throws Exception {
         String address = "ws://localhost:" + getPort() + "/websocket/web/bookstore";
 
-        WebSocketTestClient wsclient = new WebSocketTestClient(address, 1);
+        WebSocketTestClient wsclient = new WebSocketTestClient(address);
         wsclient.connect();
         try {
             wsclient.sendMessage("GET /websocket/web/bookstore/booknames/servletstream".getBytes());
             assertTrue("one book must be returned", wsclient.await(3));
-            List<Object> received = wsclient.getReceived();
+            List<WebSocketTestClient.Response> received = wsclient.getReceivedResponses();
             assertEquals(1, received.size());
-            Response resp = new Response(received.get(0));
+            WebSocketTestClient.Response resp = received.get(0);
             assertEquals(200, resp.getStatusCode());
             assertEquals("text/plain", resp.getContentType());
             String value = resp.getTextEntity();
@@ -186,15 +186,16 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
     public void testWrongMethod() throws Exception {
         String address = "ws://localhost:" + getPort() + "/websocket/web/bookstore";
 
-        WebSocketTestClient wsclient = new WebSocketTestClient(address, 1);
+        WebSocketTestClient wsclient = new WebSocketTestClient(address);
         wsclient.connect();
         try {
             // call the GET service using POST
+            wsclient.reset(1);
             wsclient.sendMessage("POST /websocket/web/bookstore/booknames".getBytes());
             assertTrue("error response expected", wsclient.await(3));
-            List<Object> received = wsclient.getReceived();
+            List<WebSocketTestClient.Response> received = wsclient.getReceivedResponses();
             assertEquals(1, received.size());
-            Response resp = new Response(received.get(0));
+            WebSocketTestClient.Response resp = received.get(0);
             assertEquals(405, resp.getStatusCode());
         } finally {
             wsclient.close();
@@ -205,110 +206,21 @@ public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestB
     public void testPathRestriction() throws Exception {
         String address = "ws://localhost:" + getPort() + "/websocket/web/bookstore";
 
-        WebSocketTestClient wsclient = new WebSocketTestClient(address, 1);
+        WebSocketTestClient wsclient = new WebSocketTestClient(address);
         wsclient.connect();
         try {
             // call the GET service over the different path
             wsclient.sendMessage("GET /websocket/bookstore2".getBytes());
             assertTrue("error response expected", wsclient.await(3));
-            List<Object> received = wsclient.getReceived();
+            List<WebSocketTestClient.Response> received = wsclient.getReceivedResponses();
             assertEquals(1, received.size());
-            Response resp = new Response(received.get(0));
+            WebSocketTestClient.Response resp = received.get(0);
             assertEquals(400, resp.getStatusCode());
         } finally {
             wsclient.close();
         }
     }
     
-    //TODO this is a temporary way to verify the response; we should come up with something
better.
-    private static class Response {
-        private Object data;
-        private int pos; 
-        private int statusCode;
-        private String contentType;
-        private Object entity;
-        
-        public Response(Object data) {
-            this.data = data;
-            String line = readLine();
-            if (line != null) {
-                statusCode = Integer.parseInt(line);
-                while ((line = readLine()) != null) {
-                    if (line.length() > 0) {
-                        int del = line.indexOf(':');
-                        String h = line.substring(0, del).trim();
-                        String v = line.substring(del + 1).trim();
-                        if ("Content-Type".equalsIgnoreCase(h)) {
-                            contentType = v;
-                        }
-                    }
-                }
-            }
-            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);
-            }
-        }
-                
-            
-        
-        public int getStatusCode() {
-            return statusCode;
-        }
-        
-        public String getContentType() {
-            return contentType;
-        }
-        
-        @SuppressWarnings("unused")
-        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(gettext(entity)).append("\r\n");
-            return sb.toString();
-        }
-        
-        private String readLine() {
-            StringBuilder sb = new StringBuilder();
-            while (pos < length(data)) {
-                int c = getchar(data, pos++);
-                if (c == '\n') {
-                    break;
-                } else if (c == '\r') {
-                    continue;
-                } else {
-                    sb.append((char)c);
-                }
-            }
-            if (sb.length() == 0) {
-                return null;
-            }
-            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() {
         return PORT;

http://git-wip-us.apache.org/repos/asf/cxf/blob/c1795538/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 ef7834c..3525e9f 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
@@ -56,10 +56,10 @@ class WebSocketTestClient {
     private WebSocket websocket;
     private String url;
     
-    public WebSocketTestClient(String url, int count) {
+    public WebSocketTestClient(String url) {
         this.received = new ArrayList<Object>();
         this.fragments = new ArrayList<Object>();
-        this.latch = new CountDownLatch(count);
+        this.latch = new CountDownLatch(1);
         this.client = new AsyncHttpClient();
         this.url = url;
     }
@@ -90,6 +90,15 @@ class WebSocketTestClient {
         return received;
     }
 
+    public List<Response> getReceivedResponses() {
+        Object[] objs = received.toArray();
+        List<Response> responses = new ArrayList<Response>(objs.length);
+        for (Object o : objs) {
+            responses.add(new Response(o));
+        }
+        return responses;
+    }
+    
     public void close() {
         websocket.close();
         client.close();
@@ -196,4 +205,94 @@ class WebSocketTestClient {
         }
         buf.append(' ');
     }
+    
+    //TODO this is a temporary way to verify the response; we should come up with something
better.
+    public static class Response {
+        private Object data;
+        private int pos; 
+        private int statusCode;
+        private String contentType;
+        private Object entity;
+        
+        public Response(Object data) {
+            this.data = data;
+            String line = readLine();
+            if (line != null) {
+                statusCode = Integer.parseInt(line);
+                while ((line = readLine()) != null) {
+                    if (line.length() > 0) {
+                        int del = line.indexOf(':');
+                        String h = line.substring(0, del).trim();
+                        String v = line.substring(del + 1).trim();
+                        if ("Content-Type".equalsIgnoreCase(h)) {
+                            contentType = v;
+                        }
+                    }
+                }
+            }
+            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);
+            }
+        }
+                
+            
+        
+        public int getStatusCode() {
+            return statusCode;
+        }
+        
+        public String getContentType() {
+            return contentType;
+        }
+        
+        @SuppressWarnings("unused")
+        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(gettext(entity)).append("\r\n");
+            return sb.toString();
+        }
+        
+        private String readLine() {
+            StringBuilder sb = new StringBuilder();
+            while (pos < length(data)) {
+                int c = getchar(data, pos++);
+                if (c == '\n') {
+                    break;
+                } else if (c == '\r') {
+                    continue;
+                } else {
+                    sb.append((char)c);
+                }
+            }
+            if (sb.length() == 0) {
+                return null;
+            }
+            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);
+        }
+    }
 }


Mime
View raw message