001: /*
002: * Copyright (C) 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 08. May 2005 by Joe Walnes
011: */
012: package com.thoughtworks.acceptance;
013:
014: import java.util.Comparator;
015: import java.util.TreeMap;
016: import java.util.TreeSet;
017:
018: public class TreeMapAndTreeSetTest extends AbstractAcceptanceTest {
019:
020: public static class MyComparator implements Comparator {
021: private String something = "stuff";
022:
023: public int compare(Object o1, Object o2) {
024: return ((String) o1).compareTo((String) o2);
025: }
026: }
027:
028: public static class UnusedComparator implements Comparator {
029:
030: private final static Comparator THROWING_COMPARATOR = new Comparator() {
031:
032: public int compare(Object o1, Object o2) {
033: throw new UnsupportedOperationException();
034: }
035:
036: };
037:
038: public int compare(Object o1, Object o2) {
039: return ((String) o1).compareTo((String) o2);
040: }
041:
042: private Object readResolve() {
043: return THROWING_COMPARATOR;
044: }
045: }
046:
047: protected void setUp() throws Exception {
048: super .setUp();
049: xstream.alias("my-comparator", MyComparator.class);
050: xstream.alias("unused-comparator", UnusedComparator.class);
051: }
052:
053: public void testTreeMapWithComparator() {
054: TreeMap map = new TreeMap(new MyComparator());
055: map.put("benny", "hill");
056: map.put("joe", "walnes");
057:
058: String expected = "" + "<tree-map>\n"
059: + " <comparator class=\"my-comparator\">\n"
060: + " <something>stuff</something>\n"
061: + " </comparator>\n" + " <entry>\n"
062: + " <string>benny</string>\n"
063: + " <string>hill</string>\n" + " </entry>\n"
064: + " <entry>\n" + " <string>joe</string>\n"
065: + " <string>walnes</string>\n" + " </entry>\n"
066: + "</tree-map>";
067:
068: TreeMap result = (TreeMap) assertBothWays(map, expected);
069: assertEquals(MyComparator.class, result.comparator().getClass());
070: }
071:
072: public void testTreeMapWithoutComparator() {
073: TreeMap map = new TreeMap();
074: map.put("benny", "hill");
075: map.put("joe", "walnes");
076:
077: String expected = "" + "<tree-map>\n" + " <no-comparator/>\n"
078: + " <entry>\n" + " <string>benny</string>\n"
079: + " <string>hill</string>\n" + " </entry>\n"
080: + " <entry>\n" + " <string>joe</string>\n"
081: + " <string>walnes</string>\n" + " </entry>\n"
082: + "</tree-map>";
083:
084: TreeMap result = (TreeMap) assertBothWays(map, expected);
085: assertNull(result.comparator());
086: }
087:
088: public void testTreeMapDoesNotUseComparatorAtDeserialization() {
089: TreeMap map = new TreeMap(new UnusedComparator());
090: map.put("benny", "hill");
091: map.put("joe", "walnes");
092:
093: String expected = "" + "<tree-map>\n"
094: + " <comparator class=\"unused-comparator\"/>\n"
095: + " <entry>\n" + " <string>benny</string>\n"
096: + " <string>hill</string>\n" + " </entry>\n"
097: + " <entry>\n" + " <string>joe</string>\n"
098: + " <string>walnes</string>\n" + " </entry>\n"
099: + "</tree-map>";
100:
101: assertEquals(expected, xstream.toXML(map));
102: TreeMap result = (TreeMap) xstream.fromXML(expected);
103: assertSame(UnusedComparator.THROWING_COMPARATOR, result
104: .comparator());
105: TreeMap compareMap = new TreeMap(new UnusedComparator());
106: compareMap.putAll(result);
107: assertEquals(map, compareMap);
108: }
109:
110: public void testTreeSetWithComparator() {
111: TreeSet set = new TreeSet(new MyComparator());
112: set.add("hi");
113: set.add("bye");
114:
115: String expected = "" + "<tree-set>\n"
116: + " <comparator class=\"my-comparator\">\n"
117: + " <something>stuff</something>\n"
118: + " </comparator>\n" + " <string>bye</string>\n"
119: + " <string>hi</string>\n" + "</tree-set>";
120:
121: TreeSet result = (TreeSet) assertBothWays(set, expected);
122: assertEquals(MyComparator.class, result.comparator().getClass());
123: }
124:
125: public void testTreeSetWithoutComparator() {
126: TreeSet set = new TreeSet();
127: set.add("hi");
128: set.add("bye");
129:
130: String expected = "" + "<tree-set>\n" + " <no-comparator/>\n"
131: + " <string>bye</string>\n"
132: + " <string>hi</string>\n" + "</tree-set>";
133:
134: assertBothWays(set, expected);
135: }
136:
137: public void testTreeSetDoesNotUseComparatorAtDeserialization() {
138: TreeSet set = new TreeSet(new UnusedComparator());
139: set.add("hi");
140: set.add("bye");
141:
142: String expected = "" + "<tree-set>\n"
143: + " <comparator class=\"unused-comparator\"/>\n"
144: + " <string>bye</string>\n"
145: + " <string>hi</string>\n" + "</tree-set>";
146:
147: assertEquals(expected, xstream.toXML(set));
148: TreeSet result = (TreeSet) xstream.fromXML(expected);
149: assertSame(UnusedComparator.THROWING_COMPARATOR, result
150: .comparator());
151: TreeSet compareSet = new TreeSet(new UnusedComparator());
152: compareSet.addAll(result);
153: assertEquals(set, compareSet);
154: }
155: }
|