KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > acceptance > someobjects > TreeMapAndTreeSetTest


1 package com.thoughtworks.acceptance.someobjects;
2
3 import com.thoughtworks.acceptance.AbstractAcceptanceTest;
4
5 import java.util.Comparator JavaDoc;
6 import java.util.TreeMap JavaDoc;
7 import java.util.TreeSet JavaDoc;
8
9 public class TreeMapAndTreeSetTest extends AbstractAcceptanceTest {
10
11     public static class MyComparator implements Comparator JavaDoc {
12         private String JavaDoc something = "stuff";
13
14         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
15             return ((String JavaDoc) o1).compareTo(o2);
16         }
17     }
18
19     protected void setUp() throws Exception JavaDoc {
20         super.setUp();
21         xstream.alias("my-comparator", MyComparator.class);
22     }
23
24     public void testTreeMapWithComparator() {
25         TreeMap JavaDoc map = new TreeMap JavaDoc(new MyComparator());
26         map.put("benny", "hill");
27         map.put("joe", "walnes");
28
29         String JavaDoc 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 JavaDoc result = (TreeMap JavaDoc) assertBothWays(map, expected);
45         assertEquals(MyComparator.class, result.comparator().getClass());
46     }
47
48     public void testTreeMapWithoutComparator() {
49         TreeMap JavaDoc map = new TreeMap JavaDoc();
50         map.put("benny", "hill");
51         map.put("joe", "walnes");
52
53         String JavaDoc 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 JavaDoc result = (TreeMap JavaDoc) assertBothWays(map, expected);
67         assertNull(result.comparator());
68     }
69
70     public void testTreeSetWithComparator() {
71         TreeSet JavaDoc set = new TreeSet JavaDoc(new MyComparator());
72         set.add("hi");
73         set.add("bye");
74
75         String JavaDoc 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 JavaDoc result = (TreeSet JavaDoc) assertBothWays(set, expected);
85         assertEquals(MyComparator.class, result.comparator().getClass());
86     }
87
88     public void testTreeSetWithoutComparator() {
89         TreeSet JavaDoc set = new TreeSet JavaDoc();
90         set.add("hi");
91         set.add("bye");
92
93         String JavaDoc 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