001: /*
002: * Copyright (C) 2004 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: import com.thoughtworks.acceptance.objects.StandardObject;
015: import com.thoughtworks.acceptance.someobjects.WithNamedList;
016: import com.thoughtworks.xstream.core.AbstractReferenceMarshaller;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: public abstract class AbstractDuplicateReferenceTest extends
022: AbstractAcceptanceTest {
023:
024: protected void setUp() throws Exception {
025: super .setUp();
026: xstream.alias("thing", Thing.class);
027: }
028:
029: public void testReferencesAreWorking() {
030:
031: Thing sameThing = new Thing("hello");
032: Thing anotherThing = new Thing("hello");
033:
034: List list = new ArrayList();
035: list.add(sameThing);
036: list.add(sameThing);
037: list.add(anotherThing);
038:
039: String xml = xstream.toXML(list);
040: List result = (List) xstream.fromXML(xml);
041:
042: assertEquals(list, result);
043: }
044:
045: public void testReferencesAreTheSameObjectWhenDeserialized() {
046:
047: Thing sameThing = new Thing("hello");
048: Thing anotherThing = new Thing("hello");
049:
050: List list = new ArrayList();
051: list.add(sameThing);
052: list.add(sameThing);
053: list.add(anotherThing);
054:
055: String xml = xstream.toXML(list);
056: List result = (List) xstream.fromXML(xml);
057:
058: Thing t0 = (Thing) result.get(0);
059: Thing t1 = (Thing) result.get(1);
060: Thing t2 = (Thing) result.get(2);
061:
062: t0.field = "bye";
063:
064: assertEquals("bye", t0.field);
065: assertEquals("bye", t1.field);
066: assertEquals("hello", t2.field);
067:
068: }
069:
070: public static class Thing extends StandardObject {
071: public String field;
072:
073: public Thing() {
074: }
075:
076: public Thing(String field) {
077: this .field = field;
078: }
079: }
080:
081: public static class MultRef {
082: public Object s1 = new Object();
083: public Object s2 = s1;
084: }
085:
086: public void testMultipleReferencesToObjectsWithNoChildren() {
087: MultRef in = new MultRef();
088: assertSame(in.s1, in.s2);
089:
090: String xml = xstream.toXML(in);
091: MultRef out = (MultRef) xstream.fromXML(xml);
092:
093: assertSame(out.s1, out.s2);
094: }
095:
096: public void testReferencesNotUsedForImmutableValueTypes() {
097: MultRef in = new MultRef();
098: in.s1 = new Integer(4);
099: in.s2 = in.s1;
100:
101: String xml = xstream.toXML(in);
102: MultRef out = (MultRef) xstream.fromXML(xml);
103:
104: assertEquals(out.s1, out.s2);
105: assertNotSame(out.s1, out.s2);
106: }
107:
108: public void testReferencesUsedForMutableValueTypes() {
109: MultRef in = new MultRef();
110: in.s1 = new StringBuffer("hi");
111: in.s2 = in.s1;
112:
113: String xml = xstream.toXML(in);
114: MultRef out = (MultRef) xstream.fromXML(xml);
115:
116: StringBuffer buffer = (StringBuffer) out.s2;
117: buffer.append("bye");
118:
119: assertEquals("hibye", out.s1.toString());
120: assertSame(out.s1, out.s2);
121: }
122:
123: public void testReferencesToImplicitCollectionIsNotPossible() {
124: xstream.alias("strings", WithNamedList.class);
125: xstream.addImplicitCollection(WithNamedList.class, "things");
126: WithNamedList[] wls = new WithNamedList[] {
127: new WithNamedList("foo"), new WithNamedList("bar") };
128: wls[0].things.add("Hello");
129: wls[0].things.add("Daniel");
130: wls[1].things = wls[0].things;
131:
132: try {
133: xstream.toXML(wls);
134: fail("Thrown "
135: + AbstractReferenceMarshaller.ReferencedImplicitElementException.class
136: .getName() + " expected");
137: } catch (final AbstractReferenceMarshaller.ReferencedImplicitElementException e) {
138: // OK
139: }
140: }
141:
142: public void testReferencesToElementsOfImplicitCollectionIsPossible() {
143: xstream.alias("strings", WithNamedList.class);
144: xstream.addImplicitCollection(WithNamedList.class, "things");
145: WithNamedList[] wls = new WithNamedList[] {
146: new WithNamedList("foo"), new WithNamedList("bar") };
147: wls[0].things.add("Hello");
148: wls[0].things.add("Daniel");
149: wls[1].things.add(wls[0]);
150:
151: String xml = xstream.toXML(wls);
152: WithNamedList[] out = (WithNamedList[]) xstream.fromXML(xml);
153:
154: assertSame(out[0], out[1].things.get(0));
155: }
156:
157: public void testReferencesToElementsOfNthImplicitCollectionIsPossible() {
158: xstream.alias("strings", WithNamedList.class);
159: xstream.addImplicitCollection(WithNamedList.class, "things");
160: WithNamedList[] wls = new WithNamedList[] {
161: new WithNamedList("foo"), new WithNamedList("bar"),
162: new WithNamedList("foobar") };
163: wls[1].things.add("Hello");
164: wls[1].things.add("Daniel");
165: wls[2].things.add(wls[1]);
166:
167: String xml = xstream.toXML(wls);
168: WithNamedList[] out = (WithNamedList[]) xstream.fromXML(xml);
169:
170: assertSame(out[1], out[2].things.get(0));
171: }
172: }
|