1 package com.thoughtworks.acceptance; 2 3 import java.io.Externalizable ; 4 import java.io.IOException ; 5 import java.io.ObjectInput ; 6 import java.io.ObjectOutput ; 7 8 public class ExternalizableTest extends AbstractAcceptanceTest { 9 10 public static class SomethingExternalizable extends StandardObject implements Externalizable { 11 12 private String first; 13 private String last; 14 15 public SomethingExternalizable() { 16 } 17 18 public SomethingExternalizable(String first, String last) { 19 this.first = first; 20 this.last = last; 21 } 22 23 public void writeExternal(ObjectOutput out) throws IOException { 24 out.writeInt(first.length()); 25 out.writeObject(first + last); 26 } 27 28 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 29 int offset = in.readInt(); 30 String full = (String ) in.readObject(); 31 first = full.substring(0, offset); 32 last = full.substring(offset); 33 } 34 } 35 36 public void testExternalizable() { 37 xstream.alias("something", SomethingExternalizable.class); 38 39 SomethingExternalizable in = new SomethingExternalizable("Joe", "Walnes"); 40 41 String expected = "" 42 + "<something>\n" 43 + " <int>3</int>\n" 44 + " <string>JoeWalnes</string>\n" 45 + "</something>"; 46 47 assertBothWays(in, expected); 48 } 49 50 static class Owner extends StandardObject { 51 SomethingExternalizable target; 52 } 53 54 public void testExternalizableAsFieldOfAnotherObject() { 55 xstream.alias("something", SomethingExternalizable.class); 56 xstream.alias("owner", Owner.class); 57 58 Owner in = new Owner(); 59 in.target = new SomethingExternalizable("Joe", "Walnes"); 60 61 String expected = "" 62 + "<owner>\n" 63 + " <target>\n" 64 + " <int>3</int>\n" 65 + " <string>JoeWalnes</string>\n" 66 + " </target>\n" 67 + "</owner>"; 68 69 assertBothWays(in, expected); 70 } 71 72 } 73 | Popular Tags |