001: /*
002: * Copyright (C) 2004, 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 07. May 2004 by Joe Walnes
011: */
012: package com.thoughtworks.acceptance;
013:
014: public abstract class AbstractCircularReferenceTest extends
015: AbstractAcceptanceTest {
016:
017: protected void setUp() throws Exception {
018: super .setUp();
019: xstream.alias("person", Person.class);
020: }
021:
022: public static class Person {
023: public String firstname;
024: public Person likes;
025: public Person loathes;
026:
027: public Person() {
028: }
029:
030: public Person(String name) {
031: this .firstname = name;
032: }
033: }
034:
035: public void testCircularReference() {
036: Person bob = new Person("bob");
037: Person jane = new Person("jane");
038: bob.likes = jane;
039: jane.likes = bob;
040:
041: String xml = xstream.toXML(bob);
042:
043: Person bobOut = (Person) xstream.fromXML(xml);
044: assertEquals("bob", bobOut.firstname);
045: Person janeOut = bobOut.likes;
046:
047: assertEquals("jane", janeOut.firstname);
048:
049: assertSame(bobOut.likes, janeOut);
050: assertSame(bobOut, janeOut.likes);
051: }
052:
053: public void testCircularReferenceToSelf() {
054: Person bob = new Person("bob");
055: bob.likes = bob;
056:
057: String xml = xstream.toXML(bob);
058:
059: Person bobOut = (Person) xstream.fromXML(xml);
060: assertEquals("bob", bobOut.firstname);
061: assertSame(bobOut, bobOut.likes);
062: }
063:
064: public void testDeepCircularReferences() {
065: Person bob = new Person("bob");
066: Person jane = new Person("jane");
067: Person ann = new Person("ann");
068: Person poo = new Person("poo");
069:
070: bob.likes = jane;
071: bob.loathes = ann;
072: ann.likes = jane;
073: ann.loathes = poo;
074: poo.likes = jane;
075: poo.loathes = ann;
076: jane.likes = jane;
077: jane.loathes = bob;
078:
079: String xml = xstream.toXML(bob);
080: Person bobOut = (Person) xstream.fromXML(xml);
081: Person janeOut = bobOut.likes;
082: Person annOut = bobOut.loathes;
083: Person pooOut = annOut.loathes;
084:
085: assertEquals("bob", bobOut.firstname);
086: assertEquals("jane", janeOut.firstname);
087: assertEquals("ann", annOut.firstname);
088: assertEquals("poo", pooOut.firstname);
089:
090: assertSame(janeOut, bobOut.likes);
091: assertSame(annOut, bobOut.loathes);
092: assertSame(janeOut, annOut.likes);
093: assertSame(pooOut, annOut.loathes);
094: assertSame(janeOut, pooOut.likes);
095: assertSame(annOut, pooOut.loathes);
096: assertSame(janeOut, janeOut.likes);
097: assertSame(bobOut, janeOut.loathes);
098: }
099:
100: }
|