import java.util.*; public class MultiHashMapTest { /** Objects to be used as keys */ private static final Object o1 = new Object(); private static final Object o2 = new Object(); private static final Object o3 = new Object(); private static final Object o4 = new Object(); /** Strings to be used as values */ private static final String s1 = "one"; private static final String s2 = "two"; private static final String s3 = "three"; private static final String s4 = "four"; /** Collections to be used to test */ private static final Collection c12 = new Vector(); private static final Collection c12n = new Vector(); /** Main entry point to program */ public static void main(String[] args) { setupCollections(); try { testMHM(); } catch (Exception excn) { excn.printStackTrace(); } } public static void setupCollections() { c12.add(s1); c12.add(s2); c12n.addAll(c12); c12n.add(null); } public static void testMHM() throws CheckFailedException { MultiHashMap mhm = new MultiHashMap(); check("Assigning value to a key that was not in before", mhm.put(o1, s1) == null); check("Verifying association caused by put", s1.equals(mhm.get(o1))); check("Verifying return value when reassigning a key to a value", s1.equals(mhm.put(o1, s2))); checkEqualCollection("Verifying getAll method", c12, mhm.getAll(o1)); mhm.putAll(o3, c12); checkEqualCollection("Verifying putAll method", c12, mhm.getAll(o3)); mhm.put(o3, null); checkEqualCollection("Verifying the ability to add null values with put method", c12n, mhm.getAll(o3)); check("Verifying the contains method works for null", mhm.contains(o3, null)); check("Verifying the contains method works", mhm.contains(o3, s1)); check("Verifying remove method return value", mhm.remove(o3, null)); check("Verifying remove method works", !mhm.contains(o3, null)); checkEqualCollection("Verifying HashMap remove method return value", (Collection) mhm.remove(o3), c12); check("Verifying HashMap remove method works", mhm.get(o3) == null); } public static void check(String message, boolean result) throws CheckFailedException { if (result == false) throw new CheckFailedException(message + " failed"); else System.out.println(message + " passed"); } public static void checkEqualCollection(String message, Collection c1, Collection c2) throws CheckFailedException { check(message, c1.containsAll(c2) && c2.containsAll(c1)); } private static class CheckFailedException extends Exception { public CheckFailedException() { super(); } public CheckFailedException(String message) { super(message); } } }