package com.michaelthomas.map.treemap; /** * @author Michael Thomas (www.michael-thomas.com) michael@michael-thomas.com * */ import static org.junit.Assert.assertEquals; import java.util.LinkedHashMap; import java.util.Map; import java.util.TreeMap; import org.junit.Before; import org.junit.Test; import com.michaelthomas.map.MyMapTraverse; import com.michaelthomas.map.linkedhashmap.MyLinkedHashMap; public class MyTreeMapTest { private MyTreeMap myMapObj = new MyTreeMap(); private MyMapTraverse myMapTraverseObj = new MyMapTraverse(); private TreeMap mapWeekDays = new TreeMap(); private TreeMap mapAbbr = new TreeMap(); private TreeMap mapPlay = new TreeMap(); @Before public void setUp() throws Exception { mapWeekDays = myMapObj.buildMapWeekDays(); mapAbbr = myMapObj.buildMapAbbreviations(); } //TODO: Add examples with Object MyItem vs String. //TODO: Add examples of all the NavigableMap interface methods (also SortedMap) @Test public void testLinkedHashMapNotes(){ //PLEASE READ THIS! //NOTE: Make sure that you look at the following JUnit test for a fairly complete // coverage of all the methods of the Map interface. // /collections/test/com/michaelthomas/map/MyMapTest.java } @Test public void testLinkedHashMapCommonuses(){ //Most common uses: LinkedHashMap mapPlay2 = new LinkedHashMap(); mapPlay2.put("NY", "New York"); mapPlay2.put("LA", "Louisiana"); mapPlay2.put("GA", "Georgia"); assertEquals("Georgia", mapPlay2.get("GA")); assertEquals(3, mapPlay2.size()); } @Test public void testLinkedHashMapCommonWeekDays(){ assertEquals("Monday", mapWeekDays.get("Mon")); assertEquals(7, mapWeekDays.size()); } @Test public void testLinkedHashMapCommonPutAll(){ mapPlay.clear(); assertEquals(0, mapPlay.size()); mapPlay.putAll(mapWeekDays); assertEquals(7, mapPlay.size()); mapPlay.putAll(mapAbbr); assertEquals(10, mapPlay.size()); assertEquals("Monday", mapPlay.get("Mon")); assertEquals("Element", mapPlay.get("E")); } @Test public void testLinkedHashMapTraverseForEachExample() { //Note: The order is natural sort order! LinkedHashMap keeps order-entry vs no guarantee for HashMap. assertEquals("Fri = Friday, Mon = Monday, Sat = Saturday, Sun = Sunday, Thu = Thursday, Tue = Tuesday, Wed = Wednesday", myMapTraverseObj.forEachExampleEntrySet(mapWeekDays)); } }