1
2
3
4
5
6
7
8
9
10 package org.dom4j;
11
12 import java.io.BufferedInputStream;
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.InputStream;
16 import java.io.StringReader;
17 import java.io.StringWriter;
18 import java.net.URL;
19
20 import javax.xml.transform.Transformer;
21 import javax.xml.transform.TransformerFactory;
22 import javax.xml.transform.stream.StreamResult;
23 import javax.xml.transform.stream.StreamSource;
24
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27 import junit.textui.TestRunner;
28
29 import org.dom4j.io.DOMReader;
30 import org.dom4j.io.DOMWriter;
31 import org.dom4j.io.DocumentResult;
32 import org.dom4j.io.DocumentSource;
33 import org.dom4j.io.SAXContentHandler;
34 import org.dom4j.io.SAXReader;
35 import org.dom4j.io.SAXWriter;
36 import org.dom4j.io.XMLWriter;
37
38 /*** A test harness to test the the round trips of Documents.
39 *
40 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
41 * @version $Revision: 1.17 $
42 */
43 public class TestRoundTrip extends AbstractTestCase {
44
45 protected String[] testDocuments = {
46 "/xml/test/encode.xml",
47 "/xml/fibo.xml",
48 "/xml/test/schema/personal-prefix.xsd",
49 "/xml/test/soap2.xml",
50 "/xml/test/test_schema.xml",
51 };
52
53 public static void main( String[] args ) {
54 TestRunner.run( suite() );
55 }
56
57 public static Test suite() {
58 return new TestSuite( TestRoundTrip.class );
59 }
60
61 public TestRoundTrip(String name) {
62 super(name);
63 }
64
65
66
67 public void testTextRoundTrip() throws Exception {
68 for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
69 Document doc = parseDocument( testDocuments[i] );
70 roundTripText( doc );
71 }
72 }
73
74 public void testSAXRoundTrip() throws Exception {
75 for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
76 Document doc = parseDocument( testDocuments[i] );
77 roundTripSAX( doc );
78 }
79 }
80
81 public void testDOMRoundTrip() throws Exception {
82 for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
83 Document doc = parseDocument( testDocuments[i] );
84 roundTripDOM( doc );
85 }
86 }
87
88 public void testJAXPRoundTrip() throws Exception {
89 for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
90 Document doc = parseDocument( testDocuments[i] );
91 roundTripJAXP( doc );
92 }
93 }
94
95 public void testFullRoundTrip() throws Exception {
96 for ( int i = 0, size = testDocuments.length; i < size; i++ ) {
97 Document doc = parseDocument( testDocuments[i] );
98 roundTripFull( doc );
99 }
100 }
101
102 public void testRoundTrip() throws Exception {
103 SAXReader reader = new SAXReader();
104 Document document = reader.read(getClass().getResource("/xml/xmlspec.xml"));
105
106
107 Document doc1 = roundTripSAX( document );
108 Document doc2 = roundTripDOM( doc1);
109 Document doc3 = roundTripSAX( doc2 );
110
111
112 Document doc5 = roundTripDOM( doc3 );
113
114 assertDocumentsEqual( document, doc5 );
115 }
116
117
118
119 protected void setUp() throws Exception {
120 }
121
122 protected Document parseDocument(String file) throws Exception {
123 SAXReader reader = new SAXReader();
124 URL url = getClass().getResource(file);
125 return reader.read(url);
126 }
127
128 protected Document roundTripDOM(Document document) throws Exception {
129
130 DOMWriter domWriter = new DOMWriter();
131 org.w3c.dom.Document domDocument = domWriter.write(document);
132
133
134 DOMReader domReader = new DOMReader();
135 Document newDocument = domReader.read( domDocument );
136
137
138 newDocument.setName( document.getName() );
139
140 assertDocumentsEqual( document, newDocument );
141
142 return newDocument;
143 }
144
145 protected Document roundTripJAXP(Document document) throws Exception {
146
147 TransformerFactory factory = TransformerFactory.newInstance();
148 Transformer transformer = factory.newTransformer();
149
150 StringWriter buffer = new StringWriter();
151 StreamResult streamResult = new StreamResult(buffer);
152 DocumentSource documentSource = new DocumentSource(document);
153
154 transformer.transform(documentSource, streamResult);
155
156
157 DocumentResult documentResult = new DocumentResult();
158 StreamSource streamSource = new StreamSource( new StringReader( buffer.toString() ) );
159
160 transformer.transform(streamSource, documentResult);
161
162 Document newDocument = documentResult.getDocument();
163
164
165 newDocument.setName( document.getName() );
166
167 assertDocumentsEqual( document, newDocument );
168
169 return newDocument;
170 }
171
172 protected Document roundTripSAX(Document document) throws Exception {
173
174
175
176 SAXContentHandler contentHandler = new SAXContentHandler();
177 SAXWriter saxWriter = new SAXWriter( contentHandler, contentHandler, contentHandler );
178
179 saxWriter.write( document );
180 Document newDocument = contentHandler.getDocument();
181
182
183 newDocument.setName( document.getName() );
184
185 assertDocumentsEqual( document, newDocument );
186
187 return newDocument;
188 }
189
190 protected Document roundTripText(Document document) throws Exception {
191 StringWriter out = new StringWriter();
192 XMLWriter xmlWriter = new XMLWriter(out);
193
194 xmlWriter.write( document );
195
196
197 String xml = out.toString();
198
199 StringReader in = new StringReader( xml );
200 SAXReader reader = new SAXReader();
201 Document newDocument = reader.read(in);
202
203
204 newDocument.setName( document.getName() );
205
206 assertDocumentsEqual( document, newDocument );
207
208 return newDocument;
209 }
210
211 protected Document roundTripFull(Document document) throws Exception {
212 Document doc2 = roundTripDOM( document );
213 Document doc3 = roundTripSAX( doc2 );
214 Document doc4 = roundTripText( doc3 );
215
216 assertDocumentsEqual( document, doc4 );
217
218 return doc4;
219 }
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268