package com.michaelthomas.map; /** * @author Michael Thomas (www.michael-thomas.com) michael@michael-thomas.com * */ import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeSet; public class MyMapTest { private MyMap myMapObj = new MyMap(); private MyMapTraverse myMapTraverseObj = new MyMapTraverse(); private Map map = new HashMap(); private Map mapAbbr = new HashMap(); @Before public void setUp() throws Exception { map = myMapObj.buildMapWeekDays(); mapAbbr = myMapObj.buildMapAbbreviations(); } @Test public void testMapSize() { assertEquals(7, map.size()); } @Test public void testMapClearIsEmpty() { assertEquals(7, map.size()); assertFalse(map.isEmpty()); map.clear(); assertEquals(0, map.size()); assertTrue(map.isEmpty()); } @Test public void testMapPutGet() { assertEquals("Monday", map.get("Mon")); assertEquals("Tuesday", map.get("Tue")); map.put("KISS", "Keep it simple stupid"); assertEquals("Keep it simple stupid", map.get("KISS")); } @Test public void testMapPut2() { assertEquals("Friday", map.get("Fri")); map.put("Fri", "TGIF"); //Replaces the current value. assertEquals("TGIF", map.get("Fri")); } @Test public void testMapPutAll() { assertEquals(7, map.size()); assertEquals(null, map.get("E")); map.putAll(mapAbbr); assertEquals(10, map.size()); assertEquals("Element", map.get("E")); } @Test public void testMapContainsKey() { assertTrue(map.containsKey("Mon")); assertFalse(map.containsKey("X")); } @Test public void testMapContainsValue() { assertTrue(map.containsValue("Monday")); assertFalse(map.containsValue("XXX")); } @Test public void testMapEquals() { assertTrue(mapAbbr.equals(mapAbbr)); Map mapDuplicate = new HashMap(mapAbbr); assertTrue(mapAbbr.equals(mapDuplicate)); //Different order. Map mapChangeOrder = new HashMap(); mapChangeOrder.put("V", "Value"); mapChangeOrder.put("K", "Key"); mapChangeOrder.put("E", "Element"); assertTrue(mapAbbr.equals(mapChangeOrder)); //Order doesn't matter! assertFalse(mapAbbr.equals(map)); //Does not not equal! } @Test public void testMapRemove() { assertTrue(map.containsValue("Monday")); //Exists! assertEquals("Monday", map.remove("Mon")); assertFalse(map.containsValue("Monday")); //Removed! } @Test public void testMapValues() { //WARNING: values() - you can't predict the order! //So I'm using TreeSet to create a sorted set. TreeSet list = new TreeSet(mapAbbr.values()); assertEquals("[Element, Key, Value]", list.toString()); ArrayList alist = new ArrayList(mapAbbr.values()); assertTrue(alist.contains("Element")); assertEquals(3, alist.size()); } @Test public void testMapKeySet() { //WARNING: keySet() - you can't predict the order! //So I'm using TreeSet to create a sorted set. TreeSet list = new TreeSet(mapAbbr.keySet()); assertEquals("[E, K, V]", list.toString()); ArrayList alist = new ArrayList(mapAbbr.keySet()); assertTrue(alist.contains("E")); assertEquals(3, alist.size()); } @Test public void testMapEntrySet() { //TODO: Look into adding more to this example. //WARNING: entrySet() - you can't predict the order! Set> set = mapAbbr.entrySet(); assertEquals("[E=Element, V=Value, K=Key]", set.toString()); StringBuffer sb = new StringBuffer(); for (Map.Entry mapEntry : set) { sb.append(mapEntry.getKey() + ": "); sb.append(mapEntry.getValue() + ", "); } assertEquals("E: Element, V: Value, K: Key, ", sb.toString()); } @Test public void testMapTraverseForEachExample() { //Note: Order is not guaranteed. assertEquals("Thu = Thursday, Wed = Wednesday, Sun = Sunday, Sat = Saturday, Fri = Friday, Tue = Tuesday, Mon = Monday", myMapTraverseObj.forEachExampleEntrySet(map)); } @Test public void testMapTraverseForEachExampleKeySet() { //Note: Order is not guaranteed. assertEquals("Thu = Thursday, Wed = Wednesday, Sun = Sunday, Sat = Saturday, Fri = Friday, Tue = Tuesday, Mon = Monday", myMapTraverseObj.forEachExampleKeySet(map)); } @Test public void testMapTraverseIterateForLoopExampleEntrySet() { //Note: Order is not guaranteed. assertEquals("Thu = Thursday, Wed = Wednesday, Sun = Sunday, Sat = Saturday, Fri = Friday, Tue = Tuesday, Mon = Monday", myMapTraverseObj.iterateForLoopExampleEntrySet(map)); } @Test public void testMapTraverseIterateForLoopExampleKeySet() { //Note: Order is not guaranteed. assertEquals("Thu = Thursday, Wed = Wednesday, Sun = Sunday, Sat = Saturday, Fri = Friday, Tue = Tuesday, Mon = Monday", myMapTraverseObj.iterateForLoopExampleKeySet(map)); } }