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.ByteArrayInputStream;
20  import java.io.InputStream;
21  import javax.xml.transform.Transformer;
22  import javax.xml.transform.TransformerException;
23  import javax.xml.transform.TransformerFactory;
24  
25  import org.springframework.ws.InvalidXmlException;
26  import org.springframework.ws.WebServiceMessage;
27  import org.springframework.ws.WebServiceMessageFactory;
28  import org.springframework.ws.soap.soap11.AbstractSoap11MessageFactoryTestCase;
29  import org.springframework.ws.transport.MockTransportInputStream;
30  import org.springframework.ws.transport.TransportInputStream;
31  import org.springframework.xml.transform.StringResult;
32  
33  import org.custommonkey.xmlunit.XMLAssert;
34  import org.custommonkey.xmlunit.XMLUnit;
35  import org.junit.Test;
36  
37  import static org.junit.Assert.assertEquals;
38  import static org.junit.Assert.fail;
39  
40  public class AxiomSoap11MessageFactoryTest extends AbstractSoap11MessageFactoryTestCase {
41  
42      private Transformer transformer;
43  
44      @Override
45      protected WebServiceMessageFactory createMessageFactory() throws Exception {
46          transformer = TransformerFactory.newInstance().newTransformer();
47  
48          AxiomSoapMessageFactory factory = new AxiomSoapMessageFactory();
49          factory.afterPropertiesSet();
50          return factory;
51      }
52  
53      @Override
54      public void testCreateSoapMessageIllFormedXml() throws Exception {
55          // Axiom parses the contents of XML lazily, so it will not throw an InvalidXmlException when a message is parsed
56          throw new InvalidXmlException(null, null);
57      }
58  
59      @Test
60      public void testGetCharsetEncoding() {
61          AxiomSoapMessageFactory messageFactory = new AxiomSoapMessageFactory();
62  
63          assertEquals("Invalid charset", "utf-8", messageFactory.getCharSetEncoding("text/html; charset=utf-8"));
64          assertEquals("Invalid charset", "utf-8", messageFactory.getCharSetEncoding("application/xop+xml;type=text/xml; charset=utf-8"));
65          assertEquals("Invalid charset", "utf-8", messageFactory.getCharSetEncoding("application/xop+xml;type=\"text/xml; charset=utf-8\""));
66      }
67  
68      @Test
69      public void testRepetitiveReadCaching() throws Exception {
70          AxiomSoapMessageFactory messageFactory = new AxiomSoapMessageFactory();
71          messageFactory.setPayloadCaching(true);
72          messageFactory.afterPropertiesSet();
73  
74          String xml = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'><soapenv:Body>" +
75                  "<root xmlns='http://springframework.org/spring-ws'><child /></root>" +
76                  "</soapenv:Body></soapenv:Envelope>";
77          TransportInputStream tis = new MockTransportInputStream(new ByteArrayInputStream(xml.getBytes()));
78          WebServiceMessage message = messageFactory.createWebServiceMessage(tis);
79  
80          StringResult result = new StringResult();
81          transformer.transform(message.getPayloadSource(), result);
82          transformer.transform(message.getPayloadSource(), result);
83      }
84  
85      @Test
86      public void testRepetitiveReadNoCaching() throws Exception {
87          AxiomSoapMessageFactory messageFactory = new AxiomSoapMessageFactory();
88          messageFactory.setPayloadCaching(false);
89          messageFactory.afterPropertiesSet();
90  
91          String xml = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'><soapenv:Body>" +
92                  "<root xmlns='http://springframework.org/spring-ws'><child /></root>" +
93                  "</soapenv:Body></soapenv:Envelope>";
94          TransportInputStream tis = new MockTransportInputStream(new ByteArrayInputStream(xml.getBytes()));
95          WebServiceMessage message = messageFactory.createWebServiceMessage(tis);
96  
97          StringResult result = new StringResult();
98          transformer.transform(message.getPayloadSource(), result);
99          try {
100             transformer.transform(message.getPayloadSource(), result);
101             fail("TransformerException expected");
102         }
103         catch (TransformerException expected) {
104             // ignore
105         }
106     }
107 
108     /**
109      * See http://jira.springframework.org/browse/SWS-502
110      */
111     @Test
112     public void testSWS502() throws Exception {
113         AxiomSoapMessageFactory messageFactory = new AxiomSoapMessageFactory();
114         messageFactory.setPayloadCaching(false);
115         messageFactory.afterPropertiesSet();
116 
117         String envelope =
118                 "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><soapenv:Body>" +
119                         "<ns1:sendMessageResponse xmlns:ns1='urn:Sole' soapenv:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>" +
120                         "<sendMessageReturn xsi:type='soapenc:string' xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'>" +
121                         "<![CDATA[<?xml version='1.0' encoding='UTF-8'?>" + "<PDresponse>" +
122                         "<isStatusOK>true</isStatusOK>" + "<status>0</status>" +
123                         "<payLoad><![CDATA[<?xml version='1.0' encoding='UTF-8'?><response>ok</response>]]]]>><![CDATA[</payLoad>" +
124                         "</PDresponse>]]></sendMessageReturn>" + "</ns1:sendMessageResponse>" +
125                         "</soapenv:Body></soapenv:Envelope>";
126 
127         InputStream inputStream = new ByteArrayInputStream(envelope.getBytes("UTF-8"));
128         AxiomSoapMessage message = messageFactory.createWebServiceMessage(new MockTransportInputStream(inputStream));
129 
130         StringResult result = new StringResult();
131         transformer.transform(message.getPayloadSource(), result);
132 
133         XMLUnit.setIgnoreWhitespace(true);
134         String expectedPayload =
135                 "<ns1:sendMessageResponse xmlns:ns1='urn:Sole' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' soapenv:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>" +
136                         "<sendMessageReturn xsi:type='soapenc:string' xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'>" +
137                         "<![CDATA[<?xml version='1.0' encoding='UTF-8'?>" + "<PDresponse>" +
138                         "<isStatusOK>true</isStatusOK>" + "<status>0</status>" +
139                         "<payLoad><![CDATA[<?xml version='1.0' encoding='UTF-8'?><response>ok</response>]]]]>><![CDATA[</payLoad>" +
140                         "</PDresponse>]]></sendMessageReturn>" + "</ns1:sendMessageResponse>";
141         XMLAssert.assertXMLEqual(expectedPayload, result.toString());
142 
143     }
144 
145 }