001: /*
002: * Copyright (C) 2006, 2007 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 12. June 2006 by Joerg Schaible
010: */
011: package com.thoughtworks.acceptance;
012:
013: import java.io.IOException;
014: import java.io.ObjectInputStream;
015: import java.io.ObjectOutputStream;
016: import java.io.Serializable;
017: import java.io.StringReader;
018: import java.io.StringWriter;
019:
020: import com.thoughtworks.acceptance.AbstractAcceptanceTest;
021:
022: /**
023: * <p>
024: * A class {@link Serializable} {@link Parent} class implements
025: * <code>writeObject()</code> and holds a {@link Child} class that also
026: * implements <code>writeObject()</code>
027: * </p>
028: *
029: * @author <a href="mailto:cleclerc@pobox.com">Cyrille Le Clerc</a>
030: */
031: public class SerializationNestedWriteObjectsTest extends
032: AbstractAcceptanceTest {
033:
034: public static class Child implements Serializable {
035:
036: private int i = 3;
037:
038: public Child(int i) {
039: this .i = i;
040: }
041:
042: public int getI() {
043: return i;
044: }
045:
046: private void readObject(java.io.ObjectInputStream in)
047: throws IOException, ClassNotFoundException {
048: in.defaultReadObject();
049: }
050:
051: private void writeObject(ObjectOutputStream out)
052: throws IOException {
053: out.defaultWriteObject();
054: }
055: }
056:
057: public static class Parent implements Serializable {
058:
059: private String name;
060:
061: private transient Child child;
062:
063: public Parent(String name, Child child) {
064: this .name = name;
065: this .child = child;
066: }
067:
068: public Child getChild() {
069: return child;
070: }
071:
072: public String getName() {
073: return name;
074: }
075:
076: private void readObject(java.io.ObjectInputStream in)
077: throws IOException, ClassNotFoundException {
078: this .child = (Child) in.readObject();
079: in.defaultReadObject();
080: }
081:
082: private void writeObject(ObjectOutputStream out)
083: throws IOException {
084: out.writeObject(this .child);
085: out.defaultWriteObject();
086: }
087: }
088:
089: public void testObjectInputStream() throws Exception {
090: xstream.alias("parent", Parent.class);
091: xstream.alias("child", Child.class);
092:
093: String sourceXml = "" + "<object-stream>\n"
094: + " <parent serialization=\"custom\">\n"
095: + " <parent>\n"
096: + " <child serialization=\"custom\">\n"
097: + " <child>\n" + " <default>\n"
098: + " <i>1</i>\n" + " </default>\n"
099: + " </child>\n" + " </child>\n"
100: + " <default>\n"
101: + " <name>ze-name</name>\n"
102: + " </default>\n" + " </parent>\n"
103: + " </parent>\n" + "</object-stream>";
104:
105: ObjectInputStream objectInputStream = xstream
106: .createObjectInputStream(new StringReader(sourceXml));
107:
108: Parent parent = (Parent) objectInputStream.readObject();
109:
110: assertEquals("ze-name", parent.getName());
111: assertEquals(1, parent.getChild().getI());
112: }
113:
114: public void testObjectOutputStream() throws Exception {
115: xstream.alias("parent", Parent.class);
116: xstream.alias("child", Child.class);
117:
118: String expectedXml = "" + "<object-stream>\n"
119: + " <parent serialization=\"custom\">\n"
120: + " <parent>\n"
121: + " <child serialization=\"custom\">\n"
122: + " <child>\n" + " <default>\n"
123: + " <i>1</i>\n" + " </default>\n"
124: + " </child>\n" + " </child>\n"
125: + " <default>\n"
126: + " <name>ze-name</name>\n"
127: + " </default>\n" + " </parent>\n"
128: + " </parent>\n" + "</object-stream>";
129:
130: Parent parent = new Parent("ze-name", new Child(1));
131: StringWriter stringWriter = new StringWriter();
132: ObjectOutputStream os = xstream
133: .createObjectOutputStream(stringWriter);
134: os.writeObject(parent);
135: os.close();
136: String actualXml = stringWriter.getBuffer().toString();
137: assertEquals(expectedXml, actualXml);
138: }
139:
140: public void testToXML() {
141:
142: xstream.alias("parent", Parent.class);
143: xstream.alias("child", Child.class);
144:
145: String expected = "" + "<parent serialization=\"custom\">\n"
146: + " <parent>\n"
147: + " <child serialization=\"custom\">\n"
148: + " <child>\n" + " <default>\n"
149: + " <i>1</i>\n" + " </default>\n"
150: + " </child>\n" + " </child>\n"
151: + " <default>\n" + " <name>ze-name</name>\n"
152: + " </default>\n" + " </parent>\n" + "</parent>";
153:
154: Parent parent = new Parent("ze-name", new Child(1));
155:
156: assertBothWays(parent, expected);
157: }
158:
159: public static class RawString implements Serializable {
160:
161: private String s;
162:
163: public RawString(String s) {
164: this .s = s;
165: }
166:
167: public String getS() {
168: return s;
169: }
170:
171: private void readObject(java.io.ObjectInputStream in)
172: throws IOException {
173: int i = in.read();
174: byte[] b = new byte[i];
175: in.read(b);
176: s = new String(b);
177: }
178:
179: private void writeObject(ObjectOutputStream out)
180: throws IOException {
181: byte[] b = s.getBytes();
182: out.write(b.length);
183: out.write(b);
184: }
185: }
186:
187: public void testCanHandleRawBytes() throws IOException,
188: ClassNotFoundException {
189: xstream.alias("raw", RawString.class);
190:
191: String expectedXml = "" + "<object-stream>\n"
192: + " <raw serialization=\"custom\">\n" + " <raw>\n"
193: + " <byte>7</byte>\n"
194: + " <byte-array>WFN0cmVhbQ==</byte-array>\n"
195: + " </raw>\n" + " </raw>\n" + "</object-stream>";
196:
197: StringWriter stringWriter = new StringWriter();
198: ObjectOutputStream os = xstream
199: .createObjectOutputStream(stringWriter);
200: os.writeObject(new RawString("XStream"));
201: os.close();
202: String actualXml = stringWriter.getBuffer().toString();
203: assertEquals(expectedXml, actualXml);
204:
205: ObjectInputStream objectInputStream = xstream
206: .createObjectInputStream(new StringReader(actualXml));
207:
208: RawString rawString = (RawString) objectInputStream
209: .readObject();
210: assertEquals("XStream", rawString.getS());
211: }
212: }
|