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