Backported refinements and polishing
This commit is contained in:
@@ -107,7 +107,7 @@ import org.springframework.util.StringUtils;
|
||||
* </pre>
|
||||
*
|
||||
* <p>Note that the above examples aim to demonstrate the general idea of using
|
||||
* header accessors. The most likely usage however is through sub-classes.
|
||||
* header accessors. The most likely usage however is through subclasses.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Juergen Hoeller
|
||||
|
||||
@@ -16,11 +16,6 @@
|
||||
|
||||
package org.springframework.messaging.simp.stomp;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
@@ -51,6 +46,14 @@ import org.springframework.util.MimeType;
|
||||
import org.springframework.util.MimeTypeUtils;
|
||||
import org.springframework.util.concurrent.SettableListenableFuture;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.notNull;
|
||||
import static org.mockito.Mockito.same;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link DefaultStompSession}.
|
||||
*
|
||||
@@ -80,7 +83,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
this.sessionHandler = mock(StompSessionHandler.class);
|
||||
@@ -96,7 +98,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void afterConnected() throws Exception {
|
||||
|
||||
assertFalse(this.session.isConnected());
|
||||
this.connectHeaders.setHost("my-host");
|
||||
this.connectHeaders.setHeartbeat(new long[] {11, 12});
|
||||
@@ -122,7 +123,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void handleConnectedFrame() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
assertTrue(this.session.isConnected());
|
||||
|
||||
@@ -141,7 +141,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void heartbeatValues() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
assertTrue(this.session.isConnected());
|
||||
|
||||
@@ -164,7 +163,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void heartbeatNotSupportedByServer() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
verify(this.connection).send(any());
|
||||
|
||||
@@ -181,7 +179,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void heartbeatTasks() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
verify(this.connection).send(any());
|
||||
|
||||
@@ -217,7 +214,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void handleErrorFrame() throws Exception {
|
||||
|
||||
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR);
|
||||
accessor.setContentType(new MimeType("text", "plain", UTF_8));
|
||||
accessor.addNativeHeader("foo", "bar");
|
||||
@@ -236,7 +232,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void handleErrorFrameWithEmptyPayload() throws Exception {
|
||||
|
||||
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR);
|
||||
accessor.addNativeHeader("foo", "bar");
|
||||
accessor.setLeaveMutable(true);
|
||||
@@ -249,7 +244,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void handleErrorFrameWithConversionException() throws Exception {
|
||||
|
||||
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.ERROR);
|
||||
accessor.setContentType(MimeTypeUtils.APPLICATION_JSON);
|
||||
accessor.addNativeHeader("foo", "bar");
|
||||
@@ -269,7 +263,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void handleMessageFrame() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
|
||||
StompFrameHandler frameHandler = mock(StompFrameHandler.class);
|
||||
@@ -296,7 +289,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void handleMessageFrameWithConversionException() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
assertTrue(this.session.isConnected());
|
||||
|
||||
@@ -344,7 +336,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void send() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
assertTrue(this.session.isConnected());
|
||||
|
||||
@@ -361,13 +352,12 @@ public class DefaultStompSessionTests {
|
||||
|
||||
assertEquals(destination, stompHeaders.getDestination());
|
||||
assertEquals(new MimeType("text", "plain", UTF_8), stompHeaders.getContentType());
|
||||
assertEquals(-1, stompHeaders.getContentLength()); // StompEncoder isn't involved
|
||||
assertEquals(-1, stompHeaders.getContentLength()); // StompEncoder isn't involved
|
||||
assertEquals(payload, new String(message.getPayload(), UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendWithReceipt() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
assertTrue(this.session.isConnected());
|
||||
|
||||
@@ -391,7 +381,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void sendWithConversionException() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
assertTrue(this.session.isConnected());
|
||||
|
||||
@@ -407,7 +396,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void sendWithExecutionException() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
assertTrue(this.session.isConnected());
|
||||
|
||||
@@ -426,7 +414,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void subscribe() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
assertTrue(this.session.isConnected());
|
||||
|
||||
@@ -446,7 +433,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void subscribeWithHeaders() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
assertTrue(this.session.isConnected());
|
||||
|
||||
@@ -473,7 +459,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void unsubscribe() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
assertTrue(this.session.isConnected());
|
||||
|
||||
@@ -493,7 +478,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void ack() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
assertTrue(this.session.isConnected());
|
||||
|
||||
@@ -511,7 +495,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void nack() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
assertTrue(this.session.isConnected());
|
||||
|
||||
@@ -529,7 +512,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void receiptReceived() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
this.session.setTaskScheduler(mock(TaskScheduler.class));
|
||||
|
||||
@@ -554,7 +536,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void receiptReceivedBeforeTaskAdded() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
this.session.setTaskScheduler(mock(TaskScheduler.class));
|
||||
|
||||
@@ -579,7 +560,6 @@ public class DefaultStompSessionTests {
|
||||
@Test
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void receiptNotReceived() throws Exception {
|
||||
|
||||
TaskScheduler taskScheduler = mock(TaskScheduler.class);
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
@@ -611,7 +591,6 @@ public class DefaultStompSessionTests {
|
||||
|
||||
@Test
|
||||
public void disconnect() throws Exception {
|
||||
|
||||
this.session.afterConnected(this.connection);
|
||||
assertTrue(this.session.isConnected());
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -33,6 +33,7 @@ import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TestName;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.messaging.Message;
|
||||
@@ -48,9 +49,7 @@ import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link StompBrokerRelayMessageHandler} running against ActiveMQ.
|
||||
@@ -59,13 +58,14 @@ import static org.junit.Assert.assertTrue;
|
||||
*/
|
||||
public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
|
||||
@Rule
|
||||
public final TestName testName = new TestName();
|
||||
|
||||
private static final Log logger = LogFactory.getLog(StompBrokerRelayMessageHandlerIntegrationTests.class);
|
||||
|
||||
private static final Charset UTF_8 = Charset.forName("UTF-8");
|
||||
|
||||
private StompBrokerRelayMessageHandler relay;
|
||||
|
||||
private BrokerService activeMQBroker;
|
||||
@@ -142,9 +142,9 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
logger.debug("Broker stopped");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void publishSubscribe() throws Exception {
|
||||
|
||||
logger.debug("Starting test publishSubscribe()");
|
||||
|
||||
String sess1 = "sess1";
|
||||
@@ -167,7 +167,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
this.responseHandler.expectMessages(send);
|
||||
}
|
||||
|
||||
@Test(expected=MessageDeliveryException.class)
|
||||
@Test(expected = MessageDeliveryException.class)
|
||||
public void messageDeliveryExceptionIfSystemSessionForwardFails() throws Exception {
|
||||
|
||||
logger.debug("Starting test messageDeliveryExceptionIfSystemSessionForwardFails()");
|
||||
@@ -181,7 +181,6 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void brokerBecomingUnvailableTriggersErrorFrame() throws Exception {
|
||||
|
||||
logger.debug("Starting test brokerBecomingUnvailableTriggersErrorFrame()");
|
||||
|
||||
String sess1 = "sess1";
|
||||
@@ -197,7 +196,6 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void brokerAvailabilityEventWhenStopped() throws Exception {
|
||||
|
||||
logger.debug("Starting test brokerAvailabilityEventWhenStopped()");
|
||||
|
||||
stopActiveMqBrokerAndAwait();
|
||||
@@ -206,7 +204,6 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void relayReconnectsIfBrokerComesBackUp() throws Exception {
|
||||
|
||||
logger.debug("Starting test relayReconnectsIfBrokerComesBackUp()");
|
||||
|
||||
String sess1 = "sess1";
|
||||
@@ -232,7 +229,6 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
|
||||
@Test
|
||||
public void disconnectWithReceipt() throws Exception {
|
||||
|
||||
logger.debug("Starting test disconnectWithReceipt()");
|
||||
|
||||
MessageExchange connect = MessageExchangeBuilder.connect("sess1").build();
|
||||
@@ -270,6 +266,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class TestMessageHandler implements MessageHandler {
|
||||
|
||||
private final BlockingQueue<Message<?>> queue = new LinkedBlockingQueue<>();
|
||||
@@ -283,17 +280,13 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
}
|
||||
|
||||
public void expectMessages(MessageExchange... messageExchanges) throws InterruptedException {
|
||||
|
||||
List<MessageExchange> expectedMessages =
|
||||
new ArrayList<MessageExchange>(Arrays.<MessageExchange>asList(messageExchanges));
|
||||
|
||||
while (expectedMessages.size() > 0) {
|
||||
Message<?> message = this.queue.poll(10000, TimeUnit.MILLISECONDS);
|
||||
assertNotNull("Timed out waiting for messages, expected [" + expectedMessages + "]", message);
|
||||
|
||||
MessageExchange match = findMatch(expectedMessages, message);
|
||||
assertNotNull("Unexpected message=" + message + ", expected [" + expectedMessages + "]", match);
|
||||
|
||||
expectedMessages.remove(match);
|
||||
}
|
||||
}
|
||||
@@ -308,6 +301,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Holds a message as well as expected and actual messages matched against expectations.
|
||||
*/
|
||||
@@ -343,6 +337,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class MessageExchangeBuilder {
|
||||
|
||||
private final Message<?> message;
|
||||
@@ -351,8 +346,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
|
||||
private final List<MessageMatcher> expected = new ArrayList<>();
|
||||
|
||||
|
||||
private MessageExchangeBuilder(Message<?> message) {
|
||||
public MessageExchangeBuilder(Message<?> message) {
|
||||
this.message = message;
|
||||
this.headers = StompHeaderAccessor.wrap(message);
|
||||
}
|
||||
@@ -442,25 +436,24 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
private static interface MessageMatcher {
|
||||
|
||||
private interface MessageMatcher {
|
||||
|
||||
boolean match(Message<?> message);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static class StompFrameMessageMatcher implements MessageMatcher {
|
||||
|
||||
private final StompCommand command;
|
||||
|
||||
private final String sessionId;
|
||||
|
||||
|
||||
public StompFrameMessageMatcher(StompCommand command, String sessionId) {
|
||||
this.command = command;
|
||||
this.sessionId = sessionId;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public final boolean match(Message<?> message) {
|
||||
StompHeaderAccessor headers = StompHeaderAccessor.wrap(message);
|
||||
@@ -480,6 +473,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class StompReceiptFrameMessageMatcher extends StompFrameMessageMatcher {
|
||||
|
||||
private final String receiptId;
|
||||
@@ -500,6 +494,7 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class StompMessageFrameMessageMatcher extends StompFrameMessageMatcher {
|
||||
|
||||
private final String subscriptionId;
|
||||
@@ -508,7 +503,6 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
|
||||
private final Object payload;
|
||||
|
||||
|
||||
public StompMessageFrameMessageMatcher(String sessionId, String subscriptionId, String destination, Object payload) {
|
||||
super(StompCommand.MESSAGE, sessionId);
|
||||
this.subscriptionId = subscriptionId;
|
||||
@@ -536,18 +530,17 @@ public class StompBrokerRelayMessageHandlerIntegrationTests {
|
||||
}
|
||||
|
||||
protected String getPayloadAsText() {
|
||||
return (this.payload instanceof byte[])
|
||||
? new String((byte[]) this.payload, UTF_8) : payload.toString();
|
||||
return (this.payload instanceof byte[]) ?
|
||||
new String((byte[]) this.payload, UTF_8) : this.payload.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private static class StompConnectedFrameMessageMatcher extends StompFrameMessageMatcher {
|
||||
|
||||
private static class StompConnectedFrameMessageMatcher extends StompFrameMessageMatcher {
|
||||
|
||||
public StompConnectedFrameMessageMatcher(String sessionId) {
|
||||
super(StompCommand.CONNECTED, sessionId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user