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.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  public class SaajSoap11MessageTest extends AbstractSoap11MessageTestCase {
34  
35      private SOAPMessage saajMessage;
36  
37      protected final SoapMessage createSoapMessage() throws Exception {
38          MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
39          saajMessage = messageFactory.createMessage();
40          saajMessage.getSOAPHeader().detachNode();
41          return new SaajSoapMessage(saajMessage);
42      }
43  
44      public void testGetPayloadSource() throws Exception {
45          saajMessage.getSOAPPart().getEnvelope().getBody().addChildElement("child");
46          Source source = soapMessage.getPayloadSource();
47          StringResult result = new StringResult();
48          transformer.transform(source, result);
49          assertXMLEqual("Invalid source", "<child/>", result.toString());
50      }
51  
52      public void testGetPayloadSourceText() throws Exception {
53          SOAPBody body = saajMessage.getSOAPPart().getEnvelope().getBody();
54          body.addTextNode(" ");
55          body.addChildElement("child");
56          Source source = soapMessage.getPayloadSource();
57          StringResult result = new StringResult();
58          transformer.transform(source, result);
59          assertXMLEqual("Invalid source", "<child/>", result.toString());
60      }
61  
62      public void testGetPayloadResult() throws Exception {
63          StringSource source = new StringSource("<child/>");
64          Result result = soapMessage.getPayloadResult();
65          transformer.transform(source, result);
66          SOAPBody body = saajMessage.getSOAPPart().getEnvelope().getBody();
67          Iterator iterator = body.getChildElements();
68          assertTrue("No child nodes created", iterator.hasNext());
69          SOAPBodyElement bodyElement = (SOAPBodyElement) iterator.next();
70          assertEquals("Invalid child node created", "child", bodyElement.getElementName().getLocalName());
71      }
72  
73  }