Created support.matcher package, with support for generic WebService matching (irrespective of whether it's a request or response).
This commit is contained in:
@@ -24,7 +24,7 @@ import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.xml.transform.TransformerObjectSupport;
|
||||
import org.springframework.xml.transform.TransformerHelper;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.springframework.xml.xpath.XPathExpressionFactory;
|
||||
|
||||
@@ -40,12 +40,14 @@ import static org.springframework.ws.test.support.AssertionErrors.fail;
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.0
|
||||
*/
|
||||
class DefaultXPathExpectations extends TransformerObjectSupport implements XPathExpectations {
|
||||
class DefaultXPathExpectations implements XPathExpectations {
|
||||
|
||||
private final XPathExpression expression;
|
||||
|
||||
private final String expressionString;
|
||||
|
||||
private final TransformerHelper transformerHelper = new TransformerHelper();
|
||||
|
||||
DefaultXPathExpectations(String expression, Map<String, String> namespaces) {
|
||||
Assert.hasLength(expression, "'expression' must not be empty");
|
||||
this.expression = XPathExpressionFactory.createXPathExpression(expression, namespaces);
|
||||
@@ -119,7 +121,7 @@ class DefaultXPathExpectations extends TransformerObjectSupport implements XPath
|
||||
private Node transformToNode(WebServiceMessage request) {
|
||||
DOMResult domResult = new DOMResult();
|
||||
try {
|
||||
transform(request.getPayloadSource(), domResult);
|
||||
transformerHelper.transform(request.getPayloadSource(), domResult);
|
||||
return domResult.getNode();
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
|
||||
@@ -25,10 +25,10 @@ import javax.xml.transform.Source;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.test.support.PayloadDiffMatcher;
|
||||
import org.springframework.ws.test.support.matcher.PayloadDiffMatcher;
|
||||
import org.springframework.ws.test.support.matcher.SchemaValidatingMatcher;
|
||||
import org.springframework.ws.test.support.matcher.WebServiceMessageMatcher;
|
||||
import org.springframework.xml.transform.ResourceSource;
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
import org.springframework.xml.validation.XmlValidatorFactory;
|
||||
|
||||
/**
|
||||
* Factory methods for {@link RequestMatcher} classes. Typically used to provide input for {@link
|
||||
@@ -62,12 +62,7 @@ public abstract class RequestMatchers {
|
||||
*/
|
||||
public static RequestMatcher payload(Source payload) {
|
||||
Assert.notNull(payload, "'payload' must not be null");
|
||||
final PayloadDiffMatcher matcher = new PayloadDiffMatcher(payload);
|
||||
return new RequestMatcher() {
|
||||
public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError {
|
||||
matcher.match(request);
|
||||
}
|
||||
};
|
||||
return new WebServiceMessageMatcherAdapter(new PayloadDiffMatcher(payload));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,18 +83,8 @@ public abstract class RequestMatchers {
|
||||
* @param furtherSchemas further schemas, if necessary
|
||||
* @return the request matcher
|
||||
*/
|
||||
public static RequestMatcher validPayload(Resource schema, Resource... furtherSchemas) {
|
||||
try {
|
||||
Resource[] joinedSchemas = new Resource[furtherSchemas.length + 1];
|
||||
joinedSchemas[0] = schema;
|
||||
System.arraycopy(furtherSchemas, 0, joinedSchemas, 1, furtherSchemas.length);
|
||||
XmlValidator validator =
|
||||
XmlValidatorFactory.createValidator(joinedSchemas, XmlValidatorFactory.SCHEMA_W3C_XML);
|
||||
return new SchemaValidatingRequestMatcher(validator);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalArgumentException("Schema(s) could not be opened", ex);
|
||||
}
|
||||
public static RequestMatcher validPayload(Resource schema, Resource... furtherSchemas) throws IOException {
|
||||
return new WebServiceMessageMatcherAdapter(new SchemaValidatingMatcher(schema, furtherSchemas));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -155,4 +140,21 @@ public abstract class RequestMatchers {
|
||||
Assert.notNull(uri, "'uri' must not be null");
|
||||
return new UriMatcher(uri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapts a {@link org.springframework.ws.test.support.matcher.WebServiceMessageMatcher} to the {@link RequestMatcher} contract.
|
||||
*/
|
||||
private static class WebServiceMessageMatcherAdapter implements RequestMatcher {
|
||||
|
||||
private final WebServiceMessageMatcher adaptee;
|
||||
|
||||
private WebServiceMessageMatcherAdapter(WebServiceMessageMatcher adaptee) {
|
||||
this.adaptee = adaptee;
|
||||
}
|
||||
|
||||
public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError {
|
||||
adaptee.match(request);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
* 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.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
/**
|
||||
* Uses the {@link XmlValidator} to validate request payload.
|
||||
*
|
||||
* @author Lukas Krecan
|
||||
* @since 2.0
|
||||
*/
|
||||
class SchemaValidatingRequestMatcher implements RequestMatcher {
|
||||
|
||||
private final XmlValidator xmlValidator;
|
||||
|
||||
public SchemaValidatingRequestMatcher(XmlValidator xmlValidator) {
|
||||
Assert.notNull(xmlValidator, "XmlValidator has to be set");
|
||||
this.xmlValidator = xmlValidator;
|
||||
}
|
||||
|
||||
public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError {
|
||||
SAXParseException[] exceptions = xmlValidator.validate(request.getPayloadSource());
|
||||
if (!ObjectUtils.isEmpty(exceptions)) {
|
||||
throw new AssertionError("XML is not valid: " + Arrays.toString(exceptions));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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 org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.WebServiceMessageFactory;
|
||||
|
||||
/**
|
||||
* Abstract base class for the {@link org.springframework.ws.test.server.RequestCreator} interface.
|
||||
* <p/>
|
||||
* Creates a response using the given {@link org.springframework.ws.WebServiceMessageFactory}, and passes it on to
|
||||
* {@link #doWithRequest(org.springframework.ws.WebServiceMessage)}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.0
|
||||
*/
|
||||
abstract class AbstractRequestCreator implements RequestCreator {
|
||||
|
||||
public final WebServiceMessage createRequest(WebServiceMessageFactory messageFactory) throws IOException {
|
||||
WebServiceMessage request = messageFactory.createWebServiceMessage();
|
||||
doWithRequest(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
protected abstract void doWithRequest(WebServiceMessage request) throws IOException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.xml.transform.TransformerHelper;
|
||||
|
||||
/**
|
||||
* Implementation of {@link org.springframework.ws.test.server.RequestCreator} that creates a request based on a {@link
|
||||
* javax.xml.transform.Source}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.0
|
||||
*/
|
||||
class PayloadRequestCreator extends AbstractRequestCreator {
|
||||
|
||||
private final Source payload;
|
||||
|
||||
private TransformerHelper transformerHelper = new TransformerHelper();
|
||||
|
||||
PayloadRequestCreator(Source payload) {
|
||||
this.payload = payload;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doWithRequest(WebServiceMessage request) throws IOException {
|
||||
try {
|
||||
transformerHelper.transform(payload, request.getPayloadResult());
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new AssertionError("Could not transform request payload to message: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,14 +18,10 @@ package org.springframework.ws.test.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.WebServiceMessageFactory;
|
||||
import org.springframework.xml.transform.ResourceSource;
|
||||
import org.springframework.xml.transform.TransformerHelper;
|
||||
|
||||
/**
|
||||
* Factory methods for {@link RequestCreator} classes. Typically used to provide input for {@link
|
||||
@@ -61,47 +57,5 @@ public abstract class RequestCreators {
|
||||
return withPayload(new ResourceSource(payload));
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract base class for the {@link RequestCreator} interface.
|
||||
* <p/>
|
||||
* Creates a response using the given {@link org.springframework.ws.WebServiceMessageFactory}, and passes it on to
|
||||
* {@link #doWithRequest(org.springframework.ws.WebServiceMessage)}.
|
||||
*/
|
||||
private static abstract class AbstractRequestCreator implements RequestCreator {
|
||||
|
||||
public final WebServiceMessage createRequest(WebServiceMessageFactory messageFactory) throws IOException {
|
||||
WebServiceMessage request = messageFactory.createWebServiceMessage();
|
||||
doWithRequest(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
protected abstract void doWithRequest(WebServiceMessage request) throws IOException;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of {@link RequestCreator} that creates a request based on a {@link javax.xml.transform.Source}.
|
||||
*/
|
||||
private static class PayloadRequestCreator extends AbstractRequestCreator {
|
||||
|
||||
private final Source payload;
|
||||
|
||||
private TransformerHelper transformerHelper = new TransformerHelper();
|
||||
|
||||
PayloadRequestCreator(Source payload) {
|
||||
this.payload = payload;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doWithRequest(WebServiceMessage request) throws IOException {
|
||||
try {
|
||||
transformerHelper.transform(payload, request.getPayloadResult());
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
throw new AssertionError("Could not transform request payload to message: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,7 +27,9 @@ 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.ws.test.support.matcher.PayloadDiffMatcher;
|
||||
import org.springframework.ws.test.support.matcher.SchemaValidatingMatcher;
|
||||
import org.springframework.ws.test.support.matcher.WebServiceMessageMatcher;
|
||||
import org.springframework.xml.transform.ResourceSource;
|
||||
|
||||
import static org.springframework.ws.test.support.AssertionErrors.assertEquals;
|
||||
@@ -67,13 +69,7 @@ public abstract class ResponseMatchers {
|
||||
* @return the response matcher
|
||||
*/
|
||||
public static ResponseMatcher payload(Source payload) {
|
||||
final PayloadDiffMatcher matcher = new PayloadDiffMatcher(payload);
|
||||
return new ResponseMatcher() {
|
||||
public void match(WebServiceMessage request, WebServiceMessage response)
|
||||
throws IOException, AssertionError {
|
||||
matcher.match(response);
|
||||
}
|
||||
};
|
||||
return new WebServiceMessageMatcherAdapter(new PayloadDiffMatcher(payload));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,6 +82,18 @@ public abstract class ResponseMatchers {
|
||||
return payload(new ResourceSource(payload));
|
||||
}
|
||||
|
||||
/**
|
||||
* Expects the payload to validate against the given XSD schema(s).
|
||||
*
|
||||
* @param schema the schema
|
||||
* @param furtherSchemas further schemas, if necessary
|
||||
* @return the response matcher
|
||||
*/
|
||||
public static ResponseMatcher validPayload(Resource schema, Resource... furtherSchemas) throws IOException {
|
||||
return new WebServiceMessageMatcherAdapter(new SchemaValidatingMatcher(schema, furtherSchemas));
|
||||
}
|
||||
|
||||
|
||||
// SOAP Fault
|
||||
|
||||
/**
|
||||
@@ -242,4 +250,20 @@ public abstract class ResponseMatchers {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapts a {@link WebServiceMessageMatcher} to the {@link ResponseMatcher} contract.
|
||||
*/
|
||||
private static class WebServiceMessageMatcherAdapter implements ResponseMatcher {
|
||||
|
||||
private final WebServiceMessageMatcher adaptee;
|
||||
|
||||
private WebServiceMessageMatcherAdapter(WebServiceMessageMatcher adaptee) {
|
||||
this.adaptee = adaptee;
|
||||
}
|
||||
|
||||
public void match(WebServiceMessage request, WebServiceMessage response) throws IOException, AssertionError {
|
||||
adaptee.match(response);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.test.support;
|
||||
package org.springframework.ws.test.support.matcher;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -24,40 +24,31 @@ import org.custommonkey.xmlunit.Diff;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
|
||||
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}.
|
||||
* Implementation of {@link WebServiceMessageMatcher} based on XMLUnit's {@link Diff}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.0
|
||||
*/
|
||||
public abstract class DiffMatcher {
|
||||
public abstract class DiffMatcher implements WebServiceMessageMatcher {
|
||||
|
||||
static {
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
}
|
||||
|
||||
public final void match(WebServiceMessage request) throws IOException, AssertionError {
|
||||
try {
|
||||
Diff diff = createDiff(request);
|
||||
assertTrue("Messages are different, " + diff.toString(), diff.similar());
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw ex;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
fail("Could not create Diff: " + ex.getMessage());
|
||||
}
|
||||
public final void match(WebServiceMessage message) throws IOException, AssertionError {
|
||||
Diff diff = createDiff(message);
|
||||
assertTrue("Messages are different, " + diff.toString(), diff.similar());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link org.custommonkey.xmlunit.Diff} for the given request message.
|
||||
* Creates a {@link Diff} for the given message.
|
||||
*
|
||||
* @param request the request message
|
||||
* @param message the message
|
||||
* @return the diff
|
||||
* @throws Exception in case of errors
|
||||
*/
|
||||
protected abstract Diff createDiff(WebServiceMessage request) throws Exception;
|
||||
protected abstract Diff createDiff(WebServiceMessage message);
|
||||
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.test.support;
|
||||
package org.springframework.ws.test.support.matcher;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
@@ -48,23 +48,29 @@ public class PayloadDiffMatcher extends DiffMatcher {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Diff createDiff(WebServiceMessage request) throws Exception {
|
||||
Source payload = request.getPayloadSource();
|
||||
protected final Diff createDiff(WebServiceMessage message) {
|
||||
Source payload = message.getPayloadSource();
|
||||
if (payload == null) {
|
||||
fail("Request message does not contain payload");
|
||||
}
|
||||
return createDiff(payload);
|
||||
}
|
||||
|
||||
protected Diff createDiff(Source payload) throws TransformerException {
|
||||
protected Diff createDiff(Source payload) {
|
||||
Document expectedDocument = createDocumentFromSource(expected);
|
||||
Document actualDocument = createDocumentFromSource(payload);
|
||||
return new Diff(expectedDocument, actualDocument);
|
||||
}
|
||||
|
||||
private Document createDocumentFromSource(Source source) throws TransformerException {
|
||||
DOMResult result = new DOMResult();
|
||||
transformerHelper.transform(source, result);
|
||||
return (Document) result.getNode();
|
||||
private Document createDocumentFromSource(Source source) {
|
||||
try {
|
||||
DOMResult result = new DOMResult();
|
||||
transformerHelper.transform(source, result);
|
||||
return (Document) result.getNode();
|
||||
}
|
||||
catch (TransformerException ex) {
|
||||
fail("Could not transform source to DOMResult" + ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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.support.matcher;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
import org.springframework.xml.validation.XmlValidatorFactory;
|
||||
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
import static org.springframework.ws.test.support.AssertionErrors.fail;
|
||||
|
||||
/**
|
||||
* Uses the {@link XmlValidator} to validate request payload.
|
||||
*
|
||||
* @author Lukas Krecan
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.0
|
||||
*/
|
||||
public class SchemaValidatingMatcher implements WebServiceMessageMatcher {
|
||||
|
||||
private final XmlValidator xmlValidator;
|
||||
|
||||
/**
|
||||
* Creates a {@code SchemaValidatingMatcher} based on the given schema resource(s).
|
||||
*
|
||||
* @param schema the schema
|
||||
* @param furtherSchemas further schemas, if necessary
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
public SchemaValidatingMatcher(Resource schema, Resource... furtherSchemas) throws IOException {
|
||||
Assert.notNull(schema, "'schema' must not be null");
|
||||
Resource[] joinedSchemas = new Resource[furtherSchemas.length + 1];
|
||||
joinedSchemas[0] = schema;
|
||||
System.arraycopy(furtherSchemas, 0, joinedSchemas, 1, furtherSchemas.length);
|
||||
xmlValidator = XmlValidatorFactory.createValidator(joinedSchemas, XmlValidatorFactory.SCHEMA_W3C_XML);
|
||||
|
||||
}
|
||||
|
||||
public void match(WebServiceMessage message) throws IOException, AssertionError {
|
||||
SAXParseException[] exceptions = xmlValidator.validate(message.getPayloadSource());
|
||||
if (!ObjectUtils.isEmpty(exceptions)) {
|
||||
fail("XML is not valid: " + Arrays.toString(exceptions));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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.support.matcher;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
|
||||
/**
|
||||
* Defines the general contract for matching messages to expectations.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface WebServiceMessageMatcher {
|
||||
|
||||
/**
|
||||
* Matches the given message against the expectations. Implementations typically make use of JUnit-based
|
||||
* assertions.
|
||||
*
|
||||
* @param message the message
|
||||
* @throws IOException in case of I/O errors
|
||||
* @throws AssertionError if expectations are not met
|
||||
*/
|
||||
void match(WebServiceMessage message) throws IOException, AssertionError;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Provides the generic {@link org.springframework.ws.test.support.matcher.WebServiceMessageMatcher WebServiceMessageMatcher}
|
||||
* interface, and implementations.
|
||||
*/
|
||||
package org.springframework.ws.test.support.matcher;
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Support classes for the testing framework, used by the classes in the {@link org.springframework.ws.test.client} and
|
||||
* {@link org.springframework.ws.test.server} packages.
|
||||
*/
|
||||
package org.springframework.ws.test.support;
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.test.support;
|
||||
package org.springframework.ws.test.support.matcher;
|
||||
|
||||
import javax.xml.soap.MessageFactory;
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.ws.test.client;
|
||||
package org.springframework.ws.test.support.matcher;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -23,15 +23,13 @@ import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
import org.springframework.xml.validation.XmlValidator;
|
||||
import org.springframework.xml.validation.XmlValidatorFactory;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
public class SchemaValidatingRequestMatcherTest {
|
||||
public class SchemaValidatingMatcherTest {
|
||||
|
||||
private Resource schema2;
|
||||
|
||||
@@ -42,7 +40,7 @@ public class SchemaValidatingRequestMatcherTest {
|
||||
@Before
|
||||
public void setUp() {
|
||||
message = createMock(WebServiceMessage.class);
|
||||
schema1 = new ClassPathResource("schemaValidatingRequestMatcherTest.xsd", SchemaValidatingRequestMatcherTest.class);
|
||||
schema1 = new ClassPathResource("schemaValidatingMatcherTest.xsd", SchemaValidatingMatcherTest.class);
|
||||
schema2 = new ByteArrayResource("".getBytes());
|
||||
}
|
||||
|
||||
@@ -51,11 +49,11 @@ public class SchemaValidatingRequestMatcherTest {
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(
|
||||
"<test xmlns=\"http://www.example.org/schema\"><number>0</number><text>text</text></test>"));
|
||||
|
||||
RequestMatcher requestMatcher = RequestMatchers.validPayload(schema1);
|
||||
SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema1);
|
||||
|
||||
replay(message);
|
||||
|
||||
requestMatcher.match(null, message);
|
||||
matcher.match(message);
|
||||
|
||||
verify(message);
|
||||
}
|
||||
@@ -65,11 +63,11 @@ public class SchemaValidatingRequestMatcherTest {
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(
|
||||
"<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>"));
|
||||
|
||||
RequestMatcher requestMatcher = RequestMatchers.validPayload(schema1);
|
||||
SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema1);
|
||||
|
||||
replay(message);
|
||||
|
||||
requestMatcher.match(null, message);
|
||||
matcher.match(message);
|
||||
|
||||
verify(message);
|
||||
}
|
||||
@@ -79,11 +77,11 @@ public class SchemaValidatingRequestMatcherTest {
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(
|
||||
"<test xmlns=\"http://www.example.org/schema\"><number>0</number><text>text</text></test>"));
|
||||
|
||||
RequestMatcher requestMatcher = RequestMatchers.validPayload(schema1, schema2);
|
||||
SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema1, schema2);
|
||||
|
||||
replay(message);
|
||||
|
||||
requestMatcher.match(null, message);
|
||||
matcher.match(message);
|
||||
|
||||
verify(message);
|
||||
}
|
||||
@@ -93,11 +91,11 @@ public class SchemaValidatingRequestMatcherTest {
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(
|
||||
"<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>"));
|
||||
|
||||
RequestMatcher requestMatcher = RequestMatchers.validPayload(schema1, schema2);
|
||||
SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema1, schema2);
|
||||
|
||||
replay(message);
|
||||
|
||||
requestMatcher.match(null, message);
|
||||
matcher.match(message);
|
||||
|
||||
verify(message);
|
||||
}
|
||||
@@ -107,11 +105,11 @@ public class SchemaValidatingRequestMatcherTest {
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(
|
||||
"<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>"));
|
||||
|
||||
RequestMatcher requestMatcher = RequestMatchers.validPayload(schema2, schema1);
|
||||
SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema2, schema1);
|
||||
|
||||
replay(message);
|
||||
|
||||
requestMatcher.match(null, message);
|
||||
matcher.match(message);
|
||||
|
||||
verify(message);
|
||||
}
|
||||
@@ -121,12 +119,11 @@ public class SchemaValidatingRequestMatcherTest {
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(
|
||||
"<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>"));
|
||||
|
||||
XmlValidator validator = XmlValidatorFactory.createValidator(schema1, XmlValidatorFactory.SCHEMA_W3C_XML);
|
||||
RequestMatcher requestMatcher = new SchemaValidatingRequestMatcher(validator);
|
||||
SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema1);
|
||||
|
||||
replay(message);
|
||||
|
||||
requestMatcher.match(null, message);
|
||||
matcher.match(message);
|
||||
|
||||
verify(message);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/schema" xmlns:tns="http://www.example.org/schema" elementFormDefault="qualified">
|
||||
|
||||
<element name="test" type="tns:testMessage"/>
|
||||
|
||||
<complexType name="testMessage">
|
||||
<sequence>
|
||||
<element name="number" type="int"/>
|
||||
<element name="text" type="string" minOccurs="0"/>
|
||||
</sequence>
|
||||
</complexType>
|
||||
</schema>
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/schema" xmlns:tns="http://www.example.org/schema" elementFormDefault="qualified">
|
||||
|
||||
<element name="test" type="tns:testMessage"/>
|
||||
|
||||
<complexType name="testMessage">
|
||||
<sequence>
|
||||
<element name="number" type="int"/>
|
||||
<element name="text" type="string" minOccurs="0"/>
|
||||
</sequence>
|
||||
</complexType>
|
||||
</schema>
|
||||
Reference in New Issue
Block a user