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.soap11;
18  
19  import java.io.InputStream;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import org.springframework.ws.WebServiceMessage;
25  import org.springframework.ws.mime.Attachment;
26  import org.springframework.ws.soap.AbstractSoapMessageFactoryTestCase;
27  import org.springframework.ws.soap.SoapMessage;
28  import org.springframework.ws.soap.SoapVersion;
29  import org.springframework.ws.transport.MockTransportInputStream;
30  import org.springframework.ws.transport.TransportInputStream;
31  
32  import org.junit.Test;
33  
34  import static org.junit.Assert.*;
35  
36  public abstract class AbstractSoap11MessageFactoryTestCase extends AbstractSoapMessageFactoryTestCase {
37  
38      @Test
39      public void testCreateEmptySoap11Message() throws Exception {
40          WebServiceMessage message = messageFactory.createWebServiceMessage();
41          assertTrue("Not a SoapMessage", message instanceof SoapMessage);
42          SoapMessage soapMessage = (SoapMessage) message;
43          assertEquals("Invalid soap version", SoapVersion.SOAP_11, soapMessage.getVersion());
44      }
45  
46      @Override
47      public void testCreateSoapMessageNoAttachment() throws Exception {
48          InputStream is = AbstractSoap11MessageFactoryTestCase.class.getResourceAsStream("soap11.xml");
49          Map<String, String> headers = new HashMap<String, String>();
50          headers.put("Content-Type", "text/xml");
51          String soapAction = "\"http://springframework.org/spring-ws/Action\"";
52          headers.put("SOAPAction", soapAction);
53          TransportInputStream tis = new MockTransportInputStream(is, headers);
54  
55          WebServiceMessage message = messageFactory.createWebServiceMessage(tis);
56          assertTrue("Not a SoapMessage", message instanceof SoapMessage);
57          SoapMessage soapMessage = (SoapMessage) message;
58          assertEquals("Invalid soap version", SoapVersion.SOAP_11, soapMessage.getVersion());
59          assertEquals("Invalid soap action", soapAction, soapMessage.getSoapAction());
60          assertFalse("Message a XOP pacakge", soapMessage.isXopPackage());
61      }
62  
63      @Override
64      public void testCreateSoapMessageIllFormedXml() throws Exception {
65          InputStream is = AbstractSoap11MessageFactoryTestCase.class.getResourceAsStream("soap11-ill-formed.xml");
66          Map<String, String> headers = new HashMap<String, String>();
67          headers.put("Content-Type", "text/xml");
68          TransportInputStream tis = new MockTransportInputStream(is, headers);
69  
70          messageFactory.createWebServiceMessage(tis);
71      }
72  
73      @Override
74      public void testCreateSoapMessageSwA() throws Exception {
75          InputStream is = AbstractSoap11MessageFactoryTestCase.class.getResourceAsStream("soap11-attachment.bin");
76          Map<String, String> headers = new HashMap<String, String>();
77          headers.put("Content-Type",
78                  "multipart/related;" + "type=\"text/xml\";" + "boundary=\"----=_Part_0_11416420.1149699787554\"");
79          TransportInputStream tis = new MockTransportInputStream(is, headers);
80  
81          WebServiceMessage message = messageFactory.createWebServiceMessage(tis);
82          assertTrue("Not a SoapMessage", message instanceof SoapMessage);
83          SoapMessage soapMessage = (SoapMessage) message;
84          assertEquals("Invalid soap version", SoapVersion.SOAP_11, soapMessage.getVersion());
85          assertFalse("Message a XOP package", soapMessage.isXopPackage());
86          Iterator<Attachment> iter = soapMessage.getAttachments();
87          assertTrue("No attachments read", iter.hasNext());
88          Attachment attachment = soapMessage.getAttachment("interface21");
89          assertNotNull("No attachment read", attachment);
90          assertEquals("Invalid content id", "interface21", attachment.getContentId());
91      }
92  
93      @Override
94      public void testCreateSoapMessageMtom() throws Exception {
95          InputStream is = AbstractSoap11MessageFactoryTestCase.class.getResourceAsStream("soap11-mtom.bin");
96          Map<String, String> headers = new HashMap<String, String>();
97          headers.put("Content-Type", "multipart/related;" + "start-info=\"text/xml\";" +
98                  "type=\"application/xop+xml\";" + "start=\"<0.urn:uuid:492264AB42E57108E01176731445508@apache.org>\";" +
99                  "boundary=\"MIMEBoundaryurn_uuid_492264AB42E57108E01176731445507\"");
100         TransportInputStream tis = new MockTransportInputStream(is, headers);
101 
102         WebServiceMessage message = messageFactory.createWebServiceMessage(tis);
103         assertTrue("Not a SoapMessage", message instanceof SoapMessage);
104         SoapMessage soapMessage = (SoapMessage) message;
105         assertEquals("Invalid soap version", SoapVersion.SOAP_11, soapMessage.getVersion());
106         assertTrue("Message not a XOP package", soapMessage.isXopPackage());
107         Iterator<Attachment> iter = soapMessage.getAttachments();
108         assertTrue("No attachments read", iter.hasNext());
109 
110         Attachment attachment = soapMessage.getAttachment("<1.urn:uuid:492264AB42E57108E01176731445504@apache.org>");
111         assertNotNull("No attachment read", attachment);
112     }
113 
114     @Test
115     public void testCreateSoapMessageMtomWeirdStartInfo() throws Exception {
116         InputStream is = AbstractSoap11MessageFactoryTestCase.class.getResourceAsStream("soap11-mtom.bin");
117         Map<String, String> headers = new HashMap<String, String>();
118         headers.put("Content-Type", "multipart/related;" + "startinfo=\"text/xml\";" +
119                 "type=\"application/xop+xml\";" + "start=\"<0.urn:uuid:492264AB42E57108E01176731445508@apache.org>\";" +
120                 "boundary=\"MIMEBoundaryurn_uuid_492264AB42E57108E01176731445507\"");
121         TransportInputStream tis = new MockTransportInputStream(is, headers);
122 
123         WebServiceMessage message = messageFactory.createWebServiceMessage(tis);
124         assertTrue("Not a SoapMessage", message instanceof SoapMessage);
125         SoapMessage soapMessage = (SoapMessage) message;
126         assertEquals("Invalid soap version", SoapVersion.SOAP_11, soapMessage.getVersion());
127         assertTrue("Message not a XOP package", soapMessage.isXopPackage());
128         Iterator<Attachment> iter = soapMessage.getAttachments();
129         assertTrue("No attachments read", iter.hasNext());
130 
131         Attachment attachment = soapMessage.getAttachment("<1.urn:uuid:492264AB42E57108E01176731445504@apache.org>");
132         assertNotNull("No attachment read", attachment);
133     }
134 
135     @Test
136     public void testCreateSoapMessageUtf8ByteOrderMark() throws Exception {
137         InputStream is = AbstractSoap11MessageFactoryTestCase.class.getResourceAsStream("soap11-utf8-bom.xml");
138         Map<String, String> headers = new HashMap<String, String>();
139         headers.put("Content-Type", "text/xml; charset=UTF-8");
140         TransportInputStream tis = new MockTransportInputStream(is, headers);
141 
142         SoapMessage message = (SoapMessage) messageFactory.createWebServiceMessage(tis);
143         assertEquals("Invalid soap version", SoapVersion.SOAP_11, message.getVersion());
144     }
145 
146     @Test
147     public void testCreateSoapMessageUtf16BigEndianByteOrderMark() throws Exception {
148         InputStream is = AbstractSoap11MessageFactoryTestCase.class.getResourceAsStream("soap11-utf16-be-bom.xml");
149         Map<String, String> headers = new HashMap<String, String>();
150         headers.put("Content-Type", "text/xml; charset=UTF-16");
151         TransportInputStream tis = new MockTransportInputStream(is, headers);
152 
153         SoapMessage message = (SoapMessage) messageFactory.createWebServiceMessage(tis);
154         assertEquals("Invalid soap version", SoapVersion.SOAP_11, message.getVersion());
155     }
156 
157     @Test
158     public void testCreateSoapMessageUtf16LittleEndianByteOrderMark() throws Exception {
159         InputStream is = AbstractSoap11MessageFactoryTestCase.class.getResourceAsStream("soap11-utf16-le-bom.xml");
160         Map<String, String> headers = new HashMap<String, String>();
161         headers.put("Content-Type", "text/xml; charset=UTF-16");
162         TransportInputStream tis = new MockTransportInputStream(is, headers);
163 
164         SoapMessage message = (SoapMessage) messageFactory.createWebServiceMessage(tis);
165         assertEquals("Invalid soap version", SoapVersion.SOAP_11, message.getVersion());
166     }
167 
168 
169 }