1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
29
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
64
65 public void testCantPutNewPair() {
66 try {
67 fixedMap.put("xyzzy","xyzzy");
68 fail("Expected IllegalArgumentException");
69 } catch(IllegalArgumentException e) {
70
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
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
97 }
98 }
99
100 public void testCantRemove() {
101 try {
102 fixedMap.remove(new Integer(1));
103 fail("Expected UnsupportedOperationException");
104 } catch(UnsupportedOperationException e) {
105
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 }