Complete unmodifiable map implementations and tests
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131363 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5a0b3842e4
commit
87da39f9cc
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/map/UnmodifiableMap.java,v 1.1 2003/11/16 00:05:45 scolebourne Exp $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/map/UnmodifiableMap.java,v 1.2 2003/11/20 22:35:50 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
|
@ -63,6 +63,7 @@ import java.util.Iterator;
|
|||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.collections.Unmodifiable;
|
||||
import org.apache.commons.collections.collection.UnmodifiableCollection;
|
||||
import org.apache.commons.collections.iterators.AbstractIteratorDecorator;
|
||||
import org.apache.commons.collections.pairs.AbstractMapEntryDecorator;
|
||||
|
@ -72,11 +73,11 @@ import org.apache.commons.collections.set.UnmodifiableSet;
|
|||
* Decorates another <code>Map</code> to ensure it can't be altered.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/16 00:05:45 $
|
||||
* @version $Revision: 1.2 $ $Date: 2003/11/20 22:35:50 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class UnmodifiableMap extends AbstractMapDecorator {
|
||||
public final class UnmodifiableMap extends AbstractMapDecorator implements Unmodifiable {
|
||||
|
||||
/**
|
||||
* Factory method to create an unmodifiable map.
|
||||
|
@ -85,6 +86,9 @@ public class UnmodifiableMap extends AbstractMapDecorator {
|
|||
* @throws IllegalArgumentException if map is null
|
||||
*/
|
||||
public static Map decorate(Map map) {
|
||||
if (map instanceof Unmodifiable) {
|
||||
return map;
|
||||
}
|
||||
return new UnmodifiableMap(map);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,149 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/map/UnmodifiableOrderedMap.java,v 1.1 2003/11/20 22:35:50 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections.map;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.collections.Unmodifiable;
|
||||
import org.apache.commons.collections.collection.UnmodifiableCollection;
|
||||
import org.apache.commons.collections.iterators.MapIterator;
|
||||
import org.apache.commons.collections.iterators.OrderedMapIterator;
|
||||
import org.apache.commons.collections.iterators.UnmodifiableMapIterator;
|
||||
import org.apache.commons.collections.iterators.UnmodifiableOrderedMapIterator;
|
||||
import org.apache.commons.collections.map.UnmodifiableMap.UnmodifiableEntrySet;
|
||||
import org.apache.commons.collections.set.UnmodifiableSet;
|
||||
|
||||
/**
|
||||
* Decorates another <code>OrderedMap</code> to ensure it can't be altered.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/20 22:35:50 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public final class UnmodifiableOrderedMap extends AbstractOrderedMapDecorator implements Unmodifiable {
|
||||
|
||||
/**
|
||||
* Factory method to create an unmodifiable sorted map.
|
||||
*
|
||||
* @param map the map to decorate, must not be null
|
||||
* @throws IllegalArgumentException if map is null
|
||||
*/
|
||||
public static OrderedMap decorate(OrderedMap map) {
|
||||
if (map instanceof Unmodifiable) {
|
||||
return map;
|
||||
}
|
||||
return new UnmodifiableOrderedMap(map);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
*
|
||||
* @param map the map to decorate, must not be null
|
||||
* @throws IllegalArgumentException if map is null
|
||||
*/
|
||||
protected UnmodifiableOrderedMap(OrderedMap map) {
|
||||
super(map);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public MapIterator mapIterator() {
|
||||
MapIterator it = getOrderedMap().mapIterator();
|
||||
return UnmodifiableMapIterator.decorate(it);
|
||||
}
|
||||
|
||||
public OrderedMapIterator orderedMapIterator() {
|
||||
OrderedMapIterator it = getOrderedMap().orderedMapIterator();
|
||||
return UnmodifiableOrderedMapIterator.decorate(it);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Object put(Object key, Object value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public void putAll(Map mapToCopy) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Object remove(Object key) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Set entrySet() {
|
||||
Set set = super.entrySet();
|
||||
return new UnmodifiableEntrySet(set);
|
||||
}
|
||||
|
||||
public Set keySet() {
|
||||
Set set = super.keySet();
|
||||
return UnmodifiableSet.decorate(set);
|
||||
}
|
||||
|
||||
public Collection values() {
|
||||
Collection coll = super.values();
|
||||
return UnmodifiableCollection.decorate(coll);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/map/UnmodifiableSortedMap.java,v 1.1 2003/11/16 00:05:45 scolebourne Exp $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/map/UnmodifiableSortedMap.java,v 1.2 2003/11/20 22:35:50 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
|
@ -57,19 +57,26 @@
|
|||
*/
|
||||
package org.apache.commons.collections.map;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
|
||||
import org.apache.commons.collections.Unmodifiable;
|
||||
import org.apache.commons.collections.collection.UnmodifiableCollection;
|
||||
import org.apache.commons.collections.map.UnmodifiableMap.UnmodifiableEntrySet;
|
||||
import org.apache.commons.collections.set.UnmodifiableSet;
|
||||
|
||||
/**
|
||||
* Decorates another <code>SortedMap</code> to ensure it can't be altered.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/16 00:05:45 $
|
||||
* @version $Revision: 1.2 $ $Date: 2003/11/20 22:35:50 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class UnmodifiableSortedMap extends UnmodifiableMap implements SortedMap {
|
||||
public final class UnmodifiableSortedMap extends AbstractSortedMapDecorator implements Unmodifiable {
|
||||
|
||||
/**
|
||||
* Factory method to create an unmodifiable sorted map.
|
||||
|
@ -78,6 +85,9 @@ public class UnmodifiableSortedMap extends UnmodifiableMap implements SortedMap
|
|||
* @throws IllegalArgumentException if map is null
|
||||
*/
|
||||
public static SortedMap decorate(SortedMap map) {
|
||||
if (map instanceof Unmodifiable) {
|
||||
return map;
|
||||
}
|
||||
return new UnmodifiableSortedMap(map);
|
||||
}
|
||||
|
||||
|
@ -88,17 +98,40 @@ public class UnmodifiableSortedMap extends UnmodifiableMap implements SortedMap
|
|||
* @param map the map to decorate, must not be null
|
||||
* @throws IllegalArgumentException if map is null
|
||||
*/
|
||||
protected UnmodifiableSortedMap(Map map) {
|
||||
protected UnmodifiableSortedMap(SortedMap map) {
|
||||
super(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the map being decorated.
|
||||
*
|
||||
* @return the decorated map
|
||||
*/
|
||||
protected SortedMap getSortedMap() {
|
||||
return (SortedMap) map;
|
||||
//-----------------------------------------------------------------------
|
||||
public void clear() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Object put(Object key, Object value) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public void putAll(Map mapToCopy) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Object remove(Object key) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public Set entrySet() {
|
||||
Set set = super.entrySet();
|
||||
return new UnmodifiableEntrySet(set);
|
||||
}
|
||||
|
||||
public Set keySet() {
|
||||
Set set = super.keySet();
|
||||
return UnmodifiableSet.decorate(set);
|
||||
}
|
||||
|
||||
public Collection values() {
|
||||
Collection coll = super.values();
|
||||
return UnmodifiableCollection.decorate(coll);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/map/TestAll.java,v 1.2 2003/11/18 23:23:05 scolebourne Exp $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/map/TestAll.java,v 1.3 2003/11/20 22:35:50 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
|
@ -65,7 +65,7 @@ import junit.framework.TestSuite;
|
|||
* Entry point for tests.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.2 $ $Date: 2003/11/18 23:23:05 $
|
||||
* @version $Revision: 1.3 $ $Date: 2003/11/20 22:35:50 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
|
@ -94,6 +94,8 @@ public class TestAll extends TestCase {
|
|||
suite.addTest(TestPredicatedMap.suite());
|
||||
suite.addTest(TestPredicatedSortedMap.suite());
|
||||
suite.addTest(TestUnmodifiableMap.suite());
|
||||
suite.addTest(TestUnmodifiableOrderedMap.suite());
|
||||
suite.addTest(TestUnmodifiableSortedMap.suite());
|
||||
|
||||
return suite;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/map/TestUnmodifiableMap.java,v 1.3 2003/11/18 22:37:17 scolebourne Exp $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/map/TestUnmodifiableMap.java,v 1.4 2003/11/20 22:35:50 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
|
@ -57,22 +57,20 @@
|
|||
*/
|
||||
package org.apache.commons.collections.map;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.apache.commons.collections.Unmodifiable;
|
||||
|
||||
/**
|
||||
* Extension of {@link AbstractTestMap} for exercising the
|
||||
* {@link UnmodifiableMap} implementation.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.3 $ $Date: 2003/11/18 22:37:17 $
|
||||
* @version $Revision: 1.4 $ $Date: 2003/11/20 22:35:50 $
|
||||
*
|
||||
* @author Phil Steitz
|
||||
*/
|
||||
|
@ -115,259 +113,20 @@ public class TestUnmodifiableMap extends AbstractTestMap{
|
|||
return UnmodifiableMap.decorate(m);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
protected UnmodifiableMap map = null;
|
||||
protected ArrayList array = null;
|
||||
|
||||
protected void setupMap() {
|
||||
map = (UnmodifiableMap) makeFullMap();
|
||||
array = new ArrayList();
|
||||
array.add("one");
|
||||
//-----------------------------------------------------------------------
|
||||
public void testUnmodifiable() {
|
||||
assertTrue(makeEmptyMap() instanceof Unmodifiable);
|
||||
assertTrue(makeFullMap() instanceof Unmodifiable);
|
||||
}
|
||||
|
||||
public void testUnmodifiableBase() {
|
||||
setupMap();
|
||||
try {
|
||||
map.put("key", "value");
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
map.putAll(new HashMap());
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
map.clear();
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
map.remove("x");
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the keyset is not modifiable -- effectively tests
|
||||
* protection of UnmodifiableSet decorator
|
||||
*/
|
||||
public void testUnmodifiableKeySet() {
|
||||
setupMap();
|
||||
Set keys = map.keySet();
|
||||
try {
|
||||
keys.add("x");
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
keys.addAll(array);
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
keys.clear();
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
keys.remove("one");
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
keys.removeAll(array);
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
keys.retainAll(array);
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
Iterator iterator = keys.iterator();
|
||||
iterator.next();
|
||||
iterator.remove();
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the values collection is not modifiable -- effectively tests
|
||||
* protection of UnmodifiableCollection decorator
|
||||
*/
|
||||
public void testUnmodifiableValues() {
|
||||
setupMap();
|
||||
Collection values = map.values();
|
||||
try {
|
||||
values.add("x");
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
values.addAll(array);
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
values.clear();
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
values.remove("one");
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
values.removeAll(array);
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
values.retainAll(array);
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
Iterator iterator = values.iterator();
|
||||
iterator.next();
|
||||
iterator.remove();
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the entrySet is not modifiable -- effectively tests
|
||||
* protection of UnmodifiableEntrySet
|
||||
*/
|
||||
public void testUnmodifiableEntries() {
|
||||
setupMap();
|
||||
Set entries = map.entrySet();
|
||||
try {
|
||||
entries.add("x");
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
entries.addAll(array);
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
entries.clear();
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
entries.remove("one");
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
entries.removeAll(array);
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
entries.retainAll(array);
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
Iterator iterator = entries.iterator();
|
||||
iterator.next();
|
||||
iterator.remove();
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
public void testDecorateFactory() {
|
||||
Map map = makeFullMap();
|
||||
assertSame(map, UnmodifiableMap.decorate(map));
|
||||
|
||||
try {
|
||||
Iterator iterator = entries.iterator();
|
||||
Map.Entry entry = (Map.Entry) iterator.next();
|
||||
entry.setValue("x");
|
||||
fail("Expecting UnsupportedOperationException.");
|
||||
} catch (UnsupportedOperationException e) {
|
||||
// expected
|
||||
}
|
||||
UnmodifiableMap.decorate(null);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that entries consists of Map.Entries corresponding to the parallel
|
||||
* keys and values arrays (not necessarily in order)
|
||||
*/
|
||||
protected void checkEntries(Object[] keys, Object[] values, Object[] entries,
|
||||
boolean checkLengths) {
|
||||
if (checkLengths) {
|
||||
assertEquals(keys.length, entries.length);
|
||||
}
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
Map.Entry entry = (Map.Entry) entries[i];
|
||||
boolean found = false;
|
||||
// Can't assume entries are in insertion order, so have
|
||||
// to search for the key
|
||||
for (int j = 0; j < keys.length; j++) {
|
||||
if (entry.getKey() == keys[j]) {
|
||||
found = true;
|
||||
assertEquals(entry.getValue(), values[j]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertTrue(found);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests EntrySet toArray() implementation
|
||||
*/
|
||||
public void testToArray() {
|
||||
setupMap();
|
||||
Object[] keys = getSampleKeys();
|
||||
Object[] values = getSampleValues();
|
||||
Object[] entries = map.entrySet().toArray();
|
||||
assertTrue(keys.length == entries.length);
|
||||
checkEntries(keys, values, entries, true);
|
||||
entries = map.entrySet().toArray(entries);
|
||||
checkEntries(keys, values, entries, true);
|
||||
Object[] testArray = new Object[2];
|
||||
entries = map.entrySet().toArray(testArray);
|
||||
checkEntries(keys, values, entries, true);
|
||||
testArray = new Object[50];
|
||||
entries = map.entrySet().toArray(testArray);
|
||||
checkEntries(keys, values, entries, false);
|
||||
assertEquals(testArray[map.size()], null);
|
||||
testArray = new Object[0];
|
||||
Object[] resultArray = new Object[0];
|
||||
resultArray = map.entrySet().toArray(testArray);
|
||||
checkEntries(keys, values, resultArray, true);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/map/TestUnmodifiableOrderedMap.java,v 1.1 2003/11/20 22:35:50 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections.map;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.apache.commons.collections.Unmodifiable;
|
||||
|
||||
/**
|
||||
* Extension of {@link AbstractTestOrderedMap} for exercising the
|
||||
* {@link UnmodifiableOrderedMap} implementation.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/20 22:35:50 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TestUnmodifiableOrderedMap extends AbstractTestOrderedMap {
|
||||
|
||||
public TestUnmodifiableOrderedMap(String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return new TestSuite(TestUnmodifiableOrderedMap.class);
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
String[] testCaseName = { TestUnmodifiableOrderedMap.class.getName()};
|
||||
junit.textui.TestRunner.main(testCaseName);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
|
||||
public Map makeEmptyMap() {
|
||||
return UnmodifiableOrderedMap.decorate(ListOrderedMap.decorate(new HashMap()));
|
||||
}
|
||||
|
||||
public boolean isPutChangeSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isPutAddSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isRemoveSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Map makeFullMap() {
|
||||
OrderedMap m = ListOrderedMap.decorate(new HashMap());
|
||||
addSampleMappings(m);
|
||||
return UnmodifiableOrderedMap.decorate(m);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testUnmodifiable() {
|
||||
assertTrue(makeEmptyMap() instanceof Unmodifiable);
|
||||
assertTrue(makeFullMap() instanceof Unmodifiable);
|
||||
}
|
||||
|
||||
public void testDecorateFactory() {
|
||||
Map map = makeFullMap();
|
||||
assertSame(map, UnmodifiableOrderedMap.decorate((OrderedMap) map));
|
||||
|
||||
try {
|
||||
UnmodifiableOrderedMap.decorate(null);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,133 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/map/TestUnmodifiableSortedMap.java,v 1.1 2003/11/20 22:35:50 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections.map;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.apache.commons.collections.Unmodifiable;
|
||||
|
||||
/**
|
||||
* Extension of {@link AbstractTestSortedMap} for exercising the
|
||||
* {@link UnmodifiableSortedMap} implementation.
|
||||
*
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.1 $ $Date: 2003/11/20 22:35:50 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TestUnmodifiableSortedMap extends AbstractTestSortedMap {
|
||||
|
||||
public TestUnmodifiableSortedMap(String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return new TestSuite(TestUnmodifiableSortedMap.class);
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
String[] testCaseName = { TestUnmodifiableSortedMap.class.getName()};
|
||||
junit.textui.TestRunner.main(testCaseName);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
|
||||
public Map makeEmptyMap() {
|
||||
return UnmodifiableSortedMap.decorate(new TreeMap());
|
||||
}
|
||||
|
||||
public boolean isPutChangeSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isPutAddSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isRemoveSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public Map makeFullMap() {
|
||||
SortedMap m = new TreeMap();
|
||||
addSampleMappings(m);
|
||||
return UnmodifiableSortedMap.decorate(m);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testUnmodifiable() {
|
||||
assertTrue(makeEmptyMap() instanceof Unmodifiable);
|
||||
assertTrue(makeFullMap() instanceof Unmodifiable);
|
||||
}
|
||||
|
||||
public void testDecorateFactory() {
|
||||
Map map = makeFullMap();
|
||||
assertSame(map, UnmodifiableSortedMap.decorate((SortedMap) map));
|
||||
|
||||
try {
|
||||
UnmodifiableSortedMap.decorate(null);
|
||||
fail();
|
||||
} catch (IllegalArgumentException ex) {}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue