From 2c7964d9c908bc62981493b90ef943dd9da7a688 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Fri, 21 Oct 2016 13:53:31 -0700 Subject: [PATCH] Issue #207 - fixing EchoTest --- .../websocket/jsr356/server/EchoTest.java | 48 ++++----------- .../server/samples/beans/DateEncoder.java | 2 +- .../server/samples/beans/DateTextSocket.java | 4 +- .../samples/echo/EchoAsyncTextSocket.java | 42 +++++++++++++ .../samples/echo/EchoBasicTextSocket.java | 60 +++++++++++++++++++ .../samples/echo/EchoReturnTextSocket.java | 32 ++++++++++ .../echo/EchoStatelessAsyncTextSocket.java | 33 ++++++++++ .../echo/EchoStatelessBasicTextSocket.java | 51 ++++++++++++++++ 8 files changed, 231 insertions(+), 41 deletions(-) create mode 100644 jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoAsyncTextSocket.java create mode 100644 jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoBasicTextSocket.java create mode 100644 jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoReturnTextSocket.java create mode 100644 jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoStatelessAsyncTextSocket.java create mode 100644 jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoStatelessBasicTextSocket.java diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/EchoTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/EchoTest.java index 44df45e15a5..b8988f75d6a 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/EchoTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/EchoTest.java @@ -37,22 +37,13 @@ import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.websocket.jsr356.server.EchoCase.PartialBinary; import org.eclipse.jetty.websocket.jsr356.server.EchoCase.PartialText; -import org.eclipse.jetty.websocket.jsr356.server.samples.BasicCloseReasonSessionSocket; -import org.eclipse.jetty.websocket.jsr356.server.samples.BasicCloseReasonSocket; -import org.eclipse.jetty.websocket.jsr356.server.samples.BasicCloseSessionReasonSocket; -import org.eclipse.jetty.websocket.jsr356.server.samples.BasicCloseSocket; -import org.eclipse.jetty.websocket.jsr356.server.samples.BasicErrorSessionSocket; -import org.eclipse.jetty.websocket.jsr356.server.samples.BasicErrorSessionThrowableSocket; -import org.eclipse.jetty.websocket.jsr356.server.samples.BasicErrorSocket; -import org.eclipse.jetty.websocket.jsr356.server.samples.BasicErrorThrowableSessionSocket; -import org.eclipse.jetty.websocket.jsr356.server.samples.BasicErrorThrowableSocket; -import org.eclipse.jetty.websocket.jsr356.server.samples.BasicOpenSessionSocket; -import org.eclipse.jetty.websocket.jsr356.server.samples.BasicOpenSocket; -import org.eclipse.jetty.websocket.jsr356.server.samples.BasicPongMessageSocket; -import org.eclipse.jetty.websocket.jsr356.server.samples.BasicTextMessageStringSocket; -import org.eclipse.jetty.websocket.jsr356.server.samples.StatelessTextMessageStringSocket; import org.eclipse.jetty.websocket.jsr356.server.samples.beans.DateTextSocket; import org.eclipse.jetty.websocket.jsr356.server.samples.binary.ByteBufferSocket; +import org.eclipse.jetty.websocket.jsr356.server.samples.echo.EchoAsyncTextSocket; +import org.eclipse.jetty.websocket.jsr356.server.samples.echo.EchoBasicTextSocket; +import org.eclipse.jetty.websocket.jsr356.server.samples.echo.EchoReturnTextSocket; +import org.eclipse.jetty.websocket.jsr356.server.samples.echo.EchoStatelessAsyncTextSocket; +import org.eclipse.jetty.websocket.jsr356.server.samples.echo.EchoStatelessBasicTextSocket; import org.eclipse.jetty.websocket.jsr356.server.samples.partial.PartialTextSessionSocket; import org.eclipse.jetty.websocket.jsr356.server.samples.partial.PartialTextSocket; import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.BooleanObjectTextSocket; @@ -200,8 +191,11 @@ public class EchoTest EchoCase.add(TESTCASES,IntParamTextSocket.class).requestPath("/echo/primitives/integer/params/5678").addMessage(1234).expect("1234|5678"); // Text based - EchoCase.add(TESTCASES,BasicTextMessageStringSocket.class).addMessage("Hello").expect("Hello"); - EchoCase.add(TESTCASES,StatelessTextMessageStringSocket.class).addMessage("Hello").expect("Hello"); + EchoCase.add(TESTCASES,EchoBasicTextSocket.class).addMessage("Hello").expect("Hello"); + EchoCase.add(TESTCASES,EchoStatelessBasicTextSocket.class).addMessage("Hello").expect("Hello"); + EchoCase.add(TESTCASES,EchoAsyncTextSocket.class).addMessage("Hello").expect("Hello"); + EchoCase.add(TESTCASES,EchoStatelessAsyncTextSocket.class).addMessage("Hello").expect("Hello"); + EchoCase.add(TESTCASES,EchoReturnTextSocket.class).addMessage("Hello").expect("Hello"); // ByteBuffer based EchoCase.add(TESTCASES,ByteBufferSocket.class).addMessage(BufferUtil.toBuffer("Hello World")).expect("Hello World"); @@ -224,27 +218,7 @@ public class EchoTest .expect("('Built',false)(' for',false)(' the',false)(' future',true)"); // Beans - EchoCase.add(TESTCASES, DateTextSocket.class).addMessage("Ooops").expect(""); - - // Pong - EchoCase.add(TESTCASES, BasicPongMessageSocket.class).addMessage("send-ping").expect("Pong[]"); - - // Open Events - EchoCase.add(TESTCASES, BasicOpenSocket.class).expect("Open[]"); - EchoCase.add(TESTCASES, BasicOpenSessionSocket.class).expect("Open[Session]"); - - // Close Events - EchoCase.add(TESTCASES, BasicCloseSocket.class).expect("Close[]"); - EchoCase.add(TESTCASES, BasicCloseReasonSocket.class).expect("Close[Reason]"); - EchoCase.add(TESTCASES, BasicCloseReasonSessionSocket.class).expect("Close[Reason,Session]"); - EchoCase.add(TESTCASES, BasicCloseSessionReasonSocket.class).expect("Close[Session,Reason]"); - - // Error Events - EchoCase.add(TESTCASES, BasicErrorSocket.class).expect("Error[]"); - EchoCase.add(TESTCASES, BasicErrorSessionSocket.class).expect("Error[Session]"); - EchoCase.add(TESTCASES, BasicErrorSessionThrowableSocket.class).expect("Error[Session,Throwable]"); - EchoCase.add(TESTCASES, BasicErrorThrowableSocket.class).expect("Error[Throwable]"); - EchoCase.add(TESTCASES, BasicErrorThrowableSessionSocket.class).expect("Error[Throwable,Session]"); + EchoCase.add(TESTCASES, DateTextSocket.class).addMessage("1995.08.01").expect("[1995/08/01]"); } @BeforeClass diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/beans/DateEncoder.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/beans/DateEncoder.java index 2317e98b47b..acfaa5d0f8b 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/beans/DateEncoder.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/beans/DateEncoder.java @@ -38,7 +38,7 @@ public class DateEncoder implements Encoder.Text @Override public String encode(Date object) throws EncodeException { - return new SimpleDateFormat("yyyy.MM.dd").format(object); + return new SimpleDateFormat("[yyyy/MM/dd]").format(object); } @Override diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/beans/DateTextSocket.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/beans/DateTextSocket.java index 487ce236845..d5a0941f2c3 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/beans/DateTextSocket.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/beans/DateTextSocket.java @@ -19,7 +19,6 @@ package org.eclipse.jetty.websocket.jsr356.server.samples.beans; import java.io.IOException; -import java.text.SimpleDateFormat; import java.util.Date; import javax.websocket.OnError; @@ -55,9 +54,8 @@ public class DateTextSocket } else { - String msg = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT).format(d); // The encoder declared in the @ServerEndpoint will be used - session.getAsyncRemote().sendText(msg); + session.getAsyncRemote().sendObject(d); } } diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoAsyncTextSocket.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoAsyncTextSocket.java new file mode 100644 index 00000000000..d869ab5df4d --- /dev/null +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoAsyncTextSocket.java @@ -0,0 +1,42 @@ +// +// ======================================================================== +// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.jsr356.server.samples.echo; + +import javax.websocket.OnMessage; +import javax.websocket.OnOpen; +import javax.websocket.Session; +import javax.websocket.server.ServerEndpoint; + +@ServerEndpoint("/echo/text/async") +public class EchoAsyncTextSocket +{ + private Session session; + + @OnOpen + public void onOpen(Session session) + { + this.session = session; + } + + @OnMessage + public void onText(String msg) + { + session.getAsyncRemote().sendText(msg); + } +} diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoBasicTextSocket.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoBasicTextSocket.java new file mode 100644 index 00000000000..59276f63880 --- /dev/null +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoBasicTextSocket.java @@ -0,0 +1,60 @@ +// +// ======================================================================== +// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.jsr356.server.samples.echo; + +import java.io.IOException; + +import javax.websocket.CloseReason; +import javax.websocket.OnMessage; +import javax.websocket.OnOpen; +import javax.websocket.Session; +import javax.websocket.server.ServerEndpoint; + +@ServerEndpoint("/echo/text/basic") +public class EchoBasicTextSocket +{ + private Session session; + + @OnOpen + public void onOpen(Session session) + { + this.session = session; + } + + @OnMessage + public void onText(String msg) + { + try + { + session.getBasicRemote().sendText(msg); + } + catch (IOException esend) + { + esend.printStackTrace(System.err); + try + { + session.close(new CloseReason(CloseReason.CloseCodes.getCloseCode(4001), "Unable to echo msg")); + } + catch (IOException eclose) + { + eclose.printStackTrace(); + } + } + } +} diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoReturnTextSocket.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoReturnTextSocket.java new file mode 100644 index 00000000000..ba09e7b16b2 --- /dev/null +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoReturnTextSocket.java @@ -0,0 +1,32 @@ +// +// ======================================================================== +// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.jsr356.server.samples.echo; + +import javax.websocket.OnMessage; +import javax.websocket.server.ServerEndpoint; + +@ServerEndpoint("/echo/text/return") +public class EchoReturnTextSocket +{ + @OnMessage + public String onText(String msg) + { + return msg; + } +} diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoStatelessAsyncTextSocket.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoStatelessAsyncTextSocket.java new file mode 100644 index 00000000000..2a8fbd1af1f --- /dev/null +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoStatelessAsyncTextSocket.java @@ -0,0 +1,33 @@ +// +// ======================================================================== +// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.jsr356.server.samples.echo; + +import javax.websocket.OnMessage; +import javax.websocket.Session; +import javax.websocket.server.ServerEndpoint; + +@ServerEndpoint("/echo/text/async/stateless") +public class EchoStatelessAsyncTextSocket +{ + @OnMessage + public void onText(Session session, String msg) + { + session.getAsyncRemote().sendText(msg); + } +} diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoStatelessBasicTextSocket.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoStatelessBasicTextSocket.java new file mode 100644 index 00000000000..801fe0bc962 --- /dev/null +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/samples/echo/EchoStatelessBasicTextSocket.java @@ -0,0 +1,51 @@ +// +// ======================================================================== +// Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.jsr356.server.samples.echo; + +import java.io.IOException; + +import javax.websocket.CloseReason; +import javax.websocket.OnMessage; +import javax.websocket.Session; +import javax.websocket.server.ServerEndpoint; + +@ServerEndpoint("/echo/text/basic/stateless") +public class EchoStatelessBasicTextSocket +{ + @OnMessage + public void onText(Session session, String msg) + { + try + { + session.getBasicRemote().sendText(msg); + } + catch (IOException esend) + { + esend.printStackTrace(System.err); + try + { + session.close(new CloseReason(CloseReason.CloseCodes.getCloseCode(4001), "Unable to echo msg")); + } + catch (IOException eclose) + { + eclose.printStackTrace(); + } + } + } +}