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.adapter.method;
18  
19  import javax.xml.transform.Source;
20  
21  import org.springframework.core.MethodParameter;
22  import org.springframework.ws.context.MessageContext;
23  import org.springframework.xml.transform.StringResult;
24  
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
29  import static org.junit.Assert.assertFalse;
30  import static org.junit.Assert.assertTrue;
31  
32  public abstract class AbstractPayloadMethodProcessorTestCase extends AbstractMethodArgumentResolverTestCase {
33  
34      private AbstractPayloadSourceMethodProcessor processor;
35  
36      private MethodParameter[] supportedParameters;
37  
38      private MethodParameter[] supportedReturnTypes;
39  
40      @Before
41      public final void setUp() throws NoSuchMethodException {
42          processor = createProcessor();
43          supportedParameters = createSupportedParameters();
44          supportedReturnTypes = createSupportedReturnTypes();
45      }
46  
47      protected abstract AbstractPayloadSourceMethodProcessor createProcessor();
48  
49      protected abstract MethodParameter[] createSupportedParameters() throws NoSuchMethodException;
50  
51      protected abstract MethodParameter[] createSupportedReturnTypes() throws NoSuchMethodException;
52  
53      @Test
54      public void supportsParameter() throws NoSuchMethodException {
55          for (MethodParameter supportedParameter : supportedParameters) {
56              assertTrue("processor does not support " + supportedParameter.getParameterType() + " parameter",
57                      processor.supportsParameter(supportedParameter));
58          }
59          MethodParameter unsupportedParameter =
60                  new MethodParameter(getClass().getMethod("unsupported", String.class), 0);
61          assertFalse("processor supports invalid parameter", processor.supportsParameter(unsupportedParameter));
62      }
63  
64      @Test
65      public void supportsReturnType() throws NoSuchMethodException {
66          for (MethodParameter supportedReturnType : supportedReturnTypes) {
67              assertTrue("processor does not support " + supportedReturnType.getParameterType() + " return type",
68                      processor.supportsReturnType(supportedReturnType));
69          }
70          MethodParameter unsupportedReturnType =
71                  new MethodParameter(getClass().getMethod("unsupported", String.class), -1);
72          assertFalse("processor supports invalid return type", processor.supportsReturnType(unsupportedReturnType));
73      }
74  
75      @Test
76      public void saajArgument() throws Exception {
77          testResolveArgument(createSaajMessageContext());
78      }
79  
80      @Test
81      public void mockArgument() throws Exception {
82          testResolveArgument(createMockMessageContext());
83      }
84  
85      @Test
86      public void axiomCachingArgument() throws Exception {
87          testResolveArgument(createCachingAxiomMessageContext());
88      }
89  
90      @Test
91      public void axiomNonCachingArgument() throws Exception {
92          testResolveArgument(createNonCachingAxiomMessageContext());
93      }
94  
95      private void testResolveArgument(MessageContext messageContext) throws Exception {
96          for (MethodParameter supportedParameter : supportedParameters) {
97              Object argument = processor.resolveArgument(messageContext, supportedParameter);
98  
99              assertTrue(argument + " is not an instance of " + supportedParameter.getParameterType(),
100                     supportedParameter.getParameterType().isInstance(argument));
101             testArgument(argument, supportedParameter);
102         }
103     }
104 
105 
106     protected void testArgument(Object argument, MethodParameter parameter) {
107     }
108 
109     @Test
110     public void saajReturnValue() throws Exception {
111         testHandleReturnValue(createSaajMessageContext());
112     }
113 
114     @Test
115     public void mockReturnValue() throws Exception {
116         testHandleReturnValue(createMockMessageContext());
117     }
118 
119     @Test
120     public void axiomCachingReturnValue() throws Exception {
121         testHandleReturnValue(createCachingAxiomMessageContext());
122     }
123 
124     @Test
125     public void axiomNonCachingReturnValue() throws Exception {
126         testHandleReturnValue(createNonCachingAxiomMessageContext());
127     }
128 
129 
130     private void testHandleReturnValue(MessageContext messageContext) throws Exception {
131         for (MethodParameter supportedReturnType : supportedReturnTypes) {
132             Object returnValue = getReturnValue(supportedReturnType);
133             processor.handleReturnValue(messageContext, supportedReturnType, returnValue);
134             assertTrue("No response created", messageContext.hasResponse());
135             Source responsePayload = messageContext.getResponse().getPayloadSource();
136             StringResult result = new StringResult();
137             transform(responsePayload, result);
138             assertXMLEqual(XML, result.toString());
139         }
140     }
141 
142     protected abstract Object getReturnValue(MethodParameter returnType) throws Exception;
143 
144     public String unsupported(String s) {
145         return s;
146     }
147 
148 }