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.stream;
18  
19  import java.io.StringReader;
20  import javax.xml.stream.XMLEventReader;
21  import javax.xml.stream.XMLInputFactory;
22  import javax.xml.transform.Transformer;
23  import javax.xml.transform.TransformerFactory;
24  
25  import org.custommonkey.xmlunit.XMLTestCase;
26  import org.springframework.xml.transform.StaxSource;
27  import org.springframework.xml.transform.StringResult;
28  
29  public class XmlEventStreamReaderTest extends XMLTestCase {
30  
31      private static final String XML =
32              "<?pi content?><root xmlns='namespace'><prefix:child xmlns:prefix='namespace2'>content</prefix:child></root>";
33  
34      private XmlEventStreamReader streamReader;
35  
36      protected void setUp() throws Exception {
37          XMLInputFactory inputFactory = XMLInputFactory.newInstance();
38          XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
39          streamReader = new XmlEventStreamReader(eventReader);
40      }
41  
42      public void testReadAll() throws Exception {
43          while (streamReader.hasNext()) {
44              streamReader.next();
45          }
46      }
47  
48      public void testReadCorrect() throws Exception {
49          Transformer transformer = TransformerFactory.newInstance().newTransformer();
50          StaxSource source = new StaxSource(streamReader);
51          StringResult result = new StringResult();
52          transformer.transform(source, result);
53          assertXMLEqual(XML, result.toString());
54      }
55  
56  }