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.server.endpoint;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.InputStream;
21  import java.io.Reader;
22  import java.io.StringReader;
23  import javax.xml.parsers.DocumentBuilder;
24  import javax.xml.parsers.DocumentBuilderFactory;
25  import javax.xml.stream.XMLEventReader;
26  import javax.xml.stream.XMLInputFactory;
27  import javax.xml.stream.XMLStreamReader;
28  import javax.xml.transform.Source;
29  import javax.xml.transform.dom.DOMSource;
30  import javax.xml.transform.sax.SAXSource;
31  import javax.xml.transform.stream.StreamSource;
32  
33  import org.springframework.util.xml.StaxUtils;
34  
35  import org.junit.Test;
36  import org.w3c.dom.Document;
37  import org.xml.sax.InputSource;
38  import org.xml.sax.XMLReader;
39  import org.xml.sax.helpers.XMLReaderFactory;
40  
41  @SuppressWarnings("Since15")
42  public abstract class AbstractEndpointTestCase {
43  
44      protected static final String NAMESPACE_URI = "http://springframework.org/ws";
45  
46      protected static final String REQUEST_ELEMENT = "request";
47  
48      protected static final String RESPONSE_ELEMENT = "response";
49  
50      protected static final String REQUEST = "<" + REQUEST_ELEMENT + " xmlns=\"" + NAMESPACE_URI + "\"/>";
51  
52      protected static final String RESPONSE = "<" + RESPONSE_ELEMENT + " xmlns=\"" + NAMESPACE_URI + "\"/>";
53  
54      @Test
55      public void testDomSource() throws Exception {
56          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
57          documentBuilderFactory.setNamespaceAware(true);
58          DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
59          Document requestDocument = documentBuilder.parse(new InputSource(new StringReader(REQUEST)));
60          testSource(new DOMSource(requestDocument.getDocumentElement()));
61      }
62  
63      @Test
64      public void testSaxSource() throws Exception {
65          XMLReader reader = XMLReaderFactory.createXMLReader();
66          InputSource inputSource = new InputSource(new StringReader(REQUEST));
67          testSource(new SAXSource(reader, inputSource));
68      }
69  
70      @Test
71      public void testStaxSourceEventReader() throws Exception {
72          XMLInputFactory inputFactory = XMLInputFactory.newInstance();
73          XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(REQUEST));
74          testSource(new SAXSource(StaxUtils.createXMLReader(eventReader), new InputSource()));
75      }
76  
77      @Test
78      public void testStaxSourceStreamReader() throws Exception {
79          XMLInputFactory inputFactory = XMLInputFactory.newInstance();
80          XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(REQUEST));
81          testSource(new SAXSource(StaxUtils.createXMLReader(streamReader), new InputSource()));
82      }
83  
84      @Test
85      public void testStreamSourceInputStream() throws Exception {
86          InputStream is = new ByteArrayInputStream(REQUEST.getBytes("UTF-8"));
87          testSource(new StreamSource(is));
88      }
89  
90      @Test
91      public void testStreamSourceReader() throws Exception {
92          Reader reader = new StringReader(REQUEST);
93          testSource(new StreamSource(reader));
94      }
95  
96      protected abstract void testSource(Source requestSource) throws Exception;
97  }