@ -22,9 +22,9 @@ import java.util.Map;
@@ -22,9 +22,9 @@ import java.util.Map;
import java.util.Set ;
import java.util.concurrent.ScheduledThreadPoolExecutor ;
import org.junit.Before ;
import org.junit.Test ;
import org.springframework.context.ApplicationContext ;
import org.springframework.context.annotation.AnnotationConfigApplicationContext ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
@ -43,13 +43,17 @@ import org.springframework.stereotype.Controller;
@@ -43,13 +43,17 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerMapping ;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping ;
import org.springframework.web.socket.TextMessage ;
import org.springframework.web.socket.WebSocketHandler ;
import org.springframework.web.socket.WebSocketSession ;
import org.springframework.web.socket.config.WebSocketMessageBrokerStats ;
import org.springframework.web.socket.handler.TestWebSocketSession ;
import org.springframework.web.socket.handler.WebSocketHandlerDecorator ;
import org.springframework.web.socket.handler.WebSocketHandlerDecoratorFactory ;
import org.springframework.web.socket.messaging.StompSubProtocolHandler ;
import org.springframework.web.socket.messaging.StompTextMessageBuilder ;
import org.springframework.web.socket.messaging.SubProtocolHandler ;
import org.springframework.web.socket.messaging.SubProtocolWebSocketHandler ;
import org.springframework.web.socket.server.support.WebSocketHttpRequestHandler ;
import static org.junit.Assert.* ;
import static org.junit.Assert.assertEquals ;
@ -62,19 +66,11 @@ import static org.junit.Assert.assertEquals;
@@ -62,19 +66,11 @@ import static org.junit.Assert.assertEquals;
* /
public class WebSocketMessageBrokerConfigurationSupportTests {
private AnnotationConfigApplicationContext config ;
@Before
public void setupOnce ( ) {
this . config = new AnnotationConfigApplicationContext ( ) ;
this . config . register ( TestWebSocketMessageBrokerConfiguration . class , TestSimpleMessageBrokerConfig . class ) ;
this . config . refresh ( ) ;
}
@Test
public void handlerMapping ( ) {
SimpleUrlHandlerMapping hm = ( SimpleUrlHandlerMapping ) this . config . getBean ( HandlerMapping . class ) ;
ApplicationContext config = createConfig ( TestChannelConfig . class , TestConfigurer . class ) ;
SimpleUrlHandlerMapping hm = ( SimpleUrlHandlerMapping ) config . getBean ( HandlerMapping . class ) ;
assertEquals ( 1 , hm . getOrder ( ) ) ;
Map < String , Object > handlerMap = hm . getHandlerMap ( ) ;
@ -84,8 +80,9 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@@ -84,8 +80,9 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@Test
public void clientInboundChannelSendMessage ( ) throws Exception {
TestChannel channel = this . config . getBean ( "clientInboundChannel" , TestChannel . class ) ;
SubProtocolWebSocketHandler webSocketHandler = this . config . getBean ( SubProtocolWebSocketHandler . class ) ;
ApplicationContext config = createConfig ( TestChannelConfig . class , TestConfigurer . class ) ;
TestChannel channel = config . getBean ( "clientInboundChannel" , TestChannel . class ) ;
SubProtocolWebSocketHandler webSocketHandler = config . getBean ( SubProtocolWebSocketHandler . class ) ;
WebSocketSession session = new TestWebSocketSession ( "s1" ) ;
webSocketHandler . afterConnectionEstablished ( session ) ;
@ -102,7 +99,8 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@@ -102,7 +99,8 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@Test
public void clientOutboundChannelChannel ( ) {
TestChannel channel = this . config . getBean ( "clientOutboundChannel" , TestChannel . class ) ;
ApplicationContext config = createConfig ( TestChannelConfig . class , TestConfigurer . class ) ;
TestChannel channel = config . getBean ( "clientOutboundChannel" , TestChannel . class ) ;
Set < MessageHandler > handlers = channel . getSubscribers ( ) ;
assertEquals ( 1 , handlers . size ( ) ) ;
@ -111,8 +109,9 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@@ -111,8 +109,9 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@Test
public void webSocketTransportOptions ( ) {
ApplicationContext config = createConfig ( TestChannelConfig . class , TestConfigurer . class ) ;
SubProtocolWebSocketHandler subProtocolWebSocketHandler =
this . config . getBean ( "subProtocolWebSocketHandler" , SubProtocolWebSocketHandler . class ) ;
config . getBean ( "subProtocolWebSocketHandler" , SubProtocolWebSocketHandler . class ) ;
assertEquals ( 1024 * 1024 , subProtocolWebSocketHandler . getSendBufferSizeLimit ( ) ) ;
assertEquals ( 25 * 1000 , subProtocolWebSocketHandler . getSendTimeLimit ( ) ) ;
@ -126,8 +125,9 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@@ -126,8 +125,9 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@Test
public void messageBrokerSockJsTaskScheduler ( ) {
ApplicationContext config = createConfig ( TestChannelConfig . class , TestConfigurer . class ) ;
ThreadPoolTaskScheduler taskScheduler =
this . config . getBean ( "messageBrokerSockJsTaskScheduler" , ThreadPoolTaskScheduler . class ) ;
config . getBean ( "messageBrokerSockJsTaskScheduler" , ThreadPoolTaskScheduler . class ) ;
ScheduledThreadPoolExecutor executor = taskScheduler . getScheduledThreadPoolExecutor ( ) ;
assertEquals ( Runtime . getRuntime ( ) . availableProcessors ( ) , executor . getCorePoolSize ( ) ) ;
@ -136,8 +136,9 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@@ -136,8 +136,9 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@Test
public void webSocketMessageBrokerStats ( ) {
ApplicationContext config = createConfig ( TestChannelConfig . class , TestConfigurer . class ) ;
String name = "webSocketMessageBrokerStats" ;
WebSocketMessageBrokerStats stats = this . config . getBean ( name , WebSocketMessageBrokerStats . class ) ;
WebSocketMessageBrokerStats stats = config . getBean ( name , WebSocketMessageBrokerStats . class ) ;
String actual = stats . toString ( ) ;
String expected = "WebSocketSession\\[0 current WS\\(0\\)-HttpStream\\(0\\)-HttpPoll\\(0\\), " +
"0 total, 0 closed abnormally \\(0 connect failure, 0 send limit, 0 transport error\\)\\], " +
@ -150,6 +151,29 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@@ -150,6 +151,29 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
assertTrue ( "\nExpected: " + expected . replace ( "\\" , "" ) + "\n Actual: " + actual , actual . matches ( expected ) ) ;
}
@Test
public void webSocketHandlerDecorator ( ) throws Exception {
ApplicationContext config = createConfig ( WebSocketHandlerDecoratorConfig . class ) ;
WebSocketHandler handler = config . getBean ( SubProtocolWebSocketHandler . class ) ;
assertNotNull ( handler ) ;
SimpleUrlHandlerMapping mapping = ( SimpleUrlHandlerMapping ) config . getBean ( "stompWebSocketHandlerMapping" ) ;
WebSocketHttpRequestHandler httpHandler = ( WebSocketHttpRequestHandler ) mapping . getHandlerMap ( ) . get ( "/test" ) ;
handler = httpHandler . getWebSocketHandler ( ) ;
WebSocketSession session = new TestWebSocketSession ( "id" ) ;
handler . afterConnectionEstablished ( session ) ;
assertEquals ( true , session . getAttributes ( ) . get ( "decorated" ) ) ;
}
private ApplicationContext createConfig ( Class < ? > . . . configClasses ) {
AnnotationConfigApplicationContext config = new AnnotationConfigApplicationContext ( ) ;
config . register ( configClasses ) ;
config . refresh ( ) ;
return config ;
}
@Controller
static class TestController {
@ -167,7 +191,7 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@@ -167,7 +191,7 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
}
@Configuration
static class TestSimpleMessageBroker Config extends AbstractWebSocketMessageBrokerConfigurer {
static class TestConfigurer extends AbstractWebSocketMessageBrokerConfigurer {
@Bean
public TestController subscriptionController ( ) {
@ -188,7 +212,7 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@@ -188,7 +212,7 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
}
@Configuration
static class TestWebSocketMessageBrokerConfiguration extends DelegatingWebSocketMessageBrokerConfiguration {
static class TestChannelConfig extends DelegatingWebSocketMessageBrokerConfiguration {
@Override
@Bean
@ -208,6 +232,31 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
@@ -208,6 +232,31 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
}
}
@Configuration
static class WebSocketHandlerDecoratorConfig extends WebSocketMessageBrokerConfigurationSupport {
@Override
protected void registerStompEndpoints ( StompEndpointRegistry registry ) {
registry . addEndpoint ( "/test" ) ;
}
@Override
protected void configureWebSocketTransport ( WebSocketTransportRegistration registry ) {
registry . addDecoratorFactory ( new WebSocketHandlerDecoratorFactory ( ) {
@Override
public WebSocketHandlerDecorator decorate ( WebSocketHandler handler ) {
return new WebSocketHandlerDecorator ( handler ) {
@Override
public void afterConnectionEstablished ( WebSocketSession session ) throws Exception {
session . getAttributes ( ) . put ( "decorated" , true ) ;
super . afterConnectionEstablished ( session ) ;
}
} ;
}
} ) ;
}
}
private static class TestChannel extends ExecutorSubscribableChannel {
private final List < Message < ? > > messages = new ArrayList < > ( ) ;