001: /*
002: * Copyright (C) 2003, 2004, 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 26. September 2003 by Joe Walnes
011: */
012: package com.thoughtworks.acceptance;
013:
014: import com.thoughtworks.acceptance.objects.Hardware;
015: import com.thoughtworks.acceptance.objects.Software;
016: import com.thoughtworks.acceptance.objects.StandardObject;
017: import com.thoughtworks.xstream.core.JVM;
018:
019: import java.util.Collections;
020: import java.util.HashMap;
021: import java.util.Hashtable;
022: import java.util.LinkedHashMap;
023: import java.util.Map;
024:
025: public class MapTest extends AbstractAcceptanceTest {
026:
027: public void testMapCanContainBasicObjects() {
028: Map map = new HashMap();
029: map.put("benny", "hill");
030: map.put("joe", "walnes");
031:
032: String expected = "" + "<map>\n" + " <entry>\n"
033: + " <string>benny</string>\n"
034: + " <string>hill</string>\n" + " </entry>\n"
035: + " <entry>\n" + " <string>joe</string>\n"
036: + " <string>walnes</string>\n" + " </entry>\n"
037: + "</map>";
038:
039: assertBothWaysNormalized(map, expected, "map", "entry",
040: "string[1]");
041: }
042:
043: public void testMapCanContainCustomObjects() {
044: Map map = new HashMap();
045: map.put(new Software("microsoft", "windows"), new Hardware(
046: "x86", "p4"));
047:
048: xstream.alias("software", Software.class);
049: xstream.alias("hardware", Hardware.class);
050:
051: String expected = "" + "<map>\n" + " <entry>\n"
052: + " <software>\n"
053: + " <vendor>microsoft</vendor>\n"
054: + " <name>windows</name>\n" + " </software>\n"
055: + " <hardware>\n" + " <arch>x86</arch>\n"
056: + " <name>p4</name>\n" + " </hardware>\n"
057: + " </entry>\n" + "</map>";
058:
059: assertBothWays(map, expected);
060: }
061:
062: static class ThingWithMap extends StandardObject {
063: Map stuff = new HashMap();
064: }
065:
066: public void testObjectCanContainMapAsField() {
067: ThingWithMap t = new ThingWithMap();
068: t.stuff.put("hi", "bye");
069:
070: xstream.alias("thing-with-map", ThingWithMap.class);
071:
072: String expected = "" + "<thing-with-map>\n" + " <stuff>\n"
073: + " <entry>\n" + " <string>hi</string>\n"
074: + " <string>bye</string>\n" + " </entry>\n"
075: + " </stuff>\n" + "</thing-with-map>";
076:
077: assertBothWays(t, expected);
078: }
079:
080: public void testSupportsOldHashtables() {
081:
082: Hashtable hashtable = new Hashtable();
083: hashtable.put("hello", "world");
084:
085: String expected = "" + "<hashtable>\n" + " <entry>\n"
086: + " <string>hello</string>\n"
087: + " <string>world</string>\n" + " </entry>\n"
088: + "</hashtable>";
089:
090: assertBothWays(hashtable, expected);
091: }
092:
093: static class ThingWithDifferentTypesOfMaps extends StandardObject {
094: private Map m1 = new HashMap();
095: private Map m2 = new Hashtable();
096: private HashMap m3 = new HashMap();
097: private Hashtable m4 = new Hashtable();
098: }
099:
100: public void testObjectCanContainDifferentMapImplementations() {
101:
102: xstream.alias("thing", ThingWithDifferentTypesOfMaps.class);
103:
104: ThingWithDifferentTypesOfMaps thing = new ThingWithDifferentTypesOfMaps();
105:
106: String expected = "" + "<thing>\n" + " <m1/>\n"
107: + " <m2 class=\"hashtable\"/>\n" + " <m3/>\n"
108: + " <m4/>\n" + "</thing>";
109:
110: assertBothWays(thing, expected);
111:
112: }
113:
114: public void testLinkedHashMapRetainsOrdering() {
115: Map map = new LinkedHashMap();
116: map.put("Z", "a");
117: map.put("C", "c");
118: map.put("X", "b");
119:
120: LinkedHashMap result = (LinkedHashMap) assertBothWays(map,
121: "<linked-hash-map>\n" + " <entry>\n"
122: + " <string>Z</string>\n"
123: + " <string>a</string>\n" + " </entry>\n"
124: + " <entry>\n" + " <string>C</string>\n"
125: + " <string>c</string>\n" + " </entry>\n"
126: + " <entry>\n" + " <string>X</string>\n"
127: + " <string>b</string>\n" + " </entry>\n"
128: + "</linked-hash-map>");
129:
130: Object[] keys = result.keySet().toArray();
131: assertEquals("Z", keys[0]);
132: assertEquals("C", keys[1]);
133: assertEquals("X", keys[2]);
134: }
135:
136: public void testAllowsEntryToBeAliasedToSomethingElse() {
137: Map map = new HashMap();
138: map.put("benny", "hill");
139: map.put("joe", "walnes");
140:
141: String expected = "" + "<map>\n" + " <thing>\n"
142: + " <string>benny</string>\n"
143: + " <string>hill</string>\n" + " </thing>\n"
144: + " <thing>\n" + " <string>joe</string>\n"
145: + " <string>walnes</string>\n" + " </thing>\n"
146: + "</map>";
147:
148: xstream.alias("thing", Map.Entry.class);
149: assertBothWaysNormalized(map, expected, "map", "thing",
150: "string[1]");
151: }
152:
153: public static class MyMap extends HashMap {
154:
155: }
156:
157: public void testSubclassesOfMapAreHandled() {
158: MyMap myMap = new MyMap();
159: myMap.put("hehe", "hoho");
160: String xml = xstream.toXML(myMap);
161: MyMap myOtherMap = (MyMap) xstream.fromXML(xml);
162: assertEquals(myMap, myOtherMap);
163: }
164:
165: public void testSynchronizedMap() {
166: final String expected;
167: if (JVM.is15()) {
168: expected = ""
169: + "<java.util.Collections_-SynchronizedMap serialization=\"custom\">\n"
170: + " <java.util.Collections_-SynchronizedMap>\n"
171: + " <default>\n"
172: + " <m/>\n"
173: + " <mutex class=\"java.util.Collections$SynchronizedMap\" reference=\"../../..\"/>\n"
174: + " </default>\n"
175: + " </java.util.Collections_-SynchronizedMap>\n"
176: + "</java.util.Collections_-SynchronizedMap>";
177: } else {
178: expected = ""
179: + "<java.util.Collections_-SynchronizedMap>\n"
180: + " <m/>\n"
181: + " <mutex class=\"java.util.Collections$SynchronizedMap\" reference=\"..\"/>\n"
182: + "</java.util.Collections_-SynchronizedMap>";
183: }
184:
185: assertBothWays(Collections.synchronizedMap(new HashMap()),
186: expected);
187: }
188:
189: public void testUnmodifiableMap() {
190: String expected = ""
191: + "<java.util.Collections_-UnmodifiableMap>\n"
192: + " <m/>\n"
193: + "</java.util.Collections_-UnmodifiableMap>";
194:
195: assertBothWays(Collections.unmodifiableMap(new HashMap()),
196: expected);
197: }
198:
199: public void testSingletonMap() {
200: String expected = ""
201: + "<java.util.Collections_-SingletonMap>\n"
202: + " <k class=\"com.thoughtworks.acceptance.objects.Software\">\n"
203: + " <vendor>microsoft</vendor>\n"
204: + " <name>windows</name>\n"
205: + " </k>\n"
206: + " <v class=\"com.thoughtworks.acceptance.objects.Hardware\">\n"
207: + " <arch>x86</arch>\n" + " <name>p4</name>\n"
208: + " </v>\n" + "</java.util.Collections_-SingletonMap>";
209:
210: assertBothWays(Collections.singletonMap(new Software(
211: "microsoft", "windows"), new Hardware("x86", "p4")),
212: expected);
213: }
214: }
|