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