From 256dec3e919221c023416a44a5b87ba4e517b636 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 28 Jun 2012 14:41:57 -0700 Subject: [PATCH] Adding more test cases for WebSocketEventDriver --- .../websocket/api/WebSocketEventDriver.java | 2 +- .../api/WebSocketEventDriverTest.java | 28 +++++++++- ...et.java => AnnotatedByteBufferSocket.java} | 2 +- .../api/samples/AnnotatedFramesSocket.java | 54 +++++++++++++++++++ 4 files changed, 82 insertions(+), 4 deletions(-) rename jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/{AnnotatedBasicSocket.java => AnnotatedByteBufferSocket.java} (96%) create mode 100644 jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedFramesSocket.java diff --git a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketEventDriver.java b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketEventDriver.java index b7d6e57c380..2460cd24bf1 100644 --- a/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketEventDriver.java +++ b/jetty-websocket/websocket-core/src/main/java/org/eclipse/jetty/websocket/api/WebSocketEventDriver.java @@ -127,7 +127,7 @@ public class WebSocketEventDriver implements Parser.Listener return; } - if (!frameType.getSuperclass().isAssignableFrom(BaseFrame.class)) + if (!BaseFrame.class.isAssignableFrom(frameType.getSuperclass())) { // not assignable return; diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/WebSocketEventDriverTest.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/WebSocketEventDriverTest.java index 4adcb1ac391..72dc5421dc0 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/WebSocketEventDriverTest.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/WebSocketEventDriverTest.java @@ -2,11 +2,13 @@ package org.eclipse.jetty.websocket.api; import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.websocket.api.samples.AdapterConnectCloseSocket; -import org.eclipse.jetty.websocket.api.samples.AnnotatedBasicSocket; import org.eclipse.jetty.websocket.api.samples.AnnotatedByteArraySocket; +import org.eclipse.jetty.websocket.api.samples.AnnotatedByteBufferSocket; +import org.eclipse.jetty.websocket.api.samples.AnnotatedFramesSocket; import org.eclipse.jetty.websocket.api.samples.ListenerBasicSocket; import org.eclipse.jetty.websocket.frames.BinaryFrame; import org.eclipse.jetty.websocket.frames.CloseFrame; +import org.eclipse.jetty.websocket.frames.PingFrame; import org.eclipse.jetty.websocket.frames.TextFrame; import org.junit.Rule; import org.junit.Test; @@ -62,7 +64,7 @@ public class WebSocketEventDriverTest @Test public void testAnnotated_ByteBuffer() { - AnnotatedBasicSocket socket = new AnnotatedBasicSocket(); + AnnotatedByteBufferSocket socket = new AnnotatedByteBufferSocket(); WebSocketEventDriver driver = newDriver(socket); LocalWebSocketConnection conn = new LocalWebSocketConnection(testname); @@ -77,6 +79,28 @@ public class WebSocketEventDriverTest socket.capture.assertEventStartsWith(2,"onClose(1000,"); } + @Test + public void testAnnotated_Frames() + { + AnnotatedFramesSocket socket = new AnnotatedFramesSocket(); + WebSocketEventDriver driver = newDriver(socket); + + LocalWebSocketConnection conn = new LocalWebSocketConnection(testname); + driver.setConnection(conn); + driver.onConnect(); + driver.onFrame(new PingFrame(StringUtil.getUtf8Bytes("PING"))); + driver.onFrame(new TextFrame("Text Me")); + driver.onFrame(new BinaryFrame(StringUtil.getUtf8Bytes("Hello Bin"))); + driver.onFrame(new CloseFrame(StatusCode.SHUTDOWN)); + + socket.capture.assertEventCount(5); + socket.capture.assertEventStartsWith(0,"onConnect("); + socket.capture.assertEventStartsWith(1,"onPingFrame("); + socket.capture.assertEventStartsWith(2,"onTextFrame("); + socket.capture.assertEventStartsWith(3,"onBaseFrame(BinaryFrame"); + socket.capture.assertEventStartsWith(4,"onClose(1001,"); + } + @Test public void testListener_Text() { diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedBasicSocket.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedByteBufferSocket.java similarity index 96% rename from jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedBasicSocket.java rename to jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedByteBufferSocket.java index 1ce8da150b7..1a4f0c80b74 100644 --- a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedBasicSocket.java +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedByteBufferSocket.java @@ -10,7 +10,7 @@ import org.eclipse.jetty.websocket.api.EventCapture; import org.eclipse.jetty.websocket.api.WebSocketConnection; @WebSocket -public class AnnotatedBasicSocket +public class AnnotatedByteBufferSocket { public EventCapture capture = new EventCapture(); diff --git a/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedFramesSocket.java b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedFramesSocket.java new file mode 100644 index 00000000000..d4fbe08b41f --- /dev/null +++ b/jetty-websocket/websocket-core/src/test/java/org/eclipse/jetty/websocket/api/samples/AnnotatedFramesSocket.java @@ -0,0 +1,54 @@ +package org.eclipse.jetty.websocket.api.samples; + +import org.eclipse.jetty.websocket.annotations.OnWebSocketClose; +import org.eclipse.jetty.websocket.annotations.OnWebSocketConnect; +import org.eclipse.jetty.websocket.annotations.OnWebSocketFrame; +import org.eclipse.jetty.websocket.annotations.WebSocket; +import org.eclipse.jetty.websocket.api.EventCapture; +import org.eclipse.jetty.websocket.api.WebSocketConnection; +import org.eclipse.jetty.websocket.frames.BaseFrame; +import org.eclipse.jetty.websocket.frames.ControlFrame; +import org.eclipse.jetty.websocket.frames.PingFrame; +import org.eclipse.jetty.websocket.frames.TextFrame; + +@WebSocket +public class AnnotatedFramesSocket +{ + public EventCapture capture = new EventCapture(); + + @OnWebSocketFrame + public void onBaseFrame(BaseFrame frame) + { + capture.add("onBaseFrame(%s)",frame); + } + + @OnWebSocketClose + public void onClose(int statusCode, String reason) + { + capture.add("onClose(%d, %s)",statusCode,capture.q(reason)); + } + + @OnWebSocketConnect + public void onConnect(WebSocketConnection conn) + { + capture.add("onConnect(%s)",conn); + } + + @OnWebSocketFrame + public void onControlFrame(ControlFrame ping) + { + capture.add("onControlFrame(%s)",ping); + } + + @OnWebSocketFrame + public void onPing(PingFrame ping) + { + capture.add("onPingFrame(%s)",ping); + } + + @OnWebSocketFrame + public void onTextFrame(TextFrame text) + { + capture.add("onTextFrame(%s)",text); + } +}