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