1   /*
2    * Copyright 2005-2012 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  import java.util.Iterator;
22  
23  import org.apache.axiom.om.OMAbstractFactory;
24  import org.apache.axiom.om.OMDocument;
25  import org.apache.axiom.om.OMElement;
26  import org.apache.axiom.om.OMFactory;
27  import org.apache.axiom.om.OMNamespace;
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.xml.sax.InputSource;
31  import org.xml.sax.XMLReader;
32  import org.xml.sax.helpers.XMLReaderFactory;
33  
34  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
35  import static org.junit.Assert.assertEquals;
36  import static org.junit.Assert.assertTrue;
37  
38  public class AxiomHandlerTest {
39  
40      private static final String XML_1 =
41              "<?xml version='1.0' encoding='UTF-8'?>" + "<?pi content?>" + "<root xmlns='namespace'>" +
42                      "<prefix:child xmlns:prefix='namespace2' xmlns:prefix2='namespace3' prefix2:attr='value'>content</prefix:child>" +
43                      "</root>";
44  
45      private static final String XML_2_EXPECTED =
46              "<?xml version='1.0' encoding='UTF-8'?>" + "<root xmlns='namespace'>" + "<child xmlns='namespace2' />" +
47                      "</root>";
48  
49      private static final String XML_2_SNIPPET =
50              "<?xml version='1.0' encoding='UTF-8'?>" + "<child xmlns='namespace2' />";
51  
52      private static final String XML_3_ENTITY =
53              "<predefined-entity-reference>&lt;&gt;&amp;&quot;&apos;</predefined-entity-reference>";
54  
55      private static final String XML_4_SNIPPET = "<?xml version='1.0' encoding='UTF-8'?>" + "<child xmlns='namespace1' />";
56      
57      private static final String XML_5_SNIPPET = "<?xml version='1.0' encoding='UTF-8'?>" + "<x:child xmlns:x='namespace1' />";
58  
59      private AxiomHandler handler;
60  
61      private OMDocument result;
62  
63      private XMLReader xmlReader;
64  
65      private OMFactory factory;
66  
67      @Before
68      public void setUp() throws Exception {
69          factory = OMAbstractFactory.getOMFactory();
70          result = factory.createOMDocument();
71          xmlReader = XMLReaderFactory.createXMLReader();
72      }
73  
74      @Test
75      public void testContentHandlerDocumentNamespacePrefixes() throws Exception {
76          xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
77          handler = new AxiomHandler(result, factory);
78          xmlReader.setContentHandler(handler);
79          xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
80          xmlReader.parse(new InputSource(new StringReader(XML_1)));
81          ByteArrayOutputStream bos = new ByteArrayOutputStream();
82          result.serialize(bos);
83          assertXMLEqual("Invalid result", XML_1, bos.toString("UTF-8"));
84      }
85  
86      @Test
87      public void testContentHandlerDocumentNoNamespacePrefixes() throws Exception {
88          xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
89          handler = new AxiomHandler(result, factory);
90          xmlReader.setContentHandler(handler);
91          xmlReader.parse(new InputSource(new StringReader(XML_1)));
92          ByteArrayOutputStream bos = new ByteArrayOutputStream();
93          result.serialize(bos);
94          assertXMLEqual("Invalid result", XML_1, bos.toString("UTF-8"));
95      }
96  
97      @Test
98      public void testContentHandlerElement() throws Exception {
99          OMNamespace namespace = factory.createOMNamespace("namespace", "");
100         OMElement rootElement = factory.createOMElement("root", namespace, result);
101         handler = new AxiomHandler(rootElement, factory);
102         xmlReader.setContentHandler(handler);
103         xmlReader.parse(new InputSource(new StringReader(XML_2_SNIPPET)));
104         ByteArrayOutputStream bos = new ByteArrayOutputStream();
105         result.serialize(bos);
106         assertXMLEqual("Invalid result", XML_2_EXPECTED, bos.toString("UTF-8"));
107     }
108     
109     @Test
110     public void testContentHandlerElementWithSamePrefixAndDifferentNamespace() throws Exception {
111         OMNamespace namespace = factory.createOMNamespace("namespace1", "");
112         OMElement rootElement = factory.createOMElement("root", namespace, result);
113         handler = new AxiomHandler(rootElement, factory);
114         xmlReader.setContentHandler(handler);
115         xmlReader.parse(new InputSource(new StringReader(XML_2_SNIPPET)));
116         Iterator<?> it = result.getOMDocumentElement().getChildrenWithLocalName("child");
117         assertTrue(it.hasNext());
118         OMElement child = (OMElement) it.next();
119         assertEquals("", child.getQName().getPrefix());
120         assertEquals("namespace2", child.getQName().getNamespaceURI());
121     }
122 
123     @Test
124     public void testContentHandlerElementWithSameNamespacesAndPrefix() throws Exception {
125         OMNamespace namespace = factory.createOMNamespace("namespace1", "");
126         OMElement rootElement = factory.createOMElement("root", namespace, result);
127         handler = new AxiomHandler(rootElement, factory);
128         xmlReader.setContentHandler(handler);
129         xmlReader.parse(new InputSource(new StringReader(XML_4_SNIPPET)));
130         Iterator<?> it = result.getOMDocumentElement().getChildrenWithLocalName("child");
131         assertTrue(it.hasNext());
132         OMElement child = (OMElement) it.next();
133         assertEquals("", child.getQName().getPrefix());
134         assertEquals("namespace1", child.getQName().getNamespaceURI());
135     }
136 
137     @Test
138     public void testContentHandlerElementWithSameNamespacesAndDifferentPrefix() throws Exception {
139         OMNamespace namespace = factory.createOMNamespace("namespace1", "");
140         OMElement rootElement = factory.createOMElement("root", namespace, result);
141         handler = new AxiomHandler(rootElement, factory);
142         xmlReader.setContentHandler(handler);
143         xmlReader.parse(new InputSource(new StringReader(XML_5_SNIPPET)));
144         Iterator<?> it = result.getOMDocumentElement().getChildrenWithLocalName("child");
145         assertTrue(it.hasNext());
146         OMElement child = (OMElement) it.next();
147         assertEquals("x", child.getQName().getPrefix());
148         assertEquals("namespace1", child.getQName().getNamespaceURI());
149     }
150 
151     @Test
152     public void testContentHandlerPredefinedEntityReference() throws Exception {
153         handler = new AxiomHandler(result, factory);
154         xmlReader.setContentHandler(handler);
155         xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
156         xmlReader.parse(new InputSource(new StringReader(XML_3_ENTITY)));
157         ByteArrayOutputStream bos = new ByteArrayOutputStream();
158         result.serialize(bos);
159         assertXMLEqual("Invalid result", XML_3_ENTITY, bos.toString("UTF-8"));
160     }
161 }