SWS-632 - Renamed Assert to AssertionErrors, to reduce confusion with JUnit's and Spring's Assert classes.

- Added SOAP fault expectations
This commit is contained in:
Arjen Poutsma
2010-11-02 12:13:27 +00:00
parent 83ce414350
commit 3f0e9f7d44
9 changed files with 174 additions and 11 deletions

View File

@@ -30,8 +30,8 @@ import org.springframework.xml.xpath.XPathExpressionFactory;
import org.w3c.dom.Node;
import static org.springframework.ws.test.support.Assert.assertEquals;
import static org.springframework.ws.test.support.Assert.fail;
import static org.springframework.ws.test.support.AssertionErrors.assertEquals;
import static org.springframework.ws.test.support.AssertionErrors.fail;
/**
* Default implementation of {@link XPathExpectations}.

View File

@@ -23,7 +23,7 @@ import java.util.Locale;
import org.springframework.ws.soap.SoapBody;
import org.springframework.ws.soap.SoapMessage;
import static org.springframework.ws.test.support.Assert.fail;
import static org.springframework.ws.test.support.AssertionErrors.fail;
/**
* Implementation of {@link ResponseCreator} that responds with a SOAP fault.

View File

@@ -26,8 +26,8 @@ import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapHeaderElement;
import org.springframework.ws.soap.SoapMessage;
import static org.springframework.ws.test.support.Assert.assertTrue;
import static org.springframework.ws.test.support.Assert.fail;
import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
import static org.springframework.ws.test.support.AssertionErrors.fail;
/**
* Matches SOAP headers.

View File

@@ -20,7 +20,7 @@ import java.net.URI;
import org.springframework.ws.WebServiceMessage;
import static org.springframework.ws.test.support.Assert.assertEquals;
import static org.springframework.ws.test.support.AssertionErrors.assertEquals;
/**
* Matches {@link URI}s.

View File

@@ -34,7 +34,7 @@ import org.springframework.ws.transport.WebServiceMessageReceiver;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import static org.springframework.ws.test.support.Assert.fail;
import static org.springframework.ws.test.support.AssertionErrors.fail;
/**
* <strong>Main entry point for server-side Web service testing</strong>. Typically used to test a {@link

View File

@@ -17,13 +17,22 @@
package org.springframework.ws.test.server;
import java.io.IOException;
import java.util.Locale;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import org.springframework.core.io.Resource;
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.PayloadDiffMatcher;
import org.springframework.xml.transform.ResourceSource;
import static org.springframework.ws.test.support.AssertionErrors.assertEquals;
import static org.springframework.ws.test.support.AssertionErrors.fail;
/**
* Factory methods for {@link ResponseMatcher} classes. Typically used to provide input for {@link
* ResponseActions#andExpect(ResponseMatcher)}.
@@ -48,6 +57,8 @@ public abstract class ResponseMatchers {
};
}
// Payload
/**
* Expects the given {@link Source} XML payload.
*
@@ -73,4 +84,153 @@ public abstract class ResponseMatchers {
return payload(new ResourceSource(payload));
}
// SOAP Fault
/**
* Expects a {@code MustUnderstand} fault.
*
* @see org.springframework.ws.soap.SoapBody#addMustUnderstandFault(String, Locale)
*/
public static ResponseMatcher mustUnderstandFault() {
return mustUnderstandFault(null);
}
/**
* Expects a {@code MustUnderstand} fault with a particular fault string or reason.
*
* @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text. If {@code null} the fault string or
* reason text will not be verified
* @see org.springframework.ws.soap.SoapBody#addMustUnderstandFault(String, Locale)
*/
public static ResponseMatcher mustUnderstandFault(String faultStringOrReason) {
return new SoapFaultResponseMatcher(faultStringOrReason) {
@Override
protected QName getExpectedFaultCode(SoapVersion version) {
return version.getMustUnderstandFaultName();
}
};
}
/**
* Expects a {@code Client} (SOAP 1.1) or {@code Sender} (SOAP 1.2) fault.
*
* @see org.springframework.ws.soap.SoapBody#addClientOrSenderFault(String, Locale)
*/
public static ResponseMatcher clientOrSenderFault() {
return clientOrSenderFault(null);
}
/**
* Expects a {@code Client} (SOAP 1.1) or {@code Sender} (SOAP 1.2) fault with a particular fault string or reason.
*
* @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text. If {@code null} the fault string or
* reason text will not be verified
* @see org.springframework.ws.soap.SoapBody#addClientOrSenderFault(String, Locale)
*/
public static ResponseMatcher clientOrSenderFault(String faultStringOrReason) {
return new SoapFaultResponseMatcher(faultStringOrReason) {
@Override
protected QName getExpectedFaultCode(SoapVersion version) {
return version.getClientOrSenderFaultName();
}
};
}
/**
* Expects a {@code Server} (SOAP 1.1) or {@code Receiver} (SOAP 1.2) fault.
*
* @see org.springframework.ws.soap.SoapBody#addServerOrReceiverFault(String, java.util.Locale)
*/
public static ResponseMatcher serverOrReceiverFault() {
return serverOrReceiverFault(null);
}
/**
* Expects a {@code Server} (SOAP 1.1) or {@code Receiver} (SOAP 1.2) fault with a particular fault string or reason.
*
* @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text. If {@code null} the fault string or
* reason text will not be verified
* @see org.springframework.ws.soap.SoapBody#addClientOrSenderFault(String, Locale)
*/
public static ResponseMatcher serverOrReceiverFault(String faultStringOrReason) {
return new SoapFaultResponseMatcher(faultStringOrReason) {
@Override
protected QName getExpectedFaultCode(SoapVersion version) {
return version.getServerOrReceiverFaultName();
}
};
}
/**
* Expects a {@code VersionMismatch} fault.
*
* @see org.springframework.ws.soap.SoapBody#addVersionMismatchFault(String, java.util.Locale)
*/
public static ResponseMatcher versionMismatchFault() {
return versionMismatchFault(null);
}
/**
* Expects a {@code VersionMismatch} fault with a particular fault string or reason.
*
* @param faultStringOrReason the SOAP 1.1 fault string or SOAP 1.2 reason text. If {@code null} the fault string or
* reason text will not be verified
* @see org.springframework.ws.soap.SoapBody#addClientOrSenderFault(String, Locale)
*/
public static ResponseMatcher versionMismatchFault(String faultStringOrReason) {
return new SoapFaultResponseMatcher(faultStringOrReason) {
@Override
protected QName getExpectedFaultCode(SoapVersion version) {
return version.getVersionMismatchFaultName();
}
};
}
private static abstract class SoapResponseMatcher implements ResponseMatcher {
public final void match(WebServiceMessage response) throws IOException, AssertionError {
if (!(response instanceof SoapMessage)) {
fail("Response is not a SOAP message");
return;
}
SoapMessage soapResponse = (SoapMessage) response;
match(soapResponse);
}
protected abstract void match(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 match(SoapMessage response) throws IOException, AssertionError {
SoapBody responseBody = response.getSoapBody();
if (responseBody == null) {
fail("Response has no SOAP Body");
return;
}
if (!responseBody.hasFault()) {
fail("Response has no SOAP Fault");
return;
}
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);
}
}

View File

@@ -23,7 +23,10 @@ package org.springframework.ws.test.support;
* @author Arjen Poutsma
* @since 2.0
*/
public class Assert {
public abstract class AssertionErrors {
private AssertionErrors() {
}
/**
* Fails a test with the given message.

View File

@@ -23,8 +23,8 @@ import org.springframework.ws.WebServiceMessage;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
import static org.springframework.ws.test.support.Assert.assertTrue;
import static org.springframework.ws.test.support.Assert.fail;
import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
import static org.springframework.ws.test.support.AssertionErrors.fail;
/**
* Implementation of {@link org.springframework.ws.test.client.RequestMatcher} based on XMLUnit's {@link Diff}.

View File

@@ -27,7 +27,7 @@ import org.springframework.xml.transform.TransformerHelper;
import org.custommonkey.xmlunit.Diff;
import org.w3c.dom.Document;
import static org.springframework.ws.test.support.Assert.fail;
import static org.springframework.ws.test.support.AssertionErrors.fail;
/**
* Matches {@link Source} payloads.