1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 import javax.xml.namespace.QName;
24 import javax.xml.stream.XMLStreamException;
25 import javax.xml.stream.XMLStreamWriter;
26
27 import org.springframework.core.io.Resource;
28 import org.springframework.util.StringUtils;
29 import org.springframework.ws.mime.AbstractMimeMessageTestCase;
30 import org.springframework.ws.mime.MimeMessage;
31 import org.springframework.ws.stream.StreamingPayload;
32 import org.springframework.ws.stream.StreamingWebServiceMessage;
33 import org.springframework.ws.transport.MockTransportOutputStream;
34 import org.springframework.ws.transport.TransportConstants;
35 import org.springframework.xml.transform.StringResult;
36 import org.springframework.xml.validation.XmlValidator;
37 import org.springframework.xml.validation.XmlValidatorFactory;
38
39 import org.junit.Test;
40 import org.xml.sax.SAXParseException;
41
42 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
43 import static org.junit.Assert.*;
44
45 public abstract class AbstractSoapMessageTestCase extends AbstractMimeMessageTestCase {
46
47 protected SoapMessage soapMessage;
48
49 @Override
50 protected MimeMessage createMimeMessage() throws Exception {
51 soapMessage = createSoapMessage();
52 return soapMessage;
53 }
54
55 protected abstract SoapMessage createSoapMessage() throws Exception;
56
57 @Test
58 public void testValidate() throws Exception {
59 XmlValidator validator =
60 XmlValidatorFactory.createValidator(getSoapSchemas(), XmlValidatorFactory.SCHEMA_W3C_XML);
61 SAXParseException[] errors = validator.validate(soapMessage.getEnvelope().getSource());
62 if (errors.length > 0) {
63 fail(StringUtils.arrayToCommaDelimitedString(errors));
64 }
65 }
66
67 @Test
68 public void testSoapAction() throws Exception {
69 assertEquals("Invalid default SOAP Action", "\"\"", soapMessage.getSoapAction());
70 soapMessage.setSoapAction("SoapAction");
71 assertEquals("Invalid SOAP Action", "\"SoapAction\"", soapMessage.getSoapAction());
72 }
73
74 @Test
75 public void testCharsetAttribute() throws Exception {
76 MockTransportOutputStream outputStream = new MockTransportOutputStream(new ByteArrayOutputStream());
77 soapMessage.writeTo(outputStream);
78 Map<String, String> headers = outputStream.getHeaders();
79 String contentType = headers.get(TransportConstants.HEADER_CONTENT_TYPE);
80 if (contentType != null) {
81 Pattern charsetPattern = Pattern.compile("charset\\s*=\\s*([^;]+)");
82 Matcher matcher = charsetPattern.matcher(contentType);
83 if (matcher.find() && matcher.groupCount() == 1) {
84 String charset = matcher.group(1).trim();
85 assertTrue("Invalid charset", charset.indexOf('"') < 0);
86 }
87 }
88 }
89
90 @Test
91 public void testSetStreamingPayload() throws Exception {
92 if (!(soapMessage instanceof StreamingWebServiceMessage)) {
93 return;
94 }
95 StreamingWebServiceMessage streamingMessage = (StreamingWebServiceMessage) soapMessage;
96
97 final QName name = new QName("http://springframework.org", "root", "prefix");
98 streamingMessage.setStreamingPayload(new StreamingPayload() {
99 public QName getName() {
100 return name;
101 }
102
103 public void writeTo(XMLStreamWriter streamWriter) throws XMLStreamException {
104 streamWriter.writeStartElement(name.getPrefix(), name.getLocalPart(), name.getNamespaceURI());
105 streamWriter.writeNamespace("prefix", name.getNamespaceURI());
106 streamWriter.writeStartElement(name.getNamespaceURI(), "child");
107 streamWriter.writeCharacters("Foo");
108 streamWriter.writeEndElement();
109 streamWriter.writeEndElement();
110 }
111 });
112
113 StringResult result = new StringResult();
114 transformer.transform(streamingMessage.getPayloadSource(), result);
115
116 String expected = "<root xmlns='http://springframework.org'><child>Foo</child></root>";
117 assertXMLEqual(expected, result.toString());
118
119 soapMessage.writeTo(new ByteArrayOutputStream());
120 }
121
122 protected abstract Resource[] getSoapSchemas();
123
124 @Test
125 public abstract void testGetVersion() throws Exception;
126
127 @Test
128 public abstract void testWriteToTransportOutputStream() throws Exception;
129
130 @Test
131 public abstract void testWriteToTransportResponseAttachment() throws Exception;
132
133 @Test
134 public abstract void testToDocument() throws Exception;
135
136 @Test
137 public abstract void testSetLiveDocument() throws Exception;
138
139 @Test
140 public abstract void testSetOtherDocument() throws Exception;
141
142
143 }