diff --git a/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/xmlunit2/PayloadDiffMatcher.java b/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/xmlunit2/PayloadDiffMatcher.java index dd2fb893..cb5e3be2 100644 --- a/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/xmlunit2/PayloadDiffMatcher.java +++ b/spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/xmlunit2/PayloadDiffMatcher.java @@ -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; } diff --git a/spring-ws-test/src/test/java/org/springframework/ws/test/support/matcher/xmlunit2/PayloadDiffMatcherTest.java b/spring-ws-test/src/test/java/org/springframework/ws/test/support/matcher/xmlunit2/PayloadDiffMatcherTest.java index 7726327f..208200a0 100644 --- a/spring-ws-test/src/test/java/org/springframework/ws/test/support/matcher/xmlunit2/PayloadDiffMatcherTest.java +++ b/spring-ws-test/src/test/java/org/springframework/ws/test/support/matcher/xmlunit2/PayloadDiffMatcherTest.java @@ -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 = ""; + var xml = ""; 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 = "true"; - String xmlWithAdditionalWhitespace = " true "; + var xml = "true"; + var xmlWithAdditionalWhitespace = " true "; 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 = ""; + var actual = ""; WebServiceMessage message = createMock(WebServiceMessage.class); + expect(message.getPayloadSource()).andReturn(new StringSource(actual)).times(2); replay(message); - String expected = ""; - PayloadDiffMatcher matcher = new PayloadDiffMatcher(new StringSource(expected)); + var expected = ""; + 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("")); - MessageFactory messageFactory = MessageFactory.newInstance(); - SoapMessage soapMessage = new SaajSoapMessage(messageFactory.createMessage()); + var matcher = new PayloadDiffMatcher(new StringSource("")); + var messageFactory = MessageFactory.newInstance(); + var soapMessage = new SaajSoapMessage(messageFactory.createMessage()); matcher.createDiff(soapMessage); }); } - }