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.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.custommonkey.xmlunit.XMLTestCase;
34  import org.springframework.xml.transform.StaxSource;
35  import org.w3c.dom.Document;
36  import org.xml.sax.InputSource;
37  import org.xml.sax.XMLReader;
38  import org.xml.sax.helpers.XMLReaderFactory;
39  
40  public abstract class AbstractEndpointTestCase extends XMLTestCase {
41  
42      protected static final String NAMESPACE_URI = "http://springframework.org/ws";
43  
44      protected static final String REQUEST_ELEMENT = "request";
45  
46      protected static final String RESPONSE_ELEMENT = "response";
47  
48      protected static final String REQUEST = "<" + REQUEST_ELEMENT + " xmlns=\"" + NAMESPACE_URI + "\"/>";
49  
50      protected static final String RESPONSE = "<" + RESPONSE_ELEMENT + " xmlns=\"" + NAMESPACE_URI + "\"/>";
51  
52      public void testDomSource() throws Exception {
53          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
54          documentBuilderFactory.setNamespaceAware(true);
55          DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
56          Document requestDocument = documentBuilder.parse(new InputSource(new StringReader(REQUEST)));
57          testSource(new DOMSource(requestDocument.getDocumentElement()));
58      }
59  
60      public void testSaxSource() throws Exception {
61          XMLReader reader = XMLReaderFactory.createXMLReader();
62          InputSource inputSource = new InputSource(new StringReader(REQUEST));
63          testSource(new SAXSource(reader, inputSource));
64      }
65  
66      public void testStaxSourceEventReader() throws Exception {
67          XMLInputFactory inputFactory = XMLInputFactory.newInstance();
68          XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(REQUEST));
69          testSource(new StaxSource(eventReader));
70      }
71  
72      public void testStaxSourceStreamReader() throws Exception {
73          XMLInputFactory inputFactory = XMLInputFactory.newInstance();
74          XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(REQUEST));
75          testSource(new StaxSource(streamReader));
76      }
77  
78      public void testStreamSourceInputStream() throws Exception {
79          InputStream is = new ByteArrayInputStream(REQUEST.getBytes("UTF-8"));
80          testSource(new StreamSource(is));
81      }
82  
83      public void testStreamSourceReader() throws Exception {
84          Reader reader = new StringReader(REQUEST);
85          testSource(new StreamSource(reader));
86      }
87  
88      protected abstract void testSource(Source requestSource) throws Exception;
89  }