1   /*
2    * Copyright 2005 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 javax.xml.transform.Source;
20  
21  import org.springframework.xml.transform.StringSource;
22  import org.xml.sax.Attributes;
23  import org.xml.sax.ContentHandler;
24  import org.xml.sax.SAXException;
25  import org.xml.sax.helpers.DefaultHandler;
26  
27  public class SaxPayloadEndpointTest extends AbstractPayloadEndpointTestCase {
28  
29      protected PayloadEndpoint createNoResponseEndpoint() throws Exception {
30          return new AbstractSaxPayloadEndpoint() {
31  
32              protected Source getResponse(ContentHandler contentHandler) {
33                  return null;
34              }
35  
36              protected ContentHandler createContentHandler() {
37                  return new DefaultHandler();
38              }
39          };
40      }
41  
42      protected PayloadEndpoint createResponseEndpoint() throws Exception {
43          return new AbstractSaxPayloadEndpoint() {
44  
45              protected ContentHandler createContentHandler() {
46                  return new TestContentHandler();
47              }
48  
49              protected Source getResponse(ContentHandler contentHandler) {
50                  return new StringSource(RESPONSE);
51              }
52          };
53      }
54  
55      protected PayloadEndpoint createNoRequestEndpoint() throws Exception {
56          return new AbstractSaxPayloadEndpoint() {
57  
58              protected ContentHandler createContentHandler() throws Exception {
59                  fail("Not expected");
60                  return null;
61              }
62  
63              protected Source getResponse(ContentHandler contentHandler) throws Exception {
64                  return null;
65              }
66          };
67      }
68  
69      private static class TestContentHandler extends DefaultHandler {
70  
71          public void endElement(String uri, String localName, String qName) throws SAXException {
72              assertEquals("Invalid local name", REQUEST_ELEMENT, localName);
73              assertEquals("Invalid qName", REQUEST_ELEMENT, localName);
74              assertEquals("Invalid namespace", NAMESPACE_URI, uri);
75          }
76  
77          public void startElement(String uri, String localName, String qName, Attributes attributes)
78                  throws SAXException {
79              assertEquals("Invalid local name", REQUEST_ELEMENT, localName);
80              assertEquals("Invalid qName", REQUEST_ELEMENT, localName);
81              assertEquals("Invalid namespace", NAMESPACE_URI, uri);
82          }
83      }
84  }