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.util.Locale;
20 import javax.xml.transform.dom.DOMResult;
21
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24 import org.w3c.dom.NodeList;
25
26 import org.springframework.xml.transform.StringResult;
27 import org.springframework.xml.transform.StringSource;
28
29 public abstract class AbstractSoapBodyTestCase extends AbstractSoapElementTestCase {
30
31 protected SoapBody soapBody;
32
33 protected final SoapElement createSoapElement() throws Exception {
34 soapBody = createSoapBody();
35 return soapBody;
36 }
37
38 protected abstract SoapBody createSoapBody() throws Exception;
39
40 public void testPayload() throws Exception {
41 String payload = "<payload xmlns='http://www.springframework.org' />";
42 transformer.transform(new StringSource(payload), soapBody.getPayloadResult());
43 assertPayloadEqual(payload);
44 }
45
46 public void testGetPayloadResultTwice() throws Exception {
47 String payload = "<payload xmlns='http://www.springframework.org' />";
48 transformer.transform(new StringSource(payload), soapBody.getPayloadResult());
49 transformer.transform(new StringSource(payload), soapBody.getPayloadResult());
50 DOMResult domResult = new DOMResult();
51 transformer.transform(soapBody.getSource(), domResult);
52 Element bodyElement = ((Document) domResult.getNode()).getDocumentElement();
53 NodeList children = bodyElement.getChildNodes();
54 assertEquals("Invalid amount of child nodes", 1, children.getLength());
55 }
56
57 public void testNoFault() throws Exception {
58 assertFalse("body has fault", soapBody.hasFault());
59 }
60
61 public void testAddFaultWithExistingPayload() throws Exception {
62 StringSource contents = new StringSource("<payload xmlns='http://www.springframework.org' />");
63 transformer.transform(contents, soapBody.getPayloadResult());
64 soapBody.addMustUnderstandFault("faultString", Locale.ENGLISH);
65 assertTrue("Body has no fault", soapBody.hasFault());
66 }
67
68 protected void assertPayloadEqual(String expectedPayload) throws Exception {
69 StringResult result = new StringResult();
70 transformer.transform(soapBody.getPayloadSource(), result);
71 assertXMLEqual("Invalid payload contents", expectedPayload, result.toString());
72 }
73
74 }