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.axiom;
18  
19  import java.io.StringWriter;
20  import javax.xml.stream.XMLStreamWriter;
21  
22  import org.springframework.util.xml.StaxUtils;
23  
24  import org.apache.axiom.om.OMAbstractFactory;
25  import org.apache.axiom.soap.SOAPBody;
26  import org.apache.axiom.soap.SOAPFactory;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
31  
32  @SuppressWarnings("Since15")
33  public class NonCachingPayloadTest {
34  
35      private Payload payload;
36  
37      private SOAPBody body;
38  
39      @Before
40      public final void setUp() {
41          SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
42          body = soapFactory.createSOAPBody();
43          payload = new NonCachingPayload(body, soapFactory);
44      }
45  
46      @Test
47      public void testDelegatingStreamWriter() throws Exception {
48          XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(payload.getResult());
49  
50          String namespace = "http://springframework.org/spring-ws";
51          streamWriter.setDefaultNamespace(namespace);
52          streamWriter.writeStartElement(namespace, "root");
53          streamWriter.writeDefaultNamespace(namespace);
54          streamWriter.writeStartElement(namespace, "child");
55          streamWriter.writeCharacters("text");
56          streamWriter.writeEndElement();
57          streamWriter.writeEndElement();
58          streamWriter.flush();
59  
60          StringWriter writer = new StringWriter();
61          body.serialize(writer);
62  
63          String expected = "<soapenv:Body xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>" +
64                  "<root xmlns='http://springframework.org/spring-ws'>" + "<child>text</child>" + "</root></soapenv:Body>"
65                  ;
66          assertXMLEqual(expected, writer.toString());
67      }
68  
69      @Test
70      public void testDelegatingStreamWriterWriteEndDocument() throws Exception {
71          XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(payload.getResult());
72  
73          String namespace = "http://springframework.org/spring-ws";
74          streamWriter.setDefaultNamespace(namespace);
75          streamWriter.writeStartElement(namespace, "root");
76          streamWriter.writeDefaultNamespace(namespace);
77          streamWriter.writeStartElement(namespace, "child");
78          streamWriter.writeCharacters("text");
79          streamWriter.writeEndDocument();
80          streamWriter.flush();
81  
82          StringWriter writer = new StringWriter();
83          body.serialize(writer);
84  
85          String expected = "<soapenv:Body xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>" +
86                  "<root xmlns='http://springframework.org/spring-ws'>" + "<child>text</child>" + "</root></soapenv:Body>"
87                  ;
88          assertXMLEqual(expected, writer.toString());
89      }
90  
91      @Test
92      public void testDelegatingStreamWriterWriteEmptyElement() throws Exception {
93          XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(payload.getResult());
94  
95          String namespace = "http://springframework.org/spring-ws";
96          streamWriter.setDefaultNamespace(namespace);
97          streamWriter.writeStartElement(namespace, "root");
98          streamWriter.writeDefaultNamespace(namespace);
99          streamWriter.writeEmptyElement(namespace, "child");
100         streamWriter.writeEndElement();
101         streamWriter.flush();
102 
103         StringWriter writer = new StringWriter();
104         body.serialize(writer);
105 
106         String expected = "<soapenv:Body xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>" +
107                 "<root xmlns='http://springframework.org/spring-ws'>" + "<child />" + "</root></soapenv:Body>";
108         assertXMLEqual(expected, writer.toString());
109     }
110 }