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.ws.soap.axiom.support;
18  
19  import java.io.StringWriter;
20  import java.util.Locale;
21  import javax.xml.namespace.QName;
22  import javax.xml.parsers.DocumentBuilder;
23  import javax.xml.parsers.DocumentBuilderFactory;
24  import javax.xml.stream.XMLInputFactory;
25  import javax.xml.stream.XMLStreamReader;
26  
27  import org.springframework.core.io.ClassPathResource;
28  import org.springframework.core.io.Resource;
29  import org.springframework.util.FileCopyUtils;
30  import org.springframework.xml.sax.SaxUtils;
31  
32  import org.apache.axiom.om.OMAbstractFactory;
33  import org.apache.axiom.om.OMElement;
34  import org.apache.axiom.om.OMFactory;
35  import org.apache.axiom.om.OMNamespace;
36  import org.apache.axiom.soap.SOAP11Constants;
37  import org.apache.axiom.soap.SOAPEnvelope;
38  import org.apache.axiom.soap.SOAPMessage;
39  import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
40  import org.custommonkey.xmlunit.XMLUnit;
41  import org.junit.Assert;
42  import org.junit.Before;
43  import org.junit.Test;
44  import org.w3c.dom.Document;
45  
46  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
47  
48  public class AxiomUtilsTest {
49  
50      private OMElement element;
51  
52      @Before
53      public void setUp() throws Exception {
54          OMFactory factory = OMAbstractFactory.getOMFactory();
55          OMNamespace namespace = factory.createOMNamespace("http://www.springframework.org", "prefix");
56          element = factory.createOMElement("element", namespace);
57          XMLUnit.setIgnoreWhitespace(true);
58      }
59  
60      @Test
61      public void testToNamespaceDeclared() throws Exception {
62          QName qName = new QName(element.getNamespace().getNamespaceURI(), "localPart");
63          OMNamespace namespace = AxiomUtils.toNamespace(qName, element);
64          Assert.assertNotNull("Invalid namespace", namespace);
65          Assert.assertEquals("Invalid namespace", qName.getNamespaceURI(), namespace.getNamespaceURI());
66      }
67  
68      @Test
69      public void testToNamespaceUndeclared() throws Exception {
70          QName qName = new QName("http://www.example.com", "localPart");
71          OMNamespace namespace = AxiomUtils.toNamespace(qName, element);
72          Assert.assertNotNull("Invalid namespace", namespace);
73          Assert.assertEquals("Invalid namespace", qName.getNamespaceURI(), namespace.getNamespaceURI());
74          Assert.assertFalse("Invalid prefix", "prefix".equals(namespace.getPrefix()));
75      }
76  
77      @Test
78      public void testToNamespacePrefixDeclared() throws Exception {
79          QName qName = new QName(element.getNamespace().getNamespaceURI(), "localPart", "prefix");
80          OMNamespace namespace = AxiomUtils.toNamespace(qName, element);
81          Assert.assertNotNull("Invalid namespace", namespace);
82          Assert.assertEquals("Invalid namespace", qName.getNamespaceURI(), namespace.getNamespaceURI());
83          Assert.assertEquals("Invalid prefix", "prefix", namespace.getPrefix());
84      }
85  
86      @Test
87      public void testToNamespacePrefixUndeclared() throws Exception {
88          QName qName = new QName("http://www.example.com", "localPart", "otherPrefix");
89          OMNamespace namespace = AxiomUtils.toNamespace(qName, element);
90          Assert.assertNotNull("Invalid namespace", namespace);
91          Assert.assertEquals("Invalid namespace", qName.getNamespaceURI(), namespace.getNamespaceURI());
92          Assert.assertEquals("Invalid prefix", qName.getPrefix(), namespace.getPrefix());
93      }
94  
95      @Test
96      public void testToLanguage() throws Exception {
97          Assert.assertEquals("Invalid conversion", "fr-CA", AxiomUtils.toLanguage(Locale.CANADA_FRENCH));
98          Assert.assertEquals("Invalid conversion", "en", AxiomUtils.toLanguage(Locale.ENGLISH));
99      }
100 
101     @Test
102     public void testToLocale() throws Exception {
103         Assert.assertEquals("Invalid conversion", Locale.CANADA_FRENCH, AxiomUtils.toLocale("fr-CA"));
104         Assert.assertEquals("Invalid conversion", Locale.ENGLISH, AxiomUtils.toLocale("en"));
105     }
106 
107     @Test
108     @SuppressWarnings("Since15")
109     public void testToDocument() throws Exception {
110         Resource resource = new ClassPathResource("org/springframework/ws/soap/soap11/soap11.xml");
111 
112         DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
113         documentBuilderFactory.setNamespaceAware(true);
114         DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
115         Document expected = documentBuilder.parse(SaxUtils.createInputSource(resource));
116 
117         XMLInputFactory inputFactory = XMLInputFactory.newInstance();
118         XMLStreamReader reader = inputFactory.createXMLStreamReader(resource.getInputStream());
119         StAXSOAPModelBuilder builder =
120                 new StAXSOAPModelBuilder(reader, OMAbstractFactory.getSOAP11Factory(), SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
121         SOAPMessage soapMessage = builder.getSoapMessage();
122 
123         Document result = AxiomUtils.toDocument(soapMessage.getSOAPEnvelope());
124 
125         assertXMLEqual("Invalid document generated from SOAPEnvelope", expected, result);
126     }
127 
128     @Test
129     public void testToEnvelope() throws Exception {
130         Resource resource = new ClassPathResource("org/springframework/ws/soap/soap11/soap11.xml");
131 
132         byte[] buf = FileCopyUtils.copyToByteArray(resource.getFile());
133         String expected = new String(buf, "UTF-8");
134 
135         DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
136         documentBuilderFactory.setNamespaceAware(true);
137         DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
138         Document document = documentBuilder.parse(SaxUtils.createInputSource(resource));
139 
140         SOAPEnvelope envelope = AxiomUtils.toEnvelope(document);
141         StringWriter writer = new StringWriter();
142         envelope.serialize(writer);
143         String result = writer.toString();
144 
145         assertXMLEqual("Invalid SOAPEnvelope generated from document", expected, result);
146     }
147 }