1 package com.thoughtworks.xstream.core.util; 2 3 import junit.framework.TestCase; 4 5 import java.util.Map ; 6 import java.util.Iterator ; 7 8 public class OrderRetainingMapTest extends TestCase { 9 private Map map; 10 11 private void assertNextEquals(Object expected, Iterator iterator) { 12 assertTrue("Expected to pull of another item from iterator : " + expected, iterator.hasNext()); 13 assertEquals(expected, iterator.next()); 14 } 15 16 private void assertNextEntryEquals(Object expectedKey, Object expectedValue, Iterator iterator) { 17 assertTrue("Expected to pull of another item from iterator : " + expectedKey + "=" + expectedValue, iterator.hasNext()); 18 Map.Entry actual = (Map.Entry ) iterator.next(); 19 assertEquals(expectedKey, actual.getKey()); 20 assertEquals(expectedValue, actual.getValue()); 21 } 22 23 private void assertNoMore(Iterator iterator) { 24 assertFalse("Should be no more items in iterator", iterator.hasNext()); 25 } 26 27 protected void setUp() throws Exception { 28 super.setUp(); 29 map = new OrderRetainingMap(); 30 map.put("one", "ONE"); 31 map.put("two", "TWO"); 32 map.put("three", "THREE"); 33 map.put("four", "FOUR"); 34 } 35 36 public void testMaintainsOrderOfKeySet() { 37 Iterator keySetIterator = map.keySet().iterator(); 38 assertNextEquals("one", keySetIterator); 39 assertNextEquals("two", keySetIterator); 40 assertNextEquals("three", keySetIterator); 41 assertNextEquals("four", keySetIterator); 42 assertNoMore(keySetIterator); 43 } 44 45 public void testMaintainsOrderOfValues() { 46 Iterator valuesIterator = map.values().iterator(); 47 assertNextEquals("ONE", valuesIterator); 48 assertNextEquals("TWO", valuesIterator); 49 assertNextEquals("THREE", valuesIterator); 50 assertNextEquals("FOUR", valuesIterator); 51 assertNoMore(valuesIterator); 52 } 53 54 public void TODOtestMaintainsOrderOfEntries() { 56 Iterator entrySetIterator = map.entrySet().iterator(); 57 assertNextEntryEquals("one", "ONE", entrySetIterator); 58 assertNextEntryEquals("two", "TWO", entrySetIterator); 59 assertNextEntryEquals("three", "THREE", entrySetIterator); 60 assertNextEntryEquals("four", "FOUR", entrySetIterator); 61 assertNoMore(entrySetIterator); 62 } 63 64 } 65 | Popular Tags |