01: /*
02: * Copyright (C) 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 06. February 2005 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.core.util;
13:
14: import junit.framework.TestCase;
15:
16: import java.util.Iterator;
17: import java.util.Map;
18:
19: public class OrderRetainingMapTest extends TestCase {
20: private Map map;
21:
22: private void assertNextEquals(Object expected, Iterator iterator) {
23: assertTrue("Expected to pull of another item from iterator : "
24: + expected, iterator.hasNext());
25: assertEquals(expected, iterator.next());
26: }
27:
28: private void assertNextEntryEquals(Object expectedKey,
29: Object expectedValue, Iterator iterator) {
30: assertTrue("Expected to pull of another item from iterator : "
31: + expectedKey + "=" + expectedValue, iterator.hasNext());
32: Map.Entry actual = (Map.Entry) iterator.next();
33: assertEquals(expectedKey, actual.getKey());
34: assertEquals(expectedValue, actual.getValue());
35: }
36:
37: private void assertNoMore(Iterator iterator) {
38: assertFalse("Should be no more items in iterator", iterator
39: .hasNext());
40: }
41:
42: protected void setUp() throws Exception {
43: super .setUp();
44: map = new OrderRetainingMap();
45: map.put("one", "ONE");
46: map.put("two", "TWO");
47: map.put("three", "THREE");
48: map.put("four", "FOUR");
49: }
50:
51: public void testMaintainsOrderOfKeySet() {
52: Iterator keySetIterator = map.keySet().iterator();
53: assertNextEquals("one", keySetIterator);
54: assertNextEquals("two", keySetIterator);
55: assertNextEquals("three", keySetIterator);
56: assertNextEquals("four", keySetIterator);
57: assertNoMore(keySetIterator);
58: }
59:
60: public void testMaintainsOrderOfValues() {
61: Iterator valuesIterator = map.values().iterator();
62: assertNextEquals("ONE", valuesIterator);
63: assertNextEquals("TWO", valuesIterator);
64: assertNextEquals("THREE", valuesIterator);
65: assertNextEquals("FOUR", valuesIterator);
66: assertNoMore(valuesIterator);
67: }
68:
69: public void testMaintainsOrderOfEntries() {
70: Iterator entrySetIterator = map.entrySet().iterator();
71: assertNextEntryEquals("one", "ONE", entrySetIterator);
72: assertNextEntryEquals("two", "TWO", entrySetIterator);
73: assertNextEntryEquals("three", "THREE", entrySetIterator);
74: assertNextEntryEquals("four", "FOUR", entrySetIterator);
75: assertNoMore(entrySetIterator);
76: }
77:
78: public void testMaintainsOrderOfEntriesAfterCopyCtor() {
79: Iterator entrySetIterator = new OrderRetainingMap(map)
80: .entrySet().iterator();
81: assertNextEntryEquals("one", "ONE", entrySetIterator);
82: assertNextEntryEquals("two", "TWO", entrySetIterator);
83: assertNextEntryEquals("three", "THREE", entrySetIterator);
84: assertNextEntryEquals("four", "FOUR", entrySetIterator);
85: assertNoMore(entrySetIterator);
86: }
87:
88: }
|