SWS-694 - Print SOAP message if validation fails

This commit is contained in:
Arjen Poutsma
2011-03-21 11:28:14 +00:00
parent fca3df0327
commit 0abc553b4e
11 changed files with 202 additions and 50 deletions

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2011 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
* 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,
@@ -37,6 +37,6 @@ class UriMatcher implements RequestMatcher {
}
public void match(URI actual, WebServiceMessage request) {
assertEquals("Unexpected connection", expected, actual);
assertEquals("Unexpected connection", expected, actual, "Payload", request.getPayloadSource());
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2011 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
* 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,
@@ -16,6 +16,8 @@
package org.springframework.ws.test.support;
import javax.xml.transform.Source;
/**
* JUnit independent assertion class.
*
@@ -37,6 +39,21 @@ public abstract class AssertionErrors {
throw new AssertionError(message);
}
/**
* Fails a test with the given message and source.
*
* @param message the message
* @param source the source
*/
public static void fail(String message, String sourceLabel, Source source) {
if (source != null) {
throw new SourceAssertionError(message, sourceLabel, source);
}
else {
fail(message);
}
}
/**
* Asserts that a condition is {@code true}. If not, throws an {@link AssertionError} with the given message.
*
@@ -44,8 +61,19 @@ public abstract class AssertionErrors {
* @param condition the condition to test for
*/
public static void assertTrue(String message, boolean condition) {
assertTrue(message, condition, null, null);
}
/**
* Asserts that a condition is {@code true}. If not, throws an {@link AssertionError} with the given message and
* source.
*
* @param message the message
* @param condition the condition to test for
*/
public static void assertTrue(String message, boolean condition, String sourceLabel, Source source) {
if (!condition) {
fail(message);
fail(message, sourceLabel, source);
}
}
@@ -57,12 +85,25 @@ public abstract class AssertionErrors {
* @param actual the actual value
*/
public static void assertEquals(String message, Object expected, Object actual) {
assertEquals(message, expected, actual, null, null);
}
/**
* Asserts that two objects are equal. If not, an {@link AssertionError} is thrown with the given message.
*
* @param message the message
* @param expected the expected value
* @param actual the actual value
* @param source the source
*/
public static void assertEquals(String message, Object expected, Object actual, String sourceLabel, Source source) {
if (expected == null && actual == null) {
return;
}
if (expected != null && expected.equals(actual)) {
return;
}
fail(message + " expected:<" + expected + "> but was:<" + actual + ">");
fail(message + " expected:<" + expected + "> but was:<" + actual + ">", sourceLabel, source);
}
}

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2005-2011 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;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.TransformerHelper;
/**
* Subclass of {@link AssertionError} that also contains a {@link Source} for more context.
*
* @author Lukas Krecan
* @author Arjen Poutsma
* @since 2.0.1
*/
public class SourceAssertionError extends AssertionError {
private final String sourceLabel;
private final Source source;
private final TransformerHelper transformerHelper = new TransformerHelper();
/**
* Creates a new instance of the {@code SourceAssertionError} class with the given parameters.
*/
public SourceAssertionError(String detailMessage, String sourceLabel, Source source) {
super(detailMessage);
this.sourceLabel = sourceLabel;
this.source = source;
}
/**
* Returns the source context of this error.
* @return the source
*/
public Source getSource() {
return source;
}
@Override
public String getMessage() {
StringBuilder builder = new StringBuilder();
builder.append(super.getMessage());
String sourceString = getSourceString();
if (sourceString != null) {
String newLine = System.getProperty("line.separator");
builder.append(newLine);
String label = sourceLabel != null ? sourceLabel : "Source";
builder.append(label);
builder.append(": ");
builder.append(sourceString);
}
return builder.toString();
}
private String getSourceString() {
if (source != null) {
try {
StringResult result = new StringResult();
Transformer transformer = createNonIndentingTransformer();
transformer.transform(source, result);
return result.toString();
}
catch (TransformerException ex) {
// Ignore
}
}
return null;
}
private Transformer createNonIndentingTransformer() throws TransformerConfigurationException {
Transformer transformer = transformerHelper.createTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "no");
return transformer;
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2011 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
* 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,
@@ -39,7 +39,7 @@ public abstract class DiffMatcher implements WebServiceMessageMatcher {
public final void match(WebServiceMessage message) throws IOException, AssertionError {
Diff diff = createDiff(message);
assertTrue("Messages are different, " + diff.toString(), diff.similar());
assertTrue("Messages are different, " + diff.toString(), diff.similar(), "Payload", message.getPayloadSource());
}
/**

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2011 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
* 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,
@@ -60,7 +60,7 @@ public class SchemaValidatingMatcher implements WebServiceMessageMatcher {
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));
fail("XML is not valid: " + Arrays.toString(exceptions), "Payload", message.getPayloadSource());
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2011 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
* 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,
@@ -51,7 +51,8 @@ public class SoapHeaderMatcher extends AbstractSoapMessageMatcher {
@Override
protected void match(SoapMessage soapMessage) throws IOException, AssertionError {
SoapHeader soapHeader = soapMessage.getSoapHeader();
assertTrue("SOAP message [" + soapMessage + "] does not contain SOAP header", soapHeader != null);
assertTrue("SOAP message [" + soapMessage + "] does not contain SOAP header", soapHeader != null, "Envelope",
soapMessage.getEnvelope().getSource());
Iterator<SoapHeaderElement> soapHeaderElementIterator = soapHeader.examineAllHeaderElements();
boolean found = false;
@@ -62,6 +63,7 @@ public class SoapHeaderMatcher extends AbstractSoapMessageMatcher {
break;
}
}
assertTrue("SOAP header [" + soapHeaderName + "] not found", found);
assertTrue("SOAP header [" + soapHeaderName + "] not found", found, "Envelope",
soapMessage.getEnvelope().getSource());
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2011 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
* 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,
@@ -73,7 +73,7 @@ public class XPathExpectationsHelper {
Node payload = transformToNode(message);
Node result = expression.evaluateAsNode(payload);
if (result == null) {
fail("No match for \"" + expressionString + "\" found");
fail("No match for \"" + expressionString + "\" found", "Payload", message.getPayloadSource());
}
}
};
@@ -85,7 +85,7 @@ public class XPathExpectationsHelper {
Node payload = transformToNode(message);
Node result = expression.evaluateAsNode(payload);
if (result != null) {
fail("Match for \"" + expressionString + "\" found");
fail("Match for \"" + expressionString + "\" found", "Payload", message.getPayloadSource());
}
}
};
@@ -97,7 +97,7 @@ public class XPathExpectationsHelper {
Node payload = transformToNode(message);
boolean result = expression.evaluateAsBoolean(payload);
assertEquals("Evaluation of XPath expression \"" + expressionString + "\" failed.", expectedValue,
result);
result, "Payload", message.getPayloadSource());
}
};
@@ -113,7 +113,7 @@ public class XPathExpectationsHelper {
Node payload = transformToNode(message);
double result = expression.evaluateAsNumber(payload);
assertEquals("Evaluation of XPath expression \"" + expressionString + "\" failed.", expectedValue,
result);
result, "Payload", message.getPayloadSource());
}
};
@@ -126,7 +126,7 @@ public class XPathExpectationsHelper {
Node payload = transformToNode(message);
String result = expression.evaluateAsString(payload);
assertEquals("Evaluation of XPath expression \"" + expressionString + "\" failed.", expectedValue,
result);
result, "Payload", message.getPayloadSource());
}
};
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2011 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
* 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,
@@ -18,21 +18,31 @@ package org.springframework.ws.test.client;
import java.net.URI;
import org.springframework.ws.WebServiceMessage;
import org.junit.Test;
import static org.easymock.EasyMock.*;
public class UriMatcherTest {
private static final URI GOOD_URI = URI.create("http://localhost");
@Test
public void match() {
WebServiceMessage message = createMock(WebServiceMessage.class);
expect(message.getPayloadSource()).andReturn(null);
replay(message);
UriMatcher matcher = new UriMatcher(GOOD_URI);
matcher.match(GOOD_URI, null);
matcher.match(GOOD_URI, message);
}
@Test(expected = AssertionError.class)
public void nonMatch() {
WebServiceMessage message = createMock(WebServiceMessage.class);
expect(message.getPayloadSource()).andReturn(null);
replay(message);
UriMatcher matcher = new UriMatcher(GOOD_URI);
matcher.match(URI.create("http://www.example.org"), null);
matcher.match(URI.create("http://www.example.org"), message);
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2011 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
* 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,
@@ -33,7 +33,7 @@ public class PayloadDiffMatcherTest {
public void match() throws Exception {
String xml = "<element xmlns='http://example.com'/>";
WebServiceMessage message = createMock(WebServiceMessage.class);
expect(message.getPayloadSource()).andReturn(new StringSource(xml));
expect(message.getPayloadSource()).andReturn(new StringSource(xml)).times(2);
replay(message);
PayloadDiffMatcher matcher = new PayloadDiffMatcher(new StringSource(xml));
@@ -46,7 +46,7 @@ public class PayloadDiffMatcherTest {
public void nonMatch() throws Exception {
String actual = "<element1 xmlns='http://example.com'/>";
WebServiceMessage message = createMock(WebServiceMessage.class);
expect(message.getPayloadSource()).andReturn(new StringSource(actual));
expect(message.getPayloadSource()).andReturn(new StringSource(actual)).times(2);
replay(message);
String expected = "<element2 xmlns='http://example.com'/>";
@@ -63,4 +63,4 @@ public class PayloadDiffMatcherTest {
matcher.createDiff(soapMessage);
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2011 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
* 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,
@@ -61,7 +61,7 @@ public class SchemaValidatingMatcherTest {
@Test(expected = AssertionError.class)
public void singleSchemaNonMatch() throws IOException, AssertionError {
expect(message.getPayloadSource()).andReturn(new StringSource(
"<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>"));
"<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>")).times(2);
SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema1);
@@ -89,7 +89,7 @@ public class SchemaValidatingMatcherTest {
@Test(expected = AssertionError.class)
public void multipleSchemaNotOk() throws IOException, AssertionError {
expect(message.getPayloadSource()).andReturn(new StringSource(
"<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>"));
"<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>")).times(2);
SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema1, schema2);
@@ -103,7 +103,7 @@ public class SchemaValidatingMatcherTest {
@Test(expected = AssertionError.class)
public void multipleSchemaDifferentOrderNotOk() throws IOException, AssertionError {
expect(message.getPayloadSource()).andReturn(new StringSource(
"<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>"));
"<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>")).times(2);
SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema2, schema1);
@@ -117,7 +117,7 @@ public class SchemaValidatingMatcherTest {
@Test(expected = AssertionError.class)
public void xmlValidatorNotOk() throws IOException, AssertionError {
expect(message.getPayloadSource()).andReturn(new StringSource(
"<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>"));
"<test xmlns=\"http://www.example.org/schema\"><number>a</number><text>text</text></test>")).times(2);
SchemaValidatingMatcher matcher = new SchemaValidatingMatcher(schema1);

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2005-2010 the original author or authors.
* Copyright 2005-2011 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
* 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,
@@ -53,7 +53,7 @@ public class XPathExpectationsHelperTest {
assertNotNull(matcher);
WebServiceMessage message = createMock(WebServiceMessage.class);
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b/></a>"));
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b/></a>")).times(2);
replay(message);
@@ -83,7 +83,7 @@ public class XPathExpectationsHelperTest {
assertNotNull(matcher);
WebServiceMessage message = createMock(WebServiceMessage.class);
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b/></a>"));
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b/></a>")).times(2);
replay(message);
@@ -97,7 +97,7 @@ public class XPathExpectationsHelperTest {
assertNotNull(matcher);
WebServiceMessage message = createMock(WebServiceMessage.class);
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>"));
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>")).times(2);
replay(message);
@@ -113,7 +113,7 @@ public class XPathExpectationsHelperTest {
assertNotNull(matcher);
WebServiceMessage message = createMock(WebServiceMessage.class);
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>"));
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>")).times(2);
replay(message);
@@ -127,7 +127,7 @@ public class XPathExpectationsHelperTest {
assertNotNull(matcher);
WebServiceMessage message = createMock(WebServiceMessage.class);
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>"));
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>")).times(2);
replay(message);
@@ -143,7 +143,7 @@ public class XPathExpectationsHelperTest {
assertNotNull(matcher);
WebServiceMessage message = createMock(WebServiceMessage.class);
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>"));
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>")).times(2);
replay(message);
@@ -157,7 +157,7 @@ public class XPathExpectationsHelperTest {
assertNotNull(matcher);
WebServiceMessage message = createMock(WebServiceMessage.class);
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>"));
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>")).times(2);
replay(message);
@@ -173,7 +173,7 @@ public class XPathExpectationsHelperTest {
assertNotNull(matcher);
WebServiceMessage message = createMock(WebServiceMessage.class);
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>"));
expect(message.getPayloadSource()).andReturn(new StringSource("<a><b>1</b></a>")).times(2);
replay(message);
@@ -207,7 +207,7 @@ public class XPathExpectationsHelperTest {
WebServiceMessage message = createMock(WebServiceMessage.class);
expect(message.getPayloadSource())
.andReturn(new StringSource("<a:a xmlns:a=\"http://example.org\"><a:b/></a:a>"));
.andReturn(new StringSource("<a:a xmlns:a=\"http://example.org\"><a:b/></a:a>")).times(2);
replay(message);