JSR-356 - Refactoring Annotated method parameter detection and use

This commit is contained in:
Joakim Erdfelt 2013-07-24 12:31:35 -07:00
parent 70a57f223f
commit d967dafe45
34 changed files with 378 additions and 182 deletions

View File

@ -183,6 +183,11 @@ public class JsrSession extends WebSocketSession implements javax.websocket.Sess
return encoderFactory;
}
public EndpointConfig getEndpointConfig()
{
return config;
}
public EndpointMetadata getEndpointMetadata()
{
return metadata;

View File

@ -35,6 +35,7 @@ public abstract class JsrCallable extends CallableMethod
protected final Param[] params;
protected final Object[] args;
protected int idxSession = -1;
protected int idxConfig = -1;
public JsrCallable(Class<?> pojo, Method method)
{
@ -59,6 +60,7 @@ public abstract class JsrCallable extends CallableMethod
{
this(copy.getPojo(),copy.getMethod());
this.idxSession = copy.idxSession;
this.idxConfig = copy.idxConfig;
System.arraycopy(copy.params,0,this.params,0,params.length);
System.arraycopy(copy.args,0,this.args,0,args.length);
}
@ -114,6 +116,13 @@ public abstract class JsrCallable extends CallableMethod
args[idxSession] = session;
}
// Optional EndpointConfig
idxConfig = findIndexForRole(Param.Role.ENDPOINT_CONFIG);
if (idxConfig >= 0)
{
args[idxConfig] = session.getEndpointConfig();
}
// Default for the path parameters
// PathParam's are optional parameters (always)
Map<String, String> pathParams = session.getPathParameters();

View File

@ -0,0 +1,51 @@
//
// ========================================================================
// Copyright (c) 1995-2013 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.annotations;
import javax.websocket.EndpointConfig;
import javax.websocket.Session;
import org.eclipse.jetty.websocket.common.events.annotated.InvalidSignatureException;
import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
/**
* Common base for Parameter Identification in JSR Callable methods
*/
public abstract class JsrParamIdBase implements IJsrParamId
{
@Override
public boolean process(Param param, JsrCallable callable) throws InvalidSignatureException
{
// Session parameter (optional)
if (param.type.isAssignableFrom(Session.class))
{
param.bind(Role.SESSION);
return true;
}
// Endpoint Config (optional)
if (param.type.isAssignableFrom(EndpointConfig.class))
{
param.bind(Role.ENDPOINT_CONFIG);
return true;
}
return false;
}
}

View File

@ -22,7 +22,6 @@ import java.io.InputStream;
import java.nio.ByteBuffer;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import org.eclipse.jetty.websocket.common.events.annotated.InvalidSignatureException;
import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
@ -39,10 +38,9 @@ public class JsrParamIdBinary extends JsrParamIdOnMessage implements IJsrParamId
@Override
public boolean process(Param param, JsrCallable callable) throws InvalidSignatureException
{
// Session parameter (optional)
if (param.type.isAssignableFrom(Session.class))
if (super.process(param,callable))
{
param.bind(Role.SESSION);
// Found common roles
return true;
}

View File

@ -20,7 +20,6 @@ package org.eclipse.jetty.websocket.jsr356.annotations;
import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.Session;
import org.eclipse.jetty.websocket.common.events.annotated.InvalidSignatureException;
import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
@ -28,16 +27,16 @@ import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
/**
* Param handling for &#064;{@link OnClose} parameters.
*/
public class JsrParamIdOnClose implements IJsrParamId
public class JsrParamIdOnClose extends JsrParamIdBase implements IJsrParamId
{
public static final IJsrParamId INSTANCE = new JsrParamIdOnClose();
@Override
public boolean process(Param param, JsrCallable callable) throws InvalidSignatureException
{
if (param.type.isAssignableFrom(Session.class))
if (super.process(param,callable))
{
param.bind(Role.SESSION);
// Found common roles
return true;
}

View File

@ -19,7 +19,6 @@
package org.eclipse.jetty.websocket.jsr356.annotations;
import javax.websocket.OnError;
import javax.websocket.Session;
import org.eclipse.jetty.websocket.common.events.annotated.InvalidSignatureException;
import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
@ -27,16 +26,16 @@ import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
/**
* Param handling for &#064;{@link OnError} parameters.
*/
public class JsrParamIdOnError implements IJsrParamId
public class JsrParamIdOnError extends JsrParamIdBase implements IJsrParamId
{
public static final IJsrParamId INSTANCE = new JsrParamIdOnError();
@Override
public boolean process(Param param, JsrCallable callable) throws InvalidSignatureException
{
if (param.type.isAssignableFrom(Session.class))
if (super.process(param,callable))
{
param.bind(Role.SESSION);
// Found common roles
return true;
}

View File

@ -20,7 +20,7 @@ package org.eclipse.jetty.websocket.jsr356.annotations;
import org.eclipse.jetty.websocket.common.events.annotated.InvalidSignatureException;
public abstract class JsrParamIdOnMessage implements IJsrParamId
public abstract class JsrParamIdOnMessage extends JsrParamIdBase implements IJsrParamId
{
protected void assertPartialMessageSupportDisabled(Param param, JsrCallable callable)
{

View File

@ -20,7 +20,6 @@ package org.eclipse.jetty.websocket.jsr356.annotations;
import javax.websocket.EndpointConfig;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import org.eclipse.jetty.websocket.common.events.annotated.InvalidSignatureException;
import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
@ -28,16 +27,16 @@ import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
/**
* Param handling for &#064;{@link OnOpen} parameters.
*/
public class JsrParamIdOnOpen implements IJsrParamId
public class JsrParamIdOnOpen extends JsrParamIdBase implements IJsrParamId
{
public static final IJsrParamId INSTANCE = new JsrParamIdOnOpen();
@Override
public boolean process(Param param, JsrCallable callable) throws InvalidSignatureException
{
if (param.type.isAssignableFrom(Session.class))
if (super.process(param,callable))
{
param.bind(Role.SESSION);
// Found common roles
return true;
}

View File

@ -19,7 +19,6 @@
package org.eclipse.jetty.websocket.jsr356.annotations;
import javax.websocket.PongMessage;
import javax.websocket.Session;
import org.eclipse.jetty.websocket.common.events.annotated.InvalidSignatureException;
import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
@ -32,10 +31,9 @@ public class JsrParamIdPong extends JsrParamIdOnMessage implements IJsrParamId
@Override
public boolean process(Param param, JsrCallable callable) throws InvalidSignatureException
{
// Session parameter (optional)
if (param.type.isAssignableFrom(Session.class))
if (super.process(param,callable))
{
param.bind(Role.SESSION);
// Found common roles
return true;
}

View File

@ -21,7 +21,6 @@ package org.eclipse.jetty.websocket.jsr356.annotations;
import java.io.Reader;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import org.eclipse.jetty.websocket.common.events.annotated.InvalidSignatureException;
import org.eclipse.jetty.websocket.jsr356.annotations.Param.Role;
@ -56,10 +55,9 @@ public class JsrParamIdText extends JsrParamIdOnMessage implements IJsrParamId
@Override
public boolean process(Param param, JsrCallable callable) throws InvalidSignatureException
{
// Session parameter (optional)
if (param.type.isAssignableFrom(Session.class))
if (super.process(param,callable))
{
param.bind(Role.SESSION);
// Found common roles
return true;
}

View File

@ -52,6 +52,17 @@ public class OnCloseCallable extends JsrCallable
this.call(endpoint,close.getStatusCode(),close.getReason());
}
public void call(Object endpoint, CloseReason closeReason)
{
// Close Reason is an optional parameter
if (idxCloseReason >= 0)
{
// convert to javax.websocket.CloseReason
super.args[idxCloseReason] = closeReason;
}
super.call(endpoint,super.args);
}
public void call(Object endpoint, int statusCode, String reason)
{
// Close Reason is an optional parameter

View File

@ -1,77 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2013 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.endpoints;
import static org.hamcrest.Matchers.*;
import javax.websocket.CloseReason;
import javax.websocket.DeploymentException;
import org.eclipse.jetty.websocket.common.events.annotated.CallableMethod;
import org.eclipse.jetty.websocket.jsr356.ClientContainer;
import org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner;
import org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicOpenCloseSessionSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicOpenCloseSocket;
import org.junit.Assert;
import org.junit.Test;
public class ClientAnnotatedEndpointScannerTest
{
private static ClientContainer container = new ClientContainer();
private void assertHasCallable(String msg, CallableMethod callable, Class<?>... expectedParameters)
{
Assert.assertThat(msg,callable,notNullValue());
int len = expectedParameters.length;
for (int i = 0; i < len; i++)
{
Class<?> expectedParam = expectedParameters[i];
Class<?> actualParam = callable.getParamTypes()[i];
Assert.assertTrue("Parameter[" + i + "] - expected:[" + expectedParam + "], actual:[" + actualParam + "]",actualParam.equals(expectedParam));
}
}
@Test
public void testScan_BasicOpenClose() throws DeploymentException
{
AnnotatedClientEndpointMetadata metadata = new AnnotatedClientEndpointMetadata(container,BasicOpenCloseSocket.class);
AnnotatedEndpointScanner scanner = new AnnotatedEndpointScanner(metadata);
scanner.scan();
Assert.assertThat("Metadata",metadata,notNullValue());
assertHasCallable("Metadata.onOpen",metadata.onOpen);
assertHasCallable("Metadata.onClose",metadata.onClose,CloseReason.class);
}
@Test
public void testScan_BasicSessionOpenClose() throws DeploymentException
{
AnnotatedClientEndpointMetadata metadata = new AnnotatedClientEndpointMetadata(container,BasicOpenCloseSessionSocket.class);
AnnotatedEndpointScanner scanner = new AnnotatedEndpointScanner(metadata);
scanner.scan();
Assert.assertThat("Metadata",metadata,notNullValue());
assertHasCallable("Metadata.onOpen",metadata.onOpen);
assertHasCallable("Metadata.onClose",metadata.onClose,CloseReason.class);
}
}

View File

@ -26,20 +26,18 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.websocket.ClientEndpoint;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.CloseReason;
import javax.websocket.PongMessage;
import javax.websocket.Session;
import org.eclipse.jetty.websocket.jsr356.ClientContainer;
import org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointMetadata;
import org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner;
import org.eclipse.jetty.websocket.jsr356.annotations.JsrCallable;
import org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointMetadata;
import org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicBinaryMessageByteBufferSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicCloseReasonSessionSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicCloseReasonSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicCloseSessionReasonSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicCloseSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicErrorSessionSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicErrorSessionThrowableSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicErrorSocket;
@ -49,6 +47,10 @@ import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicOpenSessionSock
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicOpenSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicPongMessageSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.BasicTextMessageStringSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.close.CloseReasonSessionSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.close.CloseReasonSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.close.CloseSessionReasonSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.close.CloseSocket;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -102,10 +104,10 @@ public class ClientAnnotatedEndpointScanner_GoodSignaturesTest
Case.add(data, BasicOpenSocket.class, fOpen);
Case.add(data, BasicOpenSessionSocket.class, fOpen, Session.class);
// -- Close Events
Case.add(data, BasicCloseSocket.class, fClose);
Case.add(data, BasicCloseReasonSocket.class, fClose, CloseReason.class);
Case.add(data, BasicCloseReasonSessionSocket.class, fClose, CloseReason.class, Session.class);
Case.add(data, BasicCloseSessionReasonSocket.class, fClose, Session.class, CloseReason.class);
Case.add(data, CloseSocket.class, fClose);
Case.add(data, CloseReasonSocket.class, fClose, CloseReason.class);
Case.add(data, CloseReasonSessionSocket.class, fClose, CloseReason.class, Session.class);
Case.add(data, CloseSessionReasonSocket.class, fClose, Session.class, CloseReason.class);
// -- Error Events
Case.add(data, BasicErrorSocket.class, fError);
Case.add(data, BasicErrorSessionSocket.class, fError, Session.class);
@ -141,7 +143,7 @@ public class ClientAnnotatedEndpointScanner_GoodSignaturesTest
public void testScan_Basic() throws Exception
{
AnnotatedClientEndpointMetadata metadata = new AnnotatedClientEndpointMetadata(container,testcase.pojo);
AnnotatedEndpointScanner scanner = new AnnotatedEndpointScanner(metadata);
AnnotatedEndpointScanner<ClientEndpoint, ClientEndpointConfig> scanner = new AnnotatedEndpointScanner<>(metadata);
scanner.scan();
Assert.assertThat("Metadata",metadata,notNullValue());

View File

@ -25,6 +25,8 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.websocket.ClientEndpoint;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.DeploymentException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
@ -96,7 +98,7 @@ public class ClientAnnotatedEndpointScanner_InvalidSignaturesTest
public void testScan_InvalidSignature() throws DeploymentException
{
AnnotatedClientEndpointMetadata metadata = new AnnotatedClientEndpointMetadata(container,pojo);
AnnotatedEndpointScanner scanner = new AnnotatedEndpointScanner(metadata);
AnnotatedEndpointScanner<ClientEndpoint, ClientEndpointConfig> scanner = new AnnotatedEndpointScanner<>(metadata);
try
{
scanner.scan();

View File

@ -0,0 +1,126 @@
//
// ========================================================================
// Copyright (c) 1995-2013 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.endpoints;
import static org.hamcrest.Matchers.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.websocket.ClientEndpoint;
import javax.websocket.ClientEndpointConfig;
import org.eclipse.jetty.toolchain.test.EventQueue;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.common.CloseInfo;
import org.eclipse.jetty.websocket.common.events.EventDriver;
import org.eclipse.jetty.websocket.jsr356.ClientContainer;
import org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner;
import org.eclipse.jetty.websocket.jsr356.annotations.JsrEvents;
import org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.close.CloseEndpointConfigSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.close.CloseReasonSessionSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.close.CloseReasonSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.close.CloseSessionReasonSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.close.CloseSessionSocket;
import org.eclipse.jetty.websocket.jsr356.endpoints.samples.close.CloseSocket;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class OnCloseTest
{
private static class Case
{
public static Case add(List<Case[]> data, Class<?> closeClass)
{
Case tcase = new Case();
tcase.closeClass = closeClass;
data.add(new Case[]
{ tcase });
return tcase;
}
Class<?> closeClass;
String expectedCloseEvent;
public Case expect(String expectedEvent)
{
this.expectedCloseEvent = expectedEvent;
return this;
}
}
private static ClientContainer container = new ClientContainer();
@Parameters
public static Collection<Case[]> data() throws Exception
{
List<Case[]> data = new ArrayList<>();
Case.add(data,CloseSocket.class).expect("onClose()");
Case.add(data,CloseReasonSocket.class).expect("onClose(CloseReason)");
Case.add(data,CloseSessionSocket.class).expect("onClose(Session)");
Case.add(data,CloseReasonSessionSocket.class).expect("onClose(CloseReason,Session)");
Case.add(data,CloseSessionReasonSocket.class).expect("onClose(Session,CloseReason)");
Case.add(data,CloseEndpointConfigSocket.class).expect("onClose(EndpointConfig)");
return data;
}
private final Case testcase;
public OnCloseTest(Case testcase)
{
this.testcase = testcase;
System.err.printf("Testing @OnClose for %s%n",testcase.closeClass.getName());
}
@Test
public void testOnCloseCall() throws Exception
{
// Scan annotations
AnnotatedClientEndpointMetadata metadata = new AnnotatedClientEndpointMetadata(container,testcase.closeClass);
AnnotatedEndpointScanner<ClientEndpoint, ClientEndpointConfig> scanner = new AnnotatedEndpointScanner<>(metadata);
scanner.scan();
// Build up EventDriver
WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
ClientEndpointConfig config = metadata.getConfig();
TrackingSocket endpoint = (TrackingSocket)testcase.closeClass.newInstance();
EndpointInstance ei = new EndpointInstance(endpoint,config,metadata);
JsrEvents<ClientEndpoint, ClientEndpointConfig> jsrevents = new JsrEvents<>(metadata);
EventDriver driver = new JsrAnnotatedEventDriver(policy,ei,jsrevents);
// Execute onClose call
driver.onClose(new CloseInfo(StatusCode.NORMAL,"normal"));
// Test captured event
EventQueue<String> events = endpoint.eventQueue;
Assert.assertThat("Number of Events Captured",events.size(),is(1));
String closeEvent = events.poll();
Assert.assertThat("Close Event",closeEvent,is(testcase.expectedCloseEvent));
}
}

View File

@ -20,14 +20,13 @@ package org.eclipse.jetty.websocket.jsr356.endpoints;
import static org.hamcrest.Matchers.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.websocket.CloseReason;
import javax.websocket.CloseReason.CloseCode;
import org.eclipse.jetty.util.BlockingArrayQueue;
import org.eclipse.jetty.toolchain.test.EventQueue;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.junit.Assert;
@ -38,14 +37,25 @@ import org.junit.Assert;
public abstract class TrackingSocket
{
private static final Logger LOG = Log.getLogger(TrackingSocket.class);
public CloseReason closeReason;
public BlockingQueue<String> eventQueue = new BlockingArrayQueue<String>();
public BlockingQueue<Throwable> errorQueue = new BlockingArrayQueue<>();
public EventQueue<String> eventQueue = new EventQueue<String>();
public EventQueue<Throwable> errorQueue = new EventQueue<>();
public CountDownLatch openLatch = new CountDownLatch(1);
public CountDownLatch closeLatch = new CountDownLatch(1);
public CountDownLatch dataLatch = new CountDownLatch(1);
protected void addError(Throwable t)
{
LOG.warn(t);
errorQueue.add(t);
}
protected void addEvent(String format, Object... args)
{
eventQueue.add(String.format(format,args));
}
public void assertClose(CloseCode expectedCode, String expectedReason) throws InterruptedException
{
assertCloseCode(expectedCode);
@ -64,14 +74,10 @@ public abstract class TrackingSocket
Assert.assertThat("Close Reason",closeReason.getReasonPhrase(),is(expectedReason));
}
protected void addEvent(String format, Object... args)
public void assertEvent(String expected)
{
eventQueue.add(String.format(format,args));
}
protected void addError(Throwable t)
{
errorQueue.add(t);
String actual = eventQueue.poll();
Assert.assertEquals("Event",expected,actual);
}
public void assertIsOpen() throws InterruptedException
@ -80,12 +86,6 @@ public abstract class TrackingSocket
assertNotClosed();
}
public void assertEvent(String expected)
{
String actual = eventQueue.poll();
Assert.assertEquals("Event",expected,actual);
}
public void assertNotClosed()
{
Assert.assertThat("Closed Latch",closeLatch.getCount(),greaterThanOrEqualTo(1L));

View File

@ -0,0 +1,36 @@
//
// ========================================================================
// Copyright (c) 1995-2013 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.endpoints.samples.close;
import javax.websocket.ClientEndpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import org.eclipse.jetty.websocket.jsr356.endpoints.TrackingSocket;
@ClientEndpoint
public class CloseEndpointConfigSocket extends TrackingSocket
{
@OnClose
public void onClose(EndpointConfig config)
{
addEvent("onClose(EndpointConfig)");
closeLatch.countDown();
}
}

View File

@ -16,7 +16,7 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.jsr356.endpoints.samples;
package org.eclipse.jetty.websocket.jsr356.endpoints.samples.close;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
@ -26,12 +26,12 @@ import javax.websocket.Session;
import org.eclipse.jetty.websocket.jsr356.endpoints.TrackingSocket;
@ClientEndpoint
public class BasicCloseReasonSessionSocket extends TrackingSocket
public class CloseReasonSessionSocket extends TrackingSocket
{
@OnClose
public void onClose(CloseReason reason, Session session)
{
addEvent("onClose(%s,%s)",reason,session);
addEvent("onClose(CloseReason,Session)");
closeLatch.countDown();
}
}

View File

@ -16,7 +16,7 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.jsr356.endpoints.samples;
package org.eclipse.jetty.websocket.jsr356.endpoints.samples.close;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
@ -25,12 +25,12 @@ import javax.websocket.OnClose;
import org.eclipse.jetty.websocket.jsr356.endpoints.TrackingSocket;
@ClientEndpoint
public class BasicCloseReasonSocket extends TrackingSocket
public class CloseReasonSocket extends TrackingSocket
{
@OnClose
public void onClose(CloseReason reason)
{
addEvent("onClose(%s)", reason);
addEvent("onClose(CloseReason)");
closeLatch.countDown();
}
}

View File

@ -16,7 +16,7 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.jsr356.endpoints.samples;
package org.eclipse.jetty.websocket.jsr356.endpoints.samples.close;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
@ -26,12 +26,12 @@ import javax.websocket.Session;
import org.eclipse.jetty.websocket.jsr356.endpoints.TrackingSocket;
@ClientEndpoint
public class BasicCloseSessionReasonSocket extends TrackingSocket
public class CloseSessionReasonSocket extends TrackingSocket
{
@OnClose
public void onClose(Session session, CloseReason reason)
{
addEvent("onClose(%s,%s)",session,reason);
addEvent("onClose(Session,CloseReason)");
closeLatch.countDown();
}
}

View File

@ -0,0 +1,36 @@
//
// ========================================================================
// Copyright (c) 1995-2013 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.endpoints.samples.close;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnClose;
import javax.websocket.Session;
import org.eclipse.jetty.websocket.jsr356.endpoints.TrackingSocket;
@ClientEndpoint
public class CloseSessionSocket extends TrackingSocket
{
@OnClose
public void onClose(Session session)
{
addEvent("onClose(Session)");
closeLatch.countDown();
}
}

View File

@ -16,7 +16,7 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.jsr356.endpoints.samples;
package org.eclipse.jetty.websocket.jsr356.endpoints.samples.close;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnClose;
@ -24,7 +24,7 @@ import javax.websocket.OnClose;
import org.eclipse.jetty.websocket.jsr356.endpoints.TrackingSocket;
@ClientEndpoint
public class BasicCloseSocket extends TrackingSocket
public class CloseSocket extends TrackingSocket
{
@OnClose
public void onClose()

View File

@ -115,6 +115,7 @@ public class ServerAnnotatedEndpointScanner_GoodSignaturesTest
Field fText = findFieldRef(AnnotatedServerEndpointMetadata.class,"onText");
Field fTextStream = findFieldRef(AnnotatedServerEndpointMetadata.class,"onTextStream");
Field fBinary = findFieldRef(AnnotatedServerEndpointMetadata.class,"onBinary");
@SuppressWarnings("unused")
Field fBinaryStream = findFieldRef(AnnotatedServerEndpointMetadata.class,"onBinaryStream");
Field fPong = findFieldRef(AnnotatedServerEndpointMetadata.class,"onPong");

View File

@ -29,6 +29,8 @@ import javax.websocket.DeploymentException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.server.ServerEndpointConfig;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -95,7 +97,7 @@ public class ServerAnnotatedEndpointScanner_InvalidSignaturesTest
public void testScan_InvalidSignature() throws DeploymentException
{
AnnotatedServerEndpointMetadata metadata = new AnnotatedServerEndpointMetadata(container,pojo);
AnnotatedEndpointScanner scanner = new AnnotatedEndpointScanner(metadata);
AnnotatedEndpointScanner<ServerEndpoint,ServerEndpointConfig> scanner = new AnnotatedEndpointScanner<>(metadata);
try
{

View File

@ -45,6 +45,17 @@ public abstract class TrackingSocket
public CountDownLatch closeLatch = new CountDownLatch(1);
public CountDownLatch dataLatch = new CountDownLatch(1);
protected void addError(Throwable t)
{
LOG.warn(t);
errorQueue.add(t);
}
protected void addEvent(String format, Object... args)
{
eventQueue.add(String.format(format,args));
}
public void assertClose(CloseCode expectedCode, String expectedReason) throws InterruptedException
{
assertCloseCode(expectedCode);
@ -63,14 +74,10 @@ public abstract class TrackingSocket
Assert.assertThat("Close Reason",closeReason.getReasonPhrase(),is(expectedReason));
}
protected void addEvent(String format, Object... args)
public void assertEvent(String expected)
{
eventQueue.add(String.format(format,args));
}
protected void addError(Throwable t)
{
errorQueue.add(t);
String actual = eventQueue.poll();
Assert.assertEquals("Event",expected,actual);
}
public void assertIsOpen() throws InterruptedException
@ -79,12 +86,6 @@ public abstract class TrackingSocket
assertNotClosed();
}
public void assertEvent(String expected)
{
String actual = eventQueue.poll();
Assert.assertEquals("Event",expected,actual);
}
public void assertNotClosed()
{
Assert.assertThat("Closed Latch",closeLatch.getCount(),greaterThanOrEqualTo(1L));

View File

@ -23,7 +23,7 @@ import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.websocket.jsr356.server.TrackingSocket;
@ServerEndpoint(value="/basic")
@ServerEndpoint(value = "/basic")
public class BasicCloseSocket extends TrackingSocket
{
@OnClose

View File

@ -74,7 +74,7 @@ public class BadNetworkTest
@Test
public void testAbruptClientClose() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket,wsUri);
@ -103,7 +103,7 @@ public class BadNetworkTest
@Test
public void testAbruptServerClose() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket,wsUri);

View File

@ -55,7 +55,7 @@ public class ClientConnectTest
private WebSocketClient client;
@SuppressWarnings("unchecked")
private <E extends Throwable> E assertExpectedError(ExecutionException e, TrackingSocket wsocket, Class<E> errorClass) throws IOException
private <E extends Throwable> E assertExpectedError(ExecutionException e, JettyTrackingSocket wsocket, Class<E> errorClass) throws IOException
{
// Validate thrown cause
Throwable cause = e.getCause();
@ -104,7 +104,7 @@ public class ClientConnectTest
@Test
public void testBadHandshake() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket,wsUri);
@ -133,7 +133,7 @@ public class ClientConnectTest
@Test
public void testBadHandshake_GetOK() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket,wsUri);
@ -162,7 +162,7 @@ public class ClientConnectTest
@Test
public void testBadHandshake_GetOK_WithSecWebSocketAccept() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket,wsUri);
@ -198,7 +198,7 @@ public class ClientConnectTest
@Test
public void testBadHandshake_SwitchingProtocols_InvalidConnectionHeader() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket,wsUri);
@ -234,7 +234,7 @@ public class ClientConnectTest
@Test
public void testBadHandshake_SwitchingProtocols_NoConnectionHeader() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket,wsUri);
@ -270,7 +270,7 @@ public class ClientConnectTest
@Test
public void testBadUpgrade() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket,wsUri);
@ -300,7 +300,7 @@ public class ClientConnectTest
@Ignore("Opened bug 399525")
public void testConnectionNotAccepted() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket,wsUri);
@ -330,7 +330,7 @@ public class ClientConnectTest
@Test
public void testConnectionRefused() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
// Intentionally bad port with nothing listening on it
URI wsUri = new URI("ws://127.0.0.1:1");
@ -359,7 +359,7 @@ public class ClientConnectTest
@Test(expected = TimeoutException.class)
public void testConnectionTimeout_Concurrent() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket,wsUri);

View File

@ -35,9 +35,9 @@ import org.junit.Assert;
/**
* Testing Socket used on client side WebSocket testing.
*/
public class TrackingSocket extends WebSocketAdapter
public class JettyTrackingSocket extends WebSocketAdapter
{
private static final Logger LOG = Log.getLogger(TrackingSocket.class);
private static final Logger LOG = Log.getLogger(JettyTrackingSocket.class);
public int closeCode = -1;
public Exchanger<String> messageExchanger;

View File

@ -75,7 +75,7 @@ public class SlowClientTest
@Slow
public void testClientSlowToSend() throws Exception
{
TrackingSocket tsocket = new TrackingSocket();
JettyTrackingSocket tsocket = new JettyTrackingSocket();
client.getPolicy().setIdleTimeout(60000);
URI wsUri = server.getWsUri();

View File

@ -76,7 +76,7 @@ public class SlowServerTest
@Slow
public void testServerSlowToRead() throws Exception
{
TrackingSocket tsocket = new TrackingSocket();
JettyTrackingSocket tsocket = new JettyTrackingSocket();
client.setMasker(new ZeroMasker());
client.getPolicy().setIdleTimeout(60000);
@ -125,7 +125,7 @@ public class SlowServerTest
public void testServerSlowToSend() throws Exception
{
// final Exchanger<String> exchanger = new Exchanger<String>();
TrackingSocket tsocket = new TrackingSocket();
JettyTrackingSocket tsocket = new JettyTrackingSocket();
// tsocket.messageExchanger = exchanger;
client.setMasker(new ZeroMasker());
client.getPolicy().setIdleTimeout(60000);

View File

@ -80,7 +80,7 @@ public class TimeoutTest
@Test
public void testIdleDetectedByClient() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
client.setMaxIdleTimeout(1000);

View File

@ -85,7 +85,7 @@ public class WebSocketClientBadUriTest
@Test
public void testBadURI() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
try
{

View File

@ -75,7 +75,7 @@ public class WebSocketClientTest
@Test(expected = IllegalArgumentException.class)
public void testAddExtension_NotInstalled() throws Exception
{
TrackingSocket cliSock = new TrackingSocket();
JettyTrackingSocket cliSock = new JettyTrackingSocket();
client.getPolicy().setIdleTimeout(10000);
@ -91,7 +91,7 @@ public class WebSocketClientTest
@Test
public void testBasicEcho_FromClient() throws Exception
{
TrackingSocket cliSock = new TrackingSocket();
JettyTrackingSocket cliSock = new JettyTrackingSocket();
client.getPolicy().setIdleTimeout(10000);
@ -125,7 +125,7 @@ public class WebSocketClientTest
@Test
public void testBasicEcho_FromServer() throws Exception
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
Future<Session> future = client.connect(wsocket,server.getWsUri());
// Server
@ -157,7 +157,7 @@ public class WebSocketClientTest
fact.start();
try
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket,wsUri);
@ -197,7 +197,7 @@ public class WebSocketClientTest
{
int bufferSize = 512;
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket,wsUri);
@ -235,7 +235,7 @@ public class WebSocketClientTest
fact.start();
try
{
TrackingSocket wsocket = new TrackingSocket();
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri().resolve("/test?snack=cashews&amount=handful&brand=off");
Future<Session> future = client.connect(wsocket,wsUri);