1   /*
2    * Copyright 2007 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.adapter;
18  
19  import javax.xml.transform.Source;
20  import javax.xml.transform.dom.DOMSource;
21  import javax.xml.transform.stream.StreamSource;
22  
23  import junit.framework.TestCase;
24  import org.springframework.ws.MockWebServiceMessage;
25  import org.springframework.ws.MockWebServiceMessageFactory;
26  import org.springframework.ws.WebServiceMessage;
27  import org.springframework.ws.context.DefaultMessageContext;
28  import org.springframework.ws.context.MessageContext;
29  import org.springframework.ws.server.endpoint.MethodEndpoint;
30  
31  public class PayloadMethodEndpointAdapterTest extends TestCase {
32  
33      private PayloadMethodEndpointAdapter adapter;
34  
35      private boolean noResponseInvoked;
36  
37      private boolean responseInvoked;
38  
39      private MessageContext messageContext;
40  
41      protected void setUp() throws Exception {
42          adapter = new PayloadMethodEndpointAdapter();
43          messageContext = new DefaultMessageContext(new MockWebServiceMessageFactory());
44      }
45  
46      public void testSupportedNoResponse() throws NoSuchMethodException {
47          MethodEndpoint methodEndpoint = new MethodEndpoint(this, "noResponse", new Class[]{DOMSource.class});
48          assertTrue("Method unsupported", adapter.supportsInternal(methodEndpoint));
49      }
50  
51      public void testSupportedResponse() throws NoSuchMethodException {
52          MethodEndpoint methodEndpoint = new MethodEndpoint(this, "response", new Class[]{StreamSource.class});
53          assertTrue("Method unsupported", adapter.supportsInternal(methodEndpoint));
54      }
55  
56      public void testUnsupportedMethodMultipleParams() throws NoSuchMethodException {
57          assertFalse("Method supported", adapter.supportsInternal(
58                  new MethodEndpoint(this, "unsupportedMultipleParams", new Class[]{Source.class, Source.class})));
59      }
60  
61      public void testUnsupportedMethodWrongReturnType() throws NoSuchMethodException {
62          assertFalse("Method supported", adapter.supportsInternal(
63                  new MethodEndpoint(this, "unsupportedWrongReturnType", new Class[]{Source.class})));
64      }
65  
66      public void testUnsupportedMethodWrongParam() throws NoSuchMethodException {
67          assertFalse("Method supported",
68                  adapter.supportsInternal(new MethodEndpoint(this, "unsupportedWrongParam", new Class[]{String.class})));
69      }
70  
71      public void testNoResponse() throws Exception {
72          MethodEndpoint methodEndpoint = new MethodEndpoint(this, "noResponse", new Class[]{DOMSource.class});
73          assertFalse("Method invoked", noResponseInvoked);
74          adapter.invoke(messageContext, methodEndpoint);
75          assertTrue("Method not invoked", noResponseInvoked);
76      }
77  
78      public void testResponse() throws Exception {
79          WebServiceMessage request = new MockWebServiceMessage("<request/>");
80          messageContext = new DefaultMessageContext(request, new MockWebServiceMessageFactory());
81          MethodEndpoint methodEndpoint = new MethodEndpoint(this, "response", new Class[]{StreamSource.class});
82          assertFalse("Method invoked", responseInvoked);
83          adapter.invoke(messageContext, methodEndpoint);
84          assertTrue("Method not invoked", responseInvoked);
85      }
86  
87      public void noResponse(DOMSource request) {
88          noResponseInvoked = true;
89      }
90  
91      public Source response(StreamSource request) {
92          responseInvoked = true;
93          return request;
94      }
95  
96      public void unsupportedMultipleParams(Source s1, Source s2) {
97      }
98  
99      public Source unsupportedWrongParam(String request) {
100         return null;
101     }
102 
103     public String unsupportedWrongReturnType(Source request) {
104         return null;
105     }
106 
107 }