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