Switch BufferingStompDecoder to decoration
The BufferingStompDecoder now decorates rather than extend StompDecoder. This allows a single StompDecoder instance to be configured and extended independantly while buffering remains a separate concern.
This commit is contained in:
@@ -18,12 +18,10 @@ package org.springframework.messaging.simp.stomp;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.converter.MessageConversionException;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -39,11 +37,12 @@ import static org.junit.Assert.fail;
|
||||
*/
|
||||
public class BufferingStompDecoderTests {
|
||||
|
||||
private final StompDecoder STOMP_DECODER = new StompDecoder();
|
||||
|
||||
@Test
|
||||
public void basic() throws InterruptedException {
|
||||
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(128);
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 128);
|
||||
String chunk = "SEND\na:alpha\n\nMessage body\0";
|
||||
|
||||
List<Message<byte[]>> messages = stompDecoder.decode(toByteBuffer(chunk));
|
||||
@@ -57,12 +56,12 @@ public class BufferingStompDecoderTests {
|
||||
@Test
|
||||
public void oneMessageInTwoChunks() throws InterruptedException {
|
||||
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(128);
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 128);
|
||||
String chunk1 = "SEND\na:alpha\n\nMessage";
|
||||
String chunk2 = " body\0";
|
||||
|
||||
List<Message<byte[]>> messages = stompDecoder.decode(toByteBuffer(chunk1));
|
||||
assertEquals(Arrays.asList(), messages);
|
||||
assertEquals(Collections.<Message<byte[]>>emptyList(), messages);
|
||||
|
||||
messages = stompDecoder.decode(toByteBuffer(chunk2));
|
||||
assertEquals(1, messages.size());
|
||||
@@ -75,7 +74,7 @@ public class BufferingStompDecoderTests {
|
||||
@Test
|
||||
public void twoMessagesInOneChunk() throws InterruptedException {
|
||||
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(128);
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 128);
|
||||
String chunk = "SEND\na:alpha\n\nPayload1\0" + "SEND\na:alpha\n\nPayload2\0";
|
||||
List<Message<byte[]>> messages = stompDecoder.decode(toByteBuffer(chunk));
|
||||
|
||||
@@ -92,7 +91,7 @@ public class BufferingStompDecoderTests {
|
||||
|
||||
int contentLength = "Payload2a-Payload2b".getBytes().length;
|
||||
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(128);
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 128);
|
||||
String chunk1 = "SEND\na:alpha\n\nPayload1\0SEND\ncontent-length:" + contentLength + "\n";
|
||||
List<Message<byte[]>> messages = stompDecoder.decode(toByteBuffer(chunk1));
|
||||
|
||||
@@ -121,7 +120,7 @@ public class BufferingStompDecoderTests {
|
||||
@Test
|
||||
public void oneFullAndOneSplitMessageNoContentLength() throws InterruptedException {
|
||||
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(128);
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 128);
|
||||
String chunk1 = "SEND\na:alpha\n\nPayload1\0SEND\na:alpha\n";
|
||||
List<Message<byte[]>> messages = stompDecoder.decode(toByteBuffer(chunk1));
|
||||
|
||||
@@ -150,7 +149,7 @@ public class BufferingStompDecoderTests {
|
||||
@Test
|
||||
public void oneFullAndOneSplitWithContentLengthExceedingBufferSize() throws InterruptedException {
|
||||
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(128);
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 128);
|
||||
String chunk1 = "SEND\na:alpha\n\nPayload1\0SEND\ncontent-length:129\n";
|
||||
List<Message<byte[]>> messages = stompDecoder.decode(toByteBuffer(chunk1));
|
||||
|
||||
@@ -166,12 +165,13 @@ public class BufferingStompDecoderTests {
|
||||
fail("Expected exception");
|
||||
}
|
||||
catch (StompConversionException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = StompConversionException.class)
|
||||
public void bufferSizeLimit() throws InterruptedException {
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(10);
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 10);
|
||||
String payload = "SEND\na:alpha\n\nMessage body";
|
||||
stompDecoder.decode(toByteBuffer(payload));
|
||||
}
|
||||
@@ -179,11 +179,10 @@ public class BufferingStompDecoderTests {
|
||||
@Test
|
||||
public void incompleteCommand() throws InterruptedException {
|
||||
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(128);
|
||||
BufferingStompDecoder stompDecoder = new BufferingStompDecoder(STOMP_DECODER, 128);
|
||||
String chunk = "MESSAG";
|
||||
|
||||
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
|
||||
List<Message<byte[]>> messages = stompDecoder.decode(toByteBuffer(chunk), headers);
|
||||
List<Message<byte[]>> messages = stompDecoder.decode(toByteBuffer(chunk));
|
||||
assertEquals(0, messages.size());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user