Skip to content

Commit 8ee0363

Browse files
committed
mergePropertiesIntoMap copies non-String values as well (SPR-5669)
1 parent c225b44 commit 8ee0363

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

org.springframework.core/src/main/java/org/springframework/util/CollectionUtils.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -99,7 +99,12 @@ public static void mergePropertiesIntoMap(Properties props, Map map) {
9999
if (props != null) {
100100
for (Enumeration en = props.propertyNames(); en.hasMoreElements();) {
101101
String key = (String) en.nextElement();
102-
map.put(key, props.getProperty(key));
102+
Object value = props.getProperty(key);
103+
if (value == null) {
104+
// Potentially a non-String value...
105+
value = props.get(key);
106+
}
107+
map.put(key, value);
103108
}
104109
}
105110
}

org.springframework.core/src/test/java/org/springframework/util/CollectionUtilsTests.java

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2009 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,15 +28,17 @@
2828
import java.util.Properties;
2929
import java.util.Set;
3030

31-
import junit.framework.TestCase;
31+
import static org.junit.Assert.*;
32+
import org.junit.Test;
3233

3334
/**
3435
* @author Rob Harrop
3536
* @author Juergen Hoeller
3637
* @author Rick Evans
3738
*/
38-
public class CollectionUtilsTests extends TestCase {
39+
public class CollectionUtilsTests {
3940

41+
@Test
4042
public void testIsEmpty() {
4143
assertTrue(CollectionUtils.isEmpty((Set) null));
4244
assertTrue(CollectionUtils.isEmpty((Map) null));
@@ -52,6 +54,7 @@ public void testIsEmpty() {
5254
assertFalse(CollectionUtils.isEmpty(map));
5355
}
5456

57+
@Test
5558
public void testMergeArrayIntoCollection() {
5659
Object[] arr = new Object[] {"value1", "value2"};
5760
List list = new LinkedList();
@@ -63,6 +66,7 @@ public void testMergeArrayIntoCollection() {
6366
assertEquals("value2", list.get(2));
6467
}
6568

69+
@Test
6670
public void testMergePrimitiveArrayIntoCollection() {
6771
int[] arr = new int[] {1, 2};
6872
List list = new LinkedList();
@@ -74,21 +78,25 @@ public void testMergePrimitiveArrayIntoCollection() {
7478
assertEquals(new Integer(2), list.get(2));
7579
}
7680

81+
@Test
7782
public void testMergePropertiesIntoMap() {
7883
Properties defaults = new Properties();
7984
defaults.setProperty("prop1", "value1");
8085
Properties props = new Properties(defaults);
8186
props.setProperty("prop2", "value2");
87+
props.put("prop3", new Integer(3));
8288

8389
Map map = new HashMap();
84-
map.put("prop3", "value3");
90+
map.put("prop4", "value4");
8591

8692
CollectionUtils.mergePropertiesIntoMap(props, map);
8793
assertEquals("value1", map.get("prop1"));
8894
assertEquals("value2", map.get("prop2"));
89-
assertEquals("value3", map.get("prop3"));
95+
assertEquals(new Integer(3), map.get("prop3"));
96+
assertEquals("value4", map.get("prop4"));
9097
}
9198

99+
@Test
92100
public void testContains() {
93101
assertFalse(CollectionUtils.contains((Iterator) null, "myElement"));
94102
assertFalse(CollectionUtils.contains((Enumeration) null, "myElement"));
@@ -104,6 +112,7 @@ public void testContains() {
104112
assertTrue(CollectionUtils.contains(ht.keys(), "myElement"));
105113
}
106114

115+
@Test
107116
public void testContainsAny() throws Exception {
108117
List source = new ArrayList();
109118
source.add("abc");
@@ -122,18 +131,21 @@ public void testContainsAny() throws Exception {
122131
assertFalse(CollectionUtils.containsAny(source, candidates));
123132
}
124133

134+
@Test
125135
public void testContainsInstanceWithNullCollection() throws Exception {
126136
assertFalse("Must return false if supplied Collection argument is null",
127137
CollectionUtils.containsInstance(null, this));
128138
}
129139

140+
@Test
130141
public void testContainsInstanceWithInstancesThatAreEqualButDistinct() throws Exception {
131142
List list = new ArrayList();
132143
list.add(new Instance("fiona"));
133144
assertFalse("Must return false if instance is not in the supplied Collection argument",
134145
CollectionUtils.containsInstance(list, new Instance("fiona")));
135146
}
136147

148+
@Test
137149
public void testContainsInstanceWithSameInstance() throws Exception {
138150
List list = new ArrayList();
139151
list.add(new Instance("apple"));
@@ -143,6 +155,7 @@ public void testContainsInstanceWithSameInstance() throws Exception {
143155
CollectionUtils.containsInstance(list, instance));
144156
}
145157

158+
@Test
146159
public void testContainsInstanceWithNullInstance() throws Exception {
147160
List list = new ArrayList();
148161
list.add(new Instance("apple"));
@@ -151,6 +164,7 @@ public void testContainsInstanceWithNullInstance() throws Exception {
151164
CollectionUtils.containsInstance(list, null));
152165
}
153166

167+
@Test
154168
public void testFindFirstMatch() throws Exception {
155169
List source = new ArrayList();
156170
source.add("abc");
@@ -165,6 +179,7 @@ public void testFindFirstMatch() throws Exception {
165179
assertEquals("def", CollectionUtils.findFirstMatch(source, candidates));
166180
}
167181

182+
@Test
168183
public void testHasUniqueObject() {
169184
List list = new LinkedList();
170185
list.add("myElement");

0 commit comments

Comments
 (0)