01: /*
02: * Copyright (C) 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 24. September 2005 by Joe Walnes
11: */
12: package com.thoughtworks.acceptance;
13:
14: import java.io.IOException;
15: import java.io.ObjectInputStream;
16: import java.io.ObjectOutputStream;
17: import java.io.Serializable;
18:
19: public abstract class AbstractNestedCircularReferenceTest extends
20: AbstractAcceptanceTest {
21:
22: public static class WeirdThing implements Serializable {
23: public transient Object anotherObject;
24: private NestedThing nestedThing = new NestedThing();
25:
26: private void readObject(ObjectInputStream in)
27: throws IOException, ClassNotFoundException {
28: in.defaultReadObject();
29: anotherObject = in.readObject();
30: }
31:
32: private void writeObject(ObjectOutputStream out)
33: throws IOException {
34: out.defaultWriteObject();
35: out.writeObject(anotherObject);
36: }
37:
38: private class NestedThing implements Serializable {
39: private void readObject(ObjectInputStream in)
40: throws IOException, ClassNotFoundException {
41: in.defaultReadObject();
42: }
43:
44: private void writeObject(ObjectOutputStream out)
45: throws IOException {
46: out.defaultWriteObject();
47: }
48:
49: }
50: }
51:
52: public void testWeirdCircularReference() {
53: // I cannot fully explain what's special about WeirdThing, however without ensuring that a reference is only
54: // put in the references map once, this fails.
55:
56: // This case was first noticed when serializing JComboBox, deserializing it and then serializing it again.
57: // Upon the second serialization, it would cause the Sun 1.4.1 JVM to crash:
58: // Object in = new javax.swing.JComboBox();
59: // Object out = xstream.fromXML(xstream.toXML(in));
60: // xstream.toXML(out); ....causes JVM crash on 1.4.1
61:
62: // WeirdThing is the least possible code I can create to reproduce the problem.
63:
64: // This also fails for JRockit 1.4.2 deeply nested, when it tries to set the final field
65: // AbstractNestedCircularReferenceTest$WeirdThing$NestedThing$this$1.
66:
67: // setup
68: WeirdThing in = new WeirdThing();
69: in.anotherObject = in;
70:
71: String xml = xstream.toXML(in);
72: //System.out.println(xml + "\n");
73:
74: // execute
75: WeirdThing out = (WeirdThing) xstream.fromXML(xml);
76:
77: // verify
78: assertSame(out, out.anotherObject);
79: }
80:
81: }
|