View Javadoc

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.test.server;
18  
19  import java.io.IOException;
20  import javax.xml.namespace.QName;
21  
22  import org.springframework.ws.WebServiceMessage;
23  import org.springframework.ws.soap.SoapBody;
24  import org.springframework.ws.soap.SoapFault;
25  import org.springframework.ws.soap.SoapMessage;
26  import org.springframework.ws.soap.SoapVersion;
27  
28  import static org.springframework.ws.test.support.AssertionErrors.assertEquals;
29  import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
30  
31  /**
32   * Abstract Implementation of {@link ResponseMatcher} that checks for a SOAP fault.
33   *
34   * @author Arjen Poutsma
35   * @since 2.0
36   */
37  abstract class SoapFaultResponseMatcher implements ResponseMatcher {
38  
39      private final String expectedFaultStringOrReason;
40  
41      SoapFaultResponseMatcher(String expectedFaultStringOrReason) {
42          this.expectedFaultStringOrReason = expectedFaultStringOrReason;
43      }
44  
45      public void match(WebServiceMessage request, WebServiceMessage response) throws IOException, AssertionError {
46          assertTrue("Response is not a SOAP message", response instanceof SoapMessage);
47          SoapMessage soapResponse = (SoapMessage) response;
48          SoapBody responseBody = soapResponse.getSoapBody();
49          assertTrue("Response has no SOAP Body", responseBody != null);
50          assertTrue("Response has no SOAP Fault", responseBody.hasFault());
51          SoapFault soapFault = responseBody.getFault();
52          QName expectedFaultCode = getExpectedFaultCode(soapResponse.getVersion());
53          assertEquals("Invalid SOAP Fault code", expectedFaultCode, soapFault.getFaultCode());
54          if (expectedFaultStringOrReason != null) {
55              assertEquals("Invalid SOAP Fault string/reason", expectedFaultStringOrReason,
56                      soapFault.getFaultStringOrReason());
57          }
58      }
59  
60      /**
61       * Returns the SOAP fault code to check for, given the SOAP version.
62       */
63      protected abstract QName getExpectedFaultCode(SoapVersion version);
64  
65  }