/*
* 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 03. October 2003 by Joe Walnes
*/
package com.thoughtworks.acceptance;
import com.thoughtworks.acceptance.objects.StandardObject;
public class ArraysTest extends AbstractAcceptanceTest {
public void testStringArray() {
String[] array = new String[]{"a", "b", "c"};
String expected = "" +
"\n" +
" a\n" +
" b\n" +
" c\n" +
"";
assertBothWays(array, expected);
}
public void testPrimitiveArray() {
int[] array = new int[]{1, 2};
String expected = "" +
"\n" +
" 1\n" +
" 2\n" +
"";
assertBothWays(array, expected);
}
public void testBoxedTypeArray() {
Integer[] array = new Integer[]{new Integer(1), new Integer(2)};
String expected = "" +
"\n" +
" 1\n" +
" 2\n" +
"";
assertBothWays(array, expected);
}
public static class X extends StandardObject {
String s = "hi";
}
public void testCustomObjectArray() {
X[] array = new X[]{new X(), new X()};
String expected = "" +
"\n" +
" \n" +
" hi\n" +
" \n" +
" \n" +
" hi\n" +
" \n" +
"";
assertBothWays(array, expected);
}
public void testArrayOfMixedTypes() {
Object[] array = new Number[]{new Long(2), new Integer(3)};
String expected = "" +
"\n" +
" 2\n" +
" 3\n" +
"";
assertBothWays(array, expected);
}
public void testEmptyArray() {
int[] array = new int[]{};
String expected = "";
assertBothWays(array, expected);
}
public void testUninitializedArray() {
String[] array = new String[4];
array[0] = "zero";
array[2] = "two";
String expected = "" +
"\n" +
" zero\n" +
" \n" +
" two\n" +
" \n" +
"";
assertBothWays(array, expected);
}
public void testArrayInCustomObject() {
ObjWithArray objWithArray = new ObjWithArray();
objWithArray.strings = new String[]{"hi", "bye"};
xstream.alias("owa", ObjWithArray.class);
String expected = "" +
"\n" +
" \n" +
" hi\n" +
" bye\n" +
" \n" +
"";
assertBothWays(objWithArray, expected);
}
public void testNullArrayInCustomObject() {
ObjWithArray objWithArray = new ObjWithArray();
xstream.alias("owa", ObjWithArray.class);
String expected = "";
assertBothWays(objWithArray, expected);
}
public static class ObjWithArray extends StandardObject {
String[] strings;
}
public void testDeserializingObjectWhichContainsAPrimitiveLongArray() {
String xml =
"" +
" " +
" 0" +
" 1" +
" 2" +
" " +
"";
xstream.alias("owla", ObjectWithLongArray.class);
ObjectWithLongArray o = (ObjectWithLongArray) xstream.fromXML(xml);
assertEquals(o.bits[0], 0);
assertEquals(o.bits[1], 1);
assertEquals(o.bits[2], 2);
}
public static class ObjectWithLongArray {
long[] bits;
}
public void testMultidimensionalArray() {
int[][] array = new int[3][2];
array[0][0] = 2;
array[0][1] = 4;
array[1][0] = 8;
array[1][1] = 16;
array[2] = new int[3];
array[2][0] = 33;
array[2][1] = 66;
array[2][2] = 99;
String expectedXml = "" +
"\n" +
" \n" +
" 2\n" +
" 4\n" +
" \n" +
" \n" +
" 8\n" +
" 16\n" +
" \n" +
" \n" +
" 33\n" +
" 66\n" +
" 99\n" +
" \n" +
"";
String actualXml = xstream.toXML(array);
assertEquals(expectedXml, actualXml);
int[][] result = (int[][]) xstream.fromXML(actualXml);
assertEquals(2, result[0][0]);
assertEquals(4, result[0][1]);
assertEquals(8, result[1][0]);
assertEquals(16, result[1][1]);
assertEquals(99, result[2][2]);
assertEquals(3, result.length);
assertEquals(2, result[0].length);
assertEquals(2, result[1].length);
assertEquals(3, result[2].length);
}
public static class Thing {
}
public static class SpecialThing extends Thing {
}
public void testMultidimensionalArrayOfMixedTypes() {
xstream.alias("thing", Thing.class);
xstream.alias("special-thing", SpecialThing.class);
Object[][] array = new Object[2][2];
array[0][0] = new Object();
array[0][1] = "a string";
array[1] = new Thing[2];
array[1][0] = new Thing();
array[1][1] = new SpecialThing();
String expectedXml = "" +
"\n" +
" \n" +
" \n" +
" a string\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
"";
String actualXml = xstream.toXML(array);
assertEquals(expectedXml, actualXml);
Object[][] result = (Object[][]) xstream.fromXML(actualXml);
assertEquals(Object.class, result[0][0].getClass());
assertEquals("a string", result[0][1]);
assertEquals(Thing.class, result[1][0].getClass());
assertEquals(SpecialThing.class, result[1][1].getClass());
}
public static class NoOneLikesMe extends StandardObject {
private int name;
public NoOneLikesMe(int name) {
this.name = name;
}
}
public void testHandlesArrayClassesThatHaveNotYetBeenLoaded() {
// Catch weirdness in classloader.
// Resolved by using Class.forName(x, false, classLoader), instead of classLoader.loadClass(x);
String xml = ""
+ "\n"
+ " \n"
+ " 99\n"
+ " \n"
+ "";
NoOneLikesMe[] result = (NoOneLikesMe[]) xstream.fromXML(xml);
assertEquals(1, result.length);
assertEquals(99, result[0].name);
}
}