1 package com.thoughtworks.acceptance; 2 3 import java.io.*; 4 5 public class WriteReplaceTest extends AbstractAcceptanceTest { 6 7 public static class Thing extends StandardObject implements Serializable { 8 9 int a; 10 int b; 11 12 public Thing() { 13 } 14 15 public Thing(int a, int b) { 16 this.a = a; 17 this.b = b; 18 } 19 20 private Object writeReplace() { 21 return new Thing(a * 1000, b * 1000); 22 } 23 24 private Object readResolve() { 25 return new Thing(a / 1000, b / 1000); 26 } 27 28 } 29 30 public void testReplacesAndResolves() { 31 xstream.alias("thing", Thing.class); 32 33 Thing thing = new Thing(3, 6); 34 35 String expectedXml = "" 36 + "<thing>\n" 37 + " <a>3000</a>\n" 38 + " <b>6000</b>\n" 39 + "</thing>"; 40 41 assertBothWays(thing, expectedXml); 42 } 43 44 public static class Original extends StandardObject { 45 String originalValue; 46 47 public Original() { 48 } 49 50 public Original(String originalValue) { 51 this.originalValue = originalValue; 52 } 53 54 private Object writeReplace() { 55 return new Replaced(originalValue.toUpperCase()); 56 } 57 } 58 59 public static class Replaced extends StandardObject { 60 String replacedValue; 61 62 public Replaced() { 63 } 64 65 public Replaced(String replacedValue) { 66 this.replacedValue = replacedValue; 67 } 68 69 private Object readResolve() { 70 return new Original(replacedValue.toLowerCase()); 71 } 72 } 73 74 public void testAllowsDifferentTypeToBeSubstituted() { 75 xstream.alias("original-class", Original.class); 76 xstream.alias("replaced-class", Replaced.class); 77 78 Original in = new Original("hello world"); 79 80 String expectedXml = "" 81 + "<original-class resolves-to=\"replaced-class\">\n" 82 + " <replacedValue>HELLO WORLD</replacedValue>\n" 83 + "</original-class>"; 84 85 assertBothWays(in, expectedXml); 86 } 87 88 public static class OriginalSerializable extends StandardObject { 89 String originalValue; 90 91 public OriginalSerializable() { 92 } 93 94 public OriginalSerializable(String originalValue) { 95 this.originalValue = originalValue; 96 } 97 98 private Object writeReplace() { 99 return new ReplacedSerializable(originalValue.toUpperCase()); 100 } 101 102 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 103 in.defaultReadObject(); 104 } 105 } 106 107 public static class ReplacedSerializable extends StandardObject { 108 String replacedValue; 109 110 public ReplacedSerializable() { 111 } 112 113 public ReplacedSerializable(String replacedValue) { 114 this.replacedValue = replacedValue; 115 } 116 117 private Object readResolve() { 118 return new OriginalSerializable(replacedValue.toLowerCase()); 119 } 120 121 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 122 in.defaultReadObject(); 123 } 124 } 125 126 public void testAllowsDifferentTypeToBeSubstitutedForCustomSerializableObjects() { 127 xstream.alias("original-serializable-class", OriginalSerializable.class); 128 xstream.alias("replaced-serializable-class", ReplacedSerializable.class); 129 130 OriginalSerializable in = new OriginalSerializable("hello world"); 131 132 String expectedXml = "" 133 + "<original-serializable-class resolves-to=\"replaced-serializable-class\" serialization=\"custom\">\n" 134 + " <replaced-serializable-class>\n" 135 + " <default>\n" 136 + " <replacedValue>HELLO WORLD</replacedValue>\n" 137 + " </default>\n" 138 + " </replaced-serializable-class>\n" 139 + "</original-serializable-class>"; 140 141 assertBothWays(in, expectedXml); 142 } 143 144 } 145 | Popular Tags |