From 352f016239039fd8a6647cd14c5d24a60caa8193 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Tue, 15 Sep 2009 05:57:54 +0000 Subject: [PATCH] Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956. Also see the following revisions: ------------------------------------------------------------------------ r751894 | mbenson | 2009-03-09 15:48:07 -0700 (Mon, 09 Mar 2009) | 1 line add splitmap package whose original goal is to provide a more versatile TransformedMap implementation ------------------------------------------------------------------------ git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815141 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/collections/splitmap/TestAll.java | 42 +++ .../splitmap/TestSplitMapUtils.java | 241 ++++++++++++++++++ .../splitmap/TestTransformedMap.java | 134 ++++++++++ 3 files changed, 417 insertions(+) create mode 100644 src/test/org/apache/commons/collections/splitmap/TestAll.java create mode 100644 src/test/org/apache/commons/collections/splitmap/TestSplitMapUtils.java create mode 100644 src/test/org/apache/commons/collections/splitmap/TestTransformedMap.java diff --git a/src/test/org/apache/commons/collections/splitmap/TestAll.java b/src/test/org/apache/commons/collections/splitmap/TestAll.java new file mode 100644 index 000000000..005b1f276 --- /dev/null +++ b/src/test/org/apache/commons/collections/splitmap/TestAll.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.commons.collections.splitmap; + +import junit.framework.TestCase; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +/** + * Entry point for tests. + * + * @since Commons Collections 5 + * @TODO fix version + * @version $Revision$ $Date$ + * + * @author Stephen Colebourne + * @author Stephen Kestle + * @author Matt Benson + */ +@RunWith(Suite.class) +@SuiteClasses({ + TestSplitMapUtils.class, + TestTransformedMap.class +}) +public class TestAll extends TestCase { +} diff --git a/src/test/org/apache/commons/collections/splitmap/TestSplitMapUtils.java b/src/test/org/apache/commons/collections/splitmap/TestSplitMapUtils.java new file mode 100644 index 000000000..c09cb5fd0 --- /dev/null +++ b/src/test/org/apache/commons/collections/splitmap/TestSplitMapUtils.java @@ -0,0 +1,241 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.commons.collections.splitmap; + +import java.util.HashMap; +import java.util.Map; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.apache.commons.collections.BulkTest; +import org.apache.commons.collections.IterableMap; +import org.apache.commons.collections.MapIterator; +import org.apache.commons.collections.Put; +import org.apache.commons.collections.Transformer; +import org.apache.commons.collections.Unmodifiable; +import org.apache.commons.collections.functors.NOPTransformer; +import org.apache.commons.collections.map.HashedMap; + +/** + * Tests for {@link TransformedMap} + * + * @since Commons Collections 5 + * @TODO fix version + * @version $Revision$ $Date$ + * + * @author Stephen Colebourne + * @author Matt Benson + */ +public class TestSplitMapUtils extends BulkTest { + private Map backingMap; + private TransformedMap transformedMap; + + private Transformer stringToInt = new Transformer() { + public Integer transform(String input) { + return Integer.valueOf(input); + } + }; + + public TestSplitMapUtils(String testName) { + super(testName); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + backingMap = new HashMap(); + transformedMap = TransformedMap.decorate(backingMap, NOPTransformer. getInstance(), + stringToInt); + for (int i = 0; i < 10; i++) { + transformedMap.put(String.valueOf(i), String.valueOf(i)); + } + } + + public static Test suite() { + return new TestSuite(TestSplitMapUtils.class); + } + + public static void main(String args[]) { + String[] testCaseName = { TestSplitMapUtils.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + // ----------------------------------------------------------------------- + + public void testReadableMap() { + final IterableMap map = SplitMapUtils.readableMap(transformedMap); + + // basic + for (int i = 0; i < 10; i++) { + assertFalse(map.containsValue(String.valueOf(i))); + assertEquals(i, map.get(String.valueOf(i)).intValue()); + } + + // mapIterator + MapIterator it = map.mapIterator(); + while (it.hasNext()) { + String k = it.next(); + assertEquals(k, it.getKey()); + assertEquals(Integer.valueOf(k), it.getValue()); + } + + // unmodifiable + assertTrue(map instanceof Unmodifiable); + + // check individual operations + int sz = map.size(); + + attemptPutOperation(new Runnable() { + public void run() { + map.clear(); + } + }); + + assertEquals(sz, map.size()); + + attemptPutOperation(new Runnable() { + public void run() { + map.put("foo", 100); + } + }); + + final HashMap m = new HashMap(); + m.put("foo", 100); + m.put("bar", 200); + m.put("baz", 300); + attemptPutOperation(new Runnable() { + public void run() { + map.putAll(m); + } + }); + + // equals, hashcode + IterableMap other = SplitMapUtils.readableMap(transformedMap); + assertEquals(other, map); + assertEquals(other.hashCode(), map.hashCode()); + + // remove + for (int i = 0; i < 10; i++) { + assertEquals(i, map.remove(String.valueOf(i)).intValue()); + assertEquals(--sz, map.size()); + } + assertTrue(map.isEmpty()); + assertSame(map, SplitMapUtils.readableMap(map)); + } + + public void testAlreadyReadableMap() { + HashedMap hashedMap = new HashedMap(); + assertSame(hashedMap, SplitMapUtils.readableMap(hashedMap)); + } + + @SuppressWarnings("unchecked") + public void testWritableMap() { + final Map map = SplitMapUtils.writableMap(transformedMap); + attemptGetOperation(new Runnable() { + public void run() { + map.get(null); + } + }); + attemptGetOperation(new Runnable() { + public void run() { + map.entrySet(); + } + }); + attemptGetOperation(new Runnable() { + public void run() { + map.keySet(); + } + }); + attemptGetOperation(new Runnable() { + public void run() { + map.values(); + } + }); + attemptGetOperation(new Runnable() { + public void run() { + map.size(); + } + }); + attemptGetOperation(new Runnable() { + public void run() { + map.isEmpty(); + } + }); + attemptGetOperation(new Runnable() { + public void run() { + map.containsKey(null); + } + }); + attemptGetOperation(new Runnable() { + public void run() { + map.containsValue(null); + } + }); + attemptGetOperation(new Runnable() { + public void run() { + map.remove(null); + } + }); + + // equals, hashcode + Map other = SplitMapUtils.writableMap(transformedMap); + assertEquals(other, map); + assertEquals(other.hashCode(), map.hashCode()); + + // put + int sz = backingMap.size(); + assertFalse(backingMap.containsKey("foo")); + map.put("new", "66"); + assertEquals(++sz, backingMap.size()); + + // putall + Map more = new HashMap(); + more.put("foo", "77"); + more.put("bar", "88"); + more.put("baz", "99"); + map.putAll(more); + assertEquals(sz + more.size(), backingMap.size()); + + // clear + map.clear(); + assertTrue(backingMap.isEmpty()); + assertSame(map, SplitMapUtils.writableMap((Put) map)); + } + + public void testAlreadyWritableMap() { + HashedMap hashedMap = new HashedMap(); + assertSame(hashedMap, SplitMapUtils.writableMap(hashedMap)); + } + + private void attemptGetOperation(Runnable r) { + attemptMapOperation("Put exposed as writable Map must not allow Get operations", r); + } + + private void attemptPutOperation(Runnable r) { + attemptMapOperation("Get exposed as writable Map must not allow Put operations", r); + } + + private void attemptMapOperation(String s, Runnable r) { + try { + r.run(); + fail(s); + } catch (UnsupportedOperationException e) { + } + } + +} diff --git a/src/test/org/apache/commons/collections/splitmap/TestTransformedMap.java b/src/test/org/apache/commons/collections/splitmap/TestTransformedMap.java new file mode 100644 index 000000000..b77fa4c6e --- /dev/null +++ b/src/test/org/apache/commons/collections/splitmap/TestTransformedMap.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.commons.collections.splitmap; + +import java.math.BigInteger; +import java.util.HashMap; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.apache.commons.collections.BulkTest; +import org.apache.commons.collections.MapIterator; +import org.apache.commons.collections.Transformer; +import org.apache.commons.collections.functors.NOPTransformer; + +/** + * Tests for {@link TransformedMap} + * + * @since Commons Collections 5 + * @TODO fix version, add Serialization tests + * @version $Revision$ $Date$ + * + * @author Stephen Colebourne + * @author Matt Benson + */ +public class TestTransformedMap extends BulkTest { + + private Transformer intToString = new Transformer() { + public String transform(Integer input) { + return String.valueOf(input); + }; + }; + + private Transformer> objectToClass = new Transformer>() { + public java.lang.Class transform(Object input) { + return input == null ? null : input.getClass(); + } + }; + + private Transformer stringToInt = new Transformer() { + public Integer transform(String input) { + return Integer.valueOf(input); + } + }; + + public TestTransformedMap(String testName) { + super(testName); + } + + public static Test suite() { + return new TestSuite(TestTransformedMap.class); + } + + public static void main(String args[]) { + String[] testCaseName = { TestTransformedMap.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + // ----------------------------------------------------------------------- + @SuppressWarnings("unchecked") + public void testTransformedMap() { + TransformedMap> map = TransformedMap.decorate( + new HashMap>(), intToString, objectToClass); + + Integer[] k = new Integer[] { 0, 1, 2, 3, 4, 5, 6 }; + Object[] v = new Object[] { "", new Object(), new HashMap(), 0, BigInteger.TEN, null, + new Object[0] }; + + assertEquals(0, map.size()); + for (int i = 0; i < k.length; i++) { + map.put(k[i], v[i]); + assertEquals(i + 1, map.size()); + assertTrue(map.containsKey(intToString.transform(k[i]))); + assertFalse(map.containsKey(k[i])); + assertTrue(map.containsValue(objectToClass.transform(v[i]))); + assertTrue(objectToClass.transform(v[i]) != v[i] ^ map.containsValue(v[i])); + assertEquals(objectToClass.transform(v[i]), map.get(intToString.transform(k[i]))); + } + + int sz = map.size(); + assertEquals(null, map.remove(k[0])); + assertEquals(sz, map.size()); + assertEquals(objectToClass.transform(v[0]), map.remove(intToString.transform(k[0]))); + assertEquals(--sz, map.size()); + + TransformedMap map2 = TransformedMap.decorate( + new HashMap(), NOPTransformer. getInstance(), stringToInt); + assertEquals(0, map2.size()); + for (int i = 0; i < 6; i++) { + map2.put(String.valueOf(i), String.valueOf(i)); + assertEquals(i + 1, map2.size()); + assertTrue(map2.containsValue(i)); + assertFalse(map2.containsValue(String.valueOf(i))); + assertTrue(map2.containsKey(String.valueOf(i))); + assertEquals(i, map2.get(String.valueOf(i)).intValue()); + } + + int sz2 = map2.size(); + assertEquals(Integer.valueOf(0), map2.remove("0")); + assertEquals(--sz2, map2.size()); + } + + // ----------------------------------------------------------------------- + + public void testMapIterator() { + TransformedMap map = TransformedMap.decorate( + new HashMap(), NOPTransformer. getInstance(), stringToInt); + assertEquals(0, map.size()); + for (int i = 0; i < 6; i++) { + map.put(String.valueOf(i), String.valueOf(i)); + } + + for (MapIterator it = map.mapIterator(); it.hasNext();) { + String k = it.next(); + assertEquals(k, it.getKey()); + assertEquals(map.get(k), it.getValue()); + } + } + +}