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