AbstractWebSocketIntegrationTests.java 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *
 * 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.util.HashMap;
import java.util.Map;

21 22
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
23 24 25 26 27 28
import org.junit.After;
import org.junit.Before;
import org.junit.runners.Parameterized.Parameter;
import org.springframework.context.Lifecycle;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
29
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
30 31 32 33 34
import org.springframework.web.socket.client.WebSocketClient;
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;
35
import org.springframework.web.socket.server.support.TomcatRequestUpgradeStrategy;
36 37 38 39 40 41 42 43 44


/**
 * Base class for WebSocket integration tests.
 *
 * @author Rossen Stoyanchev
 */
public abstract class AbstractWebSocketIntegrationTests {

45 46
	protected Log logger = LogFactory.getLog(getClass());

47 48 49
	private static Map<Class<?>, Class<?>> upgradeStrategyConfigTypes = new HashMap<Class<?>, Class<?>>();

	static {
50 51
		upgradeStrategyConfigTypes.put(JettyWebSocketTestServer.class, JettyUpgradeStrategyConfig.class);
		upgradeStrategyConfigTypes.put(TomcatWebSocketTestServer.class, TomcatUpgradeStrategyConfig.class);
52 53 54
	}

	@Parameter(0)
55
	public WebSocketTestServer server;
56 57 58 59

	@Parameter(1)
	public WebSocketClient webSocketClient;

60 61
	protected AnnotationConfigWebApplicationContext wac;

62 63 64

	@Before
	public void setup() throws Exception {
65 66 67 68

		this.wac = new AnnotationConfigWebApplicationContext();
		this.wac.register(getAnnotatedConfigClasses());
		this.wac.register(upgradeStrategyConfigTypes.get(this.server.getClass()));
69
		this.wac.refresh();
70

71 72 73
		if (this.webSocketClient instanceof Lifecycle) {
			((Lifecycle) this.webSocketClient).start();
		}
74

75
		this.server.deployConfig(this.wac);
76
		this.server.start();
77 78
	}

79 80
	protected abstract Class<?>[] getAnnotatedConfigClasses();

81 82 83 84 85 86 87
	@After
	public void teardown() throws Exception {
		try {
			if (this.webSocketClient instanceof Lifecycle) {
				((Lifecycle) this.webSocketClient).stop();
			}
		}
88 89 90 91 92 93 94 95 96 97 98 99
		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 {
100 101
			this.server.stop();
		}
102 103 104
		catch (Throwable t) {
			logger.error("Failed to stop server", t);
		}
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	}

	protected String getWsBaseUrl() {
		return "ws://localhost:" + this.server.getPort();
	}


	static abstract class AbstractRequestUpgradeStrategyConfig {

		@Bean
		public HandshakeHandler handshakeHandler() {
			return new DefaultHandshakeHandler(requestUpgradeStrategy());
		}

		public abstract RequestUpgradeStrategy requestUpgradeStrategy();
	}


	@Configuration
	static class JettyUpgradeStrategyConfig extends AbstractRequestUpgradeStrategyConfig {

		@Bean
		public RequestUpgradeStrategy requestUpgradeStrategy() {
			return new JettyRequestUpgradeStrategy();
		}
	}

132 133 134 135 136 137 138 139 140
	@Configuration
	static class TomcatUpgradeStrategyConfig extends AbstractRequestUpgradeStrategyConfig {

		@Bean
		public RequestUpgradeStrategy requestUpgradeStrategy() {
			return new TomcatRequestUpgradeStrategy();
		}
	}

141
}