001    /*
002     * $Id: MapTester.java,v 1.3 2013/12/19 20:45:39 oboehm Exp $
003     *
004     * Copyright (c) 2013 by Oliver Boehm
005     *
006     * Licensed under the Apache License, Version 2.0 (the "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     *
018     * (c)reated 18.12.2013 by oliver (ob@oasd.de)
019     */
020    
021    package patterntesting.runtime.junit;
022    
023    import java.util.*;
024    import java.util.Map.Entry;
025    
026    import patterntesting.runtime.util.Converter;
027    
028    /**
029     * This class asserts (among other things) that two maps are equals.
030     *
031     * @author oliver
032     * @since 1.4 (18.12.2013)
033     */
034    public class MapTester {
035    
036        /** Utility class - no need to instantiate it. */
037        private MapTester() {}
038    
039        /**
040         * Checks if each key-value pair in map m1 is equals to that of map m2. If
041         * not this method will tell you which pair is different. If the maps
042         * have different size this method will tell you which pair is missing.
043         *
044         * @param m1 the m1
045         * @param m2 the m2
046         */
047        @SuppressWarnings("unchecked")
048        public static void assertEquals(final Map<?, ?> m1, final Map<?,?> m2) {
049           Map<Object, Object> map1 = (Map<Object, Object>) m1;
050           if (m1.size() < m2.size()) {
051                assertEquals(m2, m1);
052            } else if (m1.size() > m2.size()) {
053                for (Entry<Object, Object> entry : map1.entrySet()) {
054                    if (!(m2.containsKey(entry.getKey()))) {
055                        throw new AssertionError("<" + entry + "> is missing in "
056                                + Converter.toShortString(m2));
057                    }
058                }
059            } else {
060                for (Entry<Object, Object> entry : map1.entrySet()) {
061                    Object value2 = m2.get(entry.getKey());
062                    if (value2 == null) {
063                        throw new AssertionError("{" + entry + "}> is missing in "
064                                + Converter.toShortString(m2));
065                    } else if (!(value2.equals(entry.getValue()))) {
066                        throw new AssertionError("key \"" + entry.getKey()
067                                + "\" has different values: " + entry.getValue() + " <--> " + value2);
068                    }
069                }
070            }
071        }
072    
073        /**
074         * Checks if each key in map m1 is equals to that of map m2. If not this
075         * method will tell you which key is different. If the maps have different
076         * size this method will tell you which key is missing.
077         *
078         * @param m1 the m1
079         * @param m2 the m2
080         */
081        public static void assertEqualKeys(final Map<?, ?> m1, final Map<?,?> m2) {
082            CollectionTester.assertEquals(m1.keySet(), m2.keySet(), "keys differs");
083        }
084    
085        /**
086         * Checks if each value in map m1 is equals to that of map m2. If not this
087         * method will tell you which map is different. If the maps have different
088         * size this method will tell you which value is missing.
089         *
090         * @param m1 the m1
091         * @param m2 the m2
092         */
093        public static void assertEqualValues(final Map<?, ?> m1, final Map<?,?> m2) {
094            CollectionTester.assertEquals(m1.values(), m2.values(), "values differs");
095        }
096    
097    }
098