Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #4771 - cleanup and add tests for the unused ws message handlers #4775

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@
import org.eclipse.jetty.websocket.javax.common.UpgradeRequestAdapter;
import org.eclipse.jetty.websocket.javax.tests.MessageType;
import org.eclipse.jetty.websocket.javax.tests.SessionMatchers;
import org.eclipse.jetty.websocket.javax.tests.handlers.ByteArrayWholeHandler;
import org.eclipse.jetty.websocket.javax.tests.handlers.ByteBufferPartialHandler;
import org.eclipse.jetty.websocket.javax.tests.handlers.BinaryHandlers;
import org.eclipse.jetty.websocket.javax.tests.handlers.LongMessageHandler;
import org.eclipse.jetty.websocket.javax.tests.handlers.StringWholeHandler;
import org.eclipse.jetty.websocket.javax.tests.handlers.TextHandlers;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -93,7 +92,7 @@ public void stopSession() throws Exception
@Test
public void testMessageHandlerBinary()
{
session.addMessageHandler(new ByteBufferPartialHandler());
session.addMessageHandler(new BinaryHandlers.ByteBufferPartialHandler());
assertThat("session", session, SessionMatchers.isMessageHandlerTypeRegistered(MessageType.BINARY));
assertThat("session", session, Matchers.not(SessionMatchers.isMessageHandlerTypeRegistered(MessageType.TEXT)));
assertThat("session", session, Matchers.not(SessionMatchers.isMessageHandlerTypeRegistered(MessageType.PONG)));
Expand All @@ -102,7 +101,7 @@ public void testMessageHandlerBinary()
Matchers.hasItem(
Matchers.allOf(
SessionMatchers.isMessageHandlerType(session, MessageType.BINARY),
instanceOf(ByteBufferPartialHandler.class)
instanceOf(BinaryHandlers.ByteBufferPartialHandler.class)
)
)
);
Expand All @@ -111,8 +110,8 @@ public void testMessageHandlerBinary()
@Test
public void testMessageHandlerBoth()
{
session.addMessageHandler(new StringWholeHandler());
session.addMessageHandler(new ByteArrayWholeHandler());
session.addMessageHandler(new TextHandlers.StringWholeHandler());
session.addMessageHandler(new BinaryHandlers.ByteArrayWholeHandler());
assertThat("session", session, SessionMatchers.isMessageHandlerTypeRegistered(MessageType.BINARY));
assertThat("session", session, SessionMatchers.isMessageHandlerTypeRegistered(MessageType.TEXT));
assertThat("session", session, Matchers.not(SessionMatchers.isMessageHandlerTypeRegistered(MessageType.PONG)));
Expand All @@ -121,7 +120,7 @@ public void testMessageHandlerBoth()
Matchers.hasItem(
Matchers.allOf(
SessionMatchers.isMessageHandlerType(session, MessageType.BINARY),
instanceOf(ByteArrayWholeHandler.class)
instanceOf(BinaryHandlers.ByteArrayWholeHandler.class)
)
)
);
Expand All @@ -130,7 +129,7 @@ public void testMessageHandlerBoth()
Matchers.hasItem(
Matchers.allOf(
SessionMatchers.isMessageHandlerType(session, MessageType.TEXT),
instanceOf(StringWholeHandler.class)
instanceOf(TextHandlers.StringWholeHandler.class)
)
)
);
Expand All @@ -139,9 +138,9 @@ public void testMessageHandlerBoth()
@Test
public void testMessageHandlerReplaceTextHandler()
{
MessageHandler strHandler = new StringWholeHandler();
MessageHandler strHandler = new TextHandlers.StringWholeHandler();
session.addMessageHandler(strHandler); // add a TEXT handler
session.addMessageHandler(new ByteArrayWholeHandler()); // add BINARY handler
session.addMessageHandler(new BinaryHandlers.ByteArrayWholeHandler()); // add BINARY handler
session.removeMessageHandler(strHandler); // remove original TEXT handler
session.addMessageHandler(new LongMessageHandler()); // add new TEXT handler

Expand All @@ -154,7 +153,7 @@ public void testMessageHandlerReplaceTextHandler()
Matchers.hasItem(
Matchers.allOf(
SessionMatchers.isMessageHandlerType(session, MessageType.BINARY),
instanceOf(ByteArrayWholeHandler.class)
instanceOf(BinaryHandlers.ByteArrayWholeHandler.class)
)
)
);
Expand All @@ -177,7 +176,7 @@ public void testMessageHandlerAddRemoveLambda()
MessageHandler.Whole<String> lamdaHandler = (msg) -> received.add(msg);

session.addMessageHandler(String.class, lamdaHandler); // add a TEXT handler lambda
session.addMessageHandler(new ByteArrayWholeHandler()); // add BINARY handler
session.addMessageHandler(new BinaryHandlers.ByteArrayWholeHandler()); // add BINARY handler
session.removeMessageHandler(lamdaHandler); // remove original TEXT handler

assertThat("session", session, SessionMatchers.isMessageHandlerTypeRegistered(MessageType.BINARY));
Expand All @@ -189,7 +188,7 @@ public void testMessageHandlerAddRemoveLambda()
Matchers.hasItem(
Matchers.allOf(
SessionMatchers.isMessageHandlerType(session, MessageType.BINARY),
instanceOf(ByteArrayWholeHandler.class)
instanceOf(BinaryHandlers.ByteArrayWholeHandler.class)
)
)
);
Expand All @@ -198,7 +197,7 @@ public void testMessageHandlerAddRemoveLambda()
@Test
public void testMessageHandlerText()
{
session.addMessageHandler(new StringWholeHandler());
session.addMessageHandler(new TextHandlers.StringWholeHandler());

assertThat("session", session, Matchers.not(SessionMatchers.isMessageHandlerTypeRegistered(MessageType.BINARY)));
assertThat("session", session, SessionMatchers.isMessageHandlerTypeRegistered(MessageType.TEXT));
Expand All @@ -209,7 +208,7 @@ public void testMessageHandlerText()
Matchers.hasItem(
Matchers.allOf(
SessionMatchers.isMessageHandlerType(session, MessageType.TEXT),
instanceOf(StringWholeHandler.class)
instanceOf(TextHandlers.StringWholeHandler.class)
)
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.websocket.javax.tests.handlers;

import java.io.IOException;
import java.nio.ByteBuffer;
import javax.websocket.EndpointConfig;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/")
public class AbstractAnnotatedHandler
{
protected Session _session;

@OnOpen
public void onOpen(Session session, EndpointConfig config)
{
_session = session;
}

@OnError
public void onError(Session session, Throwable thr)
{
thr.printStackTrace();
}

public void sendText(String message, boolean last)
{
try
{
_session.getBasicRemote().sendText(message, last);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}

public void sendBinary(ByteBuffer message, boolean last)
{
try
{
_session.getBasicRemote().sendBinary(message, last);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.websocket.javax.tests.handlers;

import java.io.IOException;
import java.nio.ByteBuffer;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;

public class AbstractHandler extends Endpoint implements MessageHandler
{
protected Session _session;

@Override
public void onOpen(Session session, EndpointConfig config)
{
_session = session;
_session.addMessageHandler(this);
}

@Override
public void onError(Session session, Throwable thr)
{
thr.printStackTrace();
}

public void sendText(String message, boolean last)
{
try
{
_session.getBasicRemote().sendText(message, last);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}

public void sendBinary(ByteBuffer message, boolean last)
{
try
{
_session.getBasicRemote().sendBinary(message, last);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

import javax.websocket.MessageHandler;

public class BaseMessageHandler implements MessageHandler.Whole<String>
public class BaseMessageHandler extends AbstractHandler implements MessageHandler.Whole<String>
{
@Override
public void onMessage(String message)
{
// TODO Auto-generated method stub
sendText(message, true);
}
}
Loading