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