Add Tomcat WebSocket integration tests
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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,6 +18,8 @@ package org.springframework.web.socket;
|
||||
import java.util.HashMap;
|
||||
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.runners.Parameterized.Parameter;
|
||||
@@ -31,7 +32,7 @@ import org.springframework.web.socket.server.DefaultHandshakeHandler;
|
||||
import org.springframework.web.socket.server.HandshakeHandler;
|
||||
import org.springframework.web.socket.server.RequestUpgradeStrategy;
|
||||
import org.springframework.web.socket.server.support.JettyRequestUpgradeStrategy;
|
||||
|
||||
import org.springframework.web.socket.server.support.TomcatRequestUpgradeStrategy;
|
||||
|
||||
|
||||
/**
|
||||
@@ -41,10 +42,13 @@ import org.springframework.web.socket.server.support.JettyRequestUpgradeStrategy
|
||||
*/
|
||||
public abstract class AbstractWebSocketIntegrationTests {
|
||||
|
||||
protected Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private static Map<Class<?>, Class<?>> upgradeStrategyConfigTypes = new HashMap<Class<?>, Class<?>>();
|
||||
|
||||
static {
|
||||
upgradeStrategyConfigTypes.put(JettyTestServer.class, JettyUpgradeStrategyConfig.class);
|
||||
upgradeStrategyConfigTypes.put(TomcatTestServer.class, TomcatUpgradeStrategyConfig.class);
|
||||
}
|
||||
|
||||
@Parameter(0)
|
||||
@@ -62,12 +66,13 @@ public abstract class AbstractWebSocketIntegrationTests {
|
||||
this.wac = new AnnotationConfigWebApplicationContext();
|
||||
this.wac.register(getAnnotatedConfigClasses());
|
||||
this.wac.register(upgradeStrategyConfigTypes.get(this.server.getClass()));
|
||||
this.wac.refresh();
|
||||
|
||||
if (this.webSocketClient instanceof Lifecycle) {
|
||||
((Lifecycle) this.webSocketClient).start();
|
||||
}
|
||||
|
||||
this.server.init(this.wac);
|
||||
this.server.deployConfig(this.wac);
|
||||
this.server.start();
|
||||
}
|
||||
|
||||
@@ -80,9 +85,23 @@ public abstract class AbstractWebSocketIntegrationTests {
|
||||
((Lifecycle) this.webSocketClient).stop();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
catch (Throwable t) {
|
||||
logger.error("Failed to stop WebSocket client", t);
|
||||
}
|
||||
|
||||
try {
|
||||
this.server.undeployConfig();
|
||||
}
|
||||
catch (Throwable t) {
|
||||
logger.error("Failed to undeploy application config", t);
|
||||
}
|
||||
|
||||
try {
|
||||
this.server.stop();
|
||||
}
|
||||
catch (Throwable t) {
|
||||
logger.error("Failed to stop server", t);
|
||||
}
|
||||
}
|
||||
|
||||
protected String getWsBaseUrl() {
|
||||
@@ -110,4 +129,13 @@ public abstract class AbstractWebSocketIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TomcatUpgradeStrategyConfig extends AbstractRequestUpgradeStrategyConfig {
|
||||
|
||||
@Bean
|
||||
public RequestUpgradeStrategy requestUpgradeStrategy() {
|
||||
return new TomcatRequestUpgradeStrategy();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -47,10 +47,16 @@ public class JettyTestServer implements TestServer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(WebApplicationContext cxt) {
|
||||
ServletContextHandler handler = new ServletContextHandler();
|
||||
handler.addServlet(new ServletHolder(new DispatcherServlet(cxt)), "/");
|
||||
this.jettyServer.setHandler(handler);
|
||||
public void deployConfig(WebApplicationContext cxt) {
|
||||
ServletContextHandler contextHandler = new ServletContextHandler();
|
||||
ServletHolder servletHolder = new ServletHolder(new DispatcherServlet(cxt));
|
||||
contextHandler.addServlet(servletHolder, "/");
|
||||
this.jettyServer.setHandler(contextHandler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undeployConfig() {
|
||||
// Stopping jetty will undeploy the servlet
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -27,7 +27,9 @@ public interface TestServer {
|
||||
|
||||
int getPort();
|
||||
|
||||
void init(WebApplicationContext cxt);
|
||||
void deployConfig(WebApplicationContext cxt);
|
||||
|
||||
void undeployConfig();
|
||||
|
||||
void start() throws Exception;
|
||||
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.web.socket;
|
||||
|
||||
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.util.descriptor.web.ApplicationListener;
|
||||
import org.apache.tomcat.websocket.server.WsListener;
|
||||
import org.springframework.core.NestedRuntimeException;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.SocketUtils;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
|
||||
/**
|
||||
* Tomcat based {@link TestServer}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class TomcatTestServer implements TestServer {
|
||||
|
||||
private static final ApplicationListener WS_APPLICATION_LISTENER =
|
||||
new ApplicationListener(WsListener.class.getName(), false);
|
||||
|
||||
private final Tomcat tomcatServer;
|
||||
|
||||
private final int port;
|
||||
|
||||
private Context context;
|
||||
|
||||
|
||||
public TomcatTestServer() {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
private File createTempDir(String prefix) {
|
||||
try {
|
||||
File tempFolder = File.createTempFile(prefix + ".", "." + getPort());
|
||||
tempFolder.delete();
|
||||
tempFolder.mkdir();
|
||||
tempFolder.deleteOnExit();
|
||||
return tempFolder;
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new NestedRuntimeException("Unable to create temp directory", ex) {};
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deployConfig(WebApplicationContext wac) {
|
||||
this.context = this.tomcatServer.addContext("", System.getProperty("java.io.tmpdir"));
|
||||
this.context.addApplicationListener(WS_APPLICATION_LISTENER);
|
||||
Tomcat.addServlet(context, "dispatcherServlet", new DispatcherServlet(wac));
|
||||
this.context.addServletMapping("/", "dispatcherServlet");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undeployConfig() {
|
||||
Assert.notNull(this.context, "deployConfig/undeployConfig must be invoked in pairs");
|
||||
this.context.removeServletMapping("/");
|
||||
this.tomcatServer.getHost().removeChild(this.context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() throws Exception {
|
||||
this.tomcatServer.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() throws Exception {
|
||||
this.tomcatServer.stop();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,8 +29,10 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.AbstractWebSocketIntegrationTests;
|
||||
import org.springframework.web.socket.JettyTestServer;
|
||||
import org.springframework.web.socket.TomcatTestServer;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
import org.springframework.web.socket.adapter.WebSocketHandlerAdapter;
|
||||
import org.springframework.web.socket.client.endpoint.StandardWebSocketClient;
|
||||
import org.springframework.web.socket.client.jetty.JettyWebSocketClient;
|
||||
import org.springframework.web.socket.server.HandshakeHandler;
|
||||
import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler;
|
||||
@@ -49,7 +51,9 @@ public class WebSocketConfigurationTests extends AbstractWebSocketIntegrationTes
|
||||
@Parameters
|
||||
public static Iterable<Object[]> arguments() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
{ new JettyTestServer(), new JettyWebSocketClient()} });
|
||||
{new JettyTestServer(), new JettyWebSocketClient()},
|
||||
{new TomcatTestServer(), new StandardWebSocketClient()}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -61,19 +65,25 @@ public class WebSocketConfigurationTests extends AbstractWebSocketIntegrationTes
|
||||
@Test
|
||||
public void registerWebSocketHandler() throws Exception {
|
||||
|
||||
this.webSocketClient.doHandshake(new WebSocketHandlerAdapter(), getWsBaseUrl() + "/ws");
|
||||
WebSocketSession session =
|
||||
this.webSocketClient.doHandshake(new WebSocketHandlerAdapter(), getWsBaseUrl() + "/ws");
|
||||
|
||||
TestWebSocketHandler serverHandler = this.wac.getBean(TestWebSocketHandler.class);
|
||||
assertTrue(serverHandler.latch.await(2, TimeUnit.SECONDS));
|
||||
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerWebSocketHandlerWithSockJS() throws Exception {
|
||||
|
||||
this.webSocketClient.doHandshake(new WebSocketHandlerAdapter(), getWsBaseUrl() + "/sockjs/websocket");
|
||||
WebSocketSession session =
|
||||
this.webSocketClient.doHandshake(new WebSocketHandlerAdapter(), getWsBaseUrl() + "/sockjs/websocket");
|
||||
|
||||
TestWebSocketHandler serverHandler = this.wac.getBean(TestWebSocketHandler.class);
|
||||
assertTrue(serverHandler.latch.await(2, TimeUnit.SECONDS));
|
||||
|
||||
session.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user