Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
See #1203.
  • Loading branch information
gregturn committed Jul 14, 2023
1 parent 3f322a8 commit 1d4e2dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,6 +33,7 @@
* Matches {@link Source} payloads.
*
* @author Greg Turnquist
* @author Mikołaj Fejzer
* @since 3.1
*/
public class PayloadDiffMatcher extends DiffMatcher {
Expand All @@ -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() //
Expand All @@ -66,11 +73,14 @@ protected Diff createDiff(Source payload) {
}

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@

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 javax.xml.soap.MessageFactory;

Expand All @@ -37,6 +34,7 @@ public void match() {

String xml = "<element xmlns='http://example.com'/>";
WebServiceMessage message = createMock(WebServiceMessage.class);

expect(message.getPayloadSource()).andReturn(new StringSource(xml)).times(2);
replay(message);

Expand All @@ -52,6 +50,7 @@ public void matchIgnoringWhitespace() {
String xml = "<response><success>true</success></response>";
String xmlWithAdditionalWhitespace = "<response> <success>true</success> </response>";
WebServiceMessage message = createMock(WebServiceMessage.class);

expect(message.getPayloadSource()).andReturn(new StringSource(xml)).times(2);
replay(message);

Expand All @@ -61,14 +60,14 @@ public void matchIgnoringWhitespace() {
verify(message);
}


@Test
public void nonMatch() {

assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> {

String actual = "<element1 xmlns='http://example.com'/>";
WebServiceMessage message = createMock(WebServiceMessage.class);

expect(message.getPayloadSource()).andReturn(new StringSource(actual)).times(2);
replay(message);

Expand Down

0 comments on commit 1d4e2dc

Please sign in to comment.