1   /*
2    * Copyright 2006 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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.newInstance();
39          transformer = transformerFactory.newTransformer();
40          inputFactory = XMLOutputFactory.newInstance();
41      }
42  
43      public void testStreamWriterSource() throws Exception {
44          StringWriter stringWriter = new StringWriter();
45          XMLStreamWriter streamWriter = inputFactory.createXMLStreamWriter(stringWriter);
46          Source source = new StringSource(XML);
47          StaxResult result = new StaxResult(streamWriter);
48          assertEquals("Invalid streamWriter returned", streamWriter, result.getXMLStreamWriter());
49          assertNull("EventWriter returned", result.getXMLEventWriter());
50          transformer.transform(source, result);
51          assertXMLEqual("Invalid result", XML, stringWriter.toString());
52      }
53  
54      public void testEventWriterSource() throws Exception {
55          StringWriter stringWriter = new StringWriter();
56          XMLEventWriter eventWriter = inputFactory.createXMLEventWriter(stringWriter);
57          Source source = new StringSource(XML);
58          StaxResult result = new StaxResult(eventWriter);
59          assertEquals("Invalid eventWriter returned", eventWriter, result.getXMLEventWriter());
60          assertNull("StreamWriter returned", result.getXMLStreamWriter());
61          transformer.transform(source, result);
62          assertXMLEqual("Invalid result", XML, stringWriter.toString());
63      }
64  
65  }