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 java.io.StringWriter;
21  import java.io.Writer;
22  import javax.xml.stream.XMLStreamException;
23  
24  import org.custommonkey.xmlunit.XMLTestCase;
25  import org.xml.sax.InputSource;
26  import org.xml.sax.XMLReader;
27  import org.xml.sax.helpers.XMLReaderFactory;
28  
29  public abstract class AbstractStaxContentHandlerTestCase extends XMLTestCase {
30  
31      private static final String XML_CONTENT_HANDLER =
32              "<?xml version='1.0' encoding='UTF-8'?><?pi content?><root xmlns='namespace'><prefix:child xmlns:prefix='namespace2' prefix:attr='value'>content</prefix:child></root>";
33  
34      private XMLReader xmlReader;
35  
36      protected void setUp() throws Exception {
37          xmlReader = XMLReaderFactory.createXMLReader();
38      }
39  
40      public void testContentHandler() throws Exception {
41          StringWriter stringWriter = new StringWriter();
42          AbstractStaxContentHandler handler = createStaxContentHandler(stringWriter);
43          xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
44          xmlReader.setContentHandler(handler);
45          xmlReader.parse(new InputSource(new StringReader(XML_CONTENT_HANDLER)));
46          assertXMLEqual("Invalid result", XML_CONTENT_HANDLER, stringWriter.toString());
47      }
48  
49      public void testContentHandlerNamespacePrefixes() throws Exception {
50          StringWriter stringWriter = new StringWriter();
51          AbstractStaxContentHandler handler = createStaxContentHandler(stringWriter);
52          xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
53          xmlReader.setContentHandler(handler);
54          xmlReader.parse(new InputSource(new StringReader(XML_CONTENT_HANDLER)));
55          assertXMLEqual("Invalid result", XML_CONTENT_HANDLER, stringWriter.toString());
56      }
57  
58      protected abstract AbstractStaxContentHandler createStaxContentHandler(Writer writer) throws XMLStreamException;
59  
60  
61  }