01: /*
02: * Copyright (C) 2004, 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 06. March 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.converters.extended;
13:
14: import com.thoughtworks.acceptance.AbstractAcceptanceTest;
15: import com.thoughtworks.acceptance.objects.StandardObject;
16:
17: public class EncodedByteArrayConverterTest extends
18: AbstractAcceptanceTest {
19:
20: public void testMarshallsCharArrayAsSingleString() {
21: byte[] input = { 0, 120, -124, 22, 33, 0, 5 };
22: String expected = "<byte-array>AHiEFiEABQ==</byte-array>";
23:
24: assertBothWays(input, expected);
25: }
26:
27: public void testUnmarshallsOldByteArraysThatHaveNotBeenEncoding() {
28: // for backwards compatability
29: String input = "" + "<byte-array>\n" + " <byte>0</byte>\n"
30: + " <byte>120</byte>\n" + " <byte>-124</byte>\n"
31: + " <byte>22</byte>\n" + " <byte>33</byte>\n"
32: + " <byte>0</byte>\n" + " <byte>5</byte>\n"
33: + "</byte-array>";
34:
35: byte[] expected = { 0, 120, -124, 22, 33, 0, 5 };
36: assertByteArrayEquals(expected, (byte[]) xstream.fromXML(input));
37: }
38:
39: public void testUnmarshallsEmptyByteArrays() {
40: byte[] input = {};
41: String expected = "<byte-array></byte-array>";
42:
43: assertBothWays(input, expected);
44: }
45:
46: public static class TestObject extends StandardObject {
47: private byte[] data;
48: private boolean something;
49: }
50:
51: public void testUnmarshallsEmptyByteArrayAsFieldOfAnotherObject() {
52: // exposes a weird bug that was in the XML pull readers.
53: TestObject in = new TestObject();
54: in.data = new byte[0];
55:
56: xstream.alias("TestObject", TestObject.class);
57: String expectedXml = "" + "<TestObject>\n"
58: + " <data></data>\n"
59: + " <something>false</something>\n" + "</TestObject>";
60: assertBothWays(in, expectedXml);
61: }
62:
63: }
|