1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.functor.example.map;
18  
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  
27  import org.apache.commons.functor.core.collection.Size;
28  
29  
30  /**
31   * @version $Revision: 666324 $ $Date: 2008-06-10 23:01:40 +0200 (Tue, 10 Jun 2008) $
32   * @author Rodney Waldhoff
33   */
34  @SuppressWarnings("unchecked")
35  public class TestLazyMap extends TestCase {
36  
37      public TestLazyMap(String testName) {
38          super(testName);
39      }
40  
41      public static Test suite() {
42          return new TestSuite(TestLazyMap.class);
43      }
44  
45      private Map baseMap = null;
46      private Map lazyMap = null;
47      private Map expectedMap = null;
48  
49      public void setUp() throws Exception {
50          super.setUp();
51          expectedMap = new HashMap();
52          expectedMap.put("one",new Integer(3));
53          expectedMap.put("two",new Integer(3));
54          expectedMap.put("three", new Integer(5));
55          expectedMap.put("four", new Integer(4));
56          expectedMap.put("five", new Integer(4));
57  
58          baseMap = new HashMap();
59          lazyMap = new LazyMap(baseMap,Size.instance());
60      }
61  
62      public void tearDown() throws Exception {
63          super.tearDown();
64          baseMap = null;
65          lazyMap = null;
66          expectedMap = null;
67      }
68  
69      // tests
70  
71      public void test() {
72          for (Iterator iter = expectedMap.keySet().iterator(); iter.hasNext();) {
73              Object key = iter.next();
74              assertFalse(baseMap.containsKey(key));
75              assertFalse(lazyMap.containsKey(key));
76              assertEquals(expectedMap.get(key),lazyMap.get(key));
77              assertEquals(expectedMap.get(key),baseMap.get(key));
78              assertTrue(lazyMap.containsKey(key));
79              assertTrue(baseMap.containsKey(key));
80          }
81          assertEquals(expectedMap,lazyMap);
82          assertEquals(expectedMap,baseMap);
83          baseMap.clear();
84          for (Iterator iter = expectedMap.keySet().iterator(); iter.hasNext();) {
85              Object key = iter.next();
86              assertFalse(baseMap.containsKey(key));
87              assertFalse(lazyMap.containsKey(key));
88              assertEquals(expectedMap.get(key),lazyMap.get(key));
89              assertEquals(expectedMap.get(key),baseMap.get(key));
90              assertTrue(lazyMap.containsKey(key));
91              assertTrue(baseMap.containsKey(key));
92          }
93          assertEquals(expectedMap,lazyMap);
94          assertEquals(expectedMap,baseMap);
95      }
96  
97  
98      public void testBaseMapOverrides() {
99          assertEquals(new Integer(5),lazyMap.get("xyzzy"));
100         baseMap.put("xyzzy","xyzzy");
101         assertEquals("xyzzy",lazyMap.get("xyzzy"));
102     }
103 
104 }