tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1345731 - /tomcat/trunk/test/org/apache/catalina/websocket/TestWebSocket.java
Date Sun, 03 Jun 2012 18:11:29 GMT
Author: markt
Date: Sun Jun  3 18:11:29 2012
New Revision: 1345731

URL: http://svn.apache.org/viewvc?rev=1345731&view=rev
Log:
Refactor - no functional change

Modified:
    tomcat/trunk/test/org/apache/catalina/websocket/TestWebSocket.java

Modified: tomcat/trunk/test/org/apache/catalina/websocket/TestWebSocket.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/websocket/TestWebSocket.java?rev=1345731&r1=1345730&r2=1345731&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/websocket/TestWebSocket.java (original)
+++ tomcat/trunk/test/org/apache/catalina/websocket/TestWebSocket.java Sun Jun  3 18:11:29
2012
@@ -53,10 +53,6 @@ public class TestWebSocket extends Tomca
         "258EAFA5-E914-47DA-95CA-C5AB0DC85B11".getBytes(
                 B2CConverter.ISO_8859_1);
 
-    private OutputStream os;
-    private InputStream is;
-    private boolean isContinuation = false;
-
     @Test
     public void testSimple() throws Exception {
         Tomcat tomcat = getTomcatInstance();
@@ -89,10 +85,10 @@ public class TestWebSocket extends Tomca
         }
 
         // Now we can do WebSocket
-        sendMessage("foo", false);
-        sendMessage("foo", true);
+        client.sendMessage("foo", false);
+        client.sendMessage("foo", true);
 
-        assertEquals("foofoo",readMessage());
+        assertEquals("foofoo", client.readMessage());
 
         // Finished with the socket
         client.close();
@@ -240,75 +236,14 @@ public class TestWebSocket extends Tomca
 
 
 
-    private void sendMessage(String message, boolean finalFragment)
-            throws IOException {
-        ByteChunk bc = new ByteChunk(8192);
-        C2BConverter c2b = new C2BConverter(bc, "UTF-8");
-        c2b.convert(message);
-        c2b.flushBuffer();
-
-        int len = bc.getLength();
-        assertTrue(len < 126);
-
-        byte first;
-        if (isContinuation) {
-            first = Constants.OPCODE_CONTINUATION;
-        } else {
-            first = Constants.OPCODE_TEXT;
-        }
-        if (finalFragment) {
-            first = (byte) (0x80 | first);
-        }
-        os.write(first);
-
-        os.write(0x80 | len);
-
-        // Zero mask
-        os.write(0);
-        os.write(0);
-        os.write(0);
-        os.write(0);
-
-        // Payload
-        os.write(bc.getBytes(), bc.getStart(), len);
-
-        os.flush();
-
-        // Will the next frame be a continuation frame
-        isContinuation = !finalFragment;
-    }
-
-    private String readMessage() throws IOException {
-        ByteChunk bc = new ByteChunk(125);
-        CharChunk cc = new CharChunk(125);
-
-        // Skip first byte
-        is.read();
-
-        // Get payload length
-        int len = is.read() & 0x7F;
-        assertTrue(len < 126);
-
-        // Read payload
-        int read = 0;
-        while (read < len) {
-            read = read + is.read(bc.getBytes(), read, len - read);
-        }
-
-        bc.setEnd(len);
-
-        B2CConverter b2c = new B2CConverter("UTF-8");
-        b2c.convert(bc, cc, len);
-
-        return cc.toString();
-    }
-
     private class WebSocketClient {
-        // Open the socket
+        private OutputStream os;
+        private InputStream is;
+        private boolean isContinuation = false;
         final String encoding = "ISO-8859-1";
-        Socket socket ;
-        Writer writer ;
-        BufferedReader reader;
+        private Socket socket ;
+        private Writer writer ;
+        private BufferedReader reader;
 
         private WebSocketClient() {
             SocketAddress addr = new InetSocketAddress("localhost", getPort());
@@ -330,5 +265,67 @@ public class TestWebSocket extends Tomca
             socket.close();
         }
 
+        private void sendMessage(String message, boolean finalFragment)
+                throws IOException {
+            ByteChunk bc = new ByteChunk(8192);
+            C2BConverter c2b = new C2BConverter(bc, "UTF-8");
+            c2b.convert(message);
+            c2b.flushBuffer();
+
+            int len = bc.getLength();
+            assertTrue(len < 126);
+
+            byte first;
+            if (isContinuation) {
+                first = Constants.OPCODE_CONTINUATION;
+            } else {
+                first = Constants.OPCODE_TEXT;
+            }
+            if (finalFragment) {
+                first = (byte) (0x80 | first);
+            }
+            os.write(first);
+
+            os.write(0x80 | len);
+
+            // Zero mask
+            os.write(0);
+            os.write(0);
+            os.write(0);
+            os.write(0);
+
+            // Payload
+            os.write(bc.getBytes(), bc.getStart(), len);
+
+            os.flush();
+
+            // Will the next frame be a continuation frame
+            isContinuation = !finalFragment;
+        }
+
+        private String readMessage() throws IOException {
+            ByteChunk bc = new ByteChunk(125);
+            CharChunk cc = new CharChunk(125);
+
+            // Skip first byte
+            is.read();
+
+            // Get payload length
+            int len = is.read() & 0x7F;
+            assertTrue(len < 126);
+
+            // Read payload
+            int read = 0;
+            while (read < len) {
+                read = read + is.read(bc.getBytes(), read, len - read);
+            }
+
+            bc.setEnd(len);
+
+            B2CConverter b2c = new B2CConverter("UTF-8");
+            b2c.convert(bc, cc, len);
+
+            return cc.toString();
+        }
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message