mergePropertiesIntoMap copies non-String values as well (SPR-5669)

This commit is contained in:
Juergen Hoeller 2009-04-17 12:21:57 +00:00
parent c225b44f34
commit 8ee0363776
2 changed files with 27 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -99,7 +99,12 @@ public abstract class CollectionUtils {
if (props != null) { if (props != null) {
for (Enumeration en = props.propertyNames(); en.hasMoreElements();) { for (Enumeration en = props.propertyNames(); en.hasMoreElements();) {
String key = (String) en.nextElement(); String key = (String) en.nextElement();
map.put(key, props.getProperty(key)); Object value = props.getProperty(key);
if (value == null) {
// Potentially a non-String value...
value = props.get(key);
}
map.put(key, value);
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,15 +28,17 @@ import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import junit.framework.TestCase; import static org.junit.Assert.*;
import org.junit.Test;
/** /**
* @author Rob Harrop * @author Rob Harrop
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rick Evans * @author Rick Evans
*/ */
public class CollectionUtilsTests extends TestCase { public class CollectionUtilsTests {
@Test
public void testIsEmpty() { public void testIsEmpty() {
assertTrue(CollectionUtils.isEmpty((Set) null)); assertTrue(CollectionUtils.isEmpty((Set) null));
assertTrue(CollectionUtils.isEmpty((Map) null)); assertTrue(CollectionUtils.isEmpty((Map) null));
@ -52,6 +54,7 @@ public class CollectionUtilsTests extends TestCase {
assertFalse(CollectionUtils.isEmpty(map)); assertFalse(CollectionUtils.isEmpty(map));
} }
@Test
public void testMergeArrayIntoCollection() { public void testMergeArrayIntoCollection() {
Object[] arr = new Object[] {"value1", "value2"}; Object[] arr = new Object[] {"value1", "value2"};
List list = new LinkedList(); List list = new LinkedList();
@ -63,6 +66,7 @@ public class CollectionUtilsTests extends TestCase {
assertEquals("value2", list.get(2)); assertEquals("value2", list.get(2));
} }
@Test
public void testMergePrimitiveArrayIntoCollection() { public void testMergePrimitiveArrayIntoCollection() {
int[] arr = new int[] {1, 2}; int[] arr = new int[] {1, 2};
List list = new LinkedList(); List list = new LinkedList();
@ -74,21 +78,25 @@ public class CollectionUtilsTests extends TestCase {
assertEquals(new Integer(2), list.get(2)); assertEquals(new Integer(2), list.get(2));
} }
@Test
public void testMergePropertiesIntoMap() { public void testMergePropertiesIntoMap() {
Properties defaults = new Properties(); Properties defaults = new Properties();
defaults.setProperty("prop1", "value1"); defaults.setProperty("prop1", "value1");
Properties props = new Properties(defaults); Properties props = new Properties(defaults);
props.setProperty("prop2", "value2"); props.setProperty("prop2", "value2");
props.put("prop3", new Integer(3));
Map map = new HashMap(); Map map = new HashMap();
map.put("prop3", "value3"); map.put("prop4", "value4");
CollectionUtils.mergePropertiesIntoMap(props, map); CollectionUtils.mergePropertiesIntoMap(props, map);
assertEquals("value1", map.get("prop1")); assertEquals("value1", map.get("prop1"));
assertEquals("value2", map.get("prop2")); assertEquals("value2", map.get("prop2"));
assertEquals("value3", map.get("prop3")); assertEquals(new Integer(3), map.get("prop3"));
assertEquals("value4", map.get("prop4"));
} }
@Test
public void testContains() { public void testContains() {
assertFalse(CollectionUtils.contains((Iterator) null, "myElement")); assertFalse(CollectionUtils.contains((Iterator) null, "myElement"));
assertFalse(CollectionUtils.contains((Enumeration) null, "myElement")); assertFalse(CollectionUtils.contains((Enumeration) null, "myElement"));
@ -104,6 +112,7 @@ public class CollectionUtilsTests extends TestCase {
assertTrue(CollectionUtils.contains(ht.keys(), "myElement")); assertTrue(CollectionUtils.contains(ht.keys(), "myElement"));
} }
@Test
public void testContainsAny() throws Exception { public void testContainsAny() throws Exception {
List source = new ArrayList(); List source = new ArrayList();
source.add("abc"); source.add("abc");
@ -122,11 +131,13 @@ public class CollectionUtilsTests extends TestCase {
assertFalse(CollectionUtils.containsAny(source, candidates)); assertFalse(CollectionUtils.containsAny(source, candidates));
} }
@Test
public void testContainsInstanceWithNullCollection() throws Exception { public void testContainsInstanceWithNullCollection() throws Exception {
assertFalse("Must return false if supplied Collection argument is null", assertFalse("Must return false if supplied Collection argument is null",
CollectionUtils.containsInstance(null, this)); CollectionUtils.containsInstance(null, this));
} }
@Test
public void testContainsInstanceWithInstancesThatAreEqualButDistinct() throws Exception { public void testContainsInstanceWithInstancesThatAreEqualButDistinct() throws Exception {
List list = new ArrayList(); List list = new ArrayList();
list.add(new Instance("fiona")); list.add(new Instance("fiona"));
@ -134,6 +145,7 @@ public class CollectionUtilsTests extends TestCase {
CollectionUtils.containsInstance(list, new Instance("fiona"))); CollectionUtils.containsInstance(list, new Instance("fiona")));
} }
@Test
public void testContainsInstanceWithSameInstance() throws Exception { public void testContainsInstanceWithSameInstance() throws Exception {
List list = new ArrayList(); List list = new ArrayList();
list.add(new Instance("apple")); list.add(new Instance("apple"));
@ -143,6 +155,7 @@ public class CollectionUtilsTests extends TestCase {
CollectionUtils.containsInstance(list, instance)); CollectionUtils.containsInstance(list, instance));
} }
@Test
public void testContainsInstanceWithNullInstance() throws Exception { public void testContainsInstanceWithNullInstance() throws Exception {
List list = new ArrayList(); List list = new ArrayList();
list.add(new Instance("apple")); list.add(new Instance("apple"));
@ -151,6 +164,7 @@ public class CollectionUtilsTests extends TestCase {
CollectionUtils.containsInstance(list, null)); CollectionUtils.containsInstance(list, null));
} }
@Test
public void testFindFirstMatch() throws Exception { public void testFindFirstMatch() throws Exception {
List source = new ArrayList(); List source = new ArrayList();
source.add("abc"); source.add("abc");
@ -165,6 +179,7 @@ public class CollectionUtilsTests extends TestCase {
assertEquals("def", CollectionUtils.findFirstMatch(source, candidates)); assertEquals("def", CollectionUtils.findFirstMatch(source, candidates));
} }
@Test
public void testHasUniqueObject() { public void testHasUniqueObject() {
List list = new LinkedList(); List list = new LinkedList();
list.add("myElement"); list.add("myElement");