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 03. October 2003 by Joe Walnes
011: */
012: package com.thoughtworks.acceptance;
013:
014: import com.thoughtworks.acceptance.objects.StandardObject;
015:
016: public class ArraysTest extends AbstractAcceptanceTest {
017:
018: public void testStringArray() {
019: String[] array = new String[] { "a", "b", "c" };
020:
021: String expected = "" + "<string-array>\n"
022: + " <string>a</string>\n" + " <string>b</string>\n"
023: + " <string>c</string>\n" + "</string-array>";
024:
025: assertBothWays(array, expected);
026: }
027:
028: public void testPrimitiveArray() {
029: int[] array = new int[] { 1, 2 };
030:
031: String expected = "" + "<int-array>\n" + " <int>1</int>\n"
032: + " <int>2</int>\n" + "</int-array>";
033:
034: assertBothWays(array, expected);
035: }
036:
037: public void testBoxedTypeArray() {
038: Integer[] array = new Integer[] { new Integer(1),
039: new Integer(2) };
040:
041: String expected = "" + "<java.lang.Integer-array>\n"
042: + " <int>1</int>\n" + " <int>2</int>\n"
043: + "</java.lang.Integer-array>";
044:
045: assertBothWays(array, expected);
046: }
047:
048: public static class X extends StandardObject {
049: String s = "hi";
050: }
051:
052: public void testCustomObjectArray() {
053:
054: X[] array = new X[] { new X(), new X() };
055:
056: String expected = ""
057: + "<com.thoughtworks.acceptance.ArraysTest_-X-array>\n"
058: + " <com.thoughtworks.acceptance.ArraysTest_-X>\n"
059: + " <s>hi</s>\n"
060: + " </com.thoughtworks.acceptance.ArraysTest_-X>\n"
061: + " <com.thoughtworks.acceptance.ArraysTest_-X>\n"
062: + " <s>hi</s>\n"
063: + " </com.thoughtworks.acceptance.ArraysTest_-X>\n"
064: + "</com.thoughtworks.acceptance.ArraysTest_-X-array>";
065:
066: assertBothWays(array, expected);
067: }
068:
069: public void testArrayOfMixedTypes() {
070:
071: Object[] array = new Number[] { new Long(2), new Integer(3) };
072:
073: String expected = "" + "<number-array>\n"
074: + " <long>2</long>\n" + " <int>3</int>\n"
075: + "</number-array>";
076:
077: assertBothWays(array, expected);
078:
079: }
080:
081: public void testEmptyArray() {
082: int[] array = new int[] {};
083:
084: String expected = "<int-array/>";
085:
086: assertBothWays(array, expected);
087:
088: }
089:
090: public void testUninitializedArray() {
091: String[] array = new String[4];
092: array[0] = "zero";
093: array[2] = "two";
094:
095: String expected = "" + "<string-array>\n"
096: + " <string>zero</string>\n" + " <null/>\n"
097: + " <string>two</string>\n" + " <null/>\n"
098: + "</string-array>";
099:
100: assertBothWays(array, expected);
101:
102: }
103:
104: public void testArrayInCustomObject() {
105: ObjWithArray objWithArray = new ObjWithArray();
106: objWithArray.strings = new String[] { "hi", "bye" };
107: xstream.alias("owa", ObjWithArray.class);
108: String expected = "" + "<owa>\n" + " <strings>\n"
109: + " <string>hi</string>\n"
110: + " <string>bye</string>\n" + " </strings>\n"
111: + "</owa>";
112: assertBothWays(objWithArray, expected);
113: }
114:
115: public void testNullArrayInCustomObject() {
116: ObjWithArray objWithArray = new ObjWithArray();
117: xstream.alias("owa", ObjWithArray.class);
118: String expected = "<owa/>";
119: assertBothWays(objWithArray, expected);
120: }
121:
122: public static class ObjWithArray extends StandardObject {
123: String[] strings;
124: }
125:
126: public void testDeserializingObjectWhichContainsAPrimitiveLongArray() {
127: String xml = "<owla>" + " <bits class=\"long-array\">"
128: + " <long>0</long>" + " <long>1</long>"
129: + " <long>2</long>" + " </bits>" + "</owla>";
130:
131: xstream.alias("owla", ObjectWithLongArray.class);
132:
133: ObjectWithLongArray o = (ObjectWithLongArray) xstream
134: .fromXML(xml);
135:
136: assertEquals(o.bits[0], 0);
137: assertEquals(o.bits[1], 1);
138: assertEquals(o.bits[2], 2);
139: }
140:
141: public static class ObjectWithLongArray {
142: long[] bits;
143: }
144:
145: public void testMultidimensionalArray() {
146: int[][] array = new int[3][2];
147: array[0][0] = 2;
148: array[0][1] = 4;
149: array[1][0] = 8;
150: array[1][1] = 16;
151: array[2] = new int[3];
152: array[2][0] = 33;
153: array[2][1] = 66;
154: array[2][2] = 99;
155:
156: String expectedXml = "" + "<int-array-array>\n"
157: + " <int-array>\n" + " <int>2</int>\n"
158: + " <int>4</int>\n" + " </int-array>\n"
159: + " <int-array>\n" + " <int>8</int>\n"
160: + " <int>16</int>\n" + " </int-array>\n"
161: + " <int-array>\n" + " <int>33</int>\n"
162: + " <int>66</int>\n" + " <int>99</int>\n"
163: + " </int-array>\n" + "</int-array-array>";
164:
165: String actualXml = xstream.toXML(array);
166: assertEquals(expectedXml, actualXml);
167:
168: int[][] result = (int[][]) xstream.fromXML(actualXml);
169: assertEquals(2, result[0][0]);
170: assertEquals(4, result[0][1]);
171: assertEquals(8, result[1][0]);
172: assertEquals(16, result[1][1]);
173: assertEquals(99, result[2][2]);
174: assertEquals(3, result.length);
175: assertEquals(2, result[0].length);
176: assertEquals(2, result[1].length);
177: assertEquals(3, result[2].length);
178: }
179:
180: public static class Thing {
181: }
182:
183: public static class SpecialThing extends Thing {
184: }
185:
186: public void testMultidimensionalArrayOfMixedTypes() {
187: xstream.alias("thing", Thing.class);
188: xstream.alias("special-thing", SpecialThing.class);
189:
190: Object[][] array = new Object[2][2];
191: array[0][0] = new Object();
192: array[0][1] = "a string";
193: array[1] = new Thing[2];
194: array[1][0] = new Thing();
195: array[1][1] = new SpecialThing();
196: String expectedXml = "" + "<object-array-array>\n"
197: + " <object-array>\n" + " <object/>\n"
198: + " <string>a string</string>\n"
199: + " </object-array>\n" + " <thing-array>\n"
200: + " <thing/>\n" + " <special-thing/>\n"
201: + " </thing-array>\n" + "</object-array-array>";
202:
203: String actualXml = xstream.toXML(array);
204: assertEquals(expectedXml, actualXml);
205:
206: Object[][] result = (Object[][]) xstream.fromXML(actualXml);
207: assertEquals(Object.class, result[0][0].getClass());
208: assertEquals("a string", result[0][1]);
209: assertEquals(Thing.class, result[1][0].getClass());
210: assertEquals(SpecialThing.class, result[1][1].getClass());
211: }
212:
213: public static class NoOneLikesMe extends StandardObject {
214: private int name;
215:
216: public NoOneLikesMe(int name) {
217: this .name = name;
218: }
219: }
220:
221: public void testHandlesArrayClassesThatHaveNotYetBeenLoaded() {
222: // Catch weirdness in classloader.
223: // Resolved by using Class.forName(x, false, classLoader), instead of classLoader.loadClass(x);
224: String xml = ""
225: + "<com.thoughtworks.acceptance.ArraysTest_-NoOneLikesMe-array>\n"
226: + " <com.thoughtworks.acceptance.ArraysTest_-NoOneLikesMe>\n"
227: + " <name>99</name>\n"
228: + " </com.thoughtworks.acceptance.ArraysTest_-NoOneLikesMe>\n"
229: + "</com.thoughtworks.acceptance.ArraysTest_-NoOneLikesMe-array>";
230: NoOneLikesMe[] result = (NoOneLikesMe[]) xstream.fromXML(xml);
231: assertEquals(1, result.length);
232: assertEquals(99, result[0].name);
233: }
234:
235: }
|