diff options
Diffstat (limited to 'spring-websocket/src/test/java/org/springframework/web')
10 files changed, 96 insertions, 89 deletions
diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/AbstractWebSocketIntegrationTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/AbstractWebSocketIntegrationTests.java index 7067ef25..2309f74f 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/AbstractWebSocketIntegrationTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/AbstractWebSocketIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,6 @@ import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -48,9 +47,7 @@ import org.springframework.web.socket.server.support.DefaultHandshakeHandler; */ public abstract class AbstractWebSocketIntegrationTests { - protected Log logger = LogFactory.getLog(getClass()); - - private static Map<Class<?>, Class<?>> upgradeStrategyConfigTypes = new HashMap<Class<?>, Class<?>>(); + private static Map<Class<?>, Class<?>> upgradeStrategyConfigTypes = new HashMap<>(); static { upgradeStrategyConfigTypes.put(JettyWebSocketTestServer.class, JettyUpgradeStrategyConfig.class); @@ -58,6 +55,7 @@ public abstract class AbstractWebSocketIntegrationTests { upgradeStrategyConfigTypes.put(UndertowTestServer.class, UndertowUpgradeStrategyConfig.class); } + @Rule public final TestName testName = new TestName(); @@ -67,12 +65,13 @@ public abstract class AbstractWebSocketIntegrationTests { @Parameter(1) public WebSocketClient webSocketClient; + protected final Log logger = LogFactory.getLog(getClass()); + protected AnnotationConfigWebApplicationContext wac; @Before public void setup() throws Exception { - logger.debug("Setting up '" + this.testName.getMethodName() + "', client=" + this.webSocketClient.getClass().getSimpleName() + ", server=" + this.server.getClass().getSimpleName()); @@ -155,6 +154,7 @@ public abstract class AbstractWebSocketIntegrationTests { } } + @Configuration static class TomcatUpgradeStrategyConfig extends AbstractRequestUpgradeStrategyConfig { @@ -164,6 +164,7 @@ public abstract class AbstractWebSocketIntegrationTests { } } + @Configuration static class UndertowUpgradeStrategyConfig extends AbstractRequestUpgradeStrategyConfig { diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/ContextLoaderTestUtils.java b/spring-websocket/src/test/java/org/springframework/web/socket/ContextLoaderTestUtils.java index ba56ae97..8853d824 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/ContextLoaderTestUtils.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/ContextLoaderTestUtils.java @@ -38,7 +38,8 @@ public class ContextLoaderTestUtils { public static void setCurrentWebApplicationContext(ClassLoader classLoader, WebApplicationContext applicationContext) { if (applicationContext != null) { currentContextPerThread.put(classLoader, applicationContext); - } else { + } + else { currentContextPerThread.remove(classLoader); } } diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/WebSocketIntegrationTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/WebSocketHandshakeTests.java index 1d86998d..813b7b5f 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/WebSocketIntegrationTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/WebSocketHandshakeTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,9 +46,10 @@ import static org.junit.Assert.*; * Client and server-side WebSocket integration tests. * * @author Rossen Stoyanchev + * @author Juergen Hoeller */ @RunWith(Parameterized.class) -public class WebSocketIntegrationTests extends AbstractWebSocketIntegrationTests { +public class WebSocketHandshakeTests extends AbstractWebSocketIntegrationTests { @Parameters(name = "server [{0}], client [{1}]") public static Iterable<Object[]> arguments() { @@ -62,7 +63,7 @@ public class WebSocketIntegrationTests extends AbstractWebSocketIntegrationTest @Override protected Class<?>[] getAnnotatedConfigClasses() { - return new Class<?>[] { TestConfig.class }; + return new Class<?>[] {TestConfig.class}; } @Test @@ -75,11 +76,8 @@ public class WebSocketIntegrationTests extends AbstractWebSocketIntegrationTest session.close(); } - // SPR-12727 - - @Test + @Test // SPR-12727 public void unsolicitedPongWithEmptyPayload() throws Exception { - String url = getWsBaseUrl() + "/ws"; WebSocketSession session = this.webSocketClient.doHandshake(new AbstractWebSocketHandler() {}, url).get(); @@ -126,7 +124,6 @@ public class WebSocketIntegrationTests extends AbstractWebSocketIntegrationTest private Throwable transportError; - public void setWaitMessageCount(int waitMessageCount) { this.waitMessageCount = waitMessageCount; } diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSessionTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSessionTests.java index 594f0828..a8377521 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSessionTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/jetty/JettyWebSocketSessionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,8 +22,9 @@ import java.util.Map; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.UpgradeRequest; import org.eclipse.jetty.websocket.api.UpgradeResponse; -import org.junit.Before; + import org.junit.Test; + import org.mockito.Mockito; import org.springframework.web.socket.handler.TestPrincipal; @@ -38,16 +39,11 @@ import static org.mockito.BDDMockito.*; */ public class JettyWebSocketSessionTests { - private Map<String,Object> attributes; - - - @Before - public void setup() { - this.attributes = new HashMap<>(); - } + private final Map<String, Object> attributes = new HashMap<>(); @Test + @SuppressWarnings("resource") public void getPrincipalWithConstructorArg() { TestPrincipal user = new TestPrincipal("joe"); JettyWebSocketSession session = new JettyWebSocketSession(attributes, user); @@ -56,8 +52,8 @@ public class JettyWebSocketSessionTests { } @Test + @SuppressWarnings("resource") public void getPrincipalFromNativeSession() { - TestPrincipal user = new TestPrincipal("joe"); UpgradeRequest request = Mockito.mock(UpgradeRequest.class); @@ -80,8 +76,8 @@ public class JettyWebSocketSessionTests { } @Test + @SuppressWarnings("resource") public void getPrincipalNotAvailable() { - UpgradeRequest request = Mockito.mock(UpgradeRequest.class); given(request.getUserPrincipal()).willReturn(null); @@ -102,8 +98,8 @@ public class JettyWebSocketSessionTests { } @Test + @SuppressWarnings("resource") public void getAcceptedProtocol() { - String protocol = "foo"; UpgradeRequest request = Mockito.mock(UpgradeRequest.class); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSessionTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSessionTests.java index edd17640..8c832aad 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSessionTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/adapter/standard/StandardWebSocketSessionTests.java @@ -1,4 +1,4 @@ -/* Copyright 2002-2014 the original author or authors. +/* Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,8 @@ import java.util.HashMap; import java.util.Map; import javax.websocket.Session; -import org.junit.Before; import org.junit.Test; + import org.mockito.Mockito; import org.springframework.http.HttpHeaders; @@ -36,19 +36,13 @@ import static org.mockito.BDDMockito.*; */ public class StandardWebSocketSessionTests { - private HttpHeaders headers; - - private Map<String,Object> attributes; + private final HttpHeaders headers = new HttpHeaders(); - - @Before - public void setup() { - this.headers = new HttpHeaders(); - this.attributes = new HashMap<>(); - } + private final Map<String, Object> attributes = new HashMap<>(); @Test + @SuppressWarnings("resource") public void getPrincipalWithConstructorArg() { TestPrincipal user = new TestPrincipal("joe"); StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null, user); @@ -57,8 +51,8 @@ public class StandardWebSocketSessionTests { } @Test + @SuppressWarnings("resource") public void getPrincipalWithNativeSession() { - TestPrincipal user = new TestPrincipal("joe"); Session nativeSession = Mockito.mock(Session.class); @@ -71,8 +65,8 @@ public class StandardWebSocketSessionTests { } @Test + @SuppressWarnings("resource") public void getPrincipalNone() { - Session nativeSession = Mockito.mock(Session.class); given(nativeSession.getUserPrincipal()).willReturn(null); @@ -86,8 +80,8 @@ public class StandardWebSocketSessionTests { } @Test + @SuppressWarnings("resource") public void getAcceptedProtocol() { - String protocol = "foo"; Session nativeSession = Mockito.mock(Session.class); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java index aa52922b..824cba62 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/config/MessageBrokerBeanDefinitionParserTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,6 +60,8 @@ import org.springframework.mock.web.test.MockServletContext; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.util.MimeTypeUtils; +import org.springframework.validation.Errors; +import org.springframework.validation.Validator; import org.springframework.web.HttpRequestHandler; import org.springframework.web.context.support.GenericWebApplicationContext; import org.springframework.web.servlet.HandlerMapping; @@ -356,6 +358,14 @@ public class MessageBrokerBeanDefinitionParserTests { public void customChannels() { loadBeanDefinitions("websocket-config-broker-customchannels.xml"); + SimpAnnotationMethodMessageHandler annotationMethodMessageHandler = + this.appContext.getBean(SimpAnnotationMethodMessageHandler.class); + + Validator validator = annotationMethodMessageHandler.getValidator(); + assertNotNull(validator); + assertSame(this.appContext.getBean("myValidator"), validator); + assertThat(validator, Matchers.instanceOf(TestValidator.class)); + List<Class<? extends MessageHandler>> subscriberTypes = Arrays.<Class<? extends MessageHandler>>asList(SimpAnnotationMethodMessageHandler.class, UserDestinationMessageHandler.class, SimpleBrokerMessageHandler.class); @@ -520,3 +530,13 @@ class TestWebSocketHandlerDecorator extends WebSocketHandlerDecorator { class TestStompErrorHandler extends StompSubProtocolErrorHandler { } + +class TestValidator implements Validator { + @Override + public boolean supports(Class<?> clazz) { + return false; + } + + @Override + public void validate(Object target, Errors errors) { } +} diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationTests.java index 1a66397b..f4aa0460 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/config/annotation/WebSocketConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -61,7 +61,7 @@ public class WebSocketConfigurationTests extends AbstractWebSocketIntegrationTes @Override protected Class<?>[] getAnnotatedConfigClasses() { - return new Class<?>[] { TestConfig.class }; + return new Class<?>[] {TestConfig.class}; } @Test diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/handler/ConcurrentWebSocketSessionDecoratorTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/handler/ConcurrentWebSocketSessionDecoratorTests.java index c4cdb02a..5af30742 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/handler/ConcurrentWebSocketSessionDecoratorTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/handler/ConcurrentWebSocketSessionDecoratorTests.java @@ -40,6 +40,7 @@ import static org.junit.Assert.*; @SuppressWarnings("resource") public class ConcurrentWebSocketSessionDecoratorTests { + @Test public void send() throws IOException { @@ -70,16 +71,13 @@ public class ConcurrentWebSocketSessionDecoratorTests { final ConcurrentWebSocketSessionDecorator concurrentSession = new ConcurrentWebSocketSessionDecorator(blockingSession, 10 * 1000, 1024); - Executors.newSingleThreadExecutor().submit(new Runnable() { - @Override - public void run() { - TextMessage textMessage = new TextMessage("slow message"); - try { - concurrentSession.sendMessage(textMessage); - } - catch (IOException e) { - e.printStackTrace(); - } + Executors.newSingleThreadExecutor().submit((Runnable) () -> { + TextMessage message = new TextMessage("slow message"); + try { + concurrentSession.sendMessage(message); + } + catch (IOException e) { + e.printStackTrace(); } }); @@ -90,7 +88,7 @@ public class ConcurrentWebSocketSessionDecoratorTests { assertTrue(concurrentSession.getTimeSinceSendStarted() > 0); TextMessage payload = new TextMessage("payload"); - for (int i=0; i < 5; i++) { + for (int i = 0; i < 5; i++) { concurrentSession.sendMessage(payload); } @@ -103,6 +101,7 @@ public class ConcurrentWebSocketSessionDecoratorTests { public void sendTimeLimitExceeded() throws IOException, InterruptedException { BlockingSession blockingSession = new BlockingSession(); + blockingSession.setId("123"); blockingSession.setOpen(true); CountDownLatch sentMessageLatch = blockingSession.getSentMessageLatch(); @@ -112,16 +111,13 @@ public class ConcurrentWebSocketSessionDecoratorTests { final ConcurrentWebSocketSessionDecorator concurrentSession = new ConcurrentWebSocketSessionDecorator(blockingSession, sendTimeLimit, bufferSizeLimit); - Executors.newSingleThreadExecutor().submit(new Runnable() { - @Override - public void run() { - TextMessage textMessage = new TextMessage("slow message"); - try { - concurrentSession.sendMessage(textMessage); - } - catch (IOException e) { - e.printStackTrace(); - } + Executors.newSingleThreadExecutor().submit((Runnable) () -> { + TextMessage message = new TextMessage("slow message"); + try { + concurrentSession.sendMessage(message); + } + catch (IOException e) { + e.printStackTrace(); } }); @@ -136,6 +132,9 @@ public class ConcurrentWebSocketSessionDecoratorTests { fail("Expected exception"); } catch (SessionLimitExceededException ex) { + String actual = ex.getMessage(); + String regex = "Message send time [\\d]+ \\(ms\\) for session '123' exceeded the allowed limit 100"; + assertTrue("Unexpected message: " + actual, actual.matches(regex)); assertEquals(CloseStatus.SESSION_NOT_RELIABLE, ex.getStatus()); } } @@ -144,6 +143,7 @@ public class ConcurrentWebSocketSessionDecoratorTests { public void sendBufferSizeExceeded() throws IOException, InterruptedException { BlockingSession blockingSession = new BlockingSession(); + blockingSession.setId("123"); blockingSession.setOpen(true); CountDownLatch sentMessageLatch = blockingSession.getSentMessageLatch(); @@ -153,23 +153,20 @@ public class ConcurrentWebSocketSessionDecoratorTests { final ConcurrentWebSocketSessionDecorator concurrentSession = new ConcurrentWebSocketSessionDecorator(blockingSession, sendTimeLimit, bufferSizeLimit); - Executors.newSingleThreadExecutor().submit(new Runnable() { - @Override - public void run() { - TextMessage textMessage = new TextMessage("slow message"); - try { - concurrentSession.sendMessage(textMessage); - } - catch (IOException e) { - e.printStackTrace(); - } + Executors.newSingleThreadExecutor().submit((Runnable) () -> { + TextMessage message = new TextMessage("slow message"); + try { + concurrentSession.sendMessage(message); + } + catch (IOException e) { + e.printStackTrace(); } }); assertTrue(sentMessageLatch.await(5, TimeUnit.SECONDS)); StringBuilder sb = new StringBuilder(); - for (int i=0 ; i < 1023; i++) { + for (int i = 0 ; i < 1023; i++) { sb.append("a"); } @@ -184,6 +181,10 @@ public class ConcurrentWebSocketSessionDecoratorTests { fail("Expected exception"); } catch (SessionLimitExceededException ex) { + String actual = ex.getMessage(); + String regex = "The send buffer size [\\d]+ bytes for session '123' exceeded the allowed limit 1024"; + assertTrue("Unexpected message: " + actual, actual.matches(regex)); + assertEquals(CloseStatus.SESSION_NOT_RELIABLE, ex.getStatus()); } } @@ -215,16 +216,13 @@ public class ConcurrentWebSocketSessionDecoratorTests { final ConcurrentWebSocketSessionDecorator concurrentSession = new ConcurrentWebSocketSessionDecorator(blockingSession, sendTimeLimit, bufferSizeLimit); - Executors.newSingleThreadExecutor().submit(new Runnable() { - @Override - public void run() { - TextMessage message = new TextMessage("slow message"); - try { - concurrentSession.sendMessage(message); - } - catch (IOException e) { - e.printStackTrace(); - } + Executors.newSingleThreadExecutor().submit((Runnable) () -> { + TextMessage message = new TextMessage("slow message"); + try { + concurrentSession.sendMessage(message); + } + catch (IOException e) { + e.printStackTrace(); } }); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransportTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransportTests.java index ca8f2c83..1337fcb2 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransportTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransportTests.java @@ -91,7 +91,7 @@ public class RestTemplateXhrTransportTests { @Test public void connectReceiveAndCloseWithPrelude() throws Exception { StringBuilder sb = new StringBuilder(2048); - for (int i=0; i < 2048; i++) { + for (int i = 0; i < 2048; i++) { sb.append('h'); } String body = sb.toString() + "\n" + "o\n" + "a[\"foo\"]\n" + "c[3000,\"Go away!\"]"; diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpSendingTransportHandlerTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpSendingTransportHandlerTests.java index 8b941778..41911429 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpSendingTransportHandlerTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/sockjs/transport/handler/HttpSendingTransportHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,6 @@ import java.sql.Date; import org.junit.Before; import org.junit.Test; - import org.springframework.scheduling.TaskScheduler; import org.springframework.web.socket.AbstractHttpRequestTests; import org.springframework.web.socket.WebSocketHandler; @@ -115,6 +114,7 @@ public class HttpSendingTransportHandlerTests extends AbstractHttpRequestTests setRequest("POST", "/"); if (callbackValue != null) { + // need to encode the query parameter this.servletRequest.setQueryString("c=" + UriUtils.encodeQueryParam(callbackValue, "UTF-8")); this.servletRequest.addParameter("c", callbackValue); } |