RESOLVED - issue INT-1058, INT-1098: message ID is unique to an immutable instance
This commit is contained in:
@@ -47,9 +47,10 @@ public final class MessageHeaders implements Map<String, Object>, Serializable {
|
||||
public static final String PREFIX = "$";
|
||||
|
||||
/**
|
||||
* The key for the Message ID. This is an automatically generated UUID and should
|
||||
* never be explicitly set in the header map <b>except</b> in the case of Message
|
||||
* deserialization where the serialized Message's generated UUID is being restored.
|
||||
* The key for the Message ID. This is an automatically generated UUID and
|
||||
* should never be explicitly set in the header map <b>except</b> in the
|
||||
* case of Message deserialization where the serialized Message's generated
|
||||
* UUID is being restored.
|
||||
*/
|
||||
public static final String ID = PREFIX + "id";
|
||||
|
||||
@@ -71,26 +72,17 @@ public final class MessageHeaders implements Map<String, Object>, Serializable {
|
||||
|
||||
public static final String SEQUENCE_SIZE = PREFIX + "sequenceSize";
|
||||
|
||||
|
||||
private final Map<String, Object> headers;
|
||||
|
||||
|
||||
public MessageHeaders(Map<String, Object> headers) {
|
||||
this.headers = (headers != null)
|
||||
? new HashMap<String, Object>(headers)
|
||||
: new HashMap<String, Object>();
|
||||
if (this.headers.get(ID) == null) {
|
||||
this.headers.put(ID, UUID.randomUUID());
|
||||
}
|
||||
if (this.headers.get(TIMESTAMP) == null) {
|
||||
this.headers.put(TIMESTAMP, new Long(System.currentTimeMillis()));
|
||||
}
|
||||
this.headers = (headers != null) ? new HashMap<String, Object>(headers) : new HashMap<String, Object>();
|
||||
this.headers.put(ID, UUID.randomUUID());
|
||||
this.headers.put(TIMESTAMP, new Long(System.currentTimeMillis()));
|
||||
if (this.headers.get(HISTORY) == null) {
|
||||
this.headers.put(HISTORY, new MessageHistory());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public UUID getId() {
|
||||
return this.get(ID, UUID.class);
|
||||
}
|
||||
@@ -140,8 +132,8 @@ public final class MessageHeaders implements Map<String, Object>, Serializable {
|
||||
return null;
|
||||
}
|
||||
if (!type.isAssignableFrom(value.getClass())) {
|
||||
throw new IllegalArgumentException("Incorrect type specified for header '" + key
|
||||
+ "'. Expected [" + type + "] but actual type is [" + value.getClass() + "]");
|
||||
throw new IllegalArgumentException("Incorrect type specified for header '" + key + "'. Expected [" + type
|
||||
+ "] but actual type is [" + value.getClass() + "]");
|
||||
}
|
||||
return (T) value;
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ public final class MessageBuilder<T> {
|
||||
* <code>null</code>, the header will be removed.
|
||||
*/
|
||||
public MessageBuilder<T> setHeader(String headerName, Object headerValue) {
|
||||
if (StringUtils.hasLength(headerName)) {
|
||||
if (StringUtils.hasLength(headerName) && !headerName.equals(MessageHeaders.ID) && !headerName.equals(MessageHeaders.TIMESTAMP)) {
|
||||
this.verifyType(headerName, headerValue);
|
||||
this.modified = true;
|
||||
if (headerValue == null) {
|
||||
@@ -115,7 +115,7 @@ public final class MessageBuilder<T> {
|
||||
* Remove the value for the given header name.
|
||||
*/
|
||||
public MessageBuilder<T> removeHeader(String headerName) {
|
||||
if (StringUtils.hasLength(headerName)) {
|
||||
if (StringUtils.hasLength(headerName) && !headerName.equals(MessageHeaders.ID) && !headerName.equals(MessageHeaders.TIMESTAMP)) {
|
||||
this.modified = true;
|
||||
this.headers.remove(headerName);
|
||||
}
|
||||
|
||||
@@ -34,30 +34,33 @@ import org.springframework.integration.core.Message;
|
||||
public interface MessageStore {
|
||||
|
||||
/**
|
||||
* Return the Message with the given id, or <i>null</i> if no
|
||||
* Message with that id exists in the MessageStore.
|
||||
* Return the Message with the given id, or <i>null</i> if no Message with
|
||||
* that id exists in the MessageStore.
|
||||
*/
|
||||
Message<?> get(UUID id);
|
||||
|
||||
/**
|
||||
* Put the provided Message into the MessageStore. Its id will
|
||||
* be used as an index so that the {@link #get(UUID)} and
|
||||
* {@link #delete(Object)} behave properly. If available, its
|
||||
* correlationId header will also be stored so that the
|
||||
* {@link #list(Object)} method behaves properly.
|
||||
* Put the provided Message into the MessageStore. The store may need to
|
||||
* mutate the message internally, and if it does then the return value can
|
||||
* be different than the input. The id of the return value will be used as
|
||||
* an index so that the {@link #get(UUID)} and {@link #delete(Object)}
|
||||
* behave properly. Since messages are immutable, putting the same message
|
||||
* more than once is a no-op.
|
||||
*
|
||||
* @return the message that was stored
|
||||
*/
|
||||
<T> Message<T> put(Message<T> message);
|
||||
|
||||
/**
|
||||
* Remove the Message with the given id from the MessageStore,
|
||||
* if present, and return it. If no Message with that id is
|
||||
* present in the store, this will return <i>null</i>.
|
||||
* Remove the Message with the given id from the MessageStore, if present,
|
||||
* and return it. If no Message with that id is present in the store, this
|
||||
* will return <i>null</i>.
|
||||
*/
|
||||
Message<?> delete(UUID id);
|
||||
|
||||
/**
|
||||
* Return all Messages currently in the MessageStore that
|
||||
* contain the provided correlationId header value.
|
||||
* Return all Messages currently in the MessageStore that contain the
|
||||
* provided correlationId header value.
|
||||
* @see org.springframework.integration.core.MessageHeaders#getCorrelationId()
|
||||
*/
|
||||
List<Message<?>> list(Object correlationId);
|
||||
|
||||
@@ -90,8 +90,8 @@ public class CorrelatingMessageHandlerTests {
|
||||
String correlationKey = "key";
|
||||
UUID id1 = UUID.randomUUID();
|
||||
UUID id2 = UUID.randomUUID();
|
||||
Message<?> message1 = testMessage(correlationKey, id1, 1);
|
||||
Message<?> message2 = testMessage(correlationKey, id2, 2);
|
||||
Message<?> message1 = testMessage(correlationKey, 1);
|
||||
Message<?> message2 = testMessage(correlationKey, 2);
|
||||
List<Message<?>> storedMessages = new ArrayList<Message<?>>();
|
||||
|
||||
when(store.list(correlationKey)).thenReturn(storedMessages);
|
||||
@@ -128,10 +128,10 @@ public class CorrelatingMessageHandlerTests {
|
||||
@Test
|
||||
public void shouldNotPruneWhileCompleting() throws Exception {
|
||||
String correlationKey = "key";
|
||||
UUID id1 = UUID.randomUUID();
|
||||
UUID id2 = UUID.randomUUID();
|
||||
final Message<?> message1 = testMessage(correlationKey, id1, 1);
|
||||
final Message<?> message2 = testMessage(correlationKey, id2, 2);
|
||||
final Message<?> message1 = testMessage(correlationKey, 1);
|
||||
final Message<?> message2 = testMessage(correlationKey, 2);
|
||||
UUID id1 = message1.getHeaders().getId();
|
||||
UUID id2 = message2.getHeaders().getId();
|
||||
final List<Message<?>> storedMessages = new ArrayList<Message<?>>();
|
||||
|
||||
final CountDownLatch bothMessagesHandled = new CountDownLatch(2);
|
||||
@@ -170,10 +170,9 @@ public class CorrelatingMessageHandlerTests {
|
||||
verify(store).delete(id2);
|
||||
}
|
||||
|
||||
private Message<?> testMessage(String correllationKey, UUID id, int sequenceNumber) {
|
||||
return MessageBuilder.withPayload("test" + id)
|
||||
.setHeader(MessageHeaders.ID, id)
|
||||
.setCorrelationId(correllationKey)
|
||||
private Message<?> testMessage(String correlationKey, int sequenceNumber) {
|
||||
return MessageBuilder.withPayload("test" + sequenceNumber)
|
||||
.setCorrelationId(correlationKey)
|
||||
.setSequenceNumber(sequenceNumber).build();
|
||||
}
|
||||
|
||||
|
||||
@@ -19,12 +19,14 @@ package org.springframework.integration.config;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hamcrest.Factory;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.integration.channel.PollableChannel;
|
||||
@@ -32,6 +34,7 @@ import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.MessageMatcher;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -42,7 +45,7 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class ChainParserTests {
|
||||
public class ChainParserTests {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("filterInput")
|
||||
@@ -85,12 +88,15 @@ public class ChainParserTests {
|
||||
@Autowired
|
||||
private PollableChannel numbers;
|
||||
|
||||
|
||||
public static Message<?> successMessage = MessageBuilder.withPayload("success").build();
|
||||
|
||||
@Factory
|
||||
public static Matcher<Message<?>> sameExceptImmutableHeaders(Message<?> expected) {
|
||||
return new MessageMatcher(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void chainWithAcceptingFilter() {
|
||||
public void chainWithAcceptingFilter() {
|
||||
Message<?> message = MessageBuilder.withPayload("test").build();
|
||||
this.filterInput.send(message);
|
||||
Message<?> reply = this.output.receive(0);
|
||||
@@ -143,7 +149,7 @@ public class ChainParserTests {
|
||||
this.beanInput.send(message);
|
||||
Message reply = this.output.receive(3000);
|
||||
assertNotNull(reply);
|
||||
assertEquals(reply, successMessage);
|
||||
assertThat(reply, sameExceptImmutableHeaders(successMessage));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -170,17 +176,16 @@ public class ChainParserTests {
|
||||
assertEquals(123, reply2.getPayload());
|
||||
}
|
||||
|
||||
|
||||
public static class StubHandler extends AbstractReplyProducingMessageHandler {
|
||||
@Override
|
||||
protected Object handleRequestMessage(Message<?> requestMessage) {
|
||||
return successMessage;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static class StubAggregator {
|
||||
public String aggregate(List<String> strings){
|
||||
public String aggregate(List<String> strings) {
|
||||
return StringUtils.collectionToCommaDelimitedString(strings);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,11 @@
|
||||
|
||||
package org.springframework.integration.config.xml;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.hamcrest.Factory;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.integration.channel.PollableChannel;
|
||||
@@ -28,6 +29,7 @@ import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageChannel;
|
||||
import org.springframework.integration.core.MessagingException;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.MessageMatcher;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
|
||||
@@ -59,12 +61,17 @@ public class BridgeParserTests extends AbstractJUnit4SpringContextTests {
|
||||
@Qualifier("output2")
|
||||
private PollableChannel output2;
|
||||
|
||||
@Factory
|
||||
public static Matcher<Message<?>> sameExceptImmutableHeaders(Message<?> expected) {
|
||||
return new MessageMatcher(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void pollableChannel() {
|
||||
Message<?> message = new StringMessage("test1");
|
||||
this.pollableChannel.send(message);
|
||||
Message<?> reply = this.output1.receive(1000);
|
||||
assertEquals(message, reply);
|
||||
assertThat(message, sameExceptImmutableHeaders(reply));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -72,7 +79,7 @@ public class BridgeParserTests extends AbstractJUnit4SpringContextTests {
|
||||
Message<?> message = new StringMessage("test2");
|
||||
this.subscribableChannel.send(message);
|
||||
Message<?> reply = this.output2.receive(0);
|
||||
assertEquals(message, reply);
|
||||
assertThat(message, sameExceptImmutableHeaders(reply));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -81,7 +88,7 @@ public class BridgeParserTests extends AbstractJUnit4SpringContextTests {
|
||||
Message<?> message = MessageBuilder.withPayload("test3").setReplyChannel(replyChannel).build();
|
||||
this.stopperChannel.send(message);
|
||||
Message<?> reply = replyChannel.receive(0);
|
||||
assertEquals(message, reply);
|
||||
assertThat(message, sameExceptImmutableHeaders(reply));
|
||||
}
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.integration.filter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@@ -48,7 +49,9 @@ public class MessageFilterTests {
|
||||
QueueChannel output = new QueueChannel();
|
||||
filter.setOutputChannel(output);
|
||||
filter.handleMessage(message);
|
||||
assertEquals(message, output.receive(0));
|
||||
Message<?> received = output.receive(0);
|
||||
assertEquals(message.getPayload(), received.getPayload());
|
||||
assertNotSame(message.getHeaders().getId(), received.getHeaders().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -93,7 +96,7 @@ public class MessageFilterTests {
|
||||
assertTrue(inputChannel.send(message));
|
||||
Message<?> reply = outputChannel.receive(0);
|
||||
assertNotNull(reply);
|
||||
assertEquals(message, reply);
|
||||
assertEquals(message.getPayload(), reply.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -16,17 +16,18 @@
|
||||
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.hamcrest.Factory;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.channel.PollableChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.MessageHandlingException;
|
||||
import org.springframework.integration.message.MessageMatcher;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
|
||||
/**
|
||||
@@ -37,6 +38,11 @@ public class BridgeHandlerTests {
|
||||
|
||||
private BridgeHandler handler= new BridgeHandler();
|
||||
|
||||
@Factory
|
||||
public static Matcher<Message<?>> sameExceptImmutableHeaders(Message<?> expected) {
|
||||
return new MessageMatcher(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleBridge() {
|
||||
QueueChannel outputChannel = new QueueChannel();
|
||||
@@ -45,7 +51,7 @@ public class BridgeHandlerTests {
|
||||
handler.handleMessage(request);
|
||||
Message<?> reply = outputChannel.receive(0);
|
||||
assertNotNull(reply);
|
||||
assertEquals(request, reply);
|
||||
assertThat(reply, sameExceptImmutableHeaders(request));
|
||||
}
|
||||
|
||||
@Test(expected = MessageHandlingException.class)
|
||||
@@ -60,7 +66,7 @@ public class BridgeHandlerTests {
|
||||
PollableChannel replyChannel = new QueueChannel();
|
||||
Message request = MessageBuilder.withPayload("tst").setReplyChannel(replyChannel ).build();
|
||||
handler.handleMessage(request );
|
||||
assertThat(replyChannel.receive(), is(request));
|
||||
assertThat(replyChannel.receive(), sameExceptImmutableHeaders(request));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.integration.json;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
@@ -33,18 +32,27 @@ import org.codehaus.jackson.JsonGenerationException;
|
||||
import org.codehaus.jackson.map.JsonMappingException;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.type.TypeReference;
|
||||
import org.hamcrest.Factory;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageHeaders;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.MessageMatcher;
|
||||
|
||||
/**
|
||||
* @author Jeremy Grelle
|
||||
* @author Mark Fisher
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class InboundJsonMessageMapperTests {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
private ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Factory
|
||||
public static Matcher<Message<?>> sameExceptImmutableHeaders(Message<?> operand) {
|
||||
return new MessageMatcher(operand);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToMessageWithHeadersAndStringPayload() throws Exception {
|
||||
@@ -53,7 +61,7 @@ public class InboundJsonMessageMapperTests {
|
||||
Message<String> expected = MessageBuilder.withPayload("myPayloadStuff").setHeader(MessageHeaders.TIMESTAMP, new Long(1)).setHeader(MessageHeaders.ID, id).build();
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(String.class);
|
||||
Message<String> result = (Message<String>) mapper.toMessage(jsonMessage);
|
||||
assertThat(result, is(expected));
|
||||
assertThat(result, sameExceptImmutableHeaders(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -74,7 +82,7 @@ public class InboundJsonMessageMapperTests {
|
||||
Message<TestBean> expected = MessageBuilder.withPayload(bean).setHeader(MessageHeaders.TIMESTAMP, new Long(1)).setHeader(MessageHeaders.ID, id).build();
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(TestBean.class);
|
||||
Message<?> result = mapper.toMessage(jsonMessage);
|
||||
assertEquals(expected, result);
|
||||
assertThat(result, sameExceptImmutableHeaders(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -99,7 +107,7 @@ public class InboundJsonMessageMapperTests {
|
||||
headerTypes.put("myHeader", TestBean.class);
|
||||
mapper.setHeaderTypes(headerTypes);
|
||||
Message<?> result = mapper.toMessage(jsonMessage);
|
||||
assertEquals(expected, result);
|
||||
assertThat(result, sameExceptImmutableHeaders(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -110,7 +118,7 @@ public class InboundJsonMessageMapperTests {
|
||||
Message<List<String>> expected = MessageBuilder.withPayload(expectedList).setHeader(MessageHeaders.TIMESTAMP, new Long(1)).setHeader(MessageHeaders.ID, id).build();
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(new TypeReference<List<String>>(){});
|
||||
Message<?> result = mapper.toMessage(jsonMessage);
|
||||
assertEquals(expected, result);
|
||||
assertThat(result, sameExceptImmutableHeaders(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -123,7 +131,7 @@ public class InboundJsonMessageMapperTests {
|
||||
Message<List<TestBean>> expected = MessageBuilder.withPayload(expectedList).setHeader(MessageHeaders.TIMESTAMP, new Long(1)).setHeader(MessageHeaders.ID, id).build();
|
||||
InboundJsonMessageMapper mapper = new InboundJsonMessageMapper(new TypeReference<List<TestBean>>(){});
|
||||
Message<?> result = mapper.toMessage(jsonMessage);
|
||||
assertEquals(expected, result);
|
||||
assertThat(result, sameExceptImmutableHeaders(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -236,6 +244,6 @@ public class InboundJsonMessageMapperTests {
|
||||
StringWriter writer = new StringWriter();
|
||||
mapper.writeValue(writer, bean);
|
||||
return writer.toString();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -18,8 +18,10 @@ package org.springframework.integration.message;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -67,6 +69,24 @@ public class MessageBuilderTests {
|
||||
assertEquals("2", message2.getHeaders().get("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIdHeaderValues() {
|
||||
UUID id = UUID.randomUUID();
|
||||
Message<String> message = MessageBuilder.withPayload("test")
|
||||
.setHeader(MessageHeaders.ID, id)
|
||||
.build();
|
||||
assertNotSame(id, message.getHeaders().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTimestampHeaderValues() {
|
||||
Long timestamp = 12345L;
|
||||
Message<String> message = MessageBuilder.withPayload("test")
|
||||
.setHeader(MessageHeaders.TIMESTAMP, timestamp)
|
||||
.build();
|
||||
assertNotSame(timestamp, message.getHeaders().getTimestamp());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyHeadersIfAbsent() {
|
||||
Message<String> message1 = MessageBuilder.withPayload("test1")
|
||||
@@ -88,6 +108,15 @@ public class MessageBuilderTests {
|
||||
assertEquals("bar", message2.getHeaders().get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createIdRegenerated() {
|
||||
Message<String> message1 = MessageBuilder.withPayload("test")
|
||||
.setHeader("foo", "bar").build();
|
||||
Message<String> message2 = MessageBuilder.fromMessage(message1).build();
|
||||
assertEquals("bar", message2.getHeaders().get("foo"));
|
||||
assertNotSame(message1.getHeaders().getId(), message2.getHeaders().getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPriority() {
|
||||
Message<Integer> importantMessage = MessageBuilder.withPayload(1)
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.integration.message;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@@ -44,6 +45,27 @@ public class MessageHeadersTests {
|
||||
assertNotNull(headers.getTimestamp());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTimestampOverwritten() throws Exception {
|
||||
MessageHeaders headers1 = new MessageHeaders(null);
|
||||
Thread.sleep(50L);
|
||||
MessageHeaders headers2 = new MessageHeaders(headers1);
|
||||
assertNotSame(headers1.getTimestamp(), headers2.getTimestamp());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIdOverwritten() throws Exception {
|
||||
MessageHeaders headers1 = new MessageHeaders(null);
|
||||
MessageHeaders headers2 = new MessageHeaders(headers1);
|
||||
assertNotSame(headers1.getId(), headers2.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testId() {
|
||||
MessageHeaders headers = new MessageHeaders(null);
|
||||
assertNotNull(headers.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonTypedAccessOfHeaderValue() {
|
||||
Integer value = new Integer(123);
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.springframework.integration.message;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.springframework.integration.core.Message;
|
||||
import org.springframework.integration.core.MessageHeaders;
|
||||
|
||||
/**
|
||||
* Matcher to make assertions about message equality easier. Usage:
|
||||
*
|
||||
* <pre>
|
||||
* @Test
|
||||
* public void testSomething() {
|
||||
* Message<String> expected = ...;
|
||||
* Message<String> result = ...;
|
||||
* assertThat(result, sameExceptImmutableHeaders(expected));
|
||||
* }
|
||||
*
|
||||
* @Factory
|
||||
* public static Matcher<Message<?>> sameExceptImmutableHeaders(Message<?> expected) {
|
||||
* return new MessageMatcher(expected);
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class MessageMatcher extends BaseMatcher<Message<?>> {
|
||||
|
||||
private final Object payload;
|
||||
private final Map<String, Object> headers;
|
||||
|
||||
public MessageMatcher(Message<?> operand) {
|
||||
this.payload = operand.getPayload();
|
||||
this.headers = getHeaders(operand);
|
||||
}
|
||||
|
||||
private Map<String, Object> getHeaders(Message<?> operand) {
|
||||
HashMap<String, Object> headers = new HashMap<String, Object>(operand.getHeaders());
|
||||
headers.remove(MessageHeaders.ID);
|
||||
headers.remove(MessageHeaders.TIMESTAMP);
|
||||
return headers;
|
||||
}
|
||||
|
||||
public boolean matches(Object arg) {
|
||||
Message<?> input = (Message<?>) arg;
|
||||
Map<String, Object> inputHeaders = getHeaders(input);
|
||||
return input.getPayload().equals(payload) && inputHeaders.equals(headers);
|
||||
}
|
||||
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("Headers match except ID and timestamp for payload: ").appendValue(payload).appendText(" and headers: ").appendValue(headers);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user