1 package com.thoughtworks.acceptance.someobjects; 2 3 import com.thoughtworks.acceptance.AbstractAcceptanceTest; 4 5 import java.util.Comparator ; 6 import java.util.TreeMap ; 7 import java.util.TreeSet ; 8 9 public class TreeMapAndTreeSetTest extends AbstractAcceptanceTest { 10 11 public static class MyComparator implements Comparator { 12 private String something = "stuff"; 13 14 public int compare(Object o1, Object o2) { 15 return ((String ) o1).compareTo(o2); 16 } 17 } 18 19 protected void setUp() throws Exception { 20 super.setUp(); 21 xstream.alias("my-comparator", MyComparator.class); 22 } 23 24 public void testTreeMapWithComparator() { 25 TreeMap map = new TreeMap (new MyComparator()); 26 map.put("benny", "hill"); 27 map.put("joe", "walnes"); 28 29 String expected = "" + 30 "<tree-map>\n" + 31 " <comparator class=\"my-comparator\">\n" + 32 " <something>stuff</something>\n" + 33 " </comparator>\n" + 34 " <entry>\n" + 35 " <string>benny</string>\n" + 36 " <string>hill</string>\n" + 37 " </entry>\n" + 38 " <entry>\n" + 39 " <string>joe</string>\n" + 40 " <string>walnes</string>\n" + 41 " </entry>\n" + 42 "</tree-map>"; 43 44 TreeMap result = (TreeMap ) assertBothWays(map, expected); 45 assertEquals(MyComparator.class, result.comparator().getClass()); 46 } 47 48 public void testTreeMapWithoutComparator() { 49 TreeMap map = new TreeMap (); 50 map.put("benny", "hill"); 51 map.put("joe", "walnes"); 52 53 String expected = "" + 54 "<tree-map>\n" + 55 " <no-comparator/>\n" + 56 " <entry>\n" + 57 " <string>benny</string>\n" + 58 " <string>hill</string>\n" + 59 " </entry>\n" + 60 " <entry>\n" + 61 " <string>joe</string>\n" + 62 " <string>walnes</string>\n" + 63 " </entry>\n" + 64 "</tree-map>"; 65 66 TreeMap result = (TreeMap ) assertBothWays(map, expected); 67 assertNull(result.comparator()); 68 } 69 70 public void testTreeSetWithComparator() { 71 TreeSet set = new TreeSet (new MyComparator()); 72 set.add("hi"); 73 set.add("bye"); 74 75 String expected = "" + 76 "<tree-set>\n" + 77 " <comparator class=\"my-comparator\">\n" + 78 " <something>stuff</something>\n" + 79 " </comparator>\n" + 80 " <string>bye</string>\n" + 81 " <string>hi</string>\n" + 82 "</tree-set>"; 83 84 TreeSet result = (TreeSet ) assertBothWays(set, expected); 85 assertEquals(MyComparator.class, result.comparator().getClass()); 86 } 87 88 public void testTreeSetWithoutComparator() { 89 TreeSet set = new TreeSet (); 90 set.add("hi"); 91 set.add("bye"); 92 93 String expected = "" + 94 "<tree-set>\n" + 95 " <no-comparator/>\n" + 96 " <string>bye</string>\n" + 97 " <string>hi</string>\n" + 98 "</tree-set>"; 99 100 assertBothWays(set, expected); 101 } 102 } 103 | Popular Tags |