1   /*
2    * Copyright 2006 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;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.util.Map;
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  
24  import org.xml.sax.SAXParseException;
25  
26  import org.springframework.core.io.Resource;
27  import org.springframework.util.StringUtils;
28  import org.springframework.ws.mime.AbstractMimeMessageTestCase;
29  import org.springframework.ws.mime.MimeMessage;
30  import org.springframework.ws.transport.MockTransportOutputStream;
31  import org.springframework.ws.transport.TransportConstants;
32  import org.springframework.xml.validation.XmlValidator;
33  import org.springframework.xml.validation.XmlValidatorFactory;
34  
35  public abstract class AbstractSoapMessageTestCase extends AbstractMimeMessageTestCase {
36  
37      protected SoapMessage soapMessage;
38  
39      protected MimeMessage createMimeMessage() throws Exception {
40          soapMessage = createSoapMessage();
41          return soapMessage;
42      }
43  
44      protected abstract SoapMessage createSoapMessage() throws Exception;
45  
46      public void testValidate() throws Exception {
47          XmlValidator validator =
48                  XmlValidatorFactory.createValidator(getSoapSchemas(), XmlValidatorFactory.SCHEMA_W3C_XML);
49          SAXParseException[] errors = validator.validate(soapMessage.getEnvelope().getSource());
50          if (errors.length > 0) {
51              fail(StringUtils.arrayToCommaDelimitedString(errors));
52          }
53      }
54  
55      public void testSoapAction() throws Exception {
56          assertEquals("Invalid default SOAP Action", "\"\"", soapMessage.getSoapAction());
57          soapMessage.setSoapAction("SoapAction");
58          assertEquals("Invalid SOAP Action", "\"SoapAction\"", soapMessage.getSoapAction());
59      }
60  
61      public void testCharsetAttribute() throws Exception {
62          MockTransportOutputStream outputStream = new MockTransportOutputStream(new ByteArrayOutputStream());
63          soapMessage.writeTo(outputStream);
64          Map headers = outputStream.getHeaders();
65          String contentType = (String) headers.get(TransportConstants.HEADER_CONTENT_TYPE);
66          if (contentType != null) {
67              Pattern charsetPattern = Pattern.compile("charset\\s*=\\s*([^;]+)");
68              Matcher matcher = charsetPattern.matcher(contentType);
69              if (matcher.find() && matcher.groupCount() == 1) {
70                  String charset = matcher.group(1).trim();
71                  assertTrue("Invalid charset", charset.indexOf('"') < 0);
72              }
73          }
74      }
75  
76      protected abstract Resource[] getSoapSchemas();
77  
78      public abstract void testGetVersion() throws Exception;
79  
80      public abstract void testWriteToTransportOutputStream() throws Exception;
81  
82      public abstract void testWriteToTransportResponseAttachment() throws Exception;
83  }