KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > acceptance > AbstractCircularReferenceTest


1 package com.thoughtworks.acceptance;
2
3 public abstract class AbstractCircularReferenceTest extends AbstractAcceptanceTest {
4
5     protected void setUp() throws Exception JavaDoc {
6         super.setUp();
7         xstream.alias("person", Person.class);
8     }
9
10     public void testCircularReference() {
11         Person bob = new Person("bob");
12         Person jane = new Person("jane");
13         bob.likes = jane;
14         jane.likes = bob;
15
16         String JavaDoc xml = xstream.toXML(bob);
17
18         Person bobOut = (Person) xstream.fromXML(xml);
19         assertEquals("bob", bobOut.firstname);
20         Person janeOut = bobOut.likes;
21
22         assertEquals("jane", janeOut.firstname);
23
24         assertSame(bobOut.likes, janeOut);
25         assertSame(bobOut, janeOut.likes);
26     }
27
28     public void testCircularReferenceToSelf() {
29         Person bob = new Person("bob");
30         bob.likes = bob;
31
32         String JavaDoc xml = xstream.toXML(bob);
33
34         Person bobOut = (Person) xstream.fromXML(xml);
35         assertEquals("bob", bobOut.firstname);
36         assertSame(bobOut, bobOut.likes);
37     }
38
39     public void testDeepCircularReferences() {
40         Person bob = new Person("bob");
41         Person jane = new Person("jane");
42         Person ann = new Person("ann");
43         Person poo = new Person("poo");
44
45         bob.likes = jane;
46         bob.loathes = ann;
47         ann.likes = jane;
48         ann.loathes = poo;
49         poo.likes = jane;
50         poo.loathes = ann;
51
52         String JavaDoc xml = xstream.toXML(bob);
53         Person bobOut = (Person) xstream.fromXML(xml);
54         Person janeOut = bobOut.likes;
55         Person annOut = bobOut.loathes;
56         Person pooOut = annOut.loathes;
57
58         assertEquals("bob", bobOut.firstname);
59         assertEquals("jane", janeOut.firstname);
60         assertEquals("ann", annOut.firstname);
61         assertEquals("poo", pooOut.firstname);
62
63         assertSame(janeOut, bobOut.likes);
64         assertSame(annOut, bobOut.loathes);
65         assertSame(janeOut, annOut.likes);
66         assertSame(pooOut, annOut.loathes);
67         assertSame(janeOut, pooOut.likes);
68         assertSame(annOut, pooOut.loathes);
69     }
70
71     public static class Person {
72         public String JavaDoc firstname;
73         public Person likes;
74         public Person loathes;
75
76         public Person() {
77         }
78
79         public Person(String JavaDoc name) {
80             this.firstname = name;
81         }
82     }
83
84 }
85
Popular Tags