1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.xml.transform;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.Reader;
24 import java.io.StringReader;
25 import java.io.StringWriter;
26 import java.io.Writer;
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.stream.XMLEventReader;
30 import javax.xml.stream.XMLEventWriter;
31 import javax.xml.stream.XMLInputFactory;
32 import javax.xml.stream.XMLOutputFactory;
33 import javax.xml.stream.XMLStreamReader;
34 import javax.xml.stream.XMLStreamWriter;
35 import javax.xml.transform.Result;
36 import javax.xml.transform.Source;
37 import javax.xml.transform.Transformer;
38 import javax.xml.transform.TransformerFactory;
39 import javax.xml.transform.dom.DOMResult;
40 import javax.xml.transform.dom.DOMSource;
41 import javax.xml.transform.sax.SAXResult;
42 import javax.xml.transform.sax.SAXSource;
43 import javax.xml.transform.stax.StAXResult;
44 import javax.xml.transform.stax.StAXSource;
45 import javax.xml.transform.stream.StreamResult;
46 import javax.xml.transform.stream.StreamSource;
47
48 import org.custommonkey.xmlunit.XMLTestCase;
49 import org.easymock.MockControl;
50 import org.w3c.dom.Document;
51 import org.w3c.dom.Element;
52 import org.xml.sax.ContentHandler;
53 import org.xml.sax.InputSource;
54 import org.xml.sax.XMLReader;
55 import org.xml.sax.ext.DefaultHandler2;
56 import org.xml.sax.ext.LexicalHandler;
57 import org.xml.sax.helpers.DefaultHandler;
58 import org.xml.sax.helpers.XMLReaderFactory;
59
60 public class TraxUtilsTest extends XMLTestCase {
61
62 public void testIsStaxSourceInvalid() throws Exception {
63 assertFalse("A StAX Source", TraxUtils.isStaxSource(new DOMSource()));
64 assertFalse("A StAX Source", TraxUtils.isStaxSource(new SAXSource()));
65 assertFalse("A StAX Source", TraxUtils.isStaxSource(new StreamSource()));
66 }
67
68 public void testIsStaxSource() throws Exception {
69 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
70 String expected = "<element/>";
71 XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
72 StaxSource source = new StaxSource(streamReader);
73
74 assertTrue("Not a StAX Source", TraxUtils.isStaxSource(source));
75 }
76
77 public void testIsStaxSourceJaxp14() throws Exception {
78 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
79 String expected = "<element/>";
80 XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
81 StAXSource source = new StAXSource(streamReader);
82
83 assertTrue("Not a StAX Source", TraxUtils.isStaxSource(source));
84 }
85
86 public void testIsStaxResultInvalid() throws Exception {
87 assertFalse("A StAX Result", TraxUtils.isStaxResult(new DOMResult()));
88 assertFalse("A StAX Result", TraxUtils.isStaxResult(new SAXResult()));
89 assertFalse("A StAX Result", TraxUtils.isStaxResult(new StreamResult()));
90 }
91
92 public void testIsStaxResult() throws Exception {
93 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
94 XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
95 StaxResult result = new StaxResult(streamWriter);
96
97 assertTrue("Not a StAX Result", TraxUtils.isStaxResult(result));
98 }
99
100 public void testIsStaxResultJaxp14() throws Exception {
101 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
102 XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
103 StAXResult result = new StAXResult(streamWriter);
104
105 assertTrue("Not a StAX Result", TraxUtils.isStaxResult(result));
106 }
107
108 public void testCreateStaxSourceStreamReader() throws Exception {
109 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
110 String expected = "<element/>";
111 XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
112
113 Source source = TraxUtils.createStaxSource(streamReader);
114
115 Transformer transformer = TransformerFactory.newInstance().newTransformer();
116 StringResult result = new StringResult();
117 transformer.transform(source, result);
118
119 assertXMLEqual(expected, result.toString());
120 }
121
122 public void testCreateStaxSourceEventReader() throws Exception {
123 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
124 String expected = "<element/>";
125 XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(expected));
126
127 Source source = TraxUtils.createStaxSource(eventReader);
128
129 Transformer transformer = TransformerFactory.newInstance().newTransformer();
130 StringResult result = new StringResult();
131 transformer.transform(source, result);
132
133 assertXMLEqual(expected, result.toString());
134 }
135
136 public void testGetXMLStreamReader() throws Exception {
137 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
138 String expected = "<element/>";
139 XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
140
141 StaxSource source = new StaxSource(streamReader);
142
143 assertEquals("Invalid XMLStreamReader", streamReader, TraxUtils.getXMLStreamReader(source));
144 }
145
146 public void testGetXMLStreamReaderJaxp14() throws Exception {
147 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
148 String expected = "<element/>";
149 XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected));
150
151 StAXSource source = new StAXSource(streamReader);
152
153 assertEquals("Invalid XMLStreamReader", streamReader, TraxUtils.getXMLStreamReader(source));
154 }
155
156 public void testGetXMLEventReader() throws Exception {
157 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
158 String expected = "<element/>";
159 XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(expected));
160
161 StaxSource source = new StaxSource(eventReader);
162
163 assertEquals("Invalid XMLEventReader", eventReader, TraxUtils.getXMLEventReader(source));
164 }
165
166 public void testGetXMLEventReaderJaxp14() throws Exception {
167 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
168 String expected = "<element/>";
169 XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(expected));
170
171 StAXSource source = new StAXSource(eventReader);
172
173 assertEquals("Invalid XMLEventReader", eventReader, TraxUtils.getXMLEventReader(source));
174 }
175
176 public void testGetXMLStreamWriter() throws Exception {
177 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
178 XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
179
180 StaxResult result = new StaxResult(streamWriter);
181
182 assertEquals("Invalid XMLStreamWriter", streamWriter, TraxUtils.getXMLStreamWriter(result));
183 }
184
185 public void testGetXMLStreamWriterJaxp14() throws Exception {
186 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
187 XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
188
189 StAXResult result = new StAXResult(streamWriter);
190
191 assertEquals("Invalid XMLStreamWriter", streamWriter, TraxUtils.getXMLStreamWriter(result));
192 }
193
194 public void testGetXMLEventWriter() throws Exception {
195 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
196 XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new StringWriter());
197
198 StaxResult result = new StaxResult(eventWriter);
199
200 assertEquals("Invalid XMLStreamWriter", eventWriter, TraxUtils.getXMLEventWriter(result));
201 }
202
203 public void testGetXMLEventWriterJaxp14() throws Exception {
204 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
205 XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new StringWriter());
206
207 StAXResult result = new StAXResult(eventWriter);
208
209 assertEquals("Invalid XMLEventWriter", eventWriter, TraxUtils.getXMLEventWriter(result));
210 }
211
212 public void testGetDocument() throws Exception {
213 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
214 documentBuilderFactory.setNamespaceAware(true);
215 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
216 Document document = documentBuilder.newDocument();
217 assertSame("Invalid document", document, TraxUtils.getDocument(new DOMSource(document)));
218 Element element = document.createElement("element");
219 document.appendChild(element);
220 assertSame("Invalid document", document, TraxUtils.getDocument(new DOMSource(element)));
221 }
222
223 public void testDoWithDomSource() throws Exception {
224 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
225 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
226 Document document = documentBuilder.newDocument();
227
228 MockControl control = MockControl.createControl(TraxUtils.SourceCallback.class);
229 TraxUtils.SourceCallback mock = (TraxUtils.SourceCallback) control.getMock();
230 mock.domSource(document);
231 control.replay();
232
233 TraxUtils.doWithSource(new DOMSource(document), mock);
234
235 control.verify();
236 }
237
238 public void testDoWithDomResult() throws Exception {
239 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
240 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
241 Document document = documentBuilder.newDocument();
242
243 MockControl control = MockControl.createControl(TraxUtils.ResultCallback.class);
244 TraxUtils.ResultCallback mock = (TraxUtils.ResultCallback) control.getMock();
245 mock.domResult(document);
246 control.replay();
247
248 TraxUtils.doWithResult(new DOMResult(document), mock);
249
250 control.verify();
251 }
252
253 public void testDoWithSaxSource() throws Exception {
254 XMLReader reader = XMLReaderFactory.createXMLReader();
255 InputSource inputSource = new InputSource();
256
257 MockControl control = MockControl.createControl(TraxUtils.SourceCallback.class);
258 TraxUtils.SourceCallback mock = (TraxUtils.SourceCallback) control.getMock();
259 mock.saxSource(reader, inputSource);
260 control.replay();
261
262 TraxUtils.doWithSource(new SAXSource(reader, inputSource), mock);
263
264 control.verify();
265 }
266
267 public void testDoWithSaxResult() throws Exception {
268 ContentHandler contentHandler = new DefaultHandler();
269 LexicalHandler lexicalHandler = new DefaultHandler2();
270
271 MockControl control = MockControl.createControl(TraxUtils.ResultCallback.class);
272 TraxUtils.ResultCallback mock = (TraxUtils.ResultCallback) control.getMock();
273 mock.saxResult(contentHandler, lexicalHandler);
274 control.replay();
275
276 SAXResult result = new SAXResult(contentHandler);
277 result.setLexicalHandler(lexicalHandler);
278 TraxUtils.doWithResult(result, mock);
279
280 control.verify();
281 }
282
283 public void testDoWithStaxSourceEventReader() throws Exception {
284 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
285 XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader("<element/>"));
286
287 MockControl control = MockControl.createControl(TraxUtils.SourceCallback.class);
288 TraxUtils.SourceCallback mock = (TraxUtils.SourceCallback) control.getMock();
289 mock.staxSource(eventReader);
290 control.replay();
291
292 TraxUtils.doWithSource(new StaxSource(eventReader), mock);
293
294 control.verify();
295 }
296
297 public void testDoWithStaxResultEventWriter() throws Exception {
298 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
299 XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new StringWriter());
300
301 MockControl control = MockControl.createControl(TraxUtils.ResultCallback.class);
302 TraxUtils.ResultCallback mock = (TraxUtils.ResultCallback) control.getMock();
303 mock.staxResult(eventWriter);
304 control.replay();
305
306 TraxUtils.doWithResult(new StaxResult(eventWriter), mock);
307
308 control.verify();
309 }
310
311 public void testDoWithStaxSourceStreamReader() throws Exception {
312 XMLInputFactory inputFactory = XMLInputFactory.newInstance();
313 XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader("<element/>"));
314
315 MockControl control = MockControl.createControl(TraxUtils.SourceCallback.class);
316 TraxUtils.SourceCallback mock = (TraxUtils.SourceCallback) control.getMock();
317 mock.staxSource(streamReader);
318 control.replay();
319
320 TraxUtils.doWithSource(new StaxSource(streamReader), mock);
321
322 control.verify();
323 }
324
325 public void testDoWithStaxResultStreamWriter() throws Exception {
326 XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
327 XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
328
329 MockControl control = MockControl.createControl(TraxUtils.ResultCallback.class);
330 TraxUtils.ResultCallback mock = (TraxUtils.ResultCallback) control.getMock();
331 mock.staxResult(streamWriter);
332 control.replay();
333
334 TraxUtils.doWithResult(new StaxResult(streamWriter), mock);
335
336 control.verify();
337 }
338
339 public void testDoWithStreamSourceInputStream() throws Exception {
340 byte[] xml = "<element/>".getBytes("UTF-8");
341 InputStream inputStream = new ByteArrayInputStream(xml);
342
343 MockControl control = MockControl.createControl(TraxUtils.SourceCallback.class);
344 TraxUtils.SourceCallback mock = (TraxUtils.SourceCallback) control.getMock();
345 mock.streamSource(inputStream);
346 control.replay();
347
348 TraxUtils.doWithSource(new StreamSource(inputStream), mock);
349
350 control.verify();
351 }
352
353 public void testDoWithStreamResultOutputStream() throws Exception {
354 OutputStream outputStream = new ByteArrayOutputStream();
355
356 MockControl control = MockControl.createControl(TraxUtils.ResultCallback.class);
357 TraxUtils.ResultCallback mock = (TraxUtils.ResultCallback) control.getMock();
358 mock.streamResult(outputStream);
359 control.replay();
360
361 TraxUtils.doWithResult(new StreamResult(outputStream), mock);
362
363 control.verify();
364 }
365
366 public void testDoWithStreamSourceReader() throws Exception {
367 String xml = "<element/>";
368 Reader reader = new StringReader(xml);
369
370 MockControl control = MockControl.createControl(TraxUtils.SourceCallback.class);
371 TraxUtils.SourceCallback mock = (TraxUtils.SourceCallback) control.getMock();
372 mock.streamSource(reader);
373 control.replay();
374
375 TraxUtils.doWithSource(new StreamSource(reader), mock);
376
377 control.verify();
378 }
379
380 public void testDoWithStreamResultWriter() throws Exception {
381 Writer writer = new StringWriter();
382
383 MockControl control = MockControl.createControl(TraxUtils.ResultCallback.class);
384 TraxUtils.ResultCallback mock = (TraxUtils.ResultCallback) control.getMock();
385 mock.streamResult(writer);
386 control.replay();
387
388 TraxUtils.doWithResult(new StreamResult(writer), mock);
389
390 control.verify();
391 }
392
393 public void testDoWithInvalidSource() throws Exception {
394 Source source = new Source() {
395
396 public void setSystemId(String systemId) {
397 }
398
399 public String getSystemId() {
400 return null;
401 }
402 };
403
404 try {
405 TraxUtils.doWithSource(source, null);
406 fail("IllegalArgumentException expected");
407 }
408 catch (IllegalArgumentException ex) {
409
410 }
411 }
412
413 public void testDoWithInvalidResult() throws Exception {
414 Result result = new Result() {
415
416 public void setSystemId(String systemId) {
417 }
418
419 public String getSystemId() {
420 return null;
421 }
422 };
423
424 try {
425 TraxUtils.doWithResult(result, null);
426 fail("IllegalArgumentException expected");
427 }
428 catch (IllegalArgumentException ex) {
429
430 }
431 }
432 }