01: /*
02: * Copyright 2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.xml.transform;
18:
19: import java.io.StringWriter;
20: import javax.xml.stream.XMLEventWriter;
21: import javax.xml.stream.XMLOutputFactory;
22: import javax.xml.stream.XMLStreamWriter;
23: import javax.xml.transform.Source;
24: import javax.xml.transform.Transformer;
25: import javax.xml.transform.TransformerFactory;
26:
27: import org.custommonkey.xmlunit.XMLTestCase;
28:
29: public class StaxResultTest extends XMLTestCase {
30:
31: private static final String XML = "<root xmlns='namespace'><child/></root>";
32:
33: private Transformer transformer;
34:
35: private XMLOutputFactory inputFactory;
36:
37: protected void setUp() throws Exception {
38: TransformerFactory transformerFactory = TransformerFactory
39: .newInstance();
40: transformer = transformerFactory.newTransformer();
41: inputFactory = XMLOutputFactory.newInstance();
42: }
43:
44: public void testStreamWriterSource() throws Exception {
45: StringWriter stringWriter = new StringWriter();
46: XMLStreamWriter streamWriter = inputFactory
47: .createXMLStreamWriter(stringWriter);
48: Source source = new StringSource(XML);
49: StaxResult result = new StaxResult(streamWriter);
50: assertEquals("Invalid streamWriter returned", streamWriter,
51: result.getXMLStreamWriter());
52: assertNull("EventWriter returned", result.getXMLEventWriter());
53: transformer.transform(source, result);
54: assertXMLEqual("Invalid result", XML, stringWriter.toString());
55: }
56:
57: public void testEventWriterSource() throws Exception {
58: StringWriter stringWriter = new StringWriter();
59: XMLEventWriter eventWriter = inputFactory
60: .createXMLEventWriter(stringWriter);
61: Source source = new StringSource(XML);
62: StaxResult result = new StaxResult(eventWriter);
63: assertEquals("Invalid eventWriter returned", eventWriter,
64: result.getXMLEventWriter());
65: assertNull("StreamWriter returned", result.getXMLStreamWriter());
66: transformer.transform(source, result);
67: assertXMLEqual("Invalid result", XML, stringWriter.toString());
68: }
69:
70: }
|