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;
7 import java.util.Map;
8
9 public class MapTest extends AbstractAcceptanceTest {
10
11 public void testMapCanContainBasicObjects() {
12 Map map = new HashMap();
13 map.put("benny", "hill");
14 map.put("joe", "walnes");
15
16 String expected = "" +
17 "<map>\n" +
18 " <entry>\n" +
19 " <string>benny</string>\n" +
20 " <string>hill</string>\n" +
21 " </entry>\n" +
22 " <entry>\n" +
23 " <string>joe</string>\n" +
24 " <string>walnes</string>\n" +
25 " </entry>\n" +
26 "</map>";
27
28 assertBothWays(map, expected);
29 }
30
31 public void testMapCanContainCustomObjects() {
32 Map map = new HashMap();
33 map.put(new Software("microsoft", "windows"), new Hardware("x86", "p4"));
34
35 xstream.alias("software", Software.class);
36 xstream.alias("hardware", Hardware.class);
37
38 String expected = "" +
39 "<map>\n" +
40 " <entry>\n" +
41 " <software>\n" +
42 " <vendor>microsoft</vendor>\n" +
43 " <name>windows</name>\n" +
44 " </software>\n" +
45 " <hardware>\n" +
46 " <arch>x86</arch>\n" +
47 " <name>p4</name>\n" +
48 " </hardware>\n" +
49 " </entry>\n" +
50 "</map>";
51
52 assertBothWays(map, expected);
53 }
54
55 class ThingWithMap {
56 Map stuff = new HashMap();
57
58 public boolean equals(Object obj) {
59 if (obj instanceof ThingWithMap) {
60 ThingWithMap thingWithMap = (ThingWithMap) obj;
61 return stuff.equals(thingWithMap.stuff);
62 }
63 return false;
64 }
65 }
66
67 public void testObjectCanContainMapAsField() {
68 ThingWithMap t = new ThingWithMap();
69 t.stuff.put("hi", "bye");
70
71 xstream.alias("thing-with-map", ThingWithMap.class);
72
73 String expected = "" +
74 "<thing-with-map>\n" +
75 " <stuff>\n" +
76 " <entry>\n" +
77 " <string>hi</string>\n" +
78 " <string>bye</string>\n" +
79 " </entry>\n" +
80 " </stuff>\n" +
81 "</thing-with-map>";
82
83 assertBothWays(t, expected);
84 }
85
86 }
This page was automatically generated by Maven