Browse Source

Polish MessageHeaderAccessor

Issue: SPR-12431
pull/701/head
Sebastien Deleuze 10 years ago
parent
commit
400738e804
  1. 21
      spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java
  2. 53
      spring-messaging/src/test/java/org/springframework/messaging/support/MessageHeaderAccessorTests.java

21
spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java

@ -22,7 +22,6 @@ import java.util.Arrays; @@ -22,7 +22,6 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.logging.Log;
@ -373,9 +372,9 @@ public class MessageHeaderAccessor { @@ -373,9 +372,9 @@ public class MessageHeaderAccessor {
private List<String> getMatchingHeaderNames(String pattern, Map<String, Object> headers) {
List<String> matchingHeaderNames = new ArrayList<String>();
if (headers != null) {
for (Map.Entry<String, Object> header: headers.entrySet()) {
if (PatternMatchUtils.simpleMatch(pattern, header.getKey())) {
matchingHeaderNames.add(header.getKey());
for (String key : headers.keySet()) {
if (PatternMatchUtils.simpleMatch(pattern, key)) {
matchingHeaderNames.add(key);
}
}
}
@ -389,10 +388,9 @@ public class MessageHeaderAccessor { @@ -389,10 +388,9 @@ public class MessageHeaderAccessor {
*/
public void copyHeaders(Map<String, ?> headersToCopy) {
if (headersToCopy != null) {
Set<String> keys = headersToCopy.keySet();
for (String key : keys) {
if (!isReadOnly(key)) {
setHeader(key, headersToCopy.get(key));
for (Map.Entry<String, ?> entry : headersToCopy.entrySet()) {
if (!isReadOnly(entry.getKey())) {
setHeader(entry.getKey(), entry.getValue());
}
}
}
@ -404,10 +402,9 @@ public class MessageHeaderAccessor { @@ -404,10 +402,9 @@ public class MessageHeaderAccessor {
*/
public void copyHeadersIfAbsent(Map<String, ?> headersToCopy) {
if (headersToCopy != null) {
Set<String> keys = headersToCopy.keySet();
for (String key : keys) {
if (!isReadOnly(key)) {
setHeaderIfAbsent(key, headersToCopy.get(key));
for (Map.Entry<String, ?> entry : headersToCopy.entrySet()) {
if (!isReadOnly(entry.getKey())) {
setHeaderIfAbsent(entry.getKey(), entry.getValue());
}
}
}

53
spring-messaging/src/test/java/org/springframework/messaging/support/MessageHeaderAccessorTests.java

@ -39,6 +39,7 @@ import static org.junit.Assert.*; @@ -39,6 +39,7 @@ import static org.junit.Assert.*;
* Test fixture for {@link MessageHeaderAccessor}.
*
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
*/
public class MessageHeaderAccessorTests {
@ -88,6 +89,58 @@ public class MessageHeaderAccessorTests { @@ -88,6 +89,58 @@ public class MessageHeaderAccessorTests {
assertEquals("baz", actual.get("bar"));
}
@Test
public void removeHeaders() {
Map<String, Object> map = new HashMap<>();
map.put("foo", "bar");
map.put("bar", "baz");
GenericMessage<String> message = new GenericMessage<>("payload", map);
MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
accessor.removeHeaders("fo*");
MessageHeaders actual = accessor.getMessageHeaders();
assertEquals(2, actual.size());
assertNull(actual.get("foo"));
assertEquals("baz", actual.get("bar"));
}
@Test
public void copyHeaders() {
Map<String, Object> map1 = new HashMap<>();
map1.put("foo", "bar");
GenericMessage<String> message = new GenericMessage<>("payload", map1);
MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
Map<String, Object> map2 = new HashMap<>();
map2.put("foo", "BAR");
map2.put("bar", "baz");
accessor.copyHeaders(map2);
MessageHeaders actual = accessor.getMessageHeaders();
assertEquals(3, actual.size());
assertEquals("BAR", actual.get("foo"));
assertEquals("baz", actual.get("bar"));
}
@Test
public void copyHeadersIfAbsent() {
Map<String, Object> map1 = new HashMap<>();
map1.put("foo", "bar");
GenericMessage<String> message = new GenericMessage<>("payload", map1);
MessageHeaderAccessor accessor = new MessageHeaderAccessor(message);
Map<String, Object> map2 = new HashMap<>();
map2.put("foo", "BAR");
map2.put("bar", "baz");
accessor.copyHeadersIfAbsent(map2);
MessageHeaders actual = accessor.getMessageHeaders();
assertEquals(3, actual.size());
assertEquals("bar", actual.get("foo"));
assertEquals("baz", actual.get("bar"));
}
@Test
public void copyHeadersFromNullMap() {
MessageHeaderAccessor headers = new MessageHeaderAccessor();

Loading…
Cancel
Save