@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.ws.test.support.matcher.xmlunit2;
|
||||
|
||||
import static org.springframework.ws.test.support.AssertionErrors.fail;
|
||||
import static org.springframework.ws.test.support.AssertionErrors.*;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerException;
|
||||
@@ -33,6 +33,7 @@ import org.xmlunit.diff.Diff;
|
||||
* Matches {@link Source} payloads.
|
||||
*
|
||||
* @author Greg Turnquist
|
||||
* @author Mikołaj Fejzer
|
||||
* @since 4.0
|
||||
*/
|
||||
public class PayloadDiffMatcher extends DiffMatcher {
|
||||
@@ -42,22 +43,28 @@ public class PayloadDiffMatcher extends DiffMatcher {
|
||||
private final TransformerHelper transformerHelper = new TransformerHelper();
|
||||
|
||||
public PayloadDiffMatcher(Source expected) {
|
||||
|
||||
Assert.notNull(expected, "'expected' must not be null");
|
||||
this.expected = expected;
|
||||
}
|
||||
|
||||
@Override
|
||||
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) {
|
||||
|
||||
Document expectedDocument = createDocumentFromSource(expected);
|
||||
Document actualDocument = createDocumentFromSource(payload);
|
||||
|
||||
return DiffBuilder.compare(expectedDocument) //
|
||||
.withTest(actualDocument) //
|
||||
.ignoreWhitespace() //
|
||||
@@ -66,11 +73,14 @@ public class PayloadDiffMatcher extends DiffMatcher {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -16,17 +16,13 @@
|
||||
|
||||
package org.springframework.ws.test.support.matcher.xmlunit2;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
import jakarta.xml.soap.MessageFactory;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.ws.WebServiceMessage;
|
||||
import org.springframework.ws.soap.SoapMessage;
|
||||
import org.springframework.ws.soap.saaj.SaajSoapMessage;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
@@ -35,12 +31,13 @@ public class PayloadDiffMatcherTest {
|
||||
@Test
|
||||
public void match() {
|
||||
|
||||
String xml = "<element xmlns='http://example.com'/>";
|
||||
var xml = "<element xmlns='http://example.com'/>";
|
||||
WebServiceMessage message = createMock(WebServiceMessage.class);
|
||||
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(xml)).times(2);
|
||||
replay(message);
|
||||
|
||||
PayloadDiffMatcher matcher = new PayloadDiffMatcher(new StringSource(xml));
|
||||
var matcher = new PayloadDiffMatcher(new StringSource(xml));
|
||||
matcher.match(message);
|
||||
|
||||
verify(message);
|
||||
@@ -49,31 +46,32 @@ public class PayloadDiffMatcherTest {
|
||||
@Test
|
||||
public void matchIgnoringWhitespace() {
|
||||
|
||||
String xml = "<response><success>true</success></response>";
|
||||
String xmlWithAdditionalWhitespace = "<response> <success>true</success> </response>";
|
||||
var xml = "<response><success>true</success></response>";
|
||||
var xmlWithAdditionalWhitespace = "<response> <success>true</success> </response>";
|
||||
WebServiceMessage message = createMock(WebServiceMessage.class);
|
||||
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(xml)).times(2);
|
||||
replay(message);
|
||||
|
||||
PayloadDiffMatcher matcher = new PayloadDiffMatcher(new StringSource(xmlWithAdditionalWhitespace));
|
||||
var matcher = new PayloadDiffMatcher(new StringSource(xmlWithAdditionalWhitespace));
|
||||
matcher.match(message);
|
||||
|
||||
verify(message);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void nonMatch() {
|
||||
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> {
|
||||
|
||||
String actual = "<element1 xmlns='http://example.com'/>";
|
||||
var actual = "<element1 xmlns='http://example.com'/>";
|
||||
WebServiceMessage message = createMock(WebServiceMessage.class);
|
||||
|
||||
expect(message.getPayloadSource()).andReturn(new StringSource(actual)).times(2);
|
||||
replay(message);
|
||||
|
||||
String expected = "<element2 xmlns='http://example.com'/>";
|
||||
PayloadDiffMatcher matcher = new PayloadDiffMatcher(new StringSource(expected));
|
||||
var expected = "<element2 xmlns='http://example.com'/>";
|
||||
var matcher = new PayloadDiffMatcher(new StringSource(expected));
|
||||
matcher.match(message);
|
||||
});
|
||||
}
|
||||
@@ -83,12 +81,11 @@ public class PayloadDiffMatcherTest {
|
||||
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> {
|
||||
|
||||
PayloadDiffMatcher matcher = new PayloadDiffMatcher(new StringSource("<message/>"));
|
||||
MessageFactory messageFactory = MessageFactory.newInstance();
|
||||
SoapMessage soapMessage = new SaajSoapMessage(messageFactory.createMessage());
|
||||
var matcher = new PayloadDiffMatcher(new StringSource("<message/>"));
|
||||
var messageFactory = MessageFactory.newInstance();
|
||||
var soapMessage = new SaajSoapMessage(messageFactory.createMessage());
|
||||
|
||||
matcher.createDiff(soapMessage);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user