CollectionUtilsTests.java 7.1 KB
Newer Older
1
/*
2
 * Copyright 2002-2012 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.util;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

31 32
import static org.junit.Assert.*;
import org.junit.Test;
33 34 35 36 37 38

/**
 * @author Rob Harrop
 * @author Juergen Hoeller
 * @author Rick Evans
 */
39
public class CollectionUtilsTests {
40

41
	@Test
42
	public void testIsEmpty() {
C
Chris Beams 已提交
43 44 45 46
		assertTrue(CollectionUtils.isEmpty((Set<Object>) null));
		assertTrue(CollectionUtils.isEmpty((Map<String, String>) null));
		assertTrue(CollectionUtils.isEmpty(new HashMap<String, String>()));
		assertTrue(CollectionUtils.isEmpty(new HashSet<Object>()));
47

C
Chris Beams 已提交
48
		List<Object> list = new LinkedList<Object>();
49 50 51
		list.add(new Object());
		assertFalse(CollectionUtils.isEmpty(list));

C
Chris Beams 已提交
52
		Map<String, String> map = new HashMap<String, String>();
53 54 55 56
		map.put("foo", "bar");
		assertFalse(CollectionUtils.isEmpty(map));
	}

57
	@Test
58 59
	public void testMergeArrayIntoCollection() {
		Object[] arr = new Object[] {"value1", "value2"};
C
Chris Beams 已提交
60
		List<Comparable<?>> list = new LinkedList<Comparable<?>>();
61 62 63 64 65 66 67 68
		list.add("value3");

		CollectionUtils.mergeArrayIntoCollection(arr, list);
		assertEquals("value3", list.get(0));
		assertEquals("value1", list.get(1));
		assertEquals("value2", list.get(2));
	}

69
	@Test
70 71
	public void testMergePrimitiveArrayIntoCollection() {
		int[] arr = new int[] {1, 2};
C
Chris Beams 已提交
72
		List<Comparable<?>> list = new LinkedList<Comparable<?>>();
73 74 75 76 77 78 79 80
		list.add(new Integer(3));

		CollectionUtils.mergeArrayIntoCollection(arr, list);
		assertEquals(new Integer(3), list.get(0));
		assertEquals(new Integer(1), list.get(1));
		assertEquals(new Integer(2), list.get(2));
	}

81
	@Test
82 83 84 85 86
	public void testMergePropertiesIntoMap() {
		Properties defaults = new Properties();
		defaults.setProperty("prop1", "value1");
		Properties props = new Properties(defaults);
		props.setProperty("prop2", "value2");
87
		props.put("prop3", new Integer(3));
88

C
Chris Beams 已提交
89
		Map<String, String> map = new HashMap<String, String>();
90
		map.put("prop4", "value4");
91 92 93 94

		CollectionUtils.mergePropertiesIntoMap(props, map);
		assertEquals("value1", map.get("prop1"));
		assertEquals("value2", map.get("prop2"));
95 96
		assertEquals(new Integer(3), map.get("prop3"));
		assertEquals("value4", map.get("prop4"));
97 98
	}

99
	@Test
100
	public void testContains() {
C
Chris Beams 已提交
101 102 103 104
		assertFalse(CollectionUtils.contains((Iterator<String>) null, "myElement"));
		assertFalse(CollectionUtils.contains((Enumeration<String>) null, "myElement"));
		assertFalse(CollectionUtils.contains(new LinkedList<String>().iterator(), "myElement"));
		assertFalse(CollectionUtils.contains(new Hashtable<String, Object>().keys(), "myElement"));
105

C
Chris Beams 已提交
106
		List<String> list = new LinkedList<String>();
107 108 109
		list.add("myElement");
		assertTrue(CollectionUtils.contains(list.iterator(), "myElement"));

C
Chris Beams 已提交
110
		Hashtable<String, String> ht = new Hashtable<String, String>();
111 112 113 114
		ht.put("myElement", "myValue");
		assertTrue(CollectionUtils.contains(ht.keys(), "myElement"));
	}

115
	@Test
116
	public void testContainsAny() throws Exception {
C
Chris Beams 已提交
117
		List<String> source = new ArrayList<String>();
118 119 120 121
		source.add("abc");
		source.add("def");
		source.add("ghi");

C
Chris Beams 已提交
122
		List<String> candidates = new ArrayList<String>();
123 124 125 126 127 128 129 130 131 132 133
		candidates.add("xyz");
		candidates.add("def");
		candidates.add("abc");

		assertTrue(CollectionUtils.containsAny(source, candidates));
		candidates.remove("def");
		assertTrue(CollectionUtils.containsAny(source, candidates));
		candidates.remove("abc");
		assertFalse(CollectionUtils.containsAny(source, candidates));
	}

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

140
	@Test
141
	public void testContainsInstanceWithInstancesThatAreEqualButDistinct() throws Exception {
C
Chris Beams 已提交
142
		List<Instance> list = new ArrayList<Instance>();
143 144 145 146 147
		list.add(new Instance("fiona"));
		assertFalse("Must return false if instance is not in the supplied Collection argument",
				CollectionUtils.containsInstance(list, new Instance("fiona")));
	}

148
	@Test
149
	public void testContainsInstanceWithSameInstance() throws Exception {
C
Chris Beams 已提交
150
		List<Instance> list = new ArrayList<Instance>();
151 152 153 154 155 156 157
		list.add(new Instance("apple"));
		Instance instance = new Instance("fiona");
		list.add(instance);
		assertTrue("Must return true if instance is in the supplied Collection argument",
				CollectionUtils.containsInstance(list, instance));
	}

158
	@Test
159
	public void testContainsInstanceWithNullInstance() throws Exception {
C
Chris Beams 已提交
160
		List<Instance> list = new ArrayList<Instance>();
161 162 163 164 165 166
		list.add(new Instance("apple"));
		list.add(new Instance("fiona"));
		assertFalse("Must return false if null instance is supplied",
				CollectionUtils.containsInstance(list, null));
	}

167
	@Test
168
	public void testFindFirstMatch() throws Exception {
C
Chris Beams 已提交
169
		List<String> source = new ArrayList<String>();
170 171 172 173
		source.add("abc");
		source.add("def");
		source.add("ghi");

C
Chris Beams 已提交
174
		List<String> candidates = new ArrayList<String>();
175 176 177 178 179 180 181
		candidates.add("xyz");
		candidates.add("def");
		candidates.add("abc");

		assertEquals("def", CollectionUtils.findFirstMatch(source, candidates));
	}

182
	@Test
183
	public void testHasUniqueObject() {
C
Chris Beams 已提交
184
		List<String> list = new LinkedList<String>();
185 186 187 188
		list.add("myElement");
		list.add("myOtherElement");
		assertFalse(CollectionUtils.hasUniqueObject(list));

C
Chris Beams 已提交
189
		list = new LinkedList<String>();
190 191 192
		list.add("myElement");
		assertTrue(CollectionUtils.hasUniqueObject(list));

C
Chris Beams 已提交
193
		list = new LinkedList<String>();
194 195 196 197
		list.add("myElement");
		list.add(null);
		assertFalse(CollectionUtils.hasUniqueObject(list));

C
Chris Beams 已提交
198
		list = new LinkedList<String>();
199 200 201 202
		list.add(null);
		list.add("myElement");
		assertFalse(CollectionUtils.hasUniqueObject(list));

C
Chris Beams 已提交
203
		list = new LinkedList<String>();
204 205 206 207
		list.add(null);
		list.add(null);
		assertTrue(CollectionUtils.hasUniqueObject(list));

C
Chris Beams 已提交
208
		list = new LinkedList<String>();
209 210 211
		list.add(null);
		assertTrue(CollectionUtils.hasUniqueObject(list));

C
Chris Beams 已提交
212
		list = new LinkedList<String>();
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
		assertFalse(CollectionUtils.hasUniqueObject(list));
	}


	private static final class Instance {

		private final String name;

		public Instance(String name) {
			this.name = name;
		}

		public boolean equals(Object rhs) {
			if (this == rhs) {
				return true;
			}
			if (rhs == null || this.getClass() != rhs.getClass()) {
				return false;
			}
			Instance instance = (Instance) rhs;
			return this.name.equals(instance.name);
		}

		public int hashCode() {
			return this.name.hashCode();
		}
	}

}