KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.thoughtworks.acceptance;
2
3 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
4 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
5 import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
6 import com.thoughtworks.xstream.io.xml.XppReader;
7 import com.thoughtworks.xstream.testutil.CallLog;
8 import com.thoughtworks.xstream.XStream;
9 import com.thoughtworks.acceptance.objects.Software;
10
11 import java.io.StringReader JavaDoc;
12 import java.io.StringWriter JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.Writer JavaDoc;
15 import java.io.ObjectOutputStream JavaDoc;
16 import java.io.ObjectInputStream JavaDoc;
17 import java.io.EOFException JavaDoc;
18 import java.io.Reader JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.ArrayList JavaDoc;
21
22 public class MultipleObjectsInOneStreamTest extends AbstractAcceptanceTest {
23
24     public static class Person extends StandardObject {
25
26         private String JavaDoc firstName;
27         private String JavaDoc lastName;
28         private Person secretary;
29
30         public Person(String JavaDoc firstName, String JavaDoc lastName) {
31             this.firstName = firstName;
32             this.lastName = lastName;
33         }
34
35     }
36
37     public void testReadAndWriteMultipleObjectsInOneStream() {
38         xstream.alias("person", Person.class);
39         StringWriter JavaDoc buffer = new StringWriter JavaDoc();
40
41         // serialize
42
HierarchicalStreamWriter writer = new PrettyPrintWriter(buffer);
43         writer.startNode("people");
44         xstream.marshal(new Person("Postman", "Pat"), writer);
45         xstream.marshal(new Person("Bob", "Builder"), writer);
46         xstream.marshal(new Person("Tinky", "Winky"), writer);
47         writer.endNode();
48
49         assertEquals(""
50                 + "<people>\n"
51                 + " <person>\n"
52                 + " <firstName>Postman</firstName>\n"
53                 + " <lastName>Pat</lastName>\n"
54                 + " </person>\n"
55                 + " <person>\n"
56                 + " <firstName>Bob</firstName>\n"
57                 + " <lastName>Builder</lastName>\n"
58                 + " </person>\n"
59                 + " <person>\n"
60                 + " <firstName>Tinky</firstName>\n"
61                 + " <lastName>Winky</lastName>\n"
62                 + " </person>\n"
63                 + "</people>",
64                 buffer.toString());
65
66         // deserialize
67
HierarchicalStreamReader reader = new XppReader(new StringReader JavaDoc(buffer.toString()));
68
69         assertTrue("should be another object to read (1)", reader.hasMoreChildren());
70         reader.moveDown();
71         assertEquals(new Person("Postman", "Pat"), xstream.unmarshal(reader));
72         reader.moveUp();
73
74         assertTrue("should be another object to read (2)", reader.hasMoreChildren());
75         reader.moveDown();
76         assertEquals(new Person("Bob", "Builder"), xstream.unmarshal(reader));
77         reader.moveUp();
78
79         assertTrue("should be another object to read (3)", reader.hasMoreChildren());
80         reader.moveDown();
81         assertEquals(new Person("Tinky", "Winky"), xstream.unmarshal(reader));
82         reader.moveUp();
83
84         assertFalse("should be no more objects", reader.hasMoreChildren());
85     }
86
87     public void testDrivenThroughObjectStream() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
88         Writer writer = new StringWriter JavaDoc();
89         xstream.alias("software", Software.class);
90
91         ObjectOutputStream JavaDoc oos = xstream.createObjectOutputStream(writer);
92         oos.writeInt(123);
93         oos.writeObject("hello");
94         oos.writeObject(new Software("tw", "xs"));
95         oos.close();
96
97         String JavaDoc expectedXml = ""
98                 + "<object-stream>\n"
99                 + " <int>123</int>\n"
100                 + " <string>hello</string>\n"
101                 + " <software>\n"
102                 + " <vendor>tw</vendor>\n"
103                 + " <name>xs</name>\n"
104                 + " </software>\n"
105                 + "</object-stream>";
106
107         assertEquals(expectedXml, writer.toString());
108
109         ObjectInputStream JavaDoc ois = xstream.createObjectInputStream(new StringReader JavaDoc(writer.toString()));
110         assertEquals(123, ois.readInt());
111         assertEquals("hello", ois.readObject());
112         assertEquals(new Software("tw", "xs"), ois.readObject());
113
114         try {
115             ois.readObject(); // As far as I can see this is the only clue the ObjectInputStream gives that it's done.
116
fail("Expected EOFException");
117         } catch (EOFException JavaDoc expectedException) {
118             // good
119
}
120     }
121
122     public void testObjectOutputStreamPropegatesCloseAndFlushEvents() throws IOException JavaDoc {
123         // setup
124
final CallLog log = new CallLog();
125         Writer loggingWriter = new Writer() {
126             public void close() throws IOException JavaDoc {
127                 log.actual("close");
128             }
129
130             public void flush() throws IOException JavaDoc {
131                 log.actual("flush");
132             }
133
134             public void write(char cbuf[], int off, int len) {
135                 // don't care about this
136
}
137         };
138
139         // expectations
140
log.expect("flush"); // TWO flushes are currently caused. Only one is needed, but this is no big deal.
141
log.expect("flush");
142         log.expect("close");
143
144         // execute
145
ObjectOutputStream JavaDoc objectOutputStream = xstream.createObjectOutputStream(loggingWriter);
146         objectOutputStream.flush();
147         objectOutputStream.close();
148
149         // verify
150
log.verify();
151     }
152
153     public void testObjectInputStreamPropegatesCloseEvent() throws IOException JavaDoc {
154         // setup
155
final CallLog log = new CallLog();
156         Reader loggingReader = new StringReader JavaDoc("<int>1</int>") {
157             public void close() {
158                 log.actual("close");
159             }
160         };
161
162         // expectations
163
log.expect("close");
164
165         // execute
166
ObjectInputStream JavaDoc objectInputStream = xstream.createObjectInputStream(loggingReader);
167         objectInputStream.close();
168
169         // verify
170
log.verify();
171     }
172
173     public void testByDefaultDoesNotPreserveReferencesAcrossDifferentObjectsInStream() throws Exception JavaDoc {
174         xstream.alias("person", Person.class);
175
176         // Setup initial data: two object, one referencing another...
177
Person alice = new Person("Alice", "Thing");
178         Person jane = new Person("Jane", "Blah");
179         jane.secretary = alice;
180
181         // Serialize the two individual objects.
182
StringWriter JavaDoc writer = new StringWriter JavaDoc();
183         ObjectOutputStream JavaDoc out = xstream.createObjectOutputStream(writer);
184         out.writeObject(alice);
185         out.writeObject(jane);
186         out.close();
187
188         // Deserialize the two objects.
189
ObjectInputStream JavaDoc in = xstream.createObjectInputStream(new StringReader JavaDoc(writer.toString()));
190         alice = (Person) in.readObject();
191         jane = (Person) in.readObject();
192         in.close();
193
194         assertNotSame(alice, jane.secretary); // NOT SAME
195
}
196
197     public void TODOtestSupportsOptionToPreserveReferencesAcrossDifferentObjectsInStream() throws Exception JavaDoc {
198         xstream.alias("person", Person.class);
199
200 // xstream.setObjectStreamMode(XStream.ID_REFERENCES);
201

202         // Setup initial data: two object, one referencing another...
203
Person alice = new Person("Alice", "Thing");
204         Person jane = new Person("Jane", "Blah");
205         jane.secretary = alice;
206
207         // Serialize the two individual objects.
208
StringWriter JavaDoc writer = new StringWriter JavaDoc();
209         ObjectOutputStream JavaDoc out = xstream.createObjectOutputStream(writer);
210         out.writeObject(alice);
211         out.writeObject(jane);
212         out.close();
213
214         // Deserialize the two objects.
215
ObjectInputStream JavaDoc in = xstream.createObjectInputStream(new StringReader JavaDoc(writer.toString()));
216         alice = (Person) in.readObject();
217         jane = (Person) in.readObject();
218         in.close();
219
220         assertSame(alice, jane.secretary); // NOT SAME
221
}
222 }
223
Popular Tags