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