You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2003/09/14 05:30:23 UTC
cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections/decorators TestLazyMap.java TestLazySortedMap.java TestAll.java
psteitz 2003/09/13 20:30:23
Modified: collections/src/test/org/apache/commons/collections
TestMapUtils.java
collections/src/test/org/apache/commons/collections/decorators
TestAll.java
Added: collections/src/test/org/apache/commons/collections/decorators
TestLazyMap.java TestLazySortedMap.java
Log:
Added tests for LazyMap, LazySortedMap.
Modified TestMapUtils to test only the factory method for LazyMap.
Revision Changes Path
1.12 +33 -16 jakarta-commons/collections/src/test/org/apache/commons/collections/TestMapUtils.java
Index: TestMapUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestMapUtils.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- TestMapUtils.java 13 Sep 2003 16:12:47 -0000 1.11
+++ TestMapUtils.java 14 Sep 2003 03:30:23 -0000 1.12
@@ -69,6 +69,7 @@
import java.util.TreeMap;
import org.apache.commons.collections.decorators.PredicatedMap;
+import org.apache.commons.collections.decorators.LazyMap;
import junit.framework.Test;
@@ -176,20 +177,36 @@
}
public void testLazyMapFactory() {
- Map map = MapUtils.lazyMap(new HashMap(), new Factory() {
- public Object create() {
- return new Integer(5);
- }
- });
-
- assertEquals(0, map.size());
- Integer i1 = (Integer) map.get("Five");
- assertEquals(new Integer(5), i1);
- assertEquals(1, map.size());
- Integer i2 = (Integer) map.get(new String(new char[] {'F','i','v','e'}));
- assertEquals(new Integer(5), i2);
- assertEquals(1, map.size());
- assertSame(i1, i2);
+ Factory factory = FactoryUtils.constantFactory(new Integer(5));
+ Map map = MapUtils.lazyMap(new HashMap(), factory);
+ assertTrue(map instanceof LazyMap);
+ try {
+ map = MapUtils.lazyMap(new HashMap(), (Factory) null);
+ fail("Expecting IllegalArgumentException for null factory");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ try {
+ map = MapUtils.lazyMap(null, factory);
+ fail("Expecting IllegalArgumentException for null map");
+ } catch (IllegalArgumentException e) {
+ // 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() {
1.12 +4 -2 jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestAll.java
Index: TestAll.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestAll.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- TestAll.java 13 Sep 2003 16:12:47 -0000 1.11
+++ TestAll.java 14 Sep 2003 03:30:23 -0000 1.12
@@ -106,6 +106,8 @@
suite.addTest(TestPredicatedSet.suite());
suite.addTest(TestPredicatedMap.suite());
suite.addTest(TestPredicatedSortedMap.suite());
+ suite.addTest(TestLazyMap.suite());
+ suite.addTest(TestLazySortedMap.suite());
return suite;
}
1.1 jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestLazyMap.java
Index: TestLazyMap.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/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());
}
}
1.1 jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestLazySortedMap.java
Index: TestLazySortedMap.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/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
}
}
}