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 = "<?xml version='1.0' encoding='UTF-8'?>" + "<?pi content?>" +
35              "<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 = "<?xml version='1.0' encoding='UTF-8'?>" + "<root xmlns='namespace'>" +
40              "<child xmlns='namespace2' />" + "</root>";
41  
42      private static final String XML_2_SNIPPET =
43              "<?xml version='1.0' encoding='UTF-8'?>" + "<child xmlns='namespace2' />";
44  
45      private AxiomHandler handler;
46  
47      private OMDocument result;
48  
49      private XMLReader xmlReader;
50  
51      private OMFactory factory;
52  
53      protected void setUp() throws Exception {
54          factory = OMAbstractFactory.getOMFactory();
55          result = factory.createOMDocument();
56          xmlReader = XMLReaderFactory.createXMLReader();
57      }
58  
59      public void testContentHandlerDocumentNamespacePrefixes() throws Exception {
60          xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
61          handler = new AxiomHandler(result, factory);
62          xmlReader.setContentHandler(handler);
63          xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
64          xmlReader.parse(new InputSource(new StringReader(XML_1)));
65          ByteArrayOutputStream bos = new ByteArrayOutputStream();
66          result.serialize(bos);
67          assertXMLEqual("Invalid result", XML_1, bos.toString("UTF-8"));
68      }
69  
70      public void testContentHandlerDocumentNoNamespacePrefixes() throws Exception {
71          xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
72          handler = new AxiomHandler(result, factory);
73          xmlReader.setContentHandler(handler);
74          xmlReader.parse(new InputSource(new StringReader(XML_1)));
75          ByteArrayOutputStream bos = new ByteArrayOutputStream();
76          result.serialize(bos);
77          assertXMLEqual("Invalid result", XML_1, bos.toString("UTF-8"));
78      }
79  
80      public void testContentHandlerElement() throws Exception {
81          OMNamespace namespace = factory.createOMNamespace("namespace", "");
82          OMElement rootElement = factory.createOMElement("root", namespace, result);
83          handler = new AxiomHandler(rootElement, factory);
84          xmlReader.setContentHandler(handler);
85          xmlReader.parse(new InputSource(new StringReader(XML_2_SNIPPET)));
86          ByteArrayOutputStream bos = new ByteArrayOutputStream();
87          result.serialize(bos);
88          assertXMLEqual("Invalid result", XML_2_EXPECTED, bos.toString("UTF-8"));
89      }
90  }