1 package com.thoughtworks.acceptance; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 public abstract class AbstractDuplicateReferenceTest extends AbstractAcceptanceTest { 7 8 protected void setUp() throws Exception { 9 super.setUp(); 10 xstream.alias("thing", Thing.class); 11 } 12 13 public void testReferencesAreWorking() { 14 15 Thing sameThing = new Thing("hello"); 16 Thing anotherThing = new Thing("hello"); 17 18 List list = new ArrayList (); 19 list.add(sameThing); 20 list.add(sameThing); 21 list.add(anotherThing); 22 23 String xml = xstream.toXML(list); 24 List result = (List ) xstream.fromXML(xml); 25 26 assertEquals(list, result); 27 } 28 29 public void testReferencesAreTheSameObjectWhenDeserialized() { 30 31 Thing sameThing = new Thing("hello"); 32 Thing anotherThing = new Thing("hello"); 33 34 List list = new ArrayList (); 35 list.add(sameThing); 36 list.add(sameThing); 37 list.add(anotherThing); 38 39 String xml = xstream.toXML(list); 40 List result = (List ) xstream.fromXML(xml); 41 42 Thing t0 = (Thing) result.get(0); 43 Thing t1 = (Thing) result.get(1); 44 Thing t2 = (Thing) result.get(2); 45 46 t0.field = "bye"; 47 48 assertEquals("bye", t0.field); 49 assertEquals("bye", t1.field); 50 assertEquals("hello", t2.field); 51 52 } 53 54 public static class Thing extends StandardObject { 55 public String field; 56 57 public Thing() { 58 } 59 60 public Thing(String field) { 61 this.field = field; 62 } 63 } 64 65 public static class MultRef { 66 public Object s1 = new Object (); 67 public Object s2 = s1; 68 } 69 70 public void testMultipleReferencesToObjectsWithNoChildren() { 71 MultRef in = new MultRef(); 72 assertSame(in.s1, in.s2); 73 74 String xml = xstream.toXML(in); 75 MultRef out = (MultRef) xstream.fromXML(xml); 76 77 assertSame(out.s1, out.s2); 78 } 79 80 81 } 82 | Popular Tags |