Added tests for LazyMap, LazySortedMap.

Modified TestMapUtils to test only the factory method for LazyMap.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131154 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2003-09-14 03:30:23 +00:00
parent 3ae25b6189
commit 19045cb424
4 changed files with 317 additions and 18 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestMapUtils.java,v 1.11 2003/09/13 16:12:47 psteitz Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestMapUtils.java,v 1.12 2003/09/14 03:30:23 psteitz Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -69,13 +69,14 @@ import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import org.apache.commons.collections.decorators.PredicatedMap; import org.apache.commons.collections.decorators.PredicatedMap;
import org.apache.commons.collections.decorators.LazyMap;
import junit.framework.Test; import junit.framework.Test;
/** /**
* Tests for MapUtils. * Tests for MapUtils.
* *
* @version $Revision: 1.11 $ $Date: 2003/09/13 16:12:47 $ * @version $Revision: 1.12 $ $Date: 2003/09/14 03:30:23 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Arun Mammen Thomas * @author Arun Mammen Thomas
@ -176,20 +177,36 @@ public class TestMapUtils extends BulkTest {
} }
public void testLazyMapFactory() { public void testLazyMapFactory() {
Map map = MapUtils.lazyMap(new HashMap(), new Factory() { Factory factory = FactoryUtils.constantFactory(new Integer(5));
public Object create() { Map map = MapUtils.lazyMap(new HashMap(), factory);
return new Integer(5); assertTrue(map instanceof LazyMap);
} try {
}); map = MapUtils.lazyMap(new HashMap(), (Factory) null);
fail("Expecting IllegalArgumentException for null factory");
assertEquals(0, map.size()); } catch (IllegalArgumentException e) {
Integer i1 = (Integer) map.get("Five"); // expected
assertEquals(new Integer(5), i1); }
assertEquals(1, map.size()); try {
Integer i2 = (Integer) map.get(new String(new char[] {'F','i','v','e'})); map = MapUtils.lazyMap(null, factory);
assertEquals(new Integer(5), i2); fail("Expecting IllegalArgumentException for null map");
assertEquals(1, map.size()); } catch (IllegalArgumentException e) {
assertSame(i1, i2); // expected
}
Transformer transformer = TransformerUtils.asTransformer(factory);
map = MapUtils.lazyMap(new HashMap(), transformer);
assertTrue(map instanceof LazyMap);
try {
map = MapUtils.lazyMap(new HashMap(), (Transformer) null);
fail("Expecting IllegalArgumentException for null transformer");
} catch (IllegalArgumentException e) {
// expected
}
try {
map = MapUtils.lazyMap(null, transformer);
fail("Expecting IllegalArgumentException for null map");
} catch (IllegalArgumentException e) {
// expected
}
} }
public void testLazyMapTransformer() { public void testLazyMapTransformer() {

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestAll.java,v 1.11 2003/09/13 16:12:47 psteitz Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestAll.java,v 1.12 2003/09/14 03:30:23 psteitz Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -65,7 +65,7 @@ import junit.framework.TestSuite;
* Entry point for all collections decorators tests. * Entry point for all collections decorators tests.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.11 $ $Date: 2003/09/13 16:12:47 $ * @version $Revision: 1.12 $ $Date: 2003/09/14 03:30:23 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
@ -106,6 +106,8 @@ public class TestAll extends TestCase {
suite.addTest(TestPredicatedSet.suite()); suite.addTest(TestPredicatedSet.suite());
suite.addTest(TestPredicatedMap.suite()); suite.addTest(TestPredicatedMap.suite());
suite.addTest(TestPredicatedSortedMap.suite()); suite.addTest(TestPredicatedSortedMap.suite());
suite.addTest(TestLazyMap.suite());
suite.addTest(TestLazySortedMap.suite());
return suite; return suite;
} }

View File

@ -0,0 +1,128 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestLazyMap.java,v 1.1 2003/09/14 03:30:23 psteitz 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.decorators;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections.TestMap;
import org.apache.commons.collections.Factory;
import org.apache.commons.collections.FactoryUtils;
/**
* Extension of {@link TestMap} for exercising the
* {@link LazyMap} implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/14 03:30:23 $
*
* @author Phil Steitz
*/
public class TestLazyMap extends TestMap {
public TestLazyMap(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestLazyMap.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestLazyMap.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
//-------------------------------------------------------------------
protected Factory oneFactory = FactoryUtils.constantFactory("One");
protected Factory nullFactory = FactoryUtils.nullFactory();
protected Map decorateMap(Map map, Factory factory) {
return LazyMap.decorate(map, factory);
}
public Map makeEmptyMap() {
return decorateMap(new HashMap(), nullFactory);
}
//--------------------------------------------------------------------
protected Map makeTestMap(Factory factory) {
return decorateMap(new HashMap(), factory);
}
public void testMapGet() {
Map map = makeTestMap(oneFactory);
assertEquals(0, map.size());
String s1 = (String) map.get("Five");
assertEquals("One", s1);
assertEquals(1, map.size());
String s2 = (String) map.get(new String(new char[] {'F','i','v','e'}));
assertEquals("One", s2);
assertEquals(1, map.size());
assertSame(s1, s2);
map = makeTestMap(nullFactory);
Object o = map.get("Five");
assertEquals(null,o);
assertEquals(1, map.size());
}
}

View File

@ -0,0 +1,152 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/decorators/Attic/TestLazySortedMap.java,v 1.1 2003/09/14 03:30:23 psteitz 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.decorators;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.util.TreeMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.Comparator;
import org.apache.commons.collections.Factory;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.TransformerUtils;
/**
* Extension of {@link TestLazyMap} for exercising the
* {@link LazySortedMap} implementation.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/09/14 03:30:23 $
*
* @author Phil Steitz
*/
public class TestLazySortedMap extends TestLazyMap {
public TestLazySortedMap(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestLazySortedMap.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestLazySortedMap.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
//-------------------------------------------------------------------
protected SortedMap decorateMap(SortedMap map, Factory factory) {
return LazySortedMap.decorate(map, factory);
}
public Map makeEmptyMap() {
return decorateMap(new TreeMap(), nullFactory);
}
protected boolean useNullKey() {
return false;
}
//--------------------------------------------------------------------
protected SortedMap makeTestSortedMap(Factory factory) {
return decorateMap(new TreeMap(), factory);
}
public void testSortOrder() {
SortedMap map = makeTestSortedMap(oneFactory);
map.put("A", "a");
map.get("B"); // Entry with value "One" created
map.put("C", "c");
assertEquals("First key should be A", map.firstKey(), "A");
assertEquals("Last key should be C", map.lastKey(), "C");
assertEquals("First key in tail map should be B",
map.tailMap("B").firstKey(), "B");
assertEquals("Last key in head map should be B",
map.headMap("C").lastKey(), "B");
assertEquals("Last key in submap should be B",
map.subMap("A","C").lastKey(), "B");
Comparator c = map.comparator();
assertTrue("natural order, so comparator should be null",
c == null);
}
public void testTransformerDecorate() {
Transformer transformer = TransformerUtils.asTransformer(oneFactory);
SortedMap map = LazySortedMap.decorate(new TreeMap(), transformer);
assertTrue(map instanceof LazySortedMap);
try {
map = LazySortedMap.decorate(new TreeMap(), (Transformer) null);
fail("Expecting IllegalArgumentException for null transformer");
} catch (IllegalArgumentException e) {
// expected
}
try {
map = LazySortedMap.decorate(null, transformer);
fail("Expecting IllegalArgumentException for null map");
} catch (IllegalArgumentException e) {
// expected
}
}
}