1
2
3
4
5
6
7
8 package org.dom4j;
9
10 import junit.textui.TestRunner;
11
12 import java.io.ByteArrayInputStream;
13 import java.io.ByteArrayOutputStream;
14 import java.io.ObjectInputStream;
15 import java.io.ObjectOutputStream;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.dom4j.io.SAXReader;
20
21 /***
22 * Tests that a dom4j document is Serializable
23 *
24 * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
25 * @version $Revision: 1.4 $
26 */
27 public class SerializeTest extends AbstractTestCase {
28 public static void main(String[] args) {
29 TestRunner.run(SerializeTest.class);
30 }
31
32
33
34 public void testSerializePeriodicTable() throws Exception {
35 testSerialize("/xml/periodic_table.xml");
36 }
37
38 public void testSerializeMuchAdo() throws Exception {
39 testSerialize("/xml/much_ado.xml");
40 }
41
42 public void testSerializeTestSchema() throws Exception {
43 testSerialize("/xml/test/schema/personal.xsd");
44 }
45
46 public void testSerializeXPath() throws Exception {
47 Map uris = new HashMap();
48 uris.put("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
49 uris.put("m", "urn:xmethodsBabelFish");
50
51 DocumentFactory factory = new DocumentFactory();
52 factory.setXPathNamespaceURIs(uris);
53
54
55 SAXReader reader = new SAXReader();
56 reader.setDocumentFactory(factory);
57
58 Document doc = getDocument("/xml/soap.xml", reader);
59
60
61 String expr = "/SOAP-ENV:Envelope/SOAP-ENV:Body/m:BabelFish";
62 Node element = doc.selectSingleNode(expr);
63 assertTrue("Found valid element", element != null);
64
65 XPath xpath = factory
66 .createXPath("/SOAP-ENV:Envelope/SOAP-ENV:Body/m:BabelFish");
67 element = xpath.selectSingleNode(doc);
68 assertTrue("Found valid element", element != null);
69
70
71 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
72 ObjectOutputStream out = new ObjectOutputStream(bytesOut);
73 out.writeObject(xpath);
74 out.close();
75
76 byte[] data = bytesOut.toByteArray();
77
78 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
79 data));
80 XPath xpath2 = (XPath) in.readObject();
81 in.close();
82
83 element = xpath2.selectSingleNode(doc);
84 assertTrue("Found valid element", element != null);
85 }
86
87
88
89 protected void testSerialize(String xmlFile) throws Exception {
90 Document document = getDocument(xmlFile);
91 String text = document.asXML();
92
93 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
94 ObjectOutputStream out = new ObjectOutputStream(bytesOut);
95 out.writeObject(document);
96 out.close();
97
98 byte[] data = bytesOut.toByteArray();
99
100 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
101 data));
102 Document doc2 = (Document) in.readObject();
103 in.close();
104
105 String text2 = doc2.asXML();
106
107 assertEquals("Documents text are equal", text, text2);
108
109 assertTrue("Read back document after serialization", (doc2 != null)
110 && doc2 instanceof Document);
111
112 assertDocumentsEqual(document, (Document) doc2);
113
114
115 doc2.getRootElement().addElement("new");
116 }
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154