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 import org.apache.commons.functor.core.IsInstance;
27
28
29
30
31
32
33 @SuppressWarnings("unchecked")
34 public class TestPredicatedMap extends TestCase {
35
36 public TestPredicatedMap(String testName) {
37 super(testName);
38 }
39
40 public static Test suite() {
41 return new TestSuite(TestPredicatedMap.class);
42 }
43
44 private Map baseMap = null;
45 private Map predicatedMap = null;
46 public void setUp() throws Exception {
47 super.setUp();
48 baseMap = new HashMap();
49 predicatedMap = new PredicatedMap(baseMap,IsInstance.of(String.class),IsInstance.of(Integer.class));
50 }
51
52 public void tearDown() throws Exception {
53 super.tearDown();
54 baseMap = null;
55 predicatedMap = null;
56 }
57
58
59
60 public void testCanPutMatchingPair() {
61 predicatedMap.put("xyzzy", new Integer(17));
62 }
63 public void testCantPutInvalidValue() {
64 try {
65 predicatedMap.put("xyzzy", "xyzzy");
66 fail("Expected IllegalArgumentException");
67 } catch(IllegalArgumentException e) {
68
69 }
70 }
71
72 public void testCantPutInvalidKey() {
73 try {
74 predicatedMap.put(new Long(1), new Integer(3));
75 fail("Expected IllegalArgumentException");
76 } catch(IllegalArgumentException e) {
77
78 }
79 }
80
81 public void testOnlyValidPairsAreAddedInPutAll() {
82 HashMap map = new HashMap();
83 map.put("one", new Integer(17));
84 map.put("two", "rejected");
85 map.put(new Integer(17), "xyzzy");
86 map.put(new Integer(7), new Integer(3));
87
88 predicatedMap.putAll(map);
89 assertEquals(new Integer(17), predicatedMap.get("one"));
90 assertFalse(predicatedMap.containsKey("two"));
91
92
93
94
95 }
96 }