/* * Copyright (C) 2003, 2004, 2005, 2006 Joe Walnes. * Copyright (C) 2006, 2007 XStream Committers. * All rights reserved. * * The software in this package is published under the terms of the BSD * style license a copy of which has been included with this distribution in * the LICENSE.txt file. * * Created on 01. October 2003 by Joe Walnes */ package com.thoughtworks.acceptance; import com.thoughtworks.acceptance.objects.Hardware; import com.thoughtworks.acceptance.objects.SampleLists; import com.thoughtworks.acceptance.objects.Software; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.core.JVM; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.Vector; public class CollectionsTest extends AbstractAcceptanceTest { public void testListsCanContainCustomObjects() { SampleLists lists = new SampleLists(); lists.good.add(new Software("apache", "geronimo")); lists.good.add(new Software("caucho", "resin")); lists.good.add(new Hardware("risc", "strong-arm")); lists.bad.add(new Software("apache", "jserv")); xstream.alias("lists", SampleLists.class); xstream.alias("software", Software.class); xstream.alias("hardware", Hardware.class); String expected = "" + "\n" + " \n" + " \n" + " apache\n" + " geronimo\n" + " \n" + " \n" + " caucho\n" + " resin\n" + " \n" + " \n" + " risc\n" + " strong-arm\n" + " \n" + " \n" + " \n" + " \n" + " apache\n" + " jserv\n" + " \n" + " \n" + ""; assertBothWays(lists, expected); } public void testListsCanContainBasicObjects() { SampleLists lists = new SampleLists(); lists.good.add("hello"); lists.good.add(new Integer(3)); lists.good.add(Boolean.TRUE); xstream.alias("lists", SampleLists.class); String expected = "" + "\n" + " \n" + " hello\n" + " 3\n" + " true\n" + " \n" + " \n" + ""; assertBothWays(lists, expected); } public void testListCanBeRootObject() { Collection list = new ArrayList(); list.add("hi"); list.add("bye"); String expected = "" + "\n" + " hi\n" + " bye\n" + ""; assertBothWays(list, expected); } public void testSetCanBeRootObject() { Collection set = new HashSet(); set.add("hi"); set.add("bye"); String expected = "" + "\n" + " hi\n" + " bye\n" + ""; assertBothWaysNormalized(set, expected, "set", "string", null); } public void testVector() { Vector vector = new Vector(); vector.addElement("a"); vector.addElement("b"); assertBothWays(vector, "\n" + " a\n" + " b\n" + ""); } public void testSyncronizedList() { final String xml; if (JVM.is15()) { xml = "\n" + " \n" + " \n" + " \n" + " hi\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + ""; } else { xml = "\n" + " \n" + " hi\n" + " \n" + " \n" + " \n" + ""; } // syncronized list has circular reference xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES); List list = Collections.synchronizedList(new LinkedList()); list.add("hi"); assertBothWays(list, xml); } public void testEmptyList() { assertBothWays(Collections.EMPTY_LIST, ""); } public void testUnmodifiableList() { // unodifiable list has duplicate refs xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES); List list = new ArrayList(); list.add("hi"); list = Collections.unmodifiableList(list); assertBothWays(list, "\n" + " \n" + " hi\n" + " \n" + " \n" + ""); } public void testLinkedHashSetRetainsOrdering() { Set set = new LinkedHashSet(); set.add("Z"); set.add("C"); set.add("X"); LinkedHashSet result = (LinkedHashSet) assertBothWays(set, "\n" + " Z\n" + " C\n" + " X\n" + ""); Object[] values = result.toArray(); assertEquals("Z", values[0]); assertEquals("C", values[1]); assertEquals("X", values[2]); } public void testListFromArrayAsList() { List list = Arrays.asList(new String[] {"hi", "bye"}); assertBothWays(list, "\n" + " \n" + " hi\n" + " bye\n" + " \n" + ""); } public void testKeySetOfHashMapCanBeSerialized() { final Map map = new HashMap(); map.put("JUnit", null); final Collection set = map.keySet(); assertBothWays(set, "\n" + " \n" + " \n" + " JUnit\n" + " \n" + " \n" + " \n" + ""); } public void testValueSetOfHashMapCanBeSerialized() { final Map map = new HashMap(); map.put(Boolean.TRUE, "JUnit"); final Collection set = map.values(); assertBothWays(set, "\n" + " \n" + " \n" + " true\n" + " JUnit\n" + " \n" + " \n" + ""); } public void testEntrySetOfHashMapCanBeSerialized() { final Map map = new HashMap(); map.put(Boolean.TRUE, "JUnit"); final Collection set = map.entrySet(); assertBothWays(set, "\n" + " \n" + " \n" + " true\n" + " JUnit\n" + " \n" + " \n" + ""); } }