001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.functor.example.map;
018
019 import java.util.HashMap;
020 import java.util.Iterator;
021 import java.util.Map;
022
023 import junit.framework.Test;
024 import junit.framework.TestCase;
025 import junit.framework.TestSuite;
026
027 import org.apache.commons.functor.core.collection.Size;
028
029
030 /**
031 * @version $Revision: 666324 $ $Date: 2008-06-10 23:01:40 +0200 (Tue, 10 Jun 2008) $
032 * @author Rodney Waldhoff
033 */
034 @SuppressWarnings("unchecked")
035 public class TestLazyMap extends TestCase {
036
037 public TestLazyMap(String testName) {
038 super(testName);
039 }
040
041 public static Test suite() {
042 return new TestSuite(TestLazyMap.class);
043 }
044
045 private Map baseMap = null;
046 private Map lazyMap = null;
047 private Map expectedMap = null;
048
049 public void setUp() throws Exception {
050 super.setUp();
051 expectedMap = new HashMap();
052 expectedMap.put("one",new Integer(3));
053 expectedMap.put("two",new Integer(3));
054 expectedMap.put("three", new Integer(5));
055 expectedMap.put("four", new Integer(4));
056 expectedMap.put("five", new Integer(4));
057
058 baseMap = new HashMap();
059 lazyMap = new LazyMap(baseMap,Size.instance());
060 }
061
062 public void tearDown() throws Exception {
063 super.tearDown();
064 baseMap = null;
065 lazyMap = null;
066 expectedMap = null;
067 }
068
069 // tests
070
071 public void test() {
072 for (Iterator iter = expectedMap.keySet().iterator(); iter.hasNext();) {
073 Object key = iter.next();
074 assertFalse(baseMap.containsKey(key));
075 assertFalse(lazyMap.containsKey(key));
076 assertEquals(expectedMap.get(key),lazyMap.get(key));
077 assertEquals(expectedMap.get(key),baseMap.get(key));
078 assertTrue(lazyMap.containsKey(key));
079 assertTrue(baseMap.containsKey(key));
080 }
081 assertEquals(expectedMap,lazyMap);
082 assertEquals(expectedMap,baseMap);
083 baseMap.clear();
084 for (Iterator iter = expectedMap.keySet().iterator(); iter.hasNext();) {
085 Object key = iter.next();
086 assertFalse(baseMap.containsKey(key));
087 assertFalse(lazyMap.containsKey(key));
088 assertEquals(expectedMap.get(key),lazyMap.get(key));
089 assertEquals(expectedMap.get(key),baseMap.get(key));
090 assertTrue(lazyMap.containsKey(key));
091 assertTrue(baseMap.containsKey(key));
092 }
093 assertEquals(expectedMap,lazyMap);
094 assertEquals(expectedMap,baseMap);
095 }
096
097
098 public void testBaseMapOverrides() {
099 assertEquals(new Integer(5),lazyMap.get("xyzzy"));
100 baseMap.put("xyzzy","xyzzy");
101 assertEquals("xyzzy",lazyMap.get("xyzzy"));
102 }
103
104 }