From 400738e804170e364df3fcf027613bf82f458423 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Tue, 25 Nov 2014 10:23:11 +0100 Subject: [PATCH] Polish MessageHeaderAccessor Issue: SPR-12431 --- .../support/MessageHeaderAccessor.java | 21 ++++---- .../support/MessageHeaderAccessorTests.java | 53 +++++++++++++++++++ 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java b/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java index 4ecf971046..4416d14985 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java @@ -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 { private List getMatchingHeaderNames(String pattern, Map headers) { List matchingHeaderNames = new ArrayList(); if (headers != null) { - for (Map.Entry 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 { */ public void copyHeaders(Map headersToCopy) { if (headersToCopy != null) { - Set keys = headersToCopy.keySet(); - for (String key : keys) { - if (!isReadOnly(key)) { - setHeader(key, headersToCopy.get(key)); + for (Map.Entry entry : headersToCopy.entrySet()) { + if (!isReadOnly(entry.getKey())) { + setHeader(entry.getKey(), entry.getValue()); } } } @@ -404,10 +402,9 @@ public class MessageHeaderAccessor { */ public void copyHeadersIfAbsent(Map headersToCopy) { if (headersToCopy != null) { - Set keys = headersToCopy.keySet(); - for (String key : keys) { - if (!isReadOnly(key)) { - setHeaderIfAbsent(key, headersToCopy.get(key)); + for (Map.Entry entry : headersToCopy.entrySet()) { + if (!isReadOnly(entry.getKey())) { + setHeaderIfAbsent(entry.getKey(), entry.getValue()); } } } diff --git a/spring-messaging/src/test/java/org/springframework/messaging/support/MessageHeaderAccessorTests.java b/spring-messaging/src/test/java/org/springframework/messaging/support/MessageHeaderAccessorTests.java index aa8d92fbb6..a09429fb62 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/support/MessageHeaderAccessorTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/support/MessageHeaderAccessorTests.java @@ -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 { assertEquals("baz", actual.get("bar")); } + @Test + public void removeHeaders() { + Map map = new HashMap<>(); + map.put("foo", "bar"); + map.put("bar", "baz"); + GenericMessage 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 map1 = new HashMap<>(); + map1.put("foo", "bar"); + GenericMessage message = new GenericMessage<>("payload", map1); + MessageHeaderAccessor accessor = new MessageHeaderAccessor(message); + + Map 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 map1 = new HashMap<>(); + map1.put("foo", "bar"); + GenericMessage message = new GenericMessage<>("payload", map1); + MessageHeaderAccessor accessor = new MessageHeaderAccessor(message); + + Map 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();