KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.acceptance;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 public abstract class AbstractDuplicateReferenceTest extends AbstractAcceptanceTest {
7
8     protected void setUp() throws Exception JavaDoc {
9         super.setUp();
10         xstream.alias("thing", Thing.class);
11     }
12
13     public void testReferencesAreWorking() {
14
15         Thing sameThing = new Thing("hello");
16         Thing anotherThing = new Thing("hello");
17
18         List JavaDoc list = new ArrayList JavaDoc();
19         list.add(sameThing);
20         list.add(sameThing);
21         list.add(anotherThing);
22
23         String JavaDoc xml = xstream.toXML(list);
24         List JavaDoc result = (List JavaDoc) xstream.fromXML(xml);
25
26         assertEquals(list, result);
27     }
28
29     public void testReferencesAreTheSameObjectWhenDeserialized() {
30
31         Thing sameThing = new Thing("hello");
32         Thing anotherThing = new Thing("hello");
33
34         List JavaDoc list = new ArrayList JavaDoc();
35         list.add(sameThing);
36         list.add(sameThing);
37         list.add(anotherThing);
38
39         String JavaDoc xml = xstream.toXML(list);
40         List JavaDoc result = (List JavaDoc) xstream.fromXML(xml);
41
42         Thing t0 = (Thing) result.get(0);
43         Thing t1 = (Thing) result.get(1);
44         Thing t2 = (Thing) result.get(2);
45
46         t0.field = "bye";
47
48         assertEquals("bye", t0.field);
49         assertEquals("bye", t1.field);
50         assertEquals("hello", t2.field);
51
52     }
53
54     public static class Thing extends StandardObject {
55         public String JavaDoc field;
56
57         public Thing() {
58         }
59
60         public Thing(String JavaDoc field) {
61             this.field = field;
62         }
63     }
64
65     public static class MultRef {
66         public Object JavaDoc s1 = new Object JavaDoc();
67         public Object JavaDoc s2 = s1;
68     }
69
70     public void testMultipleReferencesToObjectsWithNoChildren() {
71         MultRef in = new MultRef();
72         assertSame(in.s1, in.s2);
73
74         String JavaDoc xml = xstream.toXML(in);
75         MultRef out = (MultRef) xstream.fromXML(xml);
76
77         assertSame(out.s1, out.s2);
78     }
79
80
81 }
82
Popular Tags