01: /*
02: * Copyright (C) 2004 Joe Walnes.
03: * Copyright (C) 2006, 2007, 2008 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 07. May 2004 by Joe Walnes
11: */
12: package com.thoughtworks.acceptance;
13:
14: import com.thoughtworks.xstream.XStream;
15:
16: public class RelativeXPathCircularReferenceTest extends
17: AbstractCircularReferenceTest {
18:
19: // inherits test from superclass
20: protected void setUp() throws Exception {
21: super .setUp();
22: xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
23: }
24:
25: public void testCircularReferenceXml() {
26: Person bob = new Person("bob");
27: Person jane = new Person("jane");
28: bob.likes = jane;
29: jane.likes = bob;
30:
31: String expected = "" + "<person>\n"
32: + " <firstname>bob</firstname>\n" + " <likes>\n"
33: + " <firstname>jane</firstname>\n"
34: + " <likes reference=\"../..\"/>\n" + " </likes>\n"
35: + "</person>";
36:
37: assertEquals(expected, xstream.toXML(bob));
38: }
39:
40: public void testCircularReferenceToSelfXml() {
41: Person bob = new Person("bob");
42: bob.likes = bob;
43:
44: String expected = "" + "<person>\n"
45: + " <firstname>bob</firstname>\n"
46: + " <likes reference=\"..\"/>\n" + "</person>";
47:
48: assertEquals(expected, xstream.toXML(bob));
49: }
50:
51: static class LinkedElement {
52: String name;
53: LinkedElement next;
54:
55: LinkedElement(String name) {
56: this .name = name;
57: }
58: }
59:
60: public void testRing() {
61: LinkedElement tom = new LinkedElement("Tom");
62: LinkedElement dick = new LinkedElement("Dick");
63: LinkedElement harry = new LinkedElement("Harry");
64: tom.next = dick;
65: dick.next = harry;
66: harry.next = tom;
67:
68: xstream.alias("elem", LinkedElement.class);
69: String expected = "" + "<elem>\n" + " <name>Tom</name>\n"
70: + " <next>\n" + " <name>Dick</name>\n"
71: + " <next>\n" + " <name>Harry</name>\n"
72: + " <next reference=\"../../..\"/>\n"
73: + " </next>\n" + " </next>\n" + "</elem>";
74:
75: assertEquals(expected, xstream.toXML(tom));
76: }
77:
78: }
|