001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.functor.example.map;
018    
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import junit.framework.Test;
023    import junit.framework.TestCase;
024    import junit.framework.TestSuite;
025    
026    import org.apache.commons.functor.core.IsInstance;
027    
028    
029    /**
030     * @version $Revision: 666324 $ $Date: 2008-06-10 23:01:40 +0200 (Tue, 10 Jun 2008) $
031     * @author Rodney Waldhoff
032     */
033    @SuppressWarnings("unchecked")
034    public class TestPredicatedMap extends TestCase {
035    
036        public TestPredicatedMap(String testName) {
037            super(testName);
038        }
039    
040        public static Test suite() {
041            return new TestSuite(TestPredicatedMap.class);
042        }
043    
044        private Map baseMap = null;
045        private Map predicatedMap = null;
046        public void setUp() throws Exception {
047            super.setUp();
048            baseMap = new HashMap();
049            predicatedMap = new PredicatedMap(baseMap,IsInstance.of(String.class),IsInstance.of(Integer.class));
050        }
051    
052        public void tearDown() throws Exception {
053            super.tearDown();
054            baseMap = null;
055            predicatedMap = null;
056        }
057    
058        // tests
059    
060        public void testCanPutMatchingPair() {
061            predicatedMap.put("xyzzy", new Integer(17));
062        }
063        public void testCantPutInvalidValue() {
064            try {
065                predicatedMap.put("xyzzy", "xyzzy");
066                fail("Expected IllegalArgumentException");
067            } catch(IllegalArgumentException e) {
068                // expected
069            }
070        }
071    
072        public void testCantPutInvalidKey() {
073            try {
074                predicatedMap.put(new Long(1), new Integer(3));
075                fail("Expected IllegalArgumentException");
076            } catch(IllegalArgumentException e) {
077                // expected
078            }
079        }
080    
081        public void testOnlyValidPairsAreAddedInPutAll() {
082            HashMap map = new HashMap();
083            map.put("one", new Integer(17));
084            map.put("two", "rejected");
085            map.put(new Integer(17), "xyzzy");
086            map.put(new Integer(7), new Integer(3));
087    
088            predicatedMap.putAll(map);
089            assertEquals(new Integer(17), predicatedMap.get("one"));
090            assertFalse(predicatedMap.containsKey("two"));
091    /*
092            assertFalse(predicatedMap.containsKey(new Integer(17)));
093            assertFalse(predicatedMap.containsKey(new Integer(7)));
094    */
095        }
096    }