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.Map;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  
27  /**
28   * @version $Revision: 666324 $ $Date: 2008-06-10 23:01:40 +0200 (Tue, 10 Jun 2008) $
29   * @author Rodney Waldhoff
30   */
31  @SuppressWarnings("unchecked")
32  public class TestFixedSizeMap extends TestCase {
33  
34      public TestFixedSizeMap(String testName) {
35          super(testName);
36      }
37  
38      public static Test suite() {
39          return new TestSuite(TestFixedSizeMap.class);
40      }
41  
42      private Map baseMap = null;
43      private Map fixedMap = null;
44  
45      public void setUp() throws Exception {
46          super.setUp();
47          baseMap = new HashMap();
48          baseMap.put(new Integer(1),"one");
49          baseMap.put(new Integer(2),"two");
50          baseMap.put(new Integer(3),"three");
51          baseMap.put(new Integer(4),"four");
52          baseMap.put(new Integer(5),"five");
53  
54          fixedMap = new FixedSizeMap(baseMap);
55      }
56  
57      public void tearDown() throws Exception {
58          super.tearDown();
59          baseMap = null;
60          fixedMap = null;
61      }
62  
63      // tests
64  
65      public void testCantPutNewPair() {
66          try {
67              fixedMap.put("xyzzy","xyzzy");
68              fail("Expected IllegalArgumentException");
69          } catch(IllegalArgumentException e) {
70              // expected
71          }
72      }
73  
74      public void testCantPutNewPairViaPutAll() {
75          Map map = new HashMap();
76          map.put(new Integer(1),"uno");
77          map.put("xyzzy","xyzzy");
78          map.put(new Integer(2),"dos");
79  
80          try {
81              fixedMap.putAll(map);
82              fail("Expected IllegalArgumentException");
83          } catch(IllegalArgumentException e) {
84              // expected
85          }
86  
87          assertEquals("one",fixedMap.get(new Integer(1)));
88          assertEquals("two",fixedMap.get(new Integer(2)));
89      }
90  
91      public void testCantClear() {
92          try {
93              fixedMap.clear();
94              fail("Expected UnsupportedOperationException");
95          } catch(UnsupportedOperationException e) {
96              // expected
97          }
98      }
99  
100     public void testCantRemove() {
101         try {
102             fixedMap.remove(new Integer(1));
103             fail("Expected UnsupportedOperationException");
104         } catch(UnsupportedOperationException e) {
105             // expected
106         }
107     }
108 
109     public void testCanAssociateNewValueWithOldKey() {
110         fixedMap.put(new Integer(1),"uno");
111         assertEquals("uno",fixedMap.get(new Integer(1)));
112         assertEquals("two",fixedMap.get(new Integer(2)));
113         assertEquals("three",fixedMap.get(new Integer(3)));
114     }
115 
116     public void testCanAssociateNewValueWithOldKeyViaPutAll() {
117         Map map = new HashMap();
118         map.put(new Integer(1),"uno");
119         map.put(new Integer(2),"dos");
120 
121         fixedMap.putAll(map);
122 
123         assertEquals("uno",fixedMap.get(new Integer(1)));
124         assertEquals("dos",fixedMap.get(new Integer(2)));
125         assertEquals("three",fixedMap.get(new Integer(3)));
126     }
127 
128 
129 }