001: /*
002: * Copyright (C) 2004, 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 24. August 2004 by Joe Walnes
011: */
012: package com.thoughtworks.acceptance;
013:
014: import com.thoughtworks.acceptance.objects.StandardObject;
015:
016: import java.io.IOException;
017: import java.io.ObjectInputStream;
018: import java.io.Serializable;
019:
020: public class WriteReplaceTest extends AbstractAcceptanceTest {
021:
022: public static class Thing extends StandardObject implements
023: Serializable {
024:
025: int a;
026: int b;
027:
028: public Thing() {
029: }
030:
031: public Thing(int a, int b) {
032: this .a = a;
033: this .b = b;
034: }
035:
036: private Object writeReplace() {
037: return new Thing(a * 1000, b * 1000);
038: }
039:
040: private Object readResolve() {
041: return new Thing(a / 1000, b / 1000);
042: }
043:
044: }
045:
046: public void testReplacesAndResolves() {
047: xstream.alias("thing", Thing.class);
048:
049: Thing thing = new Thing(3, 6);
050:
051: String expectedXml = "" + "<thing>\n" + " <a>3000</a>\n"
052: + " <b>6000</b>\n" + "</thing>";
053:
054: assertBothWays(thing, expectedXml);
055: }
056:
057: public static class Original extends StandardObject {
058: String originalValue;
059:
060: public Original() {
061: }
062:
063: public Original(String originalValue) {
064: this .originalValue = originalValue;
065: }
066:
067: private Object writeReplace() {
068: return new Replaced(originalValue.toUpperCase());
069: }
070: }
071:
072: public static class Replaced extends StandardObject {
073: String replacedValue;
074:
075: public Replaced() {
076: }
077:
078: public Replaced(String replacedValue) {
079: this .replacedValue = replacedValue;
080: }
081:
082: private Object readResolve() {
083: return new Original(replacedValue.toLowerCase());
084: }
085: }
086:
087: public void testAllowsDifferentTypeToBeSubstituted() {
088: xstream.alias("original-class", Original.class);
089: xstream.alias("replaced-class", Replaced.class);
090:
091: Original in = new Original("hello world");
092:
093: String expectedXml = ""
094: + "<original-class resolves-to=\"replaced-class\">\n"
095: + " <replacedValue>HELLO WORLD</replacedValue>\n"
096: + "</original-class>";
097:
098: assertBothWays(in, expectedXml);
099: }
100:
101: public static class OriginalSerializable extends StandardObject {
102: String originalValue;
103:
104: public OriginalSerializable() {
105: }
106:
107: public OriginalSerializable(String originalValue) {
108: this .originalValue = originalValue;
109: }
110:
111: private Object writeReplace() {
112: return new ReplacedSerializable(originalValue.toUpperCase());
113: }
114:
115: private void readObject(ObjectInputStream in)
116: throws IOException, ClassNotFoundException {
117: in.defaultReadObject();
118: }
119: }
120:
121: public static class ReplacedSerializable extends StandardObject {
122: String replacedValue;
123:
124: public ReplacedSerializable() {
125: }
126:
127: public ReplacedSerializable(String replacedValue) {
128: this .replacedValue = replacedValue;
129: }
130:
131: private Object readResolve() {
132: return new OriginalSerializable(replacedValue.toLowerCase());
133: }
134:
135: private void readObject(ObjectInputStream in)
136: throws IOException, ClassNotFoundException {
137: in.defaultReadObject();
138: }
139: }
140:
141: public void testAllowsDifferentTypeToBeSubstitutedForCustomSerializableObjects() {
142: xstream.alias("original-serializable-class",
143: OriginalSerializable.class);
144: xstream.alias("replaced-serializable-class",
145: ReplacedSerializable.class);
146:
147: OriginalSerializable in = new OriginalSerializable(
148: "hello world");
149:
150: String expectedXml = ""
151: + "<original-serializable-class resolves-to=\"replaced-serializable-class\" serialization=\"custom\">\n"
152: + " <replaced-serializable-class>\n"
153: + " <default>\n"
154: + " <replacedValue>HELLO WORLD</replacedValue>\n"
155: + " </default>\n"
156: + " </replaced-serializable-class>\n"
157: + "</original-serializable-class>";
158:
159: assertBothWays(in, expectedXml);
160: }
161:
162: }
|