Issue #207 - Support javax.websocket version 1.1

This commit is contained in:
Joakim Erdfelt 2016-08-02 16:20:27 -07:00
parent b71be7c808
commit b079fba842
5 changed files with 227 additions and 81 deletions

View File

@ -1,41 +0,0 @@
//
// ========================================================================
// 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.util.component;
public final class CloseableLifeCycle<T extends LifeCycle> implements AutoCloseable
{
public T lifecycle;
public CloseableLifeCycle(T lifecycle) throws Exception
{
this.lifecycle = lifecycle;
this.lifecycle.start();
}
public T get()
{
return lifecycle;
}
@Override
public void close() throws Exception
{
lifecycle.stop();
}
}

View File

@ -40,13 +40,13 @@ public class OnReaderFunction implements Function<Reader, Void>
{
private static final Logger LOG = Log.getLogger(OnReaderFunction.class);
private static final DynamicArgs.Builder ARGBUILDER;
private static final Arg ARG_SESSION = new Arg(1, Session.class);
private static final Arg ARG_STREAM = new Arg(2, Reader.class).required();
private static final Arg ARG_SESSION = new Arg(Session.class);
private static final Arg ARG_STREAM = new Arg(Reader.class).required();
static
{
ARGBUILDER = new DynamicArgs.Builder();
ARGBUILDER.addSignature(ARG_STREAM, ARG_SESSION);
ARGBUILDER.addSignature(ARG_SESSION, ARG_STREAM);
}
public static DynamicArgs.Builder getDynamicArgsBuilder()

View File

@ -32,7 +32,6 @@ import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.component.CloseableLifeCycle;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.WebSocketConnectionListener;
@ -57,6 +56,21 @@ public class CommonEndpointFunctionsTest
@Rule
public TestName testname = new TestName();
private class CloseableEndpointFunctions extends CommonEndpointFunctions implements AutoCloseable
{
public CloseableEndpointFunctions(Object endpoint, WebSocketContainerScope containerScope) throws Exception
{
super(endpoint, containerScope.getPolicy(), containerScope.getExecutor());
start();
}
@Override
public void close() throws Exception
{
stop();
}
}
public Session initSession(Object websocket)
{
@ -90,14 +104,11 @@ public class CommonEndpointFunctionsTest
// Setup
ConnectionOnly socket = new ConnectionOnly();
Session session = initSession(socket);
try (CloseableLifeCycle<CommonEndpointFunctions> lifecycle = new CloseableLifeCycle<>(
new CommonEndpointFunctions(socket, containerScope.getPolicy(), containerScope.getExecutor())))
try (CloseableEndpointFunctions endpointFunctions = new CloseableEndpointFunctions(socket, containerScope))
{
EndpointFunctions<Session> endpointFunctions = lifecycle.get();
// Trigger Events
endpointFunctions.onOpen(session);
endpointFunctions.onText(BufferUtil.toBuffer("Hello World", UTF8), true);
endpointFunctions.onText(BufferUtil.toBuffer("Hello?", UTF8), true);
endpointFunctions.onClose(new CloseInfo(StatusCode.NORMAL, "Normal"));
}
@ -128,21 +139,18 @@ public class CommonEndpointFunctionsTest
// Setup
DataConnection socket = new DataConnection();
Session session = initSession(socket);
try (CloseableLifeCycle<CommonEndpointFunctions> lifecycle = new CloseableLifeCycle<>(
new CommonEndpointFunctions(socket, containerScope.getPolicy(), containerScope.getExecutor())))
try (CloseableEndpointFunctions endpointFunctions = new CloseableEndpointFunctions(socket, containerScope))
{
EndpointFunctions<Session> endpointFunctions = lifecycle.get();
// Trigger Events
endpointFunctions.onOpen(session);
endpointFunctions.onText(BufferUtil.toBuffer("Hello World", UTF8), true);
endpointFunctions.onText(BufferUtil.toBuffer("Hello Text", UTF8), true);
endpointFunctions.onClose(new CloseInfo(StatusCode.NORMAL, "Normal"));
}
// Validate Events
socket.assertCaptured(
"onWebSocketConnect\\([^\\)]*\\)",
"onWebSocketText\\(Hello World\\)",
"onWebSocketText\\(Hello Text\\)",
"onWebSocketClose\\([^\\)]*\\)");
}
@ -175,14 +183,12 @@ public class CommonEndpointFunctionsTest
// Setup
StreamedText socket = new StreamedText(1);
Session session = initSession(socket);
try (CloseableLifeCycle<CommonEndpointFunctions> lifecycle = new CloseableLifeCycle<>(
new CommonEndpointFunctions(socket, containerScope.getPolicy(), containerScope.getExecutor())))
try (CloseableEndpointFunctions endpointFunctions = new CloseableEndpointFunctions(socket, containerScope))
{
EndpointFunctions<Session> endpointFunctions = lifecycle.get();
// Trigger Events
endpointFunctions.onOpen(session);
endpointFunctions.onText(BufferUtil.toBuffer("Hello World", UTF8), true);
endpointFunctions.onText(BufferUtil.toBuffer("Hello Text Stream", UTF8), true);
endpointFunctions.onClose(new CloseInfo(StatusCode.NORMAL, "Normal"));
}
@ -190,7 +196,7 @@ public class CommonEndpointFunctionsTest
socket.streamLatch.await(2, TimeUnit.SECONDS);
// Validate Events
socket.assertCaptured("onTextStream\\(Hello World\\)");
socket.assertCaptured("onTextStream\\(Hello Text Stream\\)");
}
@Test(timeout = 1000)
@ -199,10 +205,8 @@ public class CommonEndpointFunctionsTest
// Setup
StreamedText socket = new StreamedText(1);
Session session = initSession(socket);
try (CloseableLifeCycle<CommonEndpointFunctions> lifecycle = new CloseableLifeCycle<>(
new CommonEndpointFunctions(socket, containerScope.getPolicy(), containerScope.getExecutor())))
try (CloseableEndpointFunctions endpointFunctions = new CloseableEndpointFunctions(socket, containerScope))
{
EndpointFunctions<Session> endpointFunctions = lifecycle.get();
// Trigger Events
endpointFunctions.onOpen(session);
endpointFunctions.onText(BufferUtil.toBuffer("Hel"), false);
@ -240,27 +244,22 @@ public class CommonEndpointFunctionsTest
// Setup
PartialData socket = new PartialData();
Session session = initSession(socket);
try (CloseableLifeCycle<CommonEndpointFunctions> lifecycle = new CloseableLifeCycle<>(
new CommonEndpointFunctions(socket, containerScope.getPolicy(), containerScope.getExecutor())))
try (CloseableEndpointFunctions endpointFunctions = new CloseableEndpointFunctions(socket, containerScope))
{
EndpointFunctions<Session> endpointFunctions = lifecycle.get();
// Trigger Events
endpointFunctions.onOpen(session);
endpointFunctions.onText(BufferUtil.toBuffer("Hel"), false);
endpointFunctions.onText(BufferUtil.toBuffer("lo "), false);
endpointFunctions.onText(BufferUtil.toBuffer("Wor"), false);
endpointFunctions.onText(BufferUtil.toBuffer("ld"), true);
endpointFunctions.onText(BufferUtil.toBuffer("Hello"), false);
endpointFunctions.onText(BufferUtil.toBuffer(" "), false);
endpointFunctions.onText(BufferUtil.toBuffer("World"), true);
endpointFunctions.onClose(new CloseInfo(StatusCode.NORMAL, "Normal"));
}
// Validate Events
socket.assertCaptured(
"onWebSocketConnect\\([^\\)]*\\)",
"onWebSocketPartialText\\(Hel, false\\)",
"onWebSocketPartialText\\(lo , false\\)",
"onWebSocketPartialText\\(Wor, false\\)",
"onWebSocketPartialText\\(ld, true\\)",
"onWebSocketPartialText\\(Hello, false\\)",
"onWebSocketPartialText\\( , false\\)",
"onWebSocketPartialText\\(World, true\\)",
"onWebSocketClose\\([^\\)]*\\)"
);
}

View File

@ -30,6 +30,36 @@ import org.junit.Test;
public class DynamicArgsTest
{
public static class A
{
private final String id;
public A(String id)
{
this.id = id;
}
public String toString()
{
return String.format("A:%s",id);
}
}
public static class B
{
private final int val;
public B(int val)
{
this.val = val;
}
public String toString()
{
return String.format("B:%d",val);
}
}
@SuppressWarnings("unused")
public static class SampleSignatures
{
@ -40,28 +70,51 @@ public class DynamicArgsTest
public String sigStr(String str)
{
return String.format("sigStr<%s>", str);
return String.format("sigStr<%s>", q(str));
}
public String sigStrFile(String str, File foo)
{
return String.format("sigStrFile<%s,%s>", str, foo);
return String.format("sigStrFile<%s,%s>", q(str), q(foo));
}
public String sigFileStr(File foo, String str)
{
return String.format("sigFileStr<%s,%s>", foo, str);
return String.format("sigFileStr<%s,%s>", q(foo), q(str));
}
public String sigFileStrFin(File foo, String str, @Name("fin") boolean fin)
{
return String.format("sigFileStrFin<%s,%s,%b>", foo, str, fin);
return String.format("sigFileStrFin<%s,%s,%b>", q(foo), q(str), fin);
}
public String sigByteArray(byte[] buf, @Name("offset") int offset, @Name("length") int len)
{
return String.format("sigByteArray<%s,%d,%d>", buf == null ? "<null>" : ("[" + buf.length + "]"), offset, len);
}
public String sigObjectArgs(A a, B b)
{
return String.format("sigObjectArgs<%s,%s>", q(a), q(b));
}
public String sigObjectA(A a)
{
return String.format("sigObjectA<%s>", q(a));
}
public String sigObjectB(B b)
{
return String.format("sigObjectB<%s>", q(b));
}
private String q(Object obj)
{
if (obj == null)
return "<null>";
else
return obj.toString();
}
}
public static Method findMethodByName(Object obj, String name)
@ -184,4 +237,103 @@ public class DynamicArgsTest
result = (String) dynamicArgs.invoke(ssigs, null, 123, 456);
assertThat("result", result, is("sigByteArray<<null>,123,456>"));
}
/**
* Test of calling a method with 2 custom objects
*
* @throws Exception on error
*/
@Test
public void testObjects_A_B() throws Exception
{
final Arg ARG_A = new Arg(A.class);
final Arg ARG_B = new Arg(B.class);
DynamicArgs.Builder dab = new DynamicArgs.Builder();
dab.addSignature(ARG_A, ARG_B);
SampleSignatures ssigs = new SampleSignatures();
Method m = findMethodByName(ssigs, "sigObjectArgs");
DynamicArgs dynamicArgs = dab.build(m, ARG_A, ARG_B);
assertThat("DynamicArgs", dynamicArgs, notNullValue());
// Test with potential args
A a = new A("foo");
B b = new B(444);
String result = (String) dynamicArgs.invoke(ssigs, a, b);
assertThat("result", result, is("sigObjectArgs<A:foo,B:444>"));
// Test with null potential args
result = (String) dynamicArgs.invoke(ssigs, null, b);
assertThat("result", result, is("sigObjectArgs<<null>,B:444>"));
result = (String) dynamicArgs.invoke(ssigs, a, null);
assertThat("result", result, is("sigObjectArgs<A:foo,<null>>"));
}
/**
* Test of calling a method with 2 custom objects, but the method only has 1 declared
*
* @throws Exception on error
*/
@Test
public void testObjects_A() throws Exception
{
final Arg ARG_A = new Arg(A.class);
final Arg ARG_B = new Arg(B.class);
DynamicArgs.Builder dab = new DynamicArgs.Builder();
dab.addSignature(ARG_A, ARG_B);
SampleSignatures ssigs = new SampleSignatures();
Method m = findMethodByName(ssigs, "sigObjectA");
DynamicArgs dynamicArgs = dab.build(m, ARG_A, ARG_B);
assertThat("DynamicArgs", dynamicArgs, notNullValue());
// Test with potential args
A a = new A("foo");
B b = new B(555);
String result = (String) dynamicArgs.invoke(ssigs, a, b);
assertThat("result", result, is("sigObjectA<A:foo>"));
// Test with null potential args
result = (String) dynamicArgs.invoke(ssigs, null, b);
assertThat("result", result, is("sigObjectA<<null>>"));
result = (String) dynamicArgs.invoke(ssigs, a, null);
assertThat("result", result, is("sigObjectA<A:foo>"));
}
/**
* Test of calling a method with 2 custom objects, but the method only has 1 declared
*
* @throws Exception on error
*/
@Test
public void testObjects_B() throws Exception
{
final Arg ARG_A = new Arg(A.class);
final Arg ARG_B = new Arg(B.class);
DynamicArgs.Builder dab = new DynamicArgs.Builder();
dab.addSignature(ARG_A, ARG_B);
SampleSignatures ssigs = new SampleSignatures();
Method m = findMethodByName(ssigs, "sigObjectB");
DynamicArgs dynamicArgs = dab.build(m, ARG_A, ARG_B);
assertThat("DynamicArgs", dynamicArgs, notNullValue());
// Test with potential args
A a = new A("foo");
B b = new B(666);
String result = (String) dynamicArgs.invoke(ssigs, a, b);
assertThat("result", result, is("sigObjectB<B:666>"));
// Test with null potential args
result = (String) dynamicArgs.invoke(ssigs, null, b);
assertThat("result", result, is("sigObjectB<B:666>"));
result = (String) dynamicArgs.invoke(ssigs, a, null);
assertThat("result", result, is("sigObjectB<<null>>"));
}
}

View File

@ -31,6 +31,37 @@ import org.junit.Test;
public class UnorderedSignatureTest
{
public static class A
{
private final String id;
public A(String id)
{
this.id = id;
}
public String toString()
{
return String.format("A:%s",id);
}
}
public static class B
{
private final int val;
public B(int val)
{
this.val = val;
}
public String toString()
{
return String.format("B:%d",val);
}
}
@SuppressWarnings("unused")
public static class SampleSignatures
{
public String sigEmpty()
@ -63,6 +94,11 @@ public class UnorderedSignatureTest
return String.format("sigByteArray<%s,%d,%d>", buf == null ? "<null>" : ("[" + buf.length + "]"), offset, len);
}
public String sigObjectArgs(A a, B b)
{
return String.format("sigObjectArgs<%s,%s>", q(a), q(b));
}
private String q(Object obj)
{
if (obj == null)