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          xmlReader = XMLReaderFactory.createXMLReader();
55          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
56          documentBuilderFactory.setNamespaceAware(true);
57          documentBuilder = documentBuilderFactory.newDocumentBuilder();
58          result = documentBuilder.newDocument();
59          xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
60      }
61  
62      public void testContentHandlerDocument() throws Exception {
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 testContentHandlerElement() throws Exception {
71          Element rootElement = result.createElementNS("namespace", "root");
72          result.appendChild(rootElement);
73          handler = new DomContentHandler(rootElement);
74          expected = documentBuilder.parse(new InputSource(new StringReader(XML_2_EXPECTED)));
75          xmlReader.setContentHandler(handler);
76          xmlReader.parse(new InputSource(new StringReader(XML_2_SNIPPET)));
77          assertXMLEqual("Invalid result", expected, result);
78  
79      }
80  }