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  import org.apache.commons.functor.core.IsInstance;
27  
28  
29  /**
30   * @version $Revision: 666324 $ $Date: 2008-06-10 23:01:40 +0200 (Tue, 10 Jun 2008) $
31   * @author Rodney Waldhoff
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      // tests
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              // expected
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              // expected
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          assertFalse(predicatedMap.containsKey(new Integer(17)));
93          assertFalse(predicatedMap.containsKey(new Integer(7)));
94  */
95      }
96  }