diff --git a/spring-integration-core/src/main/java/org/springframework/integration/history/MessageHistory.java b/spring-integration-core/src/main/java/org/springframework/integration/history/MessageHistory.java index dcf0fbe1b9..c0cc5ae2f1 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/history/MessageHistory.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/history/MessageHistory.java @@ -49,6 +49,11 @@ public class MessageHistory implements List { private final List components; + public static MessageHistory read(Message message) { + return (message != null) ? + message.getHeaders().get(HEADER_NAME, MessageHistory.class) : null; + } + public static Message addComponentToHistory(Message message, NamedComponent component) { Assert.notNull(message, "Message must not be null"); Assert.notNull(component, "Component must not be null"); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/core/MessageHistoryTests.java b/spring-integration-core/src/test/java/org/springframework/integration/core/MessageHistoryTests.java new file mode 100644 index 0000000000..bd0b43f4c6 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/core/MessageHistoryTests.java @@ -0,0 +1,76 @@ +/* + * Copyright 2002-2010 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.integration.core; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.Properties; + +import org.junit.Test; + +import org.springframework.integration.Message; +import org.springframework.integration.context.NamedComponent; +import org.springframework.integration.history.MessageHistory; + +/** + * @author Mark Fisher + * @since 2.0 + */ +public class MessageHistoryTests { + + @Test + public void addComponents() { + StringMessage original = new StringMessage("foo"); + assertNull(MessageHistory.read(original)); + Message result1 = MessageHistory.addComponentToHistory(original, new TestComponent(1)); + MessageHistory history1 = MessageHistory.read(result1); + assertNotNull(history1); + assertEquals("testComponent-1", history1.toString()); + Message result2 = MessageHistory.addComponentToHistory(result1, new TestComponent(2)); + MessageHistory history2 = MessageHistory.read(result2); + assertNotNull(history2); + assertEquals("testComponent-1,testComponent-2", history2.toString()); + } + + @Test(expected = UnsupportedOperationException.class) + public void verifyImmutability() { + Message message = MessageHistory.addComponentToHistory(MessageBuilder.withPayload("test").build(), new TestComponent(1)); + MessageHistory history = MessageHistory.read(message); + history.add(new Properties()); + } + + + private static class TestComponent implements NamedComponent { + + private final int id; + + private TestComponent(int id) { + this.id = id; + } + + public String getComponentName() { + return "testComponent-" + this.id; + } + + public String getComponentType() { + return "type-" + this.id; + } + } + +}