KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > acceptance > MapTest


1 package com.thoughtworks.acceptance;
2
3 import com.thoughtworks.acceptance.objects.Hardware;
4 import com.thoughtworks.acceptance.objects.Software;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Hashtable JavaDoc;
8 import java.util.LinkedHashMap JavaDoc;
9 import java.util.Map JavaDoc;
10
11 public class MapTest extends AbstractAcceptanceTest {
12
13     public void testMapCanContainBasicObjects() {
14         Map JavaDoc map = new HashMap JavaDoc();
15         map.put("benny", "hill");
16         map.put("joe", "walnes");
17
18         String JavaDoc expected = "" +
19                 "<map>\n" +
20                 " <entry>\n" +
21                 " <string>benny</string>\n" +
22                 " <string>hill</string>\n" +
23                 " </entry>\n" +
24                 " <entry>\n" +
25                 " <string>joe</string>\n" +
26                 " <string>walnes</string>\n" +
27                 " </entry>\n" +
28                 "</map>";
29
30         assertBothWays(map, expected);
31     }
32
33     public void testMapCanContainCustomObjects() {
34         Map JavaDoc map = new HashMap JavaDoc();
35         map.put(new Software("microsoft", "windows"), new Hardware("x86", "p4"));
36
37         xstream.alias("software", Software.class);
38         xstream.alias("hardware", Hardware.class);
39
40         String JavaDoc expected = "" +
41                 "<map>\n" +
42                 " <entry>\n" +
43                 " <software>\n" +
44                 " <name>windows</name>\n" +
45                 " <vendor>microsoft</vendor>\n" +
46                 " </software>\n" +
47                 " <hardware>\n" +
48                 " <arch>x86</arch>\n" +
49                 " <name>p4</name>\n" +
50                 " </hardware>\n" +
51                 " </entry>\n" +
52                 "</map>";
53
54         assertBothWays(map, expected);
55     }
56
57     class ThingWithMap extends StandardObject {
58         Map JavaDoc stuff = new HashMap JavaDoc();
59     }
60
61     public void testObjectCanContainMapAsField() {
62         ThingWithMap t = new ThingWithMap();
63         t.stuff.put("hi", "bye");
64
65         xstream.alias("thing-with-map", ThingWithMap.class);
66
67         String JavaDoc expected = "" +
68                 "<thing-with-map>\n" +
69                 " <stuff>\n" +
70                 " <entry>\n" +
71                 " <string>hi</string>\n" +
72                 " <string>bye</string>\n" +
73                 " </entry>\n" +
74                 " </stuff>\n" +
75                 "</thing-with-map>";
76
77         assertBothWays(t, expected);
78     }
79
80     public void testSupportsOldHashtables() {
81
82         Hashtable JavaDoc hashtable = new Hashtable JavaDoc();
83         hashtable.put("hello", "world");
84
85         String JavaDoc expected = "" +
86                 "<hashtable>\n" +
87                 " <entry>\n" +
88                 " <string>hello</string>\n" +
89                 " <string>world</string>\n" +
90                 " </entry>\n" +
91                 "</hashtable>";
92
93         assertBothWays(hashtable, expected);
94     }
95
96     class ThingWithDifferentTypesOfMaps extends StandardObject {
97         private Map JavaDoc m1 = new HashMap JavaDoc();
98         private Map JavaDoc m2 = new Hashtable JavaDoc();
99         private HashMap JavaDoc m3 = new HashMap JavaDoc();
100         private Hashtable JavaDoc m4 = new Hashtable JavaDoc();
101     }
102
103     public void testObjectCanContainDifferentMapImplementations() {
104
105         xstream.alias("thing", ThingWithDifferentTypesOfMaps.class);
106
107         ThingWithDifferentTypesOfMaps thing = new ThingWithDifferentTypesOfMaps();
108
109         String JavaDoc expected = "" +
110                 "<thing>\n" +
111                 " <m1/>\n" +
112                 " <m2 class=\"hashtable\"/>\n" +
113                 " <m3/>\n" +
114                 " <m4/>\n" +
115                 "</thing>";
116
117         assertBothWays(thing, expected);
118
119     }
120
121     public void testLinkedHashMapRetainsOrdering() {
122         Map JavaDoc map = new LinkedHashMap JavaDoc();
123         map.put("Z", "a");
124         map.put("C", "c");
125         map.put("X", "b");
126
127         LinkedHashMap JavaDoc result = (LinkedHashMap JavaDoc) assertBothWays(map,
128                 "<linked-hash-map>\n" +
129                 " <entry>\n" +
130                 " <string>Z</string>\n" +
131                 " <string>a</string>\n" +
132                 " </entry>\n" +
133                 " <entry>\n" +
134                 " <string>C</string>\n" +
135                 " <string>c</string>\n" +
136                 " </entry>\n" +
137                 " <entry>\n" +
138                 " <string>X</string>\n" +
139                 " <string>b</string>\n" +
140                 " </entry>\n" +
141                 "</linked-hash-map>");
142
143         Object JavaDoc[] keys = result.keySet().toArray();
144         assertEquals("Z", keys[0]);
145         assertEquals("C", keys[1]);
146         assertEquals("X", keys[2]);
147     }
148 }
149
Popular Tags