/* * 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; import java.util.Arrays; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import org.springframework.beans.factory.annotation.Autowired; 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; import org.springframework.messaging.simp.stomp.StompTextMessageBuilder; import org.springframework.messaging.support.channel.ExecutorSubscribableChannel; import org.springframework.stereotype.Controller; import org.springframework.web.socket.AbstractWebSocketIntegrationTests; import org.springframework.web.socket.JettyWebSocketTestServer; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.TomcatWebSocketTestServer; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.adapter.TextWebSocketHandlerAdapter; 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.server.config.WebSocketConfigurationSupport; import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler; import static org.junit.Assert.*; /** * Test fixture for {@link WebSocketConfigurationSupport}. * * @author Rossen Stoyanchev */ @RunWith(Parameterized.class) public class WebSocketMessageBrokerConfigurationTests extends AbstractWebSocketIntegrationTests { @Parameters public static Iterable arguments() { return Arrays.asList(new Object[][] { {new JettyWebSocketTestServer(), new JettyWebSocketClient()}, {new TomcatWebSocketTestServer(), new StandardWebSocketClient()} }); }; @Override protected Class[] getAnnotatedConfigClasses() { return new Class[] { TestWebSocketMessageBrokerConfiguration.class, SimpleBrokerConfigurer.class }; } @Test public void sendMessage() throws Exception { final TextMessage textMessage = StompTextMessageBuilder.create(StompCommand.SEND) .headers("destination:/app/foo").build(); WebSocketHandler clientHandler = new TextWebSocketHandlerAdapter() { @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { session.sendMessage(textMessage); } }; TestController testController = this.wac.getBean(TestController.class); WebSocketSession session = this.webSocketClient.doHandshake(clientHandler, getWsBaseUrl() + "/ws"); assertTrue(testController.latch.await(2, TimeUnit.SECONDS)); session.close(); testController.latch = new CountDownLatch(1); session = this.webSocketClient.doHandshake(clientHandler, getWsBaseUrl() + "/sockjs/websocket"); assertTrue(testController.latch.await(2, TimeUnit.SECONDS)); session.close(); } @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 { @Autowired private HandshakeHandler handshakeHandler; // can't rely on classpath for server detection @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/ws") .setHandshakeHandler(this.handshakeHandler); registry.addEndpoint("/sockjs").withSockJS() .setTransportHandlerOverrides(new WebSocketTransportHandler(this.handshakeHandler));; } @Override public void configureMessageBroker(MessageBrokerConfigurer configurer) { configurer.setAnnotationMethodDestinationPrefixes("/app/"); configurer.enableSimpleBroker("/topic"); } } @Controller private static class TestController { private CountDownLatch latch = new CountDownLatch(1); @MessageMapping(value="/app/foo") public void handleFoo() { this.latch.countDown(); } } }