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.pox.dom;
18  
19  import java.io.ByteArrayOutputStream;
20  import javax.xml.parsers.DocumentBuilder;
21  import javax.xml.parsers.DocumentBuilderFactory;
22  import javax.xml.transform.Transformer;
23  import javax.xml.transform.TransformerFactory;
24  
25  import org.springframework.xml.transform.StringResult;
26  import org.springframework.xml.transform.StringSource;
27  
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.w3c.dom.Document;
31  
32  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
33  
34  public class DomPoxMessageTest {
35  
36      private DomPoxMessage message;
37  
38      private Transformer transformer;
39  
40      @Before
41      public void setUp() throws Exception {
42          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
43          DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
44          Document document = documentBuilder.newDocument();
45          TransformerFactory transformerFactory = TransformerFactory.newInstance();
46          transformer = transformerFactory.newTransformer();
47          message = new DomPoxMessage(document, transformer, DomPoxMessageFactory.DEFAULT_CONTENT_TYPE);
48      }
49  
50      @Test
51      public void testGetPayload() throws Exception {
52          String content = "<root xmlns='http://www.springframework.org/spring-ws'>" + "<child/></root>";
53          StringSource source = new StringSource(content);
54          transformer.transform(source, message.getPayloadResult());
55          StringResult stringResult = new StringResult();
56          transformer.transform(message.getPayloadSource(), stringResult);
57          assertXMLEqual(content, stringResult.toString());
58      }
59  
60      @Test
61      public void testGetPayloadResultTwice() throws Exception {
62          String content = "<element xmlns=\"http://www.springframework.org/spring-ws\" />";
63          transformer.transform(new StringSource(content), message.getPayloadResult());
64          transformer.transform(new StringSource(content), message.getPayloadResult());
65          StringResult stringResult = new StringResult();
66          transformer.transform(message.getPayloadSource(), stringResult);
67          assertXMLEqual(content, stringResult.toString());
68      }
69  
70      @Test
71      public void testWriteTo() throws Exception {
72          String content = "<root xmlns='http://www.springframework.org/spring-ws'>" + "<child/></root>";
73          StringSource source = new StringSource(content);
74          transformer.transform(source, message.getPayloadResult());
75          ByteArrayOutputStream os = new ByteArrayOutputStream();
76          message.writeTo(os);
77          assertXMLEqual(content, os.toString("UTF-8"));
78      }
79  }