From a5bddddac295b08dcd6b510f20e1890f38fa8227 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 10 Sep 2014 19:03:37 +0300 Subject: [PATCH] INT-3515: Fix Some WebSockets Issues JIRA: https://jira.spring.io/browse/INT-3515 Change test suite to the Tomcat according IO INT-3515: IllegalStateException for the `WebSocketInboundChannelAdapter`, when `useBroker = true`, but there is no Broker Relay in the Context --- build.gradle | 12 +-- .../IntegrationWebSocketContainer.java | 4 + .../ClientWebSocketContainerParser.java | 8 +- .../WebSocketInboundChannelAdapter.java | 7 +- .../support/SubProtocolHandlerRegistry.java | 3 +- .../ClientWebSocketContainerTests.java | 10 +- .../websocket/JettyWebSocketTestServer.java | 75 ------------- .../websocket/TomcatWebSocketTestServer.java | 101 ++++++++++++++++++ .../client/StompIntegrationTests.java | 13 +-- .../client/WebSocketClientTests.java | 10 +- .../config/WebSocketParserTests-context.xml | 4 + .../config/WebSocketParserTests.java | 17 +++ .../WebSocketInboundChannelAdapterTests.java | 9 +- .../WebSocketOutboundMessageHandlerTests.java | 9 +- .../server/WebSocketServerTests.java | 33 +++++- .../SubProtocolHandlerRegistryTests.java | 6 +- 16 files changed, 196 insertions(+), 125 deletions(-) delete mode 100644 spring-integration-websocket/src/test/java/org/springframework/integration/websocket/JettyWebSocketTestServer.java create mode 100644 spring-integration-websocket/src/test/java/org/springframework/integration/websocket/TomcatWebSocketTestServer.java diff --git a/build.gradle b/build.gradle index fd46575887..0ef2345035 100644 --- a/build.gradle +++ b/build.gradle @@ -108,6 +108,7 @@ subprojects { subproject -> saajImplVersion = '1.3.23' servletApiVersion = '3.1.0' slf4jVersion = "1.7.6" + tomcatVersion = "7.0.55" smack3Version = '3.2.1' smackVersion = '4.0.0' springAmqpVersion = project.hasProperty('springAmqpVersion') ? project.springAmqpVersion : '1.4.0.M1' @@ -565,15 +566,8 @@ project('spring-integration-websocket') { compile ("org.springframework:spring-webmvc:$springVersion", optional) - testCompile("org.eclipse.jetty:jetty-webapp:$jettyVersion") { - exclude group: "javax.servlet", module: "javax.servlet" - } - testCompile("org.eclipse.jetty.websocket:websocket-server:$jettyVersion") { - exclude group: "javax.servlet", module: "javax.servlet" - } - testCompile "org.eclipse.jetty.websocket:websocket-client:$jettyVersion" - testCompile"org.eclipse.jetty:jetty-client:$jettyVersion" - testCompile "org.slf4j:slf4j-jcl:$slf4jVersion" + testCompile "org.apache.tomcat.embed:tomcat-embed-websocket:$tomcatVersion" + testCompile("org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}") } } diff --git a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/IntegrationWebSocketContainer.java b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/IntegrationWebSocketContainer.java index 9a6d6f0954..ac814fe94d 100644 --- a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/IntegrationWebSocketContainer.java +++ b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/IntegrationWebSocketContainer.java @@ -116,6 +116,10 @@ public abstract class IntegrationWebSocketContainer implements ApplicationEventP return Collections.unmodifiableList(protocols); } + public Map getSessions() { + return Collections.unmodifiableMap(this.sessions); + } + public WebSocketSession getSession(String sessionId) throws Exception { WebSocketSession session = this.sessions.get(sessionId); Assert.notNull(session, "Session not found for id '" + sessionId + "'"); diff --git a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/config/ClientWebSocketContainerParser.java b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/config/ClientWebSocketContainerParser.java index c42498ab37..2946a8850c 100644 --- a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/config/ClientWebSocketContainerParser.java +++ b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/config/ClientWebSocketContainerParser.java @@ -50,11 +50,9 @@ public class ClientWebSocketContainerParser extends AbstractSingleBeanDefinition @Override protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { builder.addConstructorArgReference(element.getAttribute("client")) - .addConstructorArgValue(element.getAttribute("uri")); - String uriVariables = element.getAttribute("uri-variables"); - if (StringUtils.hasText(uriVariables)) { - builder.addConstructorArgValue(StringUtils.commaDelimitedListToStringArray(uriVariables)); - } + .addConstructorArgValue(element.getAttribute("uri")) + .addConstructorArgValue(StringUtils.commaDelimitedListToStringArray(element.getAttribute("uri-variables"))); + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "send-buffer-size-limit"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "send-time-limit"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "origin"); diff --git a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/inbound/WebSocketInboundChannelAdapter.java b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/inbound/WebSocketInboundChannelAdapter.java index c0413c8b26..626509018e 100644 --- a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/inbound/WebSocketInboundChannelAdapter.java +++ b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/inbound/WebSocketInboundChannelAdapter.java @@ -189,10 +189,9 @@ public class WebSocketInboundChannelAdapter extends MessageProducerSupport imple break; } } - if (this.brokerHandler == null) { - logger.warn("'AbstractBrokerMessageHandler' isn't present in the application context. " + - "The non-MESSAGE WebSocketMessages will be ignored."); - } + Assert.state(this.brokerHandler != null, + "WebSocket Broker Relay isn't present in the application context; " + + "it is required when 'useBroker = true'."); } } diff --git a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/support/SubProtocolHandlerRegistry.java b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/support/SubProtocolHandlerRegistry.java index 02aee9f855..63801eda76 100644 --- a/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/support/SubProtocolHandlerRegistry.java +++ b/spring-integration-websocket/src/main/java/org/springframework/integration/websocket/support/SubProtocolHandlerRegistry.java @@ -27,6 +27,7 @@ import org.apache.commons.logging.LogFactory; import org.springframework.messaging.Message; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.messaging.SubProtocolHandler; @@ -108,7 +109,7 @@ public final class SubProtocolHandlerRegistry { public SubProtocolHandler findProtocolHandler(WebSocketSession session) { SubProtocolHandler handler; String protocol = session.getAcceptedProtocol(); - if (protocol != null) { + if (StringUtils.hasText(protocol)) { handler = this.protocolHandlers.get(protocol); Assert.state(handler != null, "No handler for sub-protocol '" + protocol + "', handlers = " + this.protocolHandlers); diff --git a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/ClientWebSocketContainerTests.java b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/ClientWebSocketContainerTests.java index 4ce3e296c0..96378e1802 100644 --- a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/ClientWebSocketContainerTests.java +++ b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/ClientWebSocketContainerTests.java @@ -25,7 +25,6 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import java.nio.ByteBuffer; import java.util.Collections; import java.util.List; import java.util.concurrent.CountDownLatch; @@ -40,7 +39,7 @@ import org.springframework.web.socket.PingMessage; import org.springframework.web.socket.PongMessage; import org.springframework.web.socket.WebSocketMessage; import org.springframework.web.socket.WebSocketSession; -import org.springframework.web.socket.client.jetty.JettyWebSocketClient; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; /** * @author Artem Bilan @@ -48,7 +47,7 @@ import org.springframework.web.socket.client.jetty.JettyWebSocketClient; */ public class ClientWebSocketContainerTests { - private final static JettyWebSocketTestServer server = new JettyWebSocketTestServer(TestServerConfig.class); + private final static TomcatWebSocketTestServer server = new TomcatWebSocketTestServer(TestServerConfig.class); @BeforeClass public static void setup() throws Exception { @@ -63,7 +62,7 @@ public class ClientWebSocketContainerTests { @Test public void testClientWebSocketContainer() throws Exception { ClientWebSocketContainer container = - new ClientWebSocketContainer(new JettyWebSocketClient(), server.getWsBaseUrl() + "/ws/websocket"); + new ClientWebSocketContainer(new StandardWebSocketClient(), server.getWsBaseUrl() + "/ws/websocket"); TestWebSocketListener messageListener = new TestWebSocketListener(); container.setMessageListener(messageListener); @@ -75,8 +74,7 @@ public class ClientWebSocketContainerTests { assertTrue(session.isOpen()); assertEquals("v10.stomp", session.getAcceptedProtocol()); - //TODO Jetty Server treats empty ByteBuffer as 'null' for PongMessage - session.sendMessage(new PingMessage(ByteBuffer.wrap("ping".getBytes()))); + session.sendMessage(new PingMessage()); assertTrue(messageListener.messageLatch.await(10, TimeUnit.SECONDS)); diff --git a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/JettyWebSocketTestServer.java b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/JettyWebSocketTestServer.java deleted file mode 100644 index d770556af8..0000000000 --- a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/JettyWebSocketTestServer.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2014 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.integration.websocket; - -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.servlet.ServletContextHandler; -import org.eclipse.jetty.servlet.ServletHolder; - -import org.springframework.beans.factory.DisposableBean; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.util.SocketUtils; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; -import org.springframework.web.servlet.DispatcherServlet; - -/** - * @author Rossen Stoyanchev - * @since 4.1 - */ -public class JettyWebSocketTestServer implements InitializingBean, DisposableBean { - - private final Server jettyServer; - - private final int port; - - private final AnnotationConfigWebApplicationContext serverContext; - - public JettyWebSocketTestServer(Class... serverConfigs) { - this.port = SocketUtils.findAvailableTcpPort(); - this.jettyServer = new Server(this.port); - this.serverContext = new AnnotationConfigWebApplicationContext(); - this.serverContext.register(serverConfigs); - this.serverContext.refresh(); - - ServletContextHandler contextHandler = new ServletContextHandler(); - ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(this.serverContext)); - contextHandler.addServlet(servletHolder, "/"); - this.jettyServer.setHandler(contextHandler); - } - - public AnnotationConfigWebApplicationContext getServerContext() { - return this.serverContext; - } - - public String getWsBaseUrl() { - return "ws://localhost:" + this.port; - } - - @Override - public void afterPropertiesSet() throws Exception { - this.jettyServer.start(); - } - - @Override - public void destroy() throws Exception { - if (this.jettyServer.isRunning()) { - this.jettyServer.setStopTimeout(0); - this.jettyServer.stop(); - } - } - -} diff --git a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/TomcatWebSocketTestServer.java b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/TomcatWebSocketTestServer.java new file mode 100644 index 0000000000..43ff1099ac --- /dev/null +++ b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/TomcatWebSocketTestServer.java @@ -0,0 +1,101 @@ +/* + * Copyright 2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.websocket; + +import java.io.File; +import java.io.IOException; + +import org.apache.catalina.Context; +import org.apache.catalina.connector.Connector; +import org.apache.catalina.startup.Tomcat; +import org.apache.coyote.http11.Http11NioProtocol; +import org.apache.tomcat.websocket.server.WsContextListener; + +import org.springframework.beans.factory.DisposableBean; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.SocketUtils; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +/** + * @author Rossen Stoyanchev + * @author Artem Bilan + * @since 4.1 + */ +public class TomcatWebSocketTestServer implements InitializingBean, DisposableBean { + + private final Tomcat tomcatServer; + + private final int port; + + private final AnnotationConfigWebApplicationContext serverContext; + + public TomcatWebSocketTestServer(Class... serverConfigs) { + this.port = SocketUtils.findAvailableTcpPort(); + Connector connector = new Connector(Http11NioProtocol.class.getName()); + connector.setPort(this.port); + + File baseDir = createTempDir("tomcat"); + String baseDirPath = baseDir.getAbsolutePath(); + + this.tomcatServer = new Tomcat(); + this.tomcatServer.setBaseDir(baseDirPath); + this.tomcatServer.setPort(this.port); + this.tomcatServer.getService().addConnector(connector); + this.tomcatServer.setConnector(connector); + + this.serverContext = new AnnotationConfigWebApplicationContext(); + this.serverContext.register(serverConfigs); + + Context context = this.tomcatServer.addContext("", System.getProperty("java.io.tmpdir")); + context.addApplicationListener(WsContextListener.class.getName()); + Tomcat.addServlet(context, "dispatcherServlet", new DispatcherServlet(this.serverContext)).setAsyncSupported(true); + context.addServletMapping("/", "dispatcherServlet"); + } + + private File createTempDir(String prefix) { + try { + File tempFolder = File.createTempFile(prefix + ".", "." + this.port); + tempFolder.delete(); + tempFolder.mkdir(); + tempFolder.deleteOnExit(); + return tempFolder; + } + catch (IOException ex) { + throw new RuntimeException("Unable to create temp directory", ex); + } + } + + public AnnotationConfigWebApplicationContext getServerContext() { + return this.serverContext; + } + + public String getWsBaseUrl() { + return "ws://localhost:" + this.port; + } + + @Override + public void afterPropertiesSet() throws Exception { + this.tomcatServer.start(); + } + + @Override + public void destroy() throws Exception { + this.tomcatServer.stop(); + } + +} diff --git a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/client/StompIntegrationTests.java b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/client/StompIntegrationTests.java index eda93c8c71..a26fa5440a 100644 --- a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/client/StompIntegrationTests.java +++ b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/client/StompIntegrationTests.java @@ -52,7 +52,7 @@ import org.springframework.integration.core.MessageProducer; import org.springframework.integration.transformer.ExpressionEvaluatingTransformer; import org.springframework.integration.websocket.ClientWebSocketContainer; import org.springframework.integration.websocket.IntegrationWebSocketContainer; -import org.springframework.integration.websocket.JettyWebSocketTestServer; +import org.springframework.integration.websocket.TomcatWebSocketTestServer; import org.springframework.integration.websocket.inbound.WebSocketInboundChannelAdapter; import org.springframework.integration.websocket.outbound.WebSocketOutboundMessageHandler; import org.springframework.integration.websocket.support.SubProtocolHandlerRegistry; @@ -72,12 +72,13 @@ import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.socket.client.jetty.JettyWebSocketClient; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.messaging.StompSubProtocolHandler; import org.springframework.web.socket.messaging.SubProtocolHandler; -import org.springframework.web.socket.server.jetty.JettyRequestUpgradeStrategy; +import org.springframework.web.socket.server.standard.TomcatRequestUpgradeStrategy; import org.springframework.web.socket.server.support.DefaultHandshakeHandler; /** @@ -247,13 +248,13 @@ public class StompIntegrationTests { public static class ContextConfiguration { @Bean - public JettyWebSocketTestServer server() { - return new JettyWebSocketTestServer(ServerConfig.class); + public TomcatWebSocketTestServer server() { + return new TomcatWebSocketTestServer(ServerConfig.class); } @Bean public IntegrationWebSocketContainer clientWebSocketContainer() { - return new ClientWebSocketContainer(new JettyWebSocketClient(), server().getWsBaseUrl() + "/ws/websocket"); + return new ClientWebSocketContainer(new StandardWebSocketClient(), server().getWsBaseUrl() + "/ws/websocket"); } @Bean @@ -370,7 +371,7 @@ public class StompIntegrationTests { @Bean public DefaultHandshakeHandler handshakeHandler() { - return new DefaultHandshakeHandler(new JettyRequestUpgradeStrategy()); + return new DefaultHandshakeHandler(new TomcatRequestUpgradeStrategy()); } @Override diff --git a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/client/WebSocketClientTests.java b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/client/WebSocketClientTests.java index 11ce949a1a..932b1bdc65 100644 --- a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/client/WebSocketClientTests.java +++ b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/client/WebSocketClientTests.java @@ -40,8 +40,8 @@ import org.springframework.integration.core.MessageProducer; import org.springframework.integration.transformer.ObjectToStringTransformer; import org.springframework.integration.websocket.ClientWebSocketContainer; import org.springframework.integration.websocket.IntegrationWebSocketContainer; -import org.springframework.integration.websocket.JettyWebSocketTestServer; import org.springframework.integration.websocket.TestServerConfig; +import org.springframework.integration.websocket.TomcatWebSocketTestServer; import org.springframework.integration.websocket.inbound.WebSocketInboundChannelAdapter; import org.springframework.integration.websocket.outbound.WebSocketOutboundMessageHandler; import org.springframework.integration.websocket.support.SubProtocolHandlerRegistry; @@ -56,7 +56,7 @@ import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.socket.client.WebSocketClient; -import org.springframework.web.socket.client.jetty.JettyWebSocketClient; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; import org.springframework.web.socket.messaging.StompSubProtocolHandler; import org.springframework.web.socket.messaging.SubProtocolHandler; import org.springframework.web.socket.sockjs.client.SockJsClient; @@ -99,13 +99,13 @@ public class WebSocketClientTests { public static class ContextConfiguration { @Bean - public JettyWebSocketTestServer server() { - return new JettyWebSocketTestServer(ServerFlowConfig.class); + public TomcatWebSocketTestServer server() { + return new TomcatWebSocketTestServer(ServerFlowConfig.class); } @Bean public WebSocketClient webSocketClient() { - return new SockJsClient(Collections.singletonList(new WebSocketTransport(new JettyWebSocketClient()))); + return new SockJsClient(Collections.singletonList(new WebSocketTransport(new StandardWebSocketClient()))); } @Bean diff --git a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/config/WebSocketParserTests-context.xml b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/config/WebSocketParserTests-context.xml index c4240a4557..d6438fe662 100644 --- a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/config/WebSocketParserTests-context.xml +++ b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/config/WebSocketParserTests-context.xml @@ -101,4 +101,8 @@ + + diff --git a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/config/WebSocketParserTests.java b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/config/WebSocketParserTests.java index 2e3f9af0c5..abb0d98931 100644 --- a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/config/WebSocketParserTests.java +++ b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/config/WebSocketParserTests.java @@ -104,6 +104,10 @@ public class WebSocketParserTests { @Qualifier("clientWebSocketContainer") private IntegrationWebSocketContainer clientWebSocketContainer; + @Autowired + @Qualifier("simpleClientWebSocketContainer") + private IntegrationWebSocketContainer simpleClientWebSocketContainer; + @Autowired @Qualifier("customInboundAdapter") private WebSocketInboundChannelAdapter customInboundAdapter; @@ -235,6 +239,19 @@ public class WebSocketParserTests { WebSocketHttpHeaders.class); assertEquals("FOO", headers.getOrigin()); assertEquals(Arrays.asList("BAR", "baz"), headers.get("FOO")); + + assertEquals(10 * 1000, TestUtils.getPropertyValue(this.simpleClientWebSocketContainer, "sendTimeLimit")); + assertEquals(512 * 1024, TestUtils.getPropertyValue(this.simpleClientWebSocketContainer, "sendBufferSizeLimit")); + assertEquals(new URI("ws://foo.bar"), + TestUtils.getPropertyValue(this.simpleClientWebSocketContainer, "connectionManager.uri", URI.class)); + assertSame(this.webSocketClient, + TestUtils.getPropertyValue(this.simpleClientWebSocketContainer, "connectionManager.client")); + assertEquals(Integer.MAX_VALUE, + TestUtils.getPropertyValue(this.simpleClientWebSocketContainer, "connectionManager.phase")); + assertFalse(TestUtils.getPropertyValue(this.simpleClientWebSocketContainer, + "connectionManager.autoStartup", Boolean.class)); + assertTrue(TestUtils.getPropertyValue(this.simpleClientWebSocketContainer, "headers", + WebSocketHttpHeaders.class).isEmpty()); } @Test diff --git a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/inbound/WebSocketInboundChannelAdapterTests.java b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/inbound/WebSocketInboundChannelAdapterTests.java index cd794fef25..84ac012064 100644 --- a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/inbound/WebSocketInboundChannelAdapterTests.java +++ b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/inbound/WebSocketInboundChannelAdapterTests.java @@ -41,7 +41,7 @@ import org.springframework.integration.core.MessageProducer; import org.springframework.integration.test.util.TestUtils; import org.springframework.integration.websocket.ClientWebSocketContainer; import org.springframework.integration.websocket.IntegrationWebSocketContainer; -import org.springframework.integration.websocket.JettyWebSocketTestServer; +import org.springframework.integration.websocket.TomcatWebSocketTestServer; import org.springframework.integration.websocket.TestServerConfig; import org.springframework.integration.websocket.support.SubProtocolHandlerRegistry; import org.springframework.messaging.Message; @@ -55,6 +55,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.client.WebSocketClient; import org.springframework.web.socket.client.jetty.JettyWebSocketClient; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; import org.springframework.web.socket.messaging.StompSubProtocolHandler; import org.springframework.web.socket.messaging.SubProtocolHandler; import org.springframework.web.socket.messaging.SubProtocolWebSocketHandler; @@ -124,13 +125,13 @@ public class WebSocketInboundChannelAdapterTests { public static class ContextConfiguration { @Bean - public JettyWebSocketTestServer server() { - return new JettyWebSocketTestServer(TestServerConfig.class); + public TomcatWebSocketTestServer server() { + return new TomcatWebSocketTestServer(TestServerConfig.class); } @Bean public WebSocketClient webSocketClient() { - return new SockJsClient(Collections.singletonList(new WebSocketTransport(new JettyWebSocketClient()))); + return new SockJsClient(Collections.singletonList(new WebSocketTransport(new StandardWebSocketClient()))); } @Bean diff --git a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/outbound/WebSocketOutboundMessageHandlerTests.java b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/outbound/WebSocketOutboundMessageHandlerTests.java index cb6507c3bf..3e70cba401 100644 --- a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/outbound/WebSocketOutboundMessageHandlerTests.java +++ b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/outbound/WebSocketOutboundMessageHandlerTests.java @@ -36,7 +36,7 @@ import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.config.EnableIntegration; import org.springframework.integration.websocket.ClientWebSocketContainer; import org.springframework.integration.websocket.IntegrationWebSocketContainer; -import org.springframework.integration.websocket.JettyWebSocketTestServer; +import org.springframework.integration.websocket.TomcatWebSocketTestServer; import org.springframework.integration.websocket.TestServerConfig; import org.springframework.integration.websocket.support.SubProtocolHandlerRegistry; import org.springframework.messaging.Message; @@ -49,6 +49,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.web.socket.client.WebSocketClient; import org.springframework.web.socket.client.jetty.JettyWebSocketClient; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; import org.springframework.web.socket.messaging.StompSubProtocolHandler; import org.springframework.web.socket.messaging.SubProtocolHandler; import org.springframework.web.socket.sockjs.client.SockJsClient; @@ -101,13 +102,13 @@ public class WebSocketOutboundMessageHandlerTests { public static class ContextConfiguration { @Bean - public JettyWebSocketTestServer server() { - return new JettyWebSocketTestServer(TestServerConfig.class); + public TomcatWebSocketTestServer server() { + return new TomcatWebSocketTestServer(TestServerConfig.class); } @Bean public WebSocketClient webSocketClient() { - return new SockJsClient(Collections.singletonList(new WebSocketTransport(new JettyWebSocketClient()))); + return new SockJsClient(Collections.singletonList(new WebSocketTransport(new StandardWebSocketClient()))); } @Bean diff --git a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/server/WebSocketServerTests.java b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/server/WebSocketServerTests.java index ad74812d59..b6d1b91f79 100644 --- a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/server/WebSocketServerTests.java +++ b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/server/WebSocketServerTests.java @@ -16,11 +16,13 @@ package org.springframework.integration.websocket.server; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.instanceOf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; import java.nio.ByteBuffer; import java.util.Collections; @@ -28,10 +30,13 @@ import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.expression.spel.standard.SpelExpressionParser; @@ -44,8 +49,8 @@ import org.springframework.integration.core.MessageProducer; import org.springframework.integration.transformer.ExpressionEvaluatingTransformer; import org.springframework.integration.websocket.ClientWebSocketContainer; import org.springframework.integration.websocket.IntegrationWebSocketContainer; -import org.springframework.integration.websocket.JettyWebSocketTestServer; import org.springframework.integration.websocket.ServerWebSocketContainer; +import org.springframework.integration.websocket.TomcatWebSocketTestServer; import org.springframework.integration.websocket.inbound.WebSocketInboundChannelAdapter; import org.springframework.integration.websocket.outbound.WebSocketOutboundMessageHandler; import org.springframework.integration.websocket.support.SubProtocolHandlerRegistry; @@ -64,7 +69,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.util.MultiValueMap; import org.springframework.web.socket.client.WebSocketClient; -import org.springframework.web.socket.client.jetty.JettyWebSocketClient; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; @@ -130,19 +135,37 @@ public class WebSocketServerTests { assertEquals("subs1", subscription.get(0)); } + @Test + public void testBrokerIsNotPresented() throws Exception { + WebSocketInboundChannelAdapter webSocketInboundChannelAdapter = + new WebSocketInboundChannelAdapter(Mockito.mock(ServerWebSocketContainer.class)); + webSocketInboundChannelAdapter.setOutputChannel(new DirectChannel()); + webSocketInboundChannelAdapter.setUseBroker(true); + webSocketInboundChannelAdapter.setBeanFactory(Mockito.mock(BeanFactory.class)); + webSocketInboundChannelAdapter.setApplicationContext(Mockito.mock(ApplicationContext.class)); + try { + webSocketInboundChannelAdapter.afterPropertiesSet(); + fail("IllegalStateException expected"); + } + catch (Exception e) { + assertThat(e, instanceOf(IllegalStateException.class)); + assertThat(e.getMessage(), containsString("WebSocket Broker Relay isn't present in the application context;")); + } + + } @Configuration @EnableIntegration public static class ContextConfiguration { @Bean - public JettyWebSocketTestServer server() { - return new JettyWebSocketTestServer(ServerConfig.class); + public TomcatWebSocketTestServer server() { + return new TomcatWebSocketTestServer(ServerConfig.class); } @Bean public WebSocketClient webSocketClient() { - return new SockJsClient(Collections.singletonList(new WebSocketTransport(new JettyWebSocketClient()))); + return new SockJsClient(Collections.singletonList(new WebSocketTransport(new StandardWebSocketClient()))); } @Bean diff --git a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/support/SubProtocolHandlerRegistryTests.java b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/support/SubProtocolHandlerRegistryTests.java index ffca3afa13..c93b969084 100644 --- a/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/support/SubProtocolHandlerRegistryTests.java +++ b/spring-integration-websocket/src/test/java/org/springframework/integration/websocket/support/SubProtocolHandlerRegistryTests.java @@ -87,7 +87,7 @@ public class SubProtocolHandlerRegistryTests { SubProtocolHandlerRegistry subProtocolHandlerRegistry = new SubProtocolHandlerRegistry(testProtocolHandler); WebSocketSession session = mock(WebSocketSession.class); - when(session.getAcceptedProtocol()).thenReturn("foo", (String) null); + when(session.getAcceptedProtocol()).thenReturn("foo", "", null); try { subProtocolHandlerRegistry.findProtocolHandler(session); @@ -101,6 +101,10 @@ public class SubProtocolHandlerRegistryTests { SubProtocolHandler protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session); assertNotNull(protocolHandler); assertSame(protocolHandler, testProtocolHandler); + + protocolHandler = subProtocolHandlerRegistry.findProtocolHandler(session); + assertNotNull(protocolHandler); + assertSame(protocolHandler, testProtocolHandler); } @Test