1 package com.thoughtworks.acceptance; 2 3 public class ArraysTest extends AbstractAcceptanceTest { 4 5 public void testStringArray() { 6 String [] array = new String []{"a", "b", "c"}; 7 8 String expected = "" + 9 "<string-array>\n" + 10 " <string>a</string>\n" + 11 " <string>b</string>\n" + 12 " <string>c</string>\n" + 13 "</string-array>"; 14 15 assertBothWays(array, expected); 16 } 17 18 public void testPrimitiveArray() { 19 int[] array = new int[]{1, 2}; 20 21 String expected = "" + 22 "<int-array>\n" + 23 " <int>1</int>\n" + 24 " <int>2</int>\n" + 25 "</int-array>"; 26 27 assertBothWays(array, expected); 28 } 29 30 public static class X extends StandardObject { 31 String s = "hi"; 32 } 33 34 public void testCustomObjectArray() { 35 36 X[] array = new X[]{new X(), new X()}; 37 38 String expected = "" + 39 "<com.thoughtworks.acceptance.ArraysTest-X-array>\n" + 40 " <com.thoughtworks.acceptance.ArraysTest-X>\n" + 41 " <s>hi</s>\n" + 42 " </com.thoughtworks.acceptance.ArraysTest-X>\n" + 43 " <com.thoughtworks.acceptance.ArraysTest-X>\n" + 44 " <s>hi</s>\n" + 45 " </com.thoughtworks.acceptance.ArraysTest-X>\n" + 46 "</com.thoughtworks.acceptance.ArraysTest-X-array>"; 47 48 assertBothWays(array, expected); 49 } 50 51 public void testArrayOfMixedTypes() { 52 53 Object [] array = new Number []{new Long (2), new Integer (3)}; 54 55 String expected = "" + 56 "<number-array>\n" + 57 " <long>2</long>\n" + 58 " <int>3</int>\n" + 59 "</number-array>"; 60 61 assertBothWays(array, expected); 62 63 } 64 65 public void testEmptyArray() { 66 int[] array = new int[]{}; 67 68 String expected = "<int-array/>"; 69 70 assertBothWays(array, expected); 71 72 } 73 74 public void testUninitializedArray() { 75 String [] array = new String [4]; 76 array[0] = "zero"; 77 array[2] = "two"; 78 79 String expected = "" + 80 "<string-array>\n" + 81 " <string>zero</string>\n" + 82 " <null/>\n" + 83 " <string>two</string>\n" + 84 " <null/>\n" + 85 "</string-array>"; 86 87 assertBothWays(array, expected); 88 89 } 90 91 public void testArrayInCustomObject() { 92 ObjWithArray objWithArray = new ObjWithArray(); 93 objWithArray.strings = new String []{"hi", "bye"}; 94 xstream.alias("owa", ObjWithArray.class); 95 String expected = "" + 96 "<owa>\n" + 97 " <strings>\n" + 98 " <string>hi</string>\n" + 99 " <string>bye</string>\n" + 100 " </strings>\n" + 101 "</owa>"; 102 assertBothWays(objWithArray, expected); 103 } 104 105 public static class ObjWithArray extends StandardObject { 106 String [] strings; 107 } 108 109 public void testDeserializingObjectWhichContainsAPrimitiveLongArray() { 110 String xml = 111 "<owla>" + 112 " <bits class=\"long-array\">" + 113 " <long>0</long>" + 114 " <long>1</long>" + 115 " <long>2</long>" + 116 " </bits>" + 117 "</owla>"; 118 119 xstream.alias("owla", ObjectWithLongArray.class); 120 121 ObjectWithLongArray o = (ObjectWithLongArray) xstream.fromXML(xml); 122 123 assertEquals(o.bits[0], 0); 124 assertEquals(o.bits[1], 1); 125 assertEquals(o.bits[2], 2); 126 } 127 128 class ObjectWithLongArray { 129 long[] bits; 130 } 131 } 132 | Popular Tags |