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.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  }