Refactor use of TaskScheduler in WebSocket Java config

Issue: SPR-15233
This commit is contained in:
Rossen Stoyanchev
2017-04-11 17:41:41 -04:00
parent 190408d1dc
commit 779779de7b
7 changed files with 202 additions and 32 deletions

View File

@@ -36,7 +36,10 @@ import org.springframework.web.socket.sockjs.transport.TransportType;
import org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService;
import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* Test fixture for
@@ -54,7 +57,7 @@ public class WebSocketHandlerRegistrationTests {
@Before
public void setup() {
this.taskScheduler = Mockito.mock(TaskScheduler.class);
this.registration = new TestWebSocketHandlerRegistration(taskScheduler);
this.registration = new TestWebSocketHandlerRegistration();
}
@Test
@@ -68,12 +71,14 @@ public class WebSocketHandlerRegistrationTests {
Mapping m1 = mappings.get(0);
assertEquals(handler, m1.webSocketHandler);
assertEquals("/foo", m1.path);
assertNotNull(m1.interceptors);
assertEquals(1, m1.interceptors.length);
assertEquals(OriginHandshakeInterceptor.class, m1.interceptors[0].getClass());
Mapping m2 = mappings.get(1);
assertEquals(handler, m2.webSocketHandler);
assertEquals("/bar", m2.path);
assertNotNull(m2.interceptors);
assertEquals(1, m2.interceptors.length);
assertEquals(OriginHandshakeInterceptor.class, m2.interceptors[0].getClass());
}
@@ -91,6 +96,7 @@ public class WebSocketHandlerRegistrationTests {
Mapping mapping = mappings.get(0);
assertEquals(handler, mapping.webSocketHandler);
assertEquals("/foo", mapping.path);
assertNotNull(mapping.interceptors);
assertEquals(2, mapping.interceptors.length);
assertEquals(interceptor, mapping.interceptors[0]);
assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
@@ -109,6 +115,7 @@ public class WebSocketHandlerRegistrationTests {
Mapping mapping = mappings.get(0);
assertEquals(handler, mapping.webSocketHandler);
assertEquals("/foo", mapping.path);
assertNotNull(mapping.interceptors);
assertEquals(2, mapping.interceptors.length);
assertEquals(interceptor, mapping.interceptors[0]);
assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
@@ -127,6 +134,7 @@ public class WebSocketHandlerRegistrationTests {
Mapping mapping = mappings.get(0);
assertEquals(handler, mapping.webSocketHandler);
assertEquals("/foo", mapping.path);
assertNotNull(mapping.interceptors);
assertEquals(2, mapping.interceptors.length);
assertEquals(interceptor, mapping.interceptors[0]);
assertEquals(OriginHandshakeInterceptor.class, mapping.interceptors[1].getClass());
@@ -137,8 +145,12 @@ public class WebSocketHandlerRegistrationTests {
WebSocketHandler handler = new TextWebSocketHandler();
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
this.registration.addHandler(handler, "/foo").addInterceptors(interceptor)
.setAllowedOrigins("http://mydomain1.com").withSockJS();
this.registration.addHandler(handler, "/foo")
.addInterceptors(interceptor)
.setAllowedOrigins("http://mydomain1.com")
.withSockJS();
this.registration.getSockJsServiceRegistration().setTaskScheduler(this.taskScheduler);
List<Mapping> mappings = this.registration.getMappings();
assertEquals(1, mappings.size());
@@ -175,6 +187,7 @@ public class WebSocketHandlerRegistrationTests {
HandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
this.registration.addHandler(handler, "/foo").setHandshakeHandler(handshakeHandler).withSockJS();
this.registration.getSockJsServiceRegistration().setTaskScheduler(this.taskScheduler);
List<Mapping> mappings = this.registration.getMappings();
assertEquals(1, mappings.size());
@@ -190,11 +203,7 @@ public class WebSocketHandlerRegistrationTests {
}
private static class TestWebSocketHandlerRegistration extends AbstractWebSocketHandlerRegistration<List<Mapping>> {
public TestWebSocketHandlerRegistration(TaskScheduler sockJsTaskScheduler) {
super(sockJsTaskScheduler);
}
private static class TestWebSocketHandlerRegistration extends AbstractWebSocketHandlerRegistration<List<Mapping>> {
@Override
protected List<Mapping> createMappings() {