Polishing

This commit is contained in:
Arjen Poutsma
2010-11-04 11:43:31 +00:00
parent b0934813aa
commit b9f5a75a30
5 changed files with 91 additions and 52 deletions

View File

@@ -22,17 +22,16 @@ import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.ws.FaultAwareWebServiceMessage;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.soap.SoapBody;
import org.springframework.ws.soap.SoapFault;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.soap.SoapVersion;
import org.springframework.ws.test.support.matcher.PayloadDiffMatcher;
import org.springframework.ws.test.support.matcher.SchemaValidatingMatcher;
import org.springframework.ws.test.support.matcher.SoapHeaderMatcher;
import org.springframework.ws.test.support.matcher.WebServiceMessageMatcher;
import org.springframework.xml.transform.ResourceSource;
import static org.springframework.ws.test.support.AssertionErrors.assertEquals;
import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
/**
@@ -94,7 +93,18 @@ public abstract class ResponseMatchers {
}
// SOAP Fault
// SOAP
/**
* Expects the given SOAP header in the outgoing message.
*
* @param soapHeaderName the qualified name of the SOAP header to expect
* @return the request matcher
*/
public static ResponseMatcher soapHeader(QName soapHeaderName) {
Assert.notNull(soapHeaderName, "'soapHeaderName' must not be null");
return new WebServiceMessageMatcherAdapter(new SoapHeaderMatcher(soapHeaderName));
}
/**
* Expects the response <strong>not</strong> to contain a SOAP fault.
@@ -102,13 +112,15 @@ public abstract class ResponseMatchers {
* @return the response matcher
*/
public static ResponseMatcher noFault() {
return new SoapResponseMatcher() {
@Override
protected void matchSoap(SoapMessage request, SoapMessage response) throws IOException, AssertionError {
SoapBody responseBody = response.getSoapBody();
assertTrue("Response has no SOAP Body", responseBody != null);
assertTrue("Response has a SOAP Fault", !responseBody.hasFault());
return new ResponseMatcher() {
public void match(WebServiceMessage request, WebServiceMessage response)
throws IOException, AssertionError {
if (response instanceof FaultAwareWebServiceMessage) {
FaultAwareWebServiceMessage faultMessage = (FaultAwareWebServiceMessage) response;
assertTrue("Response has a SOAP Fault", !faultMessage.hasFault());
}
}
};
}
@@ -212,44 +224,6 @@ public abstract class ResponseMatchers {
};
}
private static abstract class SoapResponseMatcher implements ResponseMatcher {
public final void match(WebServiceMessage request, WebServiceMessage response) throws IOException, AssertionError {
assertTrue("Request is not a SOAP message", request instanceof SoapMessage);
assertTrue("Response is not a SOAP message", response instanceof SoapMessage);
matchSoap((SoapMessage)request, (SoapMessage) response);
}
protected abstract void matchSoap(SoapMessage request, SoapMessage response) throws IOException, AssertionError;
}
private static abstract class SoapFaultResponseMatcher extends SoapResponseMatcher {
private final String expectedFaultStringOrReason;
protected SoapFaultResponseMatcher(String expectedFaultStringOrReason) {
this.expectedFaultStringOrReason = expectedFaultStringOrReason;
}
@Override
protected void matchSoap(SoapMessage request, SoapMessage response) throws IOException, AssertionError {
SoapBody responseBody = response.getSoapBody();
assertTrue("Response has no SOAP Body", responseBody != null);
assertTrue("Response has no SOAP Fault", responseBody.hasFault());
SoapFault soapFault = responseBody.getFault();
QName expectedFaultCode = getExpectedFaultCode(response.getVersion());
assertEquals("Invalid SOAP Fault code", expectedFaultCode, soapFault.getFaultCode());
if (expectedFaultStringOrReason != null) {
assertEquals("Invalid SOAP Fault string/reason", expectedFaultStringOrReason,
soapFault.getFaultStringOrReason());
}
}
protected abstract QName getExpectedFaultCode(SoapVersion version);
}
/**
* Adapts a {@link WebServiceMessageMatcher} to the {@link ResponseMatcher} contract.
*/

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ws.test.server;
import java.io.IOException;
import javax.xml.namespace.QName;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.soap.SoapBody;
import org.springframework.ws.soap.SoapFault;
import org.springframework.ws.soap.SoapMessage;
import org.springframework.ws.soap.SoapVersion;
import static org.springframework.ws.test.support.AssertionErrors.assertEquals;
import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
/**
* Abstract Implementation of {@link ResponseMatcher} that checks for a SOAP fault.
*
* @author Arjen Poutsma
* @since 2.0
*/
abstract class SoapFaultResponseMatcher implements ResponseMatcher {
private final String expectedFaultStringOrReason;
SoapFaultResponseMatcher(String expectedFaultStringOrReason) {
this.expectedFaultStringOrReason = expectedFaultStringOrReason;
}
public void match(WebServiceMessage request, WebServiceMessage response) throws IOException, AssertionError {
assertTrue("Response is not a SOAP message", response instanceof SoapMessage);
SoapMessage soapResponse = (SoapMessage) response;
SoapBody responseBody = soapResponse.getSoapBody();
assertTrue("Response has no SOAP Body", responseBody != null);
assertTrue("Response has no SOAP Fault", responseBody.hasFault());
SoapFault soapFault = responseBody.getFault();
QName expectedFaultCode = getExpectedFaultCode(soapResponse.getVersion());
assertEquals("Invalid SOAP Fault code", expectedFaultCode, soapFault.getFaultCode());
if (expectedFaultStringOrReason != null) {
assertEquals("Invalid SOAP Fault string/reason", expectedFaultStringOrReason,
soapFault.getFaultStringOrReason());
}
}
/**
* Returns the SOAP fault code to check for, given the SOAP version.
*/
protected abstract QName getExpectedFaultCode(SoapVersion version);
}

View File

@@ -22,7 +22,7 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ErrorResponseCallbackTest {
public class ErrorResponseCreatorTest {
@Test
public void callback() throws IOException {

View File

@@ -20,7 +20,7 @@ import java.io.IOException;
import org.junit.Test;
public class ExceptionResponseCallbackTest {
public class ExceptionResponseCreatorTest {
@Test(expected = IOException.class)
public void ioException() throws Exception {

View File

@@ -33,7 +33,7 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class SoapFaultResponseCallbackTest {
public class SoapFaultResponseCreatorTest {
private SoapMessage response;