Add StompProtocolHandler tests

This commit is contained in:
Rossen Stoyanchev
2013-08-29 09:58:04 -04:00
parent 364bc35709
commit 39ff1e2c53
7 changed files with 268 additions and 38 deletions

View File

@@ -17,26 +17,26 @@
package org.springframework.web.socket.server.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.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.socket.AbstractWebSocketIntegrationTests;
import org.springframework.web.socket.JettyTestServer;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.adapter.WebSocketHandlerAdapter;
import org.springframework.web.socket.client.jetty.JettyWebSocketClient;
import org.springframework.web.socket.server.HandshakeHandler;
import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
/**
@@ -63,13 +63,10 @@ public class WebSocketConfigurationTests extends AbstractWebSocketIntegrationTes
this.server.init(cxt);
this.server.start();
WebSocketHandler clientHandler = Mockito.mock(WebSocketHandler.class);
WebSocketHandler serverHandler = cxt.getBean(WebSocketHandler.class);
this.webSocketClient.doHandshake(new WebSocketHandlerAdapter(), getWsBaseUrl() + "/ws");
this.webSocketClient.doHandshake(clientHandler, getWsBaseUrl() + "/ws");
verify(serverHandler).afterConnectionEstablished(any(WebSocketSession.class));
verify(clientHandler).afterConnectionEstablished(any(WebSocketSession.class));
TestWebSocketHandler serverHandler = cxt.getBean(TestWebSocketHandler.class);
assertTrue(serverHandler.latch.await(2, TimeUnit.SECONDS));
}
@Test
@@ -81,13 +78,10 @@ public class WebSocketConfigurationTests extends AbstractWebSocketIntegrationTes
this.server.init(cxt);
this.server.start();
WebSocketHandler clientHandler = Mockito.mock(WebSocketHandler.class);
WebSocketHandler serverHandler = cxt.getBean(WebSocketHandler.class);
this.webSocketClient.doHandshake(new WebSocketHandlerAdapter(), getWsBaseUrl() + "/sockjs/websocket");
this.webSocketClient.doHandshake(clientHandler, getWsBaseUrl() + "/sockjs/websocket");
verify(serverHandler).afterConnectionEstablished(any(WebSocketSession.class));
verify(clientHandler).afterConnectionEstablished(any(WebSocketSession.class));
TestWebSocketHandler serverHandler = cxt.getBean(TestWebSocketHandler.class);
assertTrue(serverHandler.latch.await(2, TimeUnit.SECONDS));
}
@@ -110,8 +104,18 @@ public class WebSocketConfigurationTests extends AbstractWebSocketIntegrationTes
}
@Bean
public WebSocketHandler serverHandler() {
return Mockito.mock(WebSocketHandler.class);
public TestWebSocketHandler serverHandler() {
return new TestWebSocketHandler();
}
}
private static class TestWebSocketHandler extends WebSocketHandlerAdapter {
private CountDownLatch latch = new CountDownLatch(1);
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
this.latch.countDown();
}
}

View File

@@ -0,0 +1,56 @@
/*
* 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.web.socket.support;
import java.security.Principal;
/**
* An implementation of Prinicipal for testing.
* @author Rossen Stoyanchev
*/
public class TestPrincipal implements Principal {
private String name;
public TestPrincipal(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof TestPrincipal)) {
return false;
}
TestPrincipal p = (TestPrincipal) obj;
return this.name.equals(p.name);
}
@Override
public int hashCode() {
return this.name.hashCode();
}
}