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.ws.soap.axiom;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.StringReader;
21  
22  import org.apache.axiom.om.OMAbstractFactory;
23  import org.apache.axiom.om.OMDocument;
24  import org.apache.axiom.om.OMElement;
25  import org.apache.axiom.om.OMFactory;
26  import org.apache.axiom.om.OMNamespace;
27  import org.custommonkey.xmlunit.XMLTestCase;
28  import org.xml.sax.InputSource;
29  import org.xml.sax.XMLReader;
30  import org.xml.sax.helpers.XMLReaderFactory;
31  
32  public class AxiomHandlerTest extends XMLTestCase {
33  
34      private static final String XML_1 =
35              "<?xml version='1.0' encoding='UTF-8'?>" + "<?pi content?>" + "<root xmlns='namespace'>" +
36                      "<prefix:child xmlns:prefix='namespace2' xmlns:prefix2='namespace3' prefix2:attr='value'>content</prefix:child>" +
37                      "</root>";
38  
39      private static final String XML_2_EXPECTED =
40              "<?xml version='1.0' encoding='UTF-8'?>" + "<root xmlns='namespace'>" + "<child xmlns='namespace2' />" +
41                      "</root>";
42  
43      private static final String XML_2_SNIPPET =
44              "<?xml version='1.0' encoding='UTF-8'?>" + "<child xmlns='namespace2' />";
45  
46      private static final String XML_3_ENTITY =
47              "<predefined-entity-reference>&lt;&gt;&amp;&quot;&apos;</predefined-entity-reference>";
48  
49      private AxiomHandler handler;
50  
51      private OMDocument result;
52  
53      private XMLReader xmlReader;
54  
55      private OMFactory factory;
56  
57      protected void setUp() throws Exception {
58          factory = OMAbstractFactory.getOMFactory();
59          result = factory.createOMDocument();
60          xmlReader = XMLReaderFactory.createXMLReader();
61      }
62  
63      public void testContentHandlerDocumentNamespacePrefixes() throws Exception {
64          xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
65          handler = new AxiomHandler(result, factory);
66          xmlReader.setContentHandler(handler);
67          xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
68          xmlReader.parse(new InputSource(new StringReader(XML_1)));
69          ByteArrayOutputStream bos = new ByteArrayOutputStream();
70          result.serialize(bos);
71          assertXMLEqual("Invalid result", XML_1, bos.toString("UTF-8"));
72      }
73  
74      public void testContentHandlerDocumentNoNamespacePrefixes() throws Exception {
75          xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
76          handler = new AxiomHandler(result, factory);
77          xmlReader.setContentHandler(handler);
78          xmlReader.parse(new InputSource(new StringReader(XML_1)));
79          ByteArrayOutputStream bos = new ByteArrayOutputStream();
80          result.serialize(bos);
81          assertXMLEqual("Invalid result", XML_1, bos.toString("UTF-8"));
82      }
83  
84      public void testContentHandlerElement() throws Exception {
85          OMNamespace namespace = factory.createOMNamespace("namespace", "");
86          OMElement rootElement = factory.createOMElement("root", namespace, result);
87          handler = new AxiomHandler(rootElement, factory);
88          xmlReader.setContentHandler(handler);
89          xmlReader.parse(new InputSource(new StringReader(XML_2_SNIPPET)));
90          ByteArrayOutputStream bos = new ByteArrayOutputStream();
91          result.serialize(bos);
92          assertXMLEqual("Invalid result", XML_2_EXPECTED, bos.toString("UTF-8"));
93      }
94  
95      public void testContentHandlerPredefinedEntityReference() throws Exception {
96          handler = new AxiomHandler(result, factory);
97          xmlReader.setContentHandler(handler);
98          xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
99          xmlReader.parse(new InputSource(new StringReader(XML_3_ENTITY)));
100         ByteArrayOutputStream bos = new ByteArrayOutputStream();
101         result.serialize(bos);
102         assertXMLEqual("Invalid result", XML_3_ENTITY, bos.toString("UTF-8"));
103     }
104 }