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