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  import javax.xml.transform.Transformer;
21  import javax.xml.transform.TransformerFactory;
22  
23  import org.springframework.xml.transform.StringResult;
24  import org.springframework.xml.transform.StringSource;
25  
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
30  import static org.junit.Assert.assertNotNull;
31  import static org.junit.Assert.assertNull;
32  
33  public abstract class AbstractPayloadEndpointTestCase extends AbstractEndpointTestCase {
34  
35      private PayloadEndpoint endpoint;
36  
37      private Transformer transformer;
38  
39      @Before
40      public void createEndpoint() throws Exception {
41          endpoint = createResponseEndpoint();
42          transformer = TransformerFactory.newInstance().newTransformer();
43      }
44  
45      @Test
46      public void testNoResponse() throws Exception {
47          endpoint = createNoResponseEndpoint();
48          StringSource requestSource = new StringSource(REQUEST);
49          Source resultSource = endpoint.invoke(requestSource);
50          assertNull("Response source returned", resultSource);
51      }
52  
53      @Test
54      public void testNoRequest() throws Exception {
55          endpoint = createNoRequestEndpoint();
56          Source resultSource = endpoint.invoke(null);
57          assertNull("Response source returned", resultSource);
58      }
59  
60      @Override
61      protected final void testSource(Source requestSource) throws Exception {
62          Source responseSource = endpoint.invoke(requestSource);
63          assertNotNull("No response source returned", responseSource);
64          StringResult result = new StringResult();
65          transformer.transform(responseSource, result);
66          assertXMLEqual(RESPONSE, result.toString());
67      }
68  
69      protected abstract PayloadEndpoint createNoResponseEndpoint() throws Exception;
70  
71      protected abstract PayloadEndpoint createResponseEndpoint() throws Exception;
72  
73      protected abstract PayloadEndpoint createNoRequestEndpoint() throws Exception;
74  }