1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.soap.saaj;
18
19 import java.util.Iterator;
20 import javax.xml.soap.MessageFactory;
21 import javax.xml.soap.SOAPBody;
22 import javax.xml.soap.SOAPBodyElement;
23 import javax.xml.soap.SOAPConstants;
24 import javax.xml.soap.SOAPMessage;
25 import javax.xml.transform.Result;
26 import javax.xml.transform.Source;
27
28 import org.springframework.ws.soap.SoapMessage;
29 import org.springframework.ws.soap.soap11.AbstractSoap11MessageTestCase;
30 import org.springframework.xml.transform.StringResult;
31 import org.springframework.xml.transform.StringSource;
32
33 import org.junit.Test;
34
35 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
36 import static org.junit.Assert.assertEquals;
37 import static org.junit.Assert.assertTrue;
38
39 public class SaajSoap11MessageTest extends AbstractSoap11MessageTestCase {
40
41 private SOAPMessage saajMessage;
42
43 @Override
44 protected final SoapMessage createSoapMessage() throws Exception {
45 MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
46 saajMessage = messageFactory.createMessage();
47 saajMessage.getSOAPHeader().detachNode();
48 return new SaajSoapMessage(saajMessage, true, messageFactory);
49 }
50
51 @Test
52 public void testGetPayloadSource() throws Exception {
53 saajMessage.getSOAPPart().getEnvelope().getBody().addChildElement("child");
54 Source source = soapMessage.getPayloadSource();
55 StringResult result = new StringResult();
56 transformer.transform(source, result);
57 assertXMLEqual("Invalid source", "<child/>", result.toString());
58 }
59
60 @Test
61 public void testGetPayloadSourceText() throws Exception {
62 SOAPBody body = saajMessage.getSOAPPart().getEnvelope().getBody();
63 body.addTextNode(" ");
64 body.addChildElement("child");
65 Source source = soapMessage.getPayloadSource();
66 StringResult result = new StringResult();
67 transformer.transform(source, result);
68 assertXMLEqual("Invalid source", "<child/>", result.toString());
69 }
70
71 @Test
72 public void testGetPayloadResult() throws Exception {
73 StringSource source = new StringSource("<child/>");
74 Result result = soapMessage.getPayloadResult();
75 transformer.transform(source, result);
76 SOAPBody body = saajMessage.getSOAPPart().getEnvelope().getBody();
77 Iterator<?> iterator = body.getChildElements();
78 assertTrue("No child nodes created", iterator.hasNext());
79 SOAPBodyElement bodyElement = (SOAPBodyElement) iterator.next();
80 assertEquals("Invalid child node created", "child", bodyElement.getElementName().getLocalName());
81 }
82
83 }