Skip to content

Commit

Permalink
mergePropertiesIntoMap copies non-String values as well (SPR-5669)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Apr 17, 2009
1 parent c225b44 commit 8ee0363
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -99,7 +99,12 @@ public static void mergePropertiesIntoMap(Properties props, Map map) {
if (props != null) {
for (Enumeration en = props.propertyNames(); en.hasMoreElements();) {
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);
}
}
}
Expand Down
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,15 +28,17 @@
import java.util.Properties;
import java.util.Set;

import junit.framework.TestCase;
import static org.junit.Assert.*;
import org.junit.Test;

/**
* @author Rob Harrop
* @author Juergen Hoeller
* @author Rick Evans
*/
public class CollectionUtilsTests extends TestCase {
public class CollectionUtilsTests {

@Test
public void testIsEmpty() {
assertTrue(CollectionUtils.isEmpty((Set) null));
assertTrue(CollectionUtils.isEmpty((Map) null));
Expand All @@ -52,6 +54,7 @@ public void testIsEmpty() {
assertFalse(CollectionUtils.isEmpty(map));
}

@Test
public void testMergeArrayIntoCollection() {
Object[] arr = new Object[] {"value1", "value2"};
List list = new LinkedList();
Expand All @@ -63,6 +66,7 @@ public void testMergeArrayIntoCollection() {
assertEquals("value2", list.get(2));
}

@Test
public void testMergePrimitiveArrayIntoCollection() {
int[] arr = new int[] {1, 2};
List list = new LinkedList();
Expand All @@ -74,21 +78,25 @@ public void testMergePrimitiveArrayIntoCollection() {
assertEquals(new Integer(2), list.get(2));
}

@Test
public void testMergePropertiesIntoMap() {
Properties defaults = new Properties();
defaults.setProperty("prop1", "value1");
Properties props = new Properties(defaults);
props.setProperty("prop2", "value2");
props.put("prop3", new Integer(3));

Map map = new HashMap();
map.put("prop3", "value3");
map.put("prop4", "value4");

CollectionUtils.mergePropertiesIntoMap(props, map);
assertEquals("value1", map.get("prop1"));
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() {
assertFalse(CollectionUtils.contains((Iterator) null, "myElement"));
assertFalse(CollectionUtils.contains((Enumeration) null, "myElement"));
Expand All @@ -104,6 +112,7 @@ public void testContains() {
assertTrue(CollectionUtils.contains(ht.keys(), "myElement"));
}

@Test
public void testContainsAny() throws Exception {
List source = new ArrayList();
source.add("abc");
Expand All @@ -122,18 +131,21 @@ public void testContainsAny() throws Exception {
assertFalse(CollectionUtils.containsAny(source, candidates));
}

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

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

@Test
public void testContainsInstanceWithSameInstance() throws Exception {
List list = new ArrayList();
list.add(new Instance("apple"));
Expand All @@ -143,6 +155,7 @@ public void testContainsInstanceWithSameInstance() throws Exception {
CollectionUtils.containsInstance(list, instance));
}

@Test
public void testContainsInstanceWithNullInstance() throws Exception {
List list = new ArrayList();
list.add(new Instance("apple"));
Expand All @@ -151,6 +164,7 @@ public void testContainsInstanceWithNullInstance() throws Exception {
CollectionUtils.containsInstance(list, null));
}

@Test
public void testFindFirstMatch() throws Exception {
List source = new ArrayList();
source.add("abc");
Expand All @@ -165,6 +179,7 @@ public void testFindFirstMatch() throws Exception {
assertEquals("def", CollectionUtils.findFirstMatch(source, candidates));
}

@Test
public void testHasUniqueObject() {
List list = new LinkedList();
list.add("myElement");
Expand Down

0 comments on commit 8ee0363

Please sign in to comment.