moved collection utilities to util
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package org.springframework.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Test case for {@link CompositeIterator}.
|
||||
*
|
||||
* @author Erwin Vervaet
|
||||
*/
|
||||
public class CompositeIteratorTests extends TestCase {
|
||||
|
||||
public void testNoIterators() {
|
||||
CompositeIterator it = new CompositeIterator();
|
||||
assertFalse(it.hasNext());
|
||||
try {
|
||||
it.next();
|
||||
fail();
|
||||
} catch (NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testSingleIterator() {
|
||||
CompositeIterator it = new CompositeIterator();
|
||||
it.add(Arrays.asList(new String[] { "0", "1" }).iterator());
|
||||
for (int i = 0; i < 2; i++) {
|
||||
assertTrue(it.hasNext());
|
||||
assertEquals(String.valueOf(i), it.next());
|
||||
}
|
||||
assertFalse(it.hasNext());
|
||||
try {
|
||||
it.next();
|
||||
fail();
|
||||
} catch (NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testMultipleIterators() {
|
||||
CompositeIterator it = new CompositeIterator();
|
||||
it.add(Arrays.asList(new String[] { "0", "1" }).iterator());
|
||||
it.add(Arrays.asList(new String[] { "2" }).iterator());
|
||||
it.add(Arrays.asList(new String[] { "3", "4" }).iterator());
|
||||
for (int i = 0; i < 5; i++) {
|
||||
assertTrue(it.hasNext());
|
||||
assertEquals(String.valueOf(i), it.next());
|
||||
}
|
||||
assertFalse(it.hasNext());
|
||||
try {
|
||||
it.next();
|
||||
fail();
|
||||
} catch (NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testInUse() {
|
||||
List list = Arrays.asList(new String[] { "0", "1" });
|
||||
CompositeIterator it = new CompositeIterator();
|
||||
it.add(list.iterator());
|
||||
it.hasNext();
|
||||
try {
|
||||
it.add(list.iterator());
|
||||
fail();
|
||||
} catch (IllegalStateException e) {
|
||||
// expected
|
||||
}
|
||||
it = new CompositeIterator();
|
||||
it.add(list.iterator());
|
||||
it.next();
|
||||
try {
|
||||
it.add(list.iterator());
|
||||
fail();
|
||||
} catch (IllegalStateException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testDuplicateIterators() {
|
||||
List list = Arrays.asList(new String[] { "0", "1" });
|
||||
Iterator iterator = list.iterator();
|
||||
CompositeIterator it = new CompositeIterator();
|
||||
it.add(iterator);
|
||||
it.add(list.iterator());
|
||||
try {
|
||||
it.add(iterator);
|
||||
fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.springframework.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.springframework.binding.collection.SharedMapDecorator}.
|
||||
*/
|
||||
public class SharedMapDecoratorTests extends TestCase {
|
||||
|
||||
private SharedMapDecorator map = new SharedMapDecorator(new HashMap());
|
||||
|
||||
public void testGetPutRemove() {
|
||||
assertTrue(map.size() == 0);
|
||||
assertTrue(map.isEmpty());
|
||||
assertNull(map.get("foo"));
|
||||
assertFalse(map.containsKey("foo"));
|
||||
map.put("foo", "bar");
|
||||
assertTrue(map.size() == 1);
|
||||
assertFalse(map.isEmpty());
|
||||
assertNotNull(map.get("foo"));
|
||||
assertTrue(map.containsKey("foo"));
|
||||
assertTrue(map.containsValue("bar"));
|
||||
assertEquals("bar", map.get("foo"));
|
||||
map.remove("foo");
|
||||
assertTrue(map.size() == 0);
|
||||
assertNull(map.get("foo"));
|
||||
}
|
||||
|
||||
public void testPutAll() {
|
||||
Map all = new HashMap();
|
||||
all.put("foo", "bar");
|
||||
all.put("bar", "baz");
|
||||
map.putAll(all);
|
||||
assertTrue(map.size() == 2);
|
||||
}
|
||||
|
||||
public void testEntrySet() {
|
||||
map.put("foo", "bar");
|
||||
map.put("bar", "baz");
|
||||
Set entrySet = map.entrySet();
|
||||
assertTrue(entrySet.size() == 2);
|
||||
}
|
||||
|
||||
public void testKeySet() {
|
||||
map.put("foo", "bar");
|
||||
map.put("bar", "baz");
|
||||
Set keySet = map.keySet();
|
||||
assertTrue(keySet.size() == 2);
|
||||
}
|
||||
|
||||
public void testValues() {
|
||||
map.put("foo", "bar");
|
||||
map.put("bar", "baz");
|
||||
Collection values = map.values();
|
||||
assertTrue(values.size() == 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package org.springframework.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.springframework.binding.collection.StringKeyedMapAdapter}.
|
||||
*/
|
||||
public class StringKeyedMapAdapterTests extends TestCase {
|
||||
|
||||
private Map contents = new HashMap();
|
||||
|
||||
private StringKeyedMapAdapter map = new StringKeyedMapAdapter() {
|
||||
|
||||
protected Object getAttribute(String key) {
|
||||
return contents.get(key);
|
||||
}
|
||||
|
||||
protected Iterator getAttributeNames() {
|
||||
return contents.keySet().iterator();
|
||||
}
|
||||
|
||||
protected void removeAttribute(String key) {
|
||||
contents.remove(key);
|
||||
}
|
||||
|
||||
protected void setAttribute(String key, Object value) {
|
||||
contents.put(key, value);
|
||||
}
|
||||
};
|
||||
|
||||
public void testGetPutRemove() {
|
||||
assertTrue(map.size() == 0);
|
||||
assertTrue(map.isEmpty());
|
||||
assertNull(map.get("foo"));
|
||||
assertFalse(map.containsKey("foo"));
|
||||
map.put("foo", "bar");
|
||||
assertTrue(map.size() == 1);
|
||||
assertFalse(map.isEmpty());
|
||||
assertNotNull(map.get("foo"));
|
||||
assertTrue(map.containsKey("foo"));
|
||||
assertTrue(map.containsValue("bar"));
|
||||
assertEquals("bar", map.get("foo"));
|
||||
map.remove("foo");
|
||||
assertTrue(map.size() == 0);
|
||||
assertNull(map.get("foo"));
|
||||
}
|
||||
|
||||
public void testPutAll() {
|
||||
Map all = new HashMap();
|
||||
all.put("foo", "bar");
|
||||
all.put("bar", "baz");
|
||||
map.putAll(all);
|
||||
assertTrue(map.size() == 2);
|
||||
}
|
||||
|
||||
public void testEntrySet() {
|
||||
map.put("foo", "bar");
|
||||
map.put("bar", "baz");
|
||||
Set entrySet = map.entrySet();
|
||||
assertTrue(entrySet.size() == 2);
|
||||
}
|
||||
|
||||
public void testKeySet() {
|
||||
map.put("foo", "bar");
|
||||
map.put("bar", "baz");
|
||||
Set keySet = map.keySet();
|
||||
assertTrue(keySet.size() == 2);
|
||||
}
|
||||
|
||||
public void testValues() {
|
||||
map.put("foo", "bar");
|
||||
map.put("bar", "baz");
|
||||
Collection values = map.values();
|
||||
assertTrue(values.size() == 2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user