1 package com.thoughtworks.acceptance; 2 3 public class InnerClassesTest extends AbstractAcceptanceTest { 4 5 public void testSerializedInnerClassMaintainsReferenceToOuterClass() { 6 7 Outer outer = new Outer("THE-OUTER-NAME", "THE-INNER-NAME"); 8 Outer.Inner inner = outer.getInner(); 9 10 assertEquals("Hello from THE-INNER-NAME (inside THE-OUTER-NAME)", inner.getMessage()); 11 12 String xml = xstream.toXML(inner); 13 14 String expectedXml = "" 15 + "<com.thoughtworks.acceptance.Outer-Inner>\n" 16 + " <innerName>THE-INNER-NAME</innerName>\n" 17 + " <outer-class>\n" 18 + " <inner reference=\"../..\"/>\n" 19 + " <outerName>THE-OUTER-NAME</outerName>\n" 20 + " </outer-class>\n" 21 + "</com.thoughtworks.acceptance.Outer-Inner>"; 22 assertEquals(expectedXml, xml); 23 24 Outer.Inner newInner = (Outer.Inner) xstream.fromXML(xml); 25 26 assertEquals("Hello from THE-INNER-NAME (inside THE-OUTER-NAME)", newInner.getMessage()); 27 } 28 } 29 30 class Outer { 31 32 private Inner inner; 33 private String outerName; 34 35 public Outer(String outerName, String innerName) { 36 inner = new Inner(innerName); 37 this.outerName = outerName; 38 } 39 40 public Inner getInner() { 41 return inner; 42 } 43 44 public class Inner { 45 private String innerName; 46 47 public Inner(String innerName) { 48 this.innerName = innerName; 49 } 50 51 public String getMessage() { 52 return "Hello from " + innerName + " (inside " + outerName + ")"; 53 } 54 } 55 } 56 57 58 | Popular Tags |