001: /*
002: * Copyright (C) 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 29. July 2007 by Joerg Schaible
010: */
011: package com.thoughtworks.xstream.converters.reflection;
012:
013: import com.thoughtworks.acceptance.objects.StandardObject;
014: import com.thoughtworks.xstream.XStream;
015:
016: import junit.framework.TestCase;
017:
018: import java.io.IOException;
019: import java.io.ObjectInputStream;
020: import java.io.ObjectOutputStream;
021: import java.io.ObjectStreamField;
022: import java.io.Serializable;
023:
024: /**
025: * @author Jörg Schaible
026: */
027: public class SerializableConverterTest extends TestCase {
028:
029: static class SimpleType extends StandardObject {
030: private String one;
031: private String two;
032:
033: public String getOne() {
034: return this .one;
035: }
036:
037: public void setOne(String one) {
038: this .one = one;
039: }
040:
041: public String getTwo() {
042: return this .two;
043: }
044:
045: public void setTwo(String two) {
046: this .two = two;
047: }
048:
049: private void writeObject(final ObjectOutputStream out)
050: throws IOException {
051: out.defaultWriteObject();
052: }
053:
054: private void readObject(final ObjectInputStream in)
055: throws IOException, ClassNotFoundException {
056: in.defaultReadObject();
057: }
058: }
059:
060: public void testCanOmitFieldAtSerialization() {
061: XStream xstream = new XStream();
062: xstream.alias("simple", SimpleType.class);
063: xstream.omitField(SimpleType.class, "two");
064:
065: String expected = "" + "<simple serialization=\"custom\">\n"
066: + " <simple>\n" + " <default>\n"
067: + " <one>one</one>\n" + " </default>\n"
068: + " </simple>\n" + "</simple>";
069:
070: SimpleType simple = new SimpleType();
071: simple.setOne("one");
072: simple.setTwo("two");
073:
074: String xml = xstream.toXML(simple);
075: assertEquals(expected, xml);
076: }
077:
078: public void testCanOmitFieldAtDeserialization() {
079: XStream xstream = new XStream();
080: xstream.alias("simple", SimpleType.class);
081: xstream.omitField(SimpleType.class, "two");
082: xstream.omitField(SimpleType.class, "x");
083:
084: String xml = "" + "<simple serialization=\"custom\">\n"
085: + " <simple>\n" + " <default>\n"
086: + " <one>one</one>\n" + " <x>x</x>\n"
087: + " </default>\n" + " </simple>\n" + "</simple>";
088:
089: SimpleType simple = new SimpleType();
090: simple.setOne("one");
091:
092: SimpleType serialized = (SimpleType) xstream.fromXML(xml);
093: assertEquals(simple, serialized);
094: }
095:
096: static class ExtendedType extends SimpleType {
097: private String three;
098:
099: public String getThree() {
100: return this .three;
101: }
102:
103: public void setThree(String three) {
104: this .three = three;
105: }
106:
107: private void writeObject(final ObjectOutputStream out)
108: throws IOException {
109: out.defaultWriteObject();
110: }
111:
112: private void readObject(final ObjectInputStream in)
113: throws IOException, ClassNotFoundException {
114: in.defaultReadObject();
115: }
116: }
117:
118: public void testCanOmitInheritedFieldAtSerialization() {
119: XStream xstream = new XStream();
120: xstream.alias("extended", ExtendedType.class);
121: xstream.alias("simple", SimpleType.class);
122: xstream.omitField(SimpleType.class, "two");
123:
124: String expected = "" + "<extended serialization=\"custom\">\n"
125: + " <simple>\n" + " <default>\n"
126: + " <one>one</one>\n" + " </default>\n"
127: + " </simple>\n" + " <extended>\n"
128: + " <default>\n" + " <three>three</three>\n"
129: + " </default>\n" + " </extended>\n"
130: + "</extended>";
131:
132: ExtendedType extended = new ExtendedType();
133: extended.setOne("one");
134: extended.setTwo("two");
135: extended.setThree("three");
136:
137: String xml = xstream.toXML(extended);
138: assertEquals(expected, xml);
139: }
140:
141: public void testCanOmitInheritedFieldAtDeserialization() {
142: XStream xstream = new XStream();
143: xstream.alias("extended", ExtendedType.class);
144: xstream.alias("simple", SimpleType.class);
145: xstream.omitField(SimpleType.class, "two");
146: xstream.omitField(SimpleType.class, "x");
147:
148: String xml = "" + "<extended serialization=\"custom\">\n"
149: + " <simple>\n" + " <default>\n"
150: + " <one>one</one>\n" + " <x>x</x>\n"
151: + " </default>\n" + " </simple>\n"
152: + " <extended>\n" + " <default>\n"
153: + " <three>three</three>\n" + " </default>\n"
154: + " </extended>\n" + "</extended>";
155:
156: ExtendedType extended = new ExtendedType();
157: extended.setOne("one");
158: extended.setThree("three");
159:
160: SimpleType serialized = (SimpleType) xstream.fromXML(xml);
161: assertEquals(extended, serialized);
162: }
163:
164: public static class SimpleNamedFieldsType extends StandardObject
165: implements Serializable {
166:
167: private String one;
168: private String two;
169:
170: public String getOne() {
171: return this .one;
172: }
173:
174: public void setOne(String one) {
175: this .one = one;
176: }
177:
178: public String getTwo() {
179: return this .two;
180: }
181:
182: public void setTwo(String two) {
183: this .two = two;
184: }
185:
186: private static final ObjectStreamField[] serialPersistentFields = {
187: new ObjectStreamField("s1", String.class),
188: new ObjectStreamField("s2", String.class), };
189:
190: private void writeObject(ObjectOutputStream out)
191: throws IOException {
192: // don't call defaultWriteObject()
193: ObjectOutputStream.PutField fields = out.putFields();
194: fields.put("s1", one);
195: fields.put("s2", two);
196: out.writeFields();
197: }
198:
199: private void readObject(ObjectInputStream in)
200: throws IOException, ClassNotFoundException {
201: // don't call defaultReadObject()
202: ObjectInputStream.GetField fields = in.readFields();
203: one = (String) fields.get("s1", "1");
204: two = (String) fields.get("s2", "2");
205: }
206: }
207:
208: public void testCanOmitNamedFieldAtSerialization() {
209: XStream xstream = new XStream();
210: xstream.alias("simple", SimpleNamedFieldsType.class);
211: xstream.omitField(SimpleNamedFieldsType.class, "s2");
212:
213: String expected = "" + "<simple serialization=\"custom\">\n"
214: + " <simple>\n" + " <default>\n"
215: + " <s1>one</s1>\n" + " </default>\n"
216: + " </simple>\n" + "</simple>";
217:
218: SimpleNamedFieldsType simple = new SimpleNamedFieldsType();
219: simple.setOne("one");
220: simple.setTwo("two");
221:
222: String xml = xstream.toXML(simple);
223: assertEquals(expected, xml);
224: }
225:
226: public void testCanOmitNamedFieldAtDeserialization() {
227: XStream xstream = new XStream();
228: xstream.alias("simple", SimpleNamedFieldsType.class);
229: xstream.omitField(SimpleNamedFieldsType.class, "s2");
230: xstream.omitField(SimpleNamedFieldsType.class, "x");
231:
232: String xml = "" + "<simple serialization=\"custom\">\n"
233: + " <simple>\n" + " <default>\n"
234: + " <s1>one</s1>\n" + " <x>x</x>\n"
235: + " </default>\n" + " </simple>\n" + "</simple>";
236:
237: SimpleNamedFieldsType simple = new SimpleNamedFieldsType();
238: simple.setOne("one");
239: simple.setTwo("2");
240:
241: SimpleNamedFieldsType serialized = (SimpleNamedFieldsType) xstream
242: .fromXML(xml);
243: assertEquals(simple, serialized);
244: }
245:
246: public void testCanAliasField() {
247: XStream xstream = new XStream();
248: xstream.alias("simple", SimpleType.class);
249: xstream.aliasField("s2", SimpleType.class, "two");
250:
251: String expected = "" + "<simple serialization=\"custom\">\n"
252: + " <simple>\n" + " <default>\n"
253: + " <one>one</one>\n" + " <s2>two</s2>\n"
254: + " </default>\n" + " </simple>\n" + "</simple>";
255:
256: SimpleType simple = new SimpleType();
257: simple.setOne("one");
258: simple.setTwo("two");
259:
260: String xml = xstream.toXML(simple);
261: assertEquals(expected, xml);
262: SimpleType serialized = (SimpleType) xstream.fromXML(xml);
263: assertEquals(simple, serialized);
264: }
265:
266: public void testCanAliasNamedField() {
267: XStream xstream = new XStream();
268: xstream.alias("simple", SimpleNamedFieldsType.class);
269: xstream.aliasField("two", SimpleNamedFieldsType.class, "s2");
270:
271: String expected = "" + "<simple serialization=\"custom\">\n"
272: + " <simple>\n" + " <default>\n"
273: + " <s1>one</s1>\n" + " <two>two</two>\n"
274: + " </default>\n" + " </simple>\n" + "</simple>";
275:
276: SimpleNamedFieldsType simple = new SimpleNamedFieldsType();
277: simple.setOne("one");
278: simple.setTwo("two");
279:
280: String xml = xstream.toXML(simple);
281: assertEquals(expected, xml);
282: SimpleNamedFieldsType serialized = (SimpleNamedFieldsType) xstream
283: .fromXML(xml);
284: assertEquals(simple, serialized);
285: }
286: }
|