INT-1387 added 'read' method to MessageHistory and committed some unit tests

This commit is contained in:
Mark Fisher
2010-08-29 22:00:54 +00:00
parent b8ed5eeffd
commit b9b877cc6b
2 changed files with 81 additions and 0 deletions

View File

@@ -49,6 +49,11 @@ public class MessageHistory implements List<Properties> {
private final List<Properties> components;
public static MessageHistory read(Message<?> message) {
return (message != null) ?
message.getHeaders().get(HEADER_NAME, MessageHistory.class) : null;
}
public static <T> Message<T> addComponentToHistory(Message<T> message, NamedComponent component) {
Assert.notNull(message, "Message must not be null");
Assert.notNull(component, "Component must not be null");

View File

@@ -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<String> result1 = MessageHistory.addComponentToHistory(original, new TestComponent(1));
MessageHistory history1 = MessageHistory.read(result1);
assertNotNull(history1);
assertEquals("testComponent-1", history1.toString());
Message<String> 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;
}
}
}