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 ; 12 import java.io.StringWriter ; 13 import java.io.IOException ; 14 import java.io.Writer ; 15 import java.io.ObjectOutputStream ; 16 import java.io.ObjectInputStream ; 17 import java.io.EOFException ; 18 import java.io.Reader ; 19 import java.util.List ; 20 import java.util.ArrayList ; 21 22 public class MultipleObjectsInOneStreamTest extends AbstractAcceptanceTest { 23 24 public static class Person extends StandardObject { 25 26 private String firstName; 27 private String lastName; 28 private Person secretary; 29 30 public Person(String firstName, String 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 buffer = new StringWriter (); 40 41 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 HierarchicalStreamReader reader = new XppReader(new StringReader (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 , ClassNotFoundException { 88 Writer writer = new StringWriter (); 89 xstream.alias("software", Software.class); 90 91 ObjectOutputStream 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 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 ois = xstream.createObjectInputStream(new StringReader (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(); fail("Expected EOFException"); 117 } catch (EOFException expectedException) { 118 } 120 } 121 122 public void testObjectOutputStreamPropegatesCloseAndFlushEvents() throws IOException { 123 final CallLog log = new CallLog(); 125 Writer loggingWriter = new Writer() { 126 public void close() throws IOException { 127 log.actual("close"); 128 } 129 130 public void flush() throws IOException { 131 log.actual("flush"); 132 } 133 134 public void write(char cbuf[], int off, int len) { 135 } 137 }; 138 139 log.expect("flush"); log.expect("flush"); 142 log.expect("close"); 143 144 ObjectOutputStream objectOutputStream = xstream.createObjectOutputStream(loggingWriter); 146 objectOutputStream.flush(); 147 objectOutputStream.close(); 148 149 log.verify(); 151 } 152 153 public void testObjectInputStreamPropegatesCloseEvent() throws IOException { 154 final CallLog log = new CallLog(); 156 Reader loggingReader = new StringReader ("<int>1</int>") { 157 public void close() { 158 log.actual("close"); 159 } 160 }; 161 162 log.expect("close"); 164 165 ObjectInputStream objectInputStream = xstream.createObjectInputStream(loggingReader); 167 objectInputStream.close(); 168 169 log.verify(); 171 } 172 173 public void testByDefaultDoesNotPreserveReferencesAcrossDifferentObjectsInStream() throws Exception { 174 xstream.alias("person", Person.class); 175 176 Person alice = new Person("Alice", "Thing"); 178 Person jane = new Person("Jane", "Blah"); 179 jane.secretary = alice; 180 181 StringWriter writer = new StringWriter (); 183 ObjectOutputStream out = xstream.createObjectOutputStream(writer); 184 out.writeObject(alice); 185 out.writeObject(jane); 186 out.close(); 187 188 ObjectInputStream in = xstream.createObjectInputStream(new StringReader (writer.toString())); 190 alice = (Person) in.readObject(); 191 jane = (Person) in.readObject(); 192 in.close(); 193 194 assertNotSame(alice, jane.secretary); } 196 197 public void TODOtestSupportsOptionToPreserveReferencesAcrossDifferentObjectsInStream() throws Exception { 198 xstream.alias("person", Person.class); 199 200 202 Person alice = new Person("Alice", "Thing"); 204 Person jane = new Person("Jane", "Blah"); 205 jane.secretary = alice; 206 207 StringWriter writer = new StringWriter (); 209 ObjectOutputStream out = xstream.createObjectOutputStream(writer); 210 out.writeObject(alice); 211 out.writeObject(jane); 212 out.close(); 213 214 ObjectInputStream in = xstream.createObjectInputStream(new StringReader (writer.toString())); 216 alice = (Person) in.readObject(); 217 jane = (Person) in.readObject(); 218 in.close(); 219 220 assertSame(alice, jane.secretary); } 222 } 223 | Popular Tags |