1 package com.thoughtworks.acceptance; 2 3 import com.thoughtworks.acceptance.objects.Hardware; 4 import com.thoughtworks.acceptance.objects.SampleLists; 5 import com.thoughtworks.acceptance.objects.Software; 6 import com.thoughtworks.xstream.XStream; 7 8 import java.util.*; 9 10 public class CollectionsTest extends AbstractAcceptanceTest { 11 12 public void testListsCanContainCustomObjects() { 13 SampleLists lists = new SampleLists(); 14 lists.good.add(new Software("apache", "geronimo")); 15 lists.good.add(new Software("caucho", "resin")); 16 lists.good.add(new Hardware("risc", "strong-arm")); 17 lists.bad.add(new Software("apache", "jserv")); 18 19 xstream.alias("lists", SampleLists.class); 20 xstream.alias("software", Software.class); 21 xstream.alias("hardware", Hardware.class); 22 23 String expected = "" + 24 "<lists>\n" + 25 " <bad class=\"list\">\n" + 26 " <software>\n" + 27 " <name>jserv</name>\n" + 28 " <vendor>apache</vendor>\n" + 29 " </software>\n" + 30 " </bad>\n" + 31 " <good>\n" + 32 " <software>\n" + 33 " <name>geronimo</name>\n" + 34 " <vendor>apache</vendor>\n" + 35 " </software>\n" + 36 " <software>\n" + 37 " <name>resin</name>\n" + 38 " <vendor>caucho</vendor>\n" + 39 " </software>\n" + 40 " <hardware>\n" + 41 " <arch>risc</arch>\n" + 42 " <name>strong-arm</name>\n" + 43 " </hardware>\n" + 44 " </good>\n" + 45 "</lists>"; 46 47 assertBothWays(lists, expected); 48 } 49 50 public void testListsCanContainBasicObjects() { 51 SampleLists lists = new SampleLists(); 52 lists.good.add("hello"); 53 lists.good.add(new Integer (3)); 54 lists.good.add(Boolean.TRUE); 55 56 xstream.alias("lists", SampleLists.class); 57 58 String expected = "" + 59 "<lists>\n" + 60 " <bad class=\"list\"/>\n" + 61 " <good>\n" + 62 " <string>hello</string>\n" + 63 " <int>3</int>\n" + 64 " <boolean>true</boolean>\n" + 65 " </good>\n" + 66 "</lists>"; 67 68 assertBothWays(lists, expected); 69 } 70 71 public void testListCanBeRootObject() { 72 Collection list = new ArrayList(); 73 list.add("hi"); 74 list.add("bye"); 75 76 String expected = "" + 77 "<list>\n" + 78 " <string>hi</string>\n" + 79 " <string>bye</string>\n" + 80 "</list>"; 81 82 assertBothWays(list, expected); 83 } 84 85 public void testSetCanBeRootObject() { 86 Collection set = new HashSet(); 87 set.add("hi"); 88 set.add("bye"); 89 90 String expected = "" + 91 "<set>\n" + 92 " <string>hi</string>\n" + 93 " <string>bye</string>\n" + 94 "</set>"; 95 96 assertBothWays(set, expected); 97 } 98 99 public void testVector() { 100 Vector vector = new Vector(); 101 vector.addElement("a"); 102 vector.addElement("b"); 103 104 assertBothWays(vector, 105 "<vector>\n" + 106 " <string>a</string>\n" + 107 " <string>b</string>\n" + 108 "</vector>"); 109 } 110 111 public void testSyncronizedWrapper() { 112 xstream.setMode(XStream.XPATH_REFERENCES); 114 115 List list = Collections.synchronizedList(new LinkedList()); 116 list.add("hi"); 117 118 assertBothWays(list, 119 "<java.util.Collections-SynchronizedList>\n" + 120 " <c class=\"linked-list\">\n" + 121 " <string>hi</string>\n" + 122 " </c>\n" + 123 " <list class=\"linked-list\" reference=\"../c\"/>\n" + 124 " <mutex class=\"java.util.Collections-SynchronizedList\" reference=\"..\"/>\n" + 125 "</java.util.Collections-SynchronizedList>"); 126 } 127 128 public void testEmptyList() { 129 assertBothWays(Collections.EMPTY_LIST, "<java.util.Collections-EmptyList/>"); 130 } 131 132 public void testUnmodifiableList() { 133 xstream.setMode(XStream.XPATH_REFERENCES); 135 136 List list = new ArrayList(); 137 list.add("hi"); 138 list = Collections.unmodifiableList(list); 139 140 assertBothWays(list, 141 "<java.util.Collections-UnmodifiableRandomAccessList>\n" + 142 " <c class=\"list\">\n" + 143 " <string>hi</string>\n" + 144 " </c>\n" + 145 " <list reference=\"../c\"/>\n" + 146 "</java.util.Collections-UnmodifiableRandomAccessList>"); 147 } 148 149 public void testLinkedHashSetRetainsOrdering() { 150 Set set = new LinkedHashSet(); 151 set.add("Z"); 152 set.add("C"); 153 set.add("X"); 154 155 LinkedHashSet result = (LinkedHashSet) assertBothWays(set, 156 "<linked-hash-set>\n" + 157 " <string>Z</string>\n" + 158 " <string>C</string>\n" + 159 " <string>X</string>\n" + 160 "</linked-hash-set>"); 161 162 Object [] values = result.toArray(); 163 assertEquals("Z", values[0]); 164 assertEquals("C", values[1]); 165 assertEquals("X", values[2]); 166 } 167 } 168 | Popular Tags |