01: /*
02: * Copyright (C) 2006, 2007, 2008 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 17. March 2006 by Joerg Schaible
10: */
11: package com.thoughtworks.acceptance;
12:
13: import com.thoughtworks.xstream.XStream;
14:
15: public class AbsoluteXPathCircularReferenceTest extends
16: AbstractCircularReferenceTest {
17:
18: // inherits test from superclass
19: protected void setUp() throws Exception {
20: super .setUp();
21: xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
22: }
23:
24: public void testCircularReferenceXml() {
25: Person bob = new Person("bob");
26: Person jane = new Person("jane");
27: bob.likes = jane;
28: jane.likes = bob;
29:
30: String expected = "" + "<person>\n"
31: + " <firstname>bob</firstname>\n" + " <likes>\n"
32: + " <firstname>jane</firstname>\n"
33: + " <likes reference=\"/person\"/>\n"
34: + " </likes>\n" + "</person>";
35:
36: assertEquals(expected, xstream.toXML(bob));
37: }
38:
39: public void testCircularReferenceToSelfXml() {
40: Person bob = new Person("bob");
41: bob.likes = bob;
42:
43: String expected = "" + "<person>\n"
44: + " <firstname>bob</firstname>\n"
45: + " <likes reference=\"/person\"/>\n" + "</person>";
46:
47: assertEquals(expected, xstream.toXML(bob));
48: }
49:
50: static class LinkedElement {
51: String name;
52: LinkedElement next;
53:
54: LinkedElement(String name) {
55: this .name = name;
56: }
57: }
58:
59: public void testRing() {
60: LinkedElement tom = new LinkedElement("Tom");
61: LinkedElement dick = new LinkedElement("Dick");
62: LinkedElement harry = new LinkedElement("Harry");
63: tom.next = dick;
64: dick.next = harry;
65: harry.next = tom;
66:
67: xstream.alias("elem", LinkedElement.class);
68: String expected = "" + "<elem>\n" + " <name>Tom</name>\n"
69: + " <next>\n" + " <name>Dick</name>\n"
70: + " <next>\n" + " <name>Harry</name>\n"
71: + " <next reference=\"/elem\"/>\n"
72: + " </next>\n" + " </next>\n" + "</elem>";
73:
74: assertEquals(expected, xstream.toXML(tom));
75: }
76: }
|