001: /*
002: * Copyright (C) 2003, 2004, 2005, 2006 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 01. October 2003 by Joe Walnes
011: */
012: package com.thoughtworks.acceptance;
013:
014: import com.thoughtworks.acceptance.objects.Hardware;
015: import com.thoughtworks.acceptance.objects.SampleLists;
016: import com.thoughtworks.acceptance.objects.Software;
017: import com.thoughtworks.xstream.XStream;
018: import com.thoughtworks.xstream.core.JVM;
019:
020: import java.util.ArrayList;
021: import java.util.Arrays;
022: import java.util.Collection;
023: import java.util.Collections;
024: import java.util.HashMap;
025: import java.util.HashSet;
026: import java.util.LinkedHashSet;
027: import java.util.LinkedList;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.Set;
031: import java.util.Vector;
032:
033: public class CollectionsTest extends AbstractAcceptanceTest {
034:
035: public void testListsCanContainCustomObjects() {
036: SampleLists lists = new SampleLists();
037: lists.good.add(new Software("apache", "geronimo"));
038: lists.good.add(new Software("caucho", "resin"));
039: lists.good.add(new Hardware("risc", "strong-arm"));
040: lists.bad.add(new Software("apache", "jserv"));
041:
042: xstream.alias("lists", SampleLists.class);
043: xstream.alias("software", Software.class);
044: xstream.alias("hardware", Hardware.class);
045:
046: String expected = "" + "<lists>\n" + " <good>\n"
047: + " <software>\n"
048: + " <vendor>apache</vendor>\n"
049: + " <name>geronimo</name>\n" + " </software>\n"
050: + " <software>\n"
051: + " <vendor>caucho</vendor>\n"
052: + " <name>resin</name>\n" + " </software>\n"
053: + " <hardware>\n" + " <arch>risc</arch>\n"
054: + " <name>strong-arm</name>\n"
055: + " </hardware>\n" + " </good>\n"
056: + " <bad class=\"list\">\n" + " <software>\n"
057: + " <vendor>apache</vendor>\n"
058: + " <name>jserv</name>\n" + " </software>\n"
059: + " </bad>\n" + "</lists>";
060:
061: assertBothWays(lists, expected);
062: }
063:
064: public void testListsCanContainBasicObjects() {
065: SampleLists lists = new SampleLists();
066: lists.good.add("hello");
067: lists.good.add(new Integer(3));
068: lists.good.add(Boolean.TRUE);
069:
070: xstream.alias("lists", SampleLists.class);
071:
072: String expected = "" + "<lists>\n" + " <good>\n"
073: + " <string>hello</string>\n" + " <int>3</int>\n"
074: + " <boolean>true</boolean>\n" + " </good>\n"
075: + " <bad class=\"list\"/>\n" + "</lists>";
076:
077: assertBothWays(lists, expected);
078: }
079:
080: public void testListCanBeRootObject() {
081: Collection list = new ArrayList();
082: list.add("hi");
083: list.add("bye");
084:
085: String expected = "" + "<list>\n" + " <string>hi</string>\n"
086: + " <string>bye</string>\n" + "</list>";
087:
088: assertBothWays(list, expected);
089: }
090:
091: public void testSetCanBeRootObject() {
092: Collection set = new HashSet();
093: set.add("hi");
094: set.add("bye");
095:
096: String expected = "" + "<set>\n" + " <string>hi</string>\n"
097: + " <string>bye</string>\n" + "</set>";
098:
099: assertBothWaysNormalized(set, expected, "set", "string", null);
100: }
101:
102: public void testVector() {
103: Vector vector = new Vector();
104: vector.addElement("a");
105: vector.addElement("b");
106:
107: assertBothWays(vector, "<vector>\n" + " <string>a</string>\n"
108: + " <string>b</string>\n" + "</vector>");
109: }
110:
111: public void testSyncronizedList() {
112: final String xml;
113: if (JVM.is15()) {
114: xml = "<java.util.Collections_-SynchronizedList serialization=\"custom\">\n"
115: + " <java.util.Collections_-SynchronizedCollection>\n"
116: + " <default>\n"
117: + " <c class=\"linked-list\">\n"
118: + " <string>hi</string>\n"
119: + " </c>\n"
120: + " <mutex class=\"java.util.Collections$SynchronizedList\" reference=\"../../..\"/>\n"
121: + " </default>\n"
122: + " </java.util.Collections_-SynchronizedCollection>\n"
123: + " <java.util.Collections_-SynchronizedList>\n"
124: + " <default>\n"
125: + " <list class=\"linked-list\" reference=\"../../../java.util.Collections_-SynchronizedCollection/default/c\"/>\n"
126: + " </default>\n"
127: + " </java.util.Collections_-SynchronizedList>\n"
128: + "</java.util.Collections_-SynchronizedList>";
129: } else {
130: xml = "<java.util.Collections_-SynchronizedList>\n"
131: + " <c class=\"linked-list\">\n"
132: + " <string>hi</string>\n"
133: + " </c>\n"
134: + " <mutex class=\"java.util.Collections$SynchronizedList\" reference=\"..\"/>\n"
135: + " <list class=\"linked-list\" reference=\"../c\"/>\n"
136: + "</java.util.Collections_-SynchronizedList>";
137: }
138:
139: // syncronized list has circular reference
140: xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
141:
142: List list = Collections.synchronizedList(new LinkedList());
143: list.add("hi");
144:
145: assertBothWays(list, xml);
146: }
147:
148: public void testEmptyList() {
149: assertBothWays(Collections.EMPTY_LIST,
150: "<java.util.Collections_-EmptyList/>");
151: }
152:
153: public void testUnmodifiableList() {
154: // unodifiable list has duplicate refs
155: xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
156:
157: List list = new ArrayList();
158: list.add("hi");
159: list = Collections.unmodifiableList(list);
160:
161: assertBothWays(
162: list,
163: "<java.util.Collections_-UnmodifiableRandomAccessList resolves-to=\"java.util.Collections$UnmodifiableList\">\n"
164: + " <c class=\"list\">\n"
165: + " <string>hi</string>\n"
166: + " </c>\n"
167: + " <list reference=\"../c\"/>\n"
168: + "</java.util.Collections_-UnmodifiableRandomAccessList>");
169: }
170:
171: public void testLinkedHashSetRetainsOrdering() {
172: Set set = new LinkedHashSet();
173: set.add("Z");
174: set.add("C");
175: set.add("X");
176:
177: LinkedHashSet result = (LinkedHashSet) assertBothWays(set,
178: "<linked-hash-set>\n" + " <string>Z</string>\n"
179: + " <string>C</string>\n"
180: + " <string>X</string>\n"
181: + "</linked-hash-set>");
182:
183: Object[] values = result.toArray();
184: assertEquals("Z", values[0]);
185: assertEquals("C", values[1]);
186: assertEquals("X", values[2]);
187: }
188:
189: public void testListFromArrayAsList() {
190: List list = Arrays.asList(new String[] { "hi", "bye" });
191:
192: assertBothWays(list, "<java.util.Arrays_-ArrayList>\n"
193: + " <a class=\"string-array\">\n"
194: + " <string>hi</string>\n"
195: + " <string>bye</string>\n" + " </a>\n"
196: + "</java.util.Arrays_-ArrayList>");
197: }
198:
199: public void testKeySetOfHashMapCanBeSerialized() {
200: final Map map = new HashMap();
201: map.put("JUnit", null);
202: final Collection set = map.keySet();
203:
204: assertBothWays(set, "<java.util.HashMap_-KeySet>\n"
205: + " <outer-class>\n" + " <entry>\n"
206: + " <string>JUnit</string>\n" + " <null/>\n"
207: + " </entry>\n" + " </outer-class>\n"
208: + "</java.util.HashMap_-KeySet>");
209: }
210:
211: public void testValueSetOfHashMapCanBeSerialized() {
212: final Map map = new HashMap();
213: map.put(Boolean.TRUE, "JUnit");
214: final Collection set = map.values();
215:
216: assertBothWays(set, "<java.util.HashMap_-Values>\n"
217: + " <outer-class>\n" + " <entry>\n"
218: + " <boolean>true</boolean>\n"
219: + " <string>JUnit</string>\n" + " </entry>\n"
220: + " </outer-class>\n" + "</java.util.HashMap_-Values>");
221: }
222:
223: public void testEntrySetOfHashMapCanBeSerialized() {
224: final Map map = new HashMap();
225: map.put(Boolean.TRUE, "JUnit");
226: final Collection set = map.entrySet();
227:
228: assertBothWays(set, "<java.util.HashMap_-EntrySet>\n"
229: + " <outer-class>\n" + " <entry>\n"
230: + " <boolean>true</boolean>\n"
231: + " <string>JUnit</string>\n" + " </entry>\n"
232: + " </outer-class>\n"
233: + "</java.util.HashMap_-EntrySet>");
234: }
235: }
|