cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-5339] Moving JAX-RS Web Socket test to its own package
Date Tue, 25 Feb 2014 22:30:13 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 99e152091 -> 2d4c98545


[CXF-5339] Moving JAX-RS Web Socket test to its own package


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

Branch: refs/heads/master
Commit: 2d4c98545ef4ff66d6f97b514eaeca3bad2664e1
Parents: 99e1520
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Tue Feb 25 22:29:46 2014 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Tue Feb 25 22:29:46 2014 +0000

----------------------------------------------------------------------
 .../apache/cxf/systest/jaxrs/BookServer.java    |   3 -
 .../org/apache/cxf/systest/jaxrs/BookStore.java |  24 ---
 .../jaxrs/JAXRSClientServerWebSocketTest.java   |  97 ------------
 .../cxf/systest/jaxrs/WebSocketTestClient.java  | 155 -------------------
 .../jaxrs/websocket/BookServerWebSocket.java    |  83 ++++++++++
 .../jaxrs/websocket/BookStoreWebSocket.java     |  62 ++++++++
 .../JAXRSClientServerWebSocketTest.java         |  97 ++++++++++++
 .../jaxrs/websocket/WebSocketTestClient.java    | 155 +++++++++++++++++++
 8 files changed, 397 insertions(+), 279 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/2d4c9854/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
index acc54a8..f315564 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
@@ -42,7 +42,6 @@ import org.apache.cxf.jaxrs.provider.BinaryDataProvider;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
-import org.apache.cxf.transport.http_jetty.JettyHTTPDestination;
     
 public class BookServer extends AbstractBusTestServerBase {
     public static final String PORT = allocatePort(BookServer.class);
@@ -112,8 +111,6 @@ public class BookServer extends AbstractBusTestServerBase {
         sf.getProperties().put("default.content.type", "*/*");
         sf.getProperties().putAll(properties);
         server = sf.create();
-        ((JettyHTTPDestination)server.getDestination())
-            .setEnableWebSocket(Boolean.parseBoolean((String)properties.get("enableWebSocket")));
         BusFactory.setDefaultBus(null);
         BusFactory.setThreadDefaultBus(null);
     }

http://git-wip-us.apache.org/repos/asf/cxf/blob/2d4c9854/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
index d343d27..7274e03 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
@@ -1151,30 +1151,6 @@ public class BookStore {
         }
     }
 
-    @GET
-    @Path("/bookbought")
-    @Produces("text/*")
-    public StreamingOutput getBookBought() {
-        return new StreamingOutput() {
-            public void write(final OutputStream out) throws IOException, WebApplicationException
{
-                out.write(("Today: " + new java.util.Date()).getBytes());
-                // just for testing, using a thread
-                new Thread(new Runnable() {
-                    public void run() {
-                        try {
-                            for (int r = 2, i = 1; i <= 5; r *= 2, i++) {
-                                Thread.sleep(500);
-                                out.write(Integer.toString(r).getBytes());
-                            }
-                        } catch (Exception e) {
-                            e.printStackTrace();
-                        }
-                    }
-                }).start();
-            }
-        };
-    }
-
     @POST
     @Path("/books/null")
     @Produces("application/xml")

http://git-wip-us.apache.org/repos/asf/cxf/blob/2d4c9854/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerWebSocketTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerWebSocketTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerWebSocketTest.java
deleted file mode 100644
index 06cdce4..0000000
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerWebSocketTest.java
+++ /dev/null
@@ -1,97 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.cxf.systest.jaxrs;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
-import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
-
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestBase {
-    public static final String PORT = BookServer.PORT;
-    
-    @BeforeClass
-    public static void startServers() throws Exception {
-        final Map< String, Object > properties = new HashMap< String, Object >();
       
-        properties.put("enableWebSocket", "true");
-
-        AbstractResourceInfo.clearAllMaps();
-        assertTrue("server did not launch correctly", launchServer(new BookServer(properties)));
-        createStaticBus();
-    }
-        
-    @Test
-    // seems to cause PermGen issue to another systests/jaxrs later in some system. the code
under analysis
-    @org.junit.Ignore 
-    public void testBookWithWebSocket() throws Exception {
-        String address = "ws://localhost:" + PORT + "/bookstore";
-
-        WebSocketTestClient wsclient = new WebSocketTestClient(address, 1);
-        wsclient.connect();
-        try {
-            // call the GET service
-            wsclient.sendMessage("GET /bookstore/booknames/123".getBytes());
-            assertTrue("one book must be returned", wsclient.await(3));
-            List<byte[]> received = wsclient.getReceivedBytes();
-            assertEquals(1, received.size());
-            String value = new String(received.get(0));
-            assertEquals("CXF in Action", value);
-
-            // call another GET service
-            wsclient.reset(1);
-            wsclient.sendMessage("GET /bookstore/books/123".getBytes());
-            assertTrue("response expected", wsclient.await(3));
-            received = wsclient.getReceivedBytes();
-            value = new String(received.get(0));
-            assertTrue(value.startsWith("<?xml ") && value.endsWith("</Book>"));
-            
-            // call the GET service using POST
-            wsclient.reset(1);
-            wsclient.sendMessage("POST /bookstore/booknames\r\n\r\n123".getBytes());
-            assertFalse("wrong method, no response expected", wsclient.await(3));
-            
-            // call the POST service
-            wsclient.reset(1);
-            wsclient.sendMessage("POST /bookstore/booksplain\r\n\r\n123".getBytes());
-            assertTrue("response expected", wsclient.await(3));
-            received = wsclient.getReceivedBytes();
-            value = new String(received.get(0));
-            assertEquals("123", value);
-            
-            // call the GET service returning a continous stream output
-            wsclient.reset(6);
-            wsclient.sendMessage("GET /bookstore/bookbought".getBytes());
-            assertTrue("wrong method, no response expected", wsclient.await(5));
-            received = wsclient.getReceivedBytes();
-            assertEquals(6, received.size());
-            assertTrue((new String(received.get(0))).startsWith("Today:"));
-            for (int r = 2, i = 1; i < 6; r *= 2, i++) {
-                assertEquals(r, Integer.parseInt(new String(received.get(i))));
-            }
-        } finally {
-            wsclient.close();
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/cxf/blob/2d4c9854/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/WebSocketTestClient.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/WebSocketTestClient.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/WebSocketTestClient.java
deleted file mode 100644
index 3e1b3bc..0000000
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/WebSocketTestClient.java
+++ /dev/null
@@ -1,155 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package org.apache.cxf.systest.jaxrs;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.logging.Logger;
-
-import com.ning.http.client.AsyncHttpClient;
-import com.ning.http.client.websocket.WebSocket;
-import com.ning.http.client.websocket.WebSocketByteListener;
-import com.ning.http.client.websocket.WebSocketTextListener;
-import com.ning.http.client.websocket.WebSocketUpgradeHandler;
-
-import org.apache.cxf.common.logging.LogUtils;
-
-
-
-/**
- * Test client to do websocket calls.
- * @see JAXRSClientServerWebSocketTest
- * 
- * we may put this in test-tools so that other systests can use this code.
- * for now keep it here to experiment jaxrs websocket scenarios.
- */
-class WebSocketTestClient {
-    private static final Logger LOG = LogUtils.getL7dLogger(WebSocketTestClient.class);
-
-    private List<String> received;
-    private List<byte[]> receivedBytes;
-    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.latch = new CountDownLatch(count);
-        this.client = new AsyncHttpClient();
-        this.url = url;
-    }
-    
-    public void connect() throws InterruptedException, ExecutionException, IOException {
-        websocket = client.prepareGet(url).execute(
-            new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WsSocketListener()).build()).get();
-    }
-
-    public void sendTextMessage(String message) {
-        websocket.sendTextMessage(message);
-    }
-
-    public void sendMessage(byte[] message) {
-        websocket.sendMessage(message);
-    }
-    
-    public boolean await(int secs) throws InterruptedException {
-        return latch.await(secs, TimeUnit.SECONDS);
-    }
-    
-    public void reset(int count) {
-        latch = new CountDownLatch(count);
-        received.clear();
-        receivedBytes.clear();
-    }
-
-    public List<String> getReceived() {
-        return received;
-    }
-    
-    public List<byte[]> getReceivedBytes() {
-        return receivedBytes;
-    }
-
-    public void close() {
-        websocket.close();
-        client.close();
-    }
-
-    class WsSocketListener implements WebSocketTextListener, WebSocketByteListener {
-
-        public void onOpen(WebSocket ws) {
-            LOG.info("[ws] opened");            
-        }
-
-        public void onClose(WebSocket ws) {
-            LOG.info("[ws] closed");            
-        }
-
-        public void onError(Throwable t) {
-            LOG.info("[ws] error: " + t);                        
-        }
-
-        public void onMessage(byte[] message) {
-            receivedBytes.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);
-        }
-
-        public void onMessage(String message) {
-            received.add(message);
-            LOG.info("[ws] received --> " + message);
-            latch.countDown();
-        }
-
-        public void onFragment(String fragment, boolean last) {
-            // TODO Auto-generated method stub
-            LOG.info("TODO [ws] received fragment --> " + fragment + "; last? " + last);
-        }
-        
-    }
-    
-    private static String makeString(byte[] data) {
-        return data == null ? null : makeString(data, 0, data.length).toString();
-    }
-
-    private static StringBuilder makeString(byte[] data, int offset, int length) {
-        if (data .length > 256) {
-            return makeString(data, offset, 256).append("...");
-        }
-        StringBuilder xbuf = new StringBuilder().append("\nHEX: ");
-        StringBuilder cbuf = new StringBuilder().append("\nASC: ");
-        for (byte b : data) {
-            xbuf.append(Integer.toHexString(0xff & b)).append(' ');
-            cbuf.append((0x80 & b) != 0 ? '.' : (char)b).append("  ");
-        }
-        return xbuf.append(cbuf);
-    }
-}

http://git-wip-us.apache.org/repos/asf/cxf/blob/2d4c9854/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/BookServerWebSocket.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/BookServerWebSocket.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/BookServerWebSocket.java
new file mode 100644
index 0000000..7084406
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/BookServerWebSocket.java
@@ -0,0 +1,83 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.systest.jaxrs.websocket;
+
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.apache.cxf.transport.http_jetty.JettyHTTPDestination;
+    
+public class BookServerWebSocket extends AbstractBusTestServerBase {
+    public static final String PORT = allocatePort(BookServerWebSocket.class);
+     
+    org.apache.cxf.endpoint.Server server;
+    private Map< ? extends String, ? extends Object > properties;
+    
+    public BookServerWebSocket() {
+        this(Collections.< String, Object >emptyMap());
+    }
+    
+    /**
+     * Allow to specified custom contextual properties to be passed to factory bean
+     */
+    public BookServerWebSocket(final Map< ? extends String, ? extends Object > properties)
{
+        this.properties = properties;
+    }
+    
+    protected void run() {
+        Bus bus = BusFactory.getDefaultBus();
+        setBus(bus);
+        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+        sf.setBus(bus);
+        sf.setResourceClasses(BookStoreWebSocket.class);
+        sf.setResourceProvider(BookStoreWebSocket.class,
+                               new SingletonResourceProvider(new BookStoreWebSocket(), true));
+        sf.setAddress("http://localhost:" + PORT + "/");
+        server = sf.create();
+        ((JettyHTTPDestination)server.getDestination())
+            .setEnableWebSocket(Boolean.parseBoolean((String)properties.get("enableWebSocket")));
+        BusFactory.setDefaultBus(null);
+        BusFactory.setThreadDefaultBus(null);
+    }
+    
+    public void tearDown() throws Exception {
+        server.stop();
+        server.destroy();
+        server = null;
+    }
+
+    public static void main(String[] args) {
+        try {
+            BookServerWebSocket s = new BookServerWebSocket();
+            s.start();
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            System.exit(-1);
+        } finally {
+            System.out.println("done!");
+        }
+    }
+    
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/2d4c9854/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/BookStoreWebSocket.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/BookStoreWebSocket.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/BookStoreWebSocket.java
new file mode 100644
index 0000000..6dd5149
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/BookStoreWebSocket.java
@@ -0,0 +1,62 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.systest.jaxrs.websocket;
+
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.StreamingOutput;
+
+@Path("/bookstore")
+public class BookStoreWebSocket {
+    
+    
+    @GET
+    @Path("/bookbought")
+    @Produces("text/*")
+    public StreamingOutput getBookBought() {
+        return new StreamingOutput() {
+            public void write(final OutputStream out) throws IOException, WebApplicationException
{
+                out.write(("Today: " + new java.util.Date()).getBytes());
+                // just for testing, using a thread
+                new Thread(new Runnable() {
+                    public void run() {
+                        try {
+                            for (int r = 2, i = 1; i <= 5; r *= 2, i++) {
+                                Thread.sleep(500);
+                                out.write(Integer.toString(r).getBytes());
+                            }
+                        } catch (Exception e) {
+                            e.printStackTrace();
+                        }
+                    }
+                }).start();
+            }
+        };
+    }
+
+}
+
+

http://git-wip-us.apache.org/repos/asf/cxf/blob/2d4c9854/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
new file mode 100644
index 0000000..1933079
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/JAXRSClientServerWebSocketTest.java
@@ -0,0 +1,97 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.systest.jaxrs.websocket;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class JAXRSClientServerWebSocketTest extends AbstractBusClientServerTestBase {
+    public static final String PORT = BookServerWebSocket.PORT;
+    
+    @BeforeClass
+    public static void startServers() throws Exception {
+        final Map< String, Object > properties = new HashMap< String, Object >();
       
+        properties.put("enableWebSocket", "true");
+
+        AbstractResourceInfo.clearAllMaps();
+        assertTrue("server did not launch correctly", launchServer(new BookServerWebSocket(properties)));
+        createStaticBus();
+    }
+        
+    @Test
+    // seems to cause PermGen issue to another systests/jaxrs later in some system. the code
under analysis
+    @org.junit.Ignore 
+    public void testBookWithWebSocket() throws Exception {
+        String address = "ws://localhost:" + PORT + "/bookstore";
+
+        WebSocketTestClient wsclient = new WebSocketTestClient(address, 1);
+        wsclient.connect();
+        try {
+            // call the GET service
+            wsclient.sendMessage("GET /bookstore/booknames/123".getBytes());
+            assertTrue("one book must be returned", wsclient.await(3));
+            List<byte[]> received = wsclient.getReceivedBytes();
+            assertEquals(1, received.size());
+            String value = new String(received.get(0));
+            assertEquals("CXF in Action", value);
+
+            // call another GET service
+            wsclient.reset(1);
+            wsclient.sendMessage("GET /bookstore/books/123".getBytes());
+            assertTrue("response expected", wsclient.await(3));
+            received = wsclient.getReceivedBytes();
+            value = new String(received.get(0));
+            assertTrue(value.startsWith("<?xml ") && value.endsWith("</Book>"));
+            
+            // call the GET service using POST
+            wsclient.reset(1);
+            wsclient.sendMessage("POST /bookstore/booknames\r\n\r\n123".getBytes());
+            assertFalse("wrong method, no response expected", wsclient.await(3));
+            
+            // call the POST service
+            wsclient.reset(1);
+            wsclient.sendMessage("POST /bookstore/booksplain\r\n\r\n123".getBytes());
+            assertTrue("response expected", wsclient.await(3));
+            received = wsclient.getReceivedBytes();
+            value = new String(received.get(0));
+            assertEquals("123", value);
+            
+            // call the GET service returning a continous stream output
+            wsclient.reset(6);
+            wsclient.sendMessage("GET /bookstore/bookbought".getBytes());
+            assertTrue("wrong method, no response expected", wsclient.await(5));
+            received = wsclient.getReceivedBytes();
+            assertEquals(6, received.size());
+            assertTrue((new String(received.get(0))).startsWith("Today:"));
+            for (int r = 2, i = 1; i < 6; r *= 2, i++) {
+                assertEquals(r, Integer.parseInt(new String(received.get(i))));
+            }
+        } finally {
+            wsclient.close();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/2d4c9854/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
new file mode 100644
index 0000000..24af539
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/websocket/WebSocketTestClient.java
@@ -0,0 +1,155 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.systest.jaxrs.websocket;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Logger;
+
+import com.ning.http.client.AsyncHttpClient;
+import com.ning.http.client.websocket.WebSocket;
+import com.ning.http.client.websocket.WebSocketByteListener;
+import com.ning.http.client.websocket.WebSocketTextListener;
+import com.ning.http.client.websocket.WebSocketUpgradeHandler;
+
+import org.apache.cxf.common.logging.LogUtils;
+
+
+
+/**
+ * Test client to do websocket calls.
+ * @see JAXRSClientServerWebSocketTest
+ * 
+ * we may put this in test-tools so that other systests can use this code.
+ * for now keep it here to experiment jaxrs websocket scenarios.
+ */
+class WebSocketTestClient {
+    private static final Logger LOG = LogUtils.getL7dLogger(WebSocketTestClient.class);
+
+    private List<String> received;
+    private List<byte[]> receivedBytes;
+    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.latch = new CountDownLatch(count);
+        this.client = new AsyncHttpClient();
+        this.url = url;
+    }
+    
+    public void connect() throws InterruptedException, ExecutionException, IOException {
+        websocket = client.prepareGet(url).execute(
+            new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WsSocketListener()).build()).get();
+    }
+
+    public void sendTextMessage(String message) {
+        websocket.sendTextMessage(message);
+    }
+
+    public void sendMessage(byte[] message) {
+        websocket.sendMessage(message);
+    }
+    
+    public boolean await(int secs) throws InterruptedException {
+        return latch.await(secs, TimeUnit.SECONDS);
+    }
+    
+    public void reset(int count) {
+        latch = new CountDownLatch(count);
+        received.clear();
+        receivedBytes.clear();
+    }
+
+    public List<String> getReceived() {
+        return received;
+    }
+    
+    public List<byte[]> getReceivedBytes() {
+        return receivedBytes;
+    }
+
+    public void close() {
+        websocket.close();
+        client.close();
+    }
+
+    class WsSocketListener implements WebSocketTextListener, WebSocketByteListener {
+
+        public void onOpen(WebSocket ws) {
+            LOG.info("[ws] opened");            
+        }
+
+        public void onClose(WebSocket ws) {
+            LOG.info("[ws] closed");            
+        }
+
+        public void onError(Throwable t) {
+            LOG.info("[ws] error: " + t);                        
+        }
+
+        public void onMessage(byte[] message) {
+            receivedBytes.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);
+        }
+
+        public void onMessage(String message) {
+            received.add(message);
+            LOG.info("[ws] received --> " + message);
+            latch.countDown();
+        }
+
+        public void onFragment(String fragment, boolean last) {
+            // TODO Auto-generated method stub
+            LOG.info("TODO [ws] received fragment --> " + fragment + "; last? " + last);
+        }
+        
+    }
+    
+    private static String makeString(byte[] data) {
+        return data == null ? null : makeString(data, 0, data.length).toString();
+    }
+
+    private static StringBuilder makeString(byte[] data, int offset, int length) {
+        if (data .length > 256) {
+            return makeString(data, offset, 256).append("...");
+        }
+        StringBuilder xbuf = new StringBuilder().append("\nHEX: ");
+        StringBuilder cbuf = new StringBuilder().append("\nASC: ");
+        for (byte b : data) {
+            xbuf.append(Integer.toHexString(0xff & b)).append(' ');
+            cbuf.append((0x80 & b) != 0 ? '.' : (char)b).append("  ");
+        }
+        return xbuf.append(cbuf);
+    }
+}


Mime
View raw message