WebSocketMessageBrokerConfigurationTests.java 5.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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.messaging.simp.config;

19 20 21 22
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

23
import org.junit.Test;
24 25 26 27
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
28 29 30 31 32
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.stomp.StompCommand;
33
import org.springframework.messaging.simp.stomp.StompTextMessageBuilder;
34 35
import org.springframework.messaging.support.channel.ExecutorSubscribableChannel;
import org.springframework.stereotype.Controller;
36 37
import org.springframework.web.socket.AbstractWebSocketIntegrationTests;
import org.springframework.web.socket.JettyWebSocketTestServer;
38
import org.springframework.web.socket.TextMessage;
39
import org.springframework.web.socket.TomcatWebSocketTestServer;
40 41 42
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.adapter.TextWebSocketHandlerAdapter;
43
import org.springframework.web.socket.client.endpoint.StandardWebSocketClient;
44 45
import org.springframework.web.socket.client.jetty.JettyWebSocketClient;
import org.springframework.web.socket.server.HandshakeHandler;
46
import org.springframework.web.socket.server.config.WebSocketConfigurationSupport;
47
import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler;
48 49 50 51 52 53 54 55 56

import static org.junit.Assert.*;


/**
 * Test fixture for {@link WebSocketConfigurationSupport}.
 *
 * @author Rossen Stoyanchev
 */
57 58
@RunWith(Parameterized.class)
public class WebSocketMessageBrokerConfigurationTests extends AbstractWebSocketIntegrationTests {
59

60 61 62
	@Parameters
	public static Iterable<Object[]> arguments() {
		return Arrays.asList(new Object[][] {
63 64
				{new JettyWebSocketTestServer(), new JettyWebSocketClient()},
				{new TomcatWebSocketTestServer(), new StandardWebSocketClient()}
65
		});
66
	};
67 68


69 70 71 72 73
	@Override
	protected Class<?>[] getAnnotatedConfigClasses() {
		return new Class<?>[] { TestWebSocketMessageBrokerConfiguration.class, SimpleBrokerConfigurer.class };
	}

74
	@Test
75
	public void sendMessage() throws Exception {
76

77 78
		final TextMessage textMessage = StompTextMessageBuilder.create(StompCommand.SEND)
				.headers("destination:/app/foo").build();
79

80 81 82
		WebSocketHandler clientHandler = new TextWebSocketHandlerAdapter() {
			@Override
			public void afterConnectionEstablished(WebSocketSession session) throws Exception {
83
				session.sendMessage(textMessage);
84 85
			}
		};
86

87
		TestController testController = this.wac.getBean(TestController.class);
88

89
		WebSocketSession session = this.webSocketClient.doHandshake(clientHandler, getWsBaseUrl() + "/ws");
90
		assertTrue(testController.latch.await(2, TimeUnit.SECONDS));
91
		session.close();
92

93
		testController.latch = new CountDownLatch(1);
94
		session = this.webSocketClient.doHandshake(clientHandler, getWsBaseUrl() + "/sockjs/websocket");
95
		assertTrue(testController.latch.await(2, TimeUnit.SECONDS));
96
		session.close();
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	}


	@Configuration
	static class TestWebSocketMessageBrokerConfiguration extends DelegatingWebSocketMessageBrokerConfiguration {

		@Override
		@Bean
		public SubscribableChannel webSocketRequestChannel() {
			return new ExecutorSubscribableChannel(); // synchronous
		}

		@Override
		@Bean
		public SubscribableChannel webSocketReplyChannel() {
			return new ExecutorSubscribableChannel(); // synchronous
		}

		@Bean
		public TestController testController() {
			return new TestController();
		}
	}

	@Configuration
	static class SimpleBrokerConfigurer implements WebSocketMessageBrokerConfigurer {

124 125 126 127
		@Autowired
		private HandshakeHandler handshakeHandler; // can't rely on classpath for server detection


128 129
		@Override
		public void registerStompEndpoints(StompEndpointRegistry registry) {
130 131 132 133 134 135

			registry.addEndpoint("/ws")
				.setHandshakeHandler(this.handshakeHandler);

			registry.addEndpoint("/sockjs").withSockJS()
				.setTransportHandlerOverrides(new WebSocketTransportHandler(this.handshakeHandler));;
136 137 138 139 140 141 142 143 144 145 146 147
		}

		@Override
		public void configureMessageBroker(MessageBrokerConfigurer configurer) {
			configurer.setAnnotationMethodDestinationPrefixes("/app/");
			configurer.enableSimpleBroker("/topic");
		}
	}

	@Controller
	private static class TestController {

148
		private CountDownLatch latch = new CountDownLatch(1);
149 150 151

		@MessageMapping(value="/app/foo")
		public void handleFoo() {
152
			this.latch.countDown();
153 154 155 156
		}
	}

}