01: /*
02: * Copyright (C) 2005, 2006 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 31. January 2005 by Joe Walnes
11: */
12: package com.thoughtworks.acceptance;
13:
14: public class InnerClassesTest extends AbstractAcceptanceTest {
15:
16: public void testSerializedInnerClassMaintainsReferenceToOuterClass() {
17:
18: Outer outer = new Outer("THE-OUTER-NAME", "THE-INNER-NAME");
19: Outer.Inner inner = outer.getInner();
20:
21: assertEquals(
22: "Hello from THE-INNER-NAME (inside THE-OUTER-NAME)",
23: inner.getMessage());
24:
25: String xml = xstream.toXML(inner);
26:
27: String expectedXml = ""
28: + "<com.thoughtworks.acceptance.Outer_-Inner>\n"
29: + " <innerName>THE-INNER-NAME</innerName>\n"
30: + " <outer-class>\n"
31: + " <inner reference=\"../..\"/>\n"
32: + " <outerName>THE-OUTER-NAME</outerName>\n"
33: + " </outer-class>\n"
34: + "</com.thoughtworks.acceptance.Outer_-Inner>";
35: assertEquals(expectedXml, xml);
36:
37: Outer.Inner newInner = (Outer.Inner) xstream.fromXML(xml);
38:
39: assertEquals(
40: "Hello from THE-INNER-NAME (inside THE-OUTER-NAME)",
41: newInner.getMessage());
42: }
43: }
44:
45: class Outer {
46:
47: private Inner inner;
48: private String outerName;
49:
50: public Outer(String outerName, String innerName) {
51: inner = new Inner(innerName);
52: this .outerName = outerName;
53: }
54:
55: public Inner getInner() {
56: return inner;
57: }
58:
59: public class Inner {
60: private String innerName;
61:
62: public Inner(String innerName) {
63: this .innerName = innerName;
64: }
65:
66: public String getMessage() {
67: return "Hello from " + innerName + " (inside " + outerName
68: + ")";
69: }
70: }
71: }
|