1
2
3
4
5
6
7
8
9
10 package org.dom4j;
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 junit.framework.Test;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22
23 import org.dom4j.io.SAXReader;
24
25 /*** Tests that a dom4j document is Serializable
26 *
27 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
28 * @version $Revision: 1.13 $
29 */
30 public class TestSerialize extends AbstractTestCase {
31
32 protected static final boolean VERBOSE = false;
33
34
35 public static void main( String[] args ) {
36 TestRunner.run( suite() );
37 }
38
39 public static Test suite() {
40 return new TestSuite( TestSerialize.class );
41 }
42
43 public TestSerialize(String name) {
44 super(name);
45 }
46
47
48
49 public void testSerializePeriodicTable() throws Exception {
50 testSerialize( "/xml/periodic_table.xml" );
51 }
52
53 public void testSerializeMuchAdo() throws Exception {
54 testSerialize( "/xml/much_ado.xml" );
55 }
56
57 public void testSerializeTestSchema() throws Exception {
58 testSerialize( "/xml/test/schema/personal.xsd" );
59 }
60
61 public void testSerializeXPath() throws Exception {
62 Map uris = new HashMap();
63 uris.put( "SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/" );
64 uris.put( "m", "urn:xmethodsBabelFish" );
65
66 DocumentFactory factory = new DocumentFactory();
67 factory.setXPathNamespaceURIs( uris );
68
69
70 SAXReader reader = new SAXReader();
71 reader.setDocumentFactory( factory );
72 Document doc = reader.read(getClass().getResource("/xml/soap.xml"));
73
74
75 Node element = doc.selectSingleNode( "/SOAP-ENV:Envelope/SOAP-ENV:Body/m:BabelFish" );
76 assertTrue( "Found valid element", element != null );
77
78 XPath xpath = factory.createXPath( "/SOAP-ENV:Envelope/SOAP-ENV:Body/m:BabelFish" );
79 element = xpath.selectSingleNode( doc );
80 assertTrue( "Found valid element", element != null );
81
82
83 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
84 ObjectOutputStream out = new ObjectOutputStream( bytesOut );
85 out.writeObject( xpath );
86 out.close();
87
88 byte[] data = bytesOut.toByteArray();
89
90 ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( data ) );
91 XPath xpath2 = (XPath) in.readObject();
92 in.close();
93
94 element = xpath2.selectSingleNode( doc );
95 assertTrue( "Found valid element", element != null );
96 }
97
98
99
100 protected void testSerialize(String xmlFile) throws Exception {
101 SAXReader reader = new SAXReader();
102 Document document = reader.read(getClass().getResource(xmlFile));
103 String text = document.asXML();
104
105 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
106 ObjectOutputStream out = new ObjectOutputStream( bytesOut );
107 out.writeObject( document );
108 out.close();
109
110 byte[] data = bytesOut.toByteArray();
111
112 ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( data ) );
113 Document doc2 = (Document) in.readObject();
114 in.close();
115
116 String text2 = doc2.asXML();
117
118 assertEquals( "Documents text are equal", text, text2 );
119
120 assertTrue( "Read back document after serialization", doc2 != null && doc2 instanceof Document );
121
122 assertDocumentsEqual( document, (Document) doc2 );
123
124
125
126 doc2.getRootElement().addElement( "new" );
127 }
128
129 protected void setUp() throws Exception {
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179