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.dom;
18  
19  import java.io.StringReader;
20  import javax.xml.parsers.DocumentBuilder;
21  import javax.xml.parsers.DocumentBuilderFactory;
22  
23  import org.custommonkey.xmlunit.XMLTestCase;
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  import org.xml.sax.InputSource;
27  import org.xml.sax.XMLReader;
28  import org.xml.sax.helpers.XMLReaderFactory;
29  
30  public class DomContentHandlerTest extends XMLTestCase {
31  
32      private static final String XML_1 = "<?xml version='1.0' encoding='UTF-8'?>" + "<?pi content?>" +
33              "<root xmlns='namespace'>" +
34              "<prefix:child xmlns:prefix='namespace2' xmlns:prefix2='namespace3' prefix2:attr='value'>content</prefix:child>" +
35              "</root>";
36  
37      private static final String XML_2_EXPECTED = "<?xml version='1.0' encoding='UTF-8'?>" + "<root xmlns='namespace'>" +
38              "<child xmlns='namespace2' />" + "</root>";
39  
40      private static final String XML_2_SNIPPET =
41              "<?xml version='1.0' encoding='UTF-8'?>" + "<child xmlns='namespace2' />";
42  
43      private Document expected;
44  
45      private DomContentHandler handler;
46  
47      private Document result;
48  
49      private XMLReader xmlReader;
50  
51      private DocumentBuilder documentBuilder;
52  
53      protected void setUp() throws Exception {
54          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
55          documentBuilderFactory.setNamespaceAware(true);
56          documentBuilder = documentBuilderFactory.newDocumentBuilder();
57          result = documentBuilder.newDocument();
58          xmlReader = XMLReaderFactory.createXMLReader();
59      }
60  
61      public void testContentHandlerDocumentNamespacePrefixes() throws Exception {
62          xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
63          handler = new DomContentHandler(result);
64          expected = documentBuilder.parse(new InputSource(new StringReader(XML_1)));
65          xmlReader.setContentHandler(handler);
66          xmlReader.parse(new InputSource(new StringReader(XML_1)));
67          assertXMLEqual("Invalid result", expected, result);
68      }
69  
70      public void testContentHandlerDocumentNoNamespacePrefixes() throws Exception {
71          handler = new DomContentHandler(result);
72          expected = documentBuilder.parse(new InputSource(new StringReader(XML_1)));
73          xmlReader.setContentHandler(handler);
74          xmlReader.parse(new InputSource(new StringReader(XML_1)));
75          assertXMLEqual("Invalid result", expected, result);
76      }
77  
78      public void testContentHandlerElement() throws Exception {
79          Element rootElement = result.createElementNS("namespace", "root");
80          result.appendChild(rootElement);
81          handler = new DomContentHandler(rootElement);
82          expected = documentBuilder.parse(new InputSource(new StringReader(XML_2_EXPECTED)));
83          xmlReader.setContentHandler(handler);
84          xmlReader.parse(new InputSource(new StringReader(XML_2_SNIPPET)));
85          assertXMLEqual("Invalid result", expected, result);
86  
87      }
88  }