Skip to content

Commit c39d3d1

Browse files
AlbericMartelgregturn
authored andcommitted
Use XMLUnit 2 for RequestMatcher and ResponseMatcher.
Implement newer versions of PayloadDiffMatcher and SoapEnvelopematcher based on XMLUnit 2. Deprecate the prior versions of these supporting types so they can be removed in a future release. XMLUnit 2 eases the testing of MockWebServiceServer. Resolves #1193.
1 parent 43b6ecf commit c39d3d1

File tree

14 files changed

+460
-19
lines changed

14 files changed

+460
-19
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
<xmlsec.version>2.3.1</xmlsec.version>
123123
<xml-schema-core.version>2.2.2</xml-schema-core.version>
124124
<xmlunit1.version>1.6</xmlunit1.version>
125-
<xmlunit.version>2.7.0</xmlunit.version>
125+
<xmlunit.version>2.9.0</xmlunit.version>
126126
<xws-security.version>3.0</xws-security.version>
127127
<xom.version>1.2.5</xom.version>
128128
<spring-asciidoctor-backends.version>0.0.3</spring-asciidoctor-backends.version>

spring-ws-test/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
<version>${xmlunit1.version}</version>
4141
</dependency>
4242

43+
<dependency>
44+
<groupId>org.xmlunit</groupId>
45+
<artifactId>xmlunit-core</artifactId>
46+
<version>${xmlunit.version}</version>
47+
</dependency>
48+
4349
<dependency>
4450
<groupId>org.springframework</groupId>
4551
<artifactId>spring-test</artifactId>
@@ -76,4 +82,4 @@
7682
</profile>
7783
</profiles>
7884

79-
</project>
85+
</project>

spring-ws-test/src/main/java/org/springframework/ws/test/client/MockSenderConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*/
3636
class MockSenderConnection implements WebServiceConnection, ResponseActions {
3737

38-
private final List<RequestMatcher> requestMatchers = new LinkedList<RequestMatcher>();
38+
private final List<RequestMatcher> requestMatchers = new LinkedList<>();
3939

4040
private URI uri;
4141

spring-ws-test/src/main/java/org/springframework/ws/test/client/RequestMatchers.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@
2525

2626
import org.springframework.core.io.Resource;
2727
import org.springframework.util.Assert;
28-
import org.springframework.ws.WebServiceMessage;
29-
import org.springframework.ws.test.support.matcher.PayloadDiffMatcher;
3028
import org.springframework.ws.test.support.matcher.SchemaValidatingMatcher;
31-
import org.springframework.ws.test.support.matcher.SoapEnvelopeDiffMatcher;
3229
import org.springframework.ws.test.support.matcher.SoapHeaderMatcher;
30+
import org.springframework.ws.test.support.matcher.xmlunit2.PayloadDiffMatcher;
31+
import org.springframework.ws.test.support.matcher.xmlunit2.SoapEnvelopeDiffMatcher;
3332
import org.springframework.xml.transform.ResourceSource;
3433

3534
/**
@@ -49,9 +48,7 @@ private RequestMatchers() {}
4948
* @return the request matcher
5049
*/
5150
public static RequestMatcher anything() {
52-
return new RequestMatcher() {
53-
public void match(URI uri, WebServiceMessage request) throws IOException, AssertionError {}
54-
};
51+
return (uri, request) -> {};
5552
}
5653

5754
// Payload

spring-ws-test/src/main/java/org/springframework/ws/test/server/ResponseMatchers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.ws.test.server;
1818

19-
import static org.springframework.ws.test.support.AssertionErrors.*;
19+
import static org.springframework.ws.test.support.AssertionErrors.fail;
2020

2121
import java.io.IOException;
2222
import java.util.Locale;
@@ -30,10 +30,10 @@
3030
import org.springframework.ws.FaultAwareWebServiceMessage;
3131
import org.springframework.ws.WebServiceMessage;
3232
import org.springframework.ws.soap.SoapVersion;
33-
import org.springframework.ws.test.support.matcher.PayloadDiffMatcher;
3433
import org.springframework.ws.test.support.matcher.SchemaValidatingMatcher;
3534
import org.springframework.ws.test.support.matcher.SoapEnvelopeDiffMatcher;
3635
import org.springframework.ws.test.support.matcher.SoapHeaderMatcher;
36+
import org.springframework.ws.test.support.matcher.xmlunit2.PayloadDiffMatcher;
3737
import org.springframework.xml.transform.ResourceSource;
3838

3939
/**

spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/DiffMatcher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.ws.test.support.matcher;
1818

19-
import static org.springframework.ws.test.support.AssertionErrors.*;
19+
import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
2020

2121
import org.custommonkey.xmlunit.Diff;
2222
import org.springframework.ws.WebServiceMessage;
@@ -26,6 +26,7 @@
2626
*
2727
* @author Arjen Poutsma
2828
* @since 2.0
29+
* @deprecated Migrate to {@link org.springframework.ws.test.support.matcher.xmlunit2.DiffMatcher}.
2930
*/
3031
public abstract class DiffMatcher implements WebServiceMessageMatcher {
3132

spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/PayloadDiffMatcher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.ws.test.support.matcher;
1818

19-
import static org.springframework.ws.test.support.AssertionErrors.*;
19+
import static org.springframework.ws.test.support.AssertionErrors.fail;
2020

2121
import javax.xml.transform.Source;
2222
import javax.xml.transform.TransformerException;
@@ -34,6 +34,7 @@
3434
* @author Arjen Poutsma
3535
* @author Lukas Krecan
3636
* @since 2.0
37+
* @deprecated Migrate to {@link org.springframework.ws.test.support.matcher.xmlunit2.PayloadDiffMatcher}.s
3738
*/
3839
public class PayloadDiffMatcher extends DiffMatcher {
3940

spring-ws-test/src/main/java/org/springframework/ws/test/support/matcher/SoapEnvelopeDiffMatcher.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
package org.springframework.ws.test.support.matcher;
1818

19-
import static org.springframework.ws.test.support.AssertionErrors.*;
19+
import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
20+
import static org.springframework.ws.test.support.AssertionErrors.fail;
2021

2122
import java.io.IOException;
2223

@@ -36,6 +37,7 @@
3637
*
3738
* @author Alexander Shutyaev
3839
* @since 2.1.1
40+
* @deprecated Migrate to {@link org.springframework.ws.test.support.matcher.xmlunit2.SoapEnvelopeDiffMatcher}.
3941
*/
4042
public class SoapEnvelopeDiffMatcher extends AbstractSoapMessageMatcher {
4143

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2005-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.test.support.matcher.xmlunit2;
18+
19+
import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
20+
21+
import org.springframework.ws.WebServiceMessage;
22+
import org.springframework.ws.test.support.matcher.WebServiceMessageMatcher;
23+
import org.xmlunit.diff.Diff;
24+
25+
/**
26+
* Implementation of {@link WebServiceMessageMatcher} based on XMLUnit's {@link Diff}.
27+
*
28+
* @author Greg Turnquist
29+
* @since 3.1
30+
*/
31+
public abstract class DiffMatcher implements WebServiceMessageMatcher {
32+
33+
@Override
34+
public final void match(WebServiceMessage message) throws AssertionError {
35+
36+
Diff diff = createDiff(message);
37+
assertTrue("Messages are different, " + diff.toString(), !diff.hasDifferences(), "Payload",
38+
message.getPayloadSource());
39+
}
40+
41+
/**
42+
* Creates a {@link Diff} for the given message.
43+
*
44+
* @param message the message
45+
* @return the diff
46+
* @throws Exception in case of errors
47+
*/
48+
protected abstract Diff createDiff(WebServiceMessage message);
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2005-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.test.support.matcher.xmlunit2;
18+
19+
import static org.springframework.ws.test.support.AssertionErrors.fail;
20+
21+
import javax.xml.transform.Source;
22+
import javax.xml.transform.TransformerException;
23+
import javax.xml.transform.dom.DOMResult;
24+
25+
import org.springframework.util.Assert;
26+
import org.springframework.ws.WebServiceMessage;
27+
import org.springframework.xml.transform.TransformerHelper;
28+
import org.w3c.dom.Document;
29+
import org.xmlunit.builder.DiffBuilder;
30+
import org.xmlunit.diff.Diff;
31+
32+
/**
33+
* Matches {@link Source} payloads.
34+
*
35+
* @author Greg Turnquist
36+
* @since 3.1
37+
*/
38+
public class PayloadDiffMatcher extends DiffMatcher {
39+
40+
private final Source expected;
41+
42+
private final TransformerHelper transformerHelper = new TransformerHelper();
43+
44+
public PayloadDiffMatcher(Source expected) {
45+
Assert.notNull(expected, "'expected' must not be null");
46+
this.expected = expected;
47+
}
48+
49+
@Override
50+
protected final Diff createDiff(WebServiceMessage message) {
51+
Source payload = message.getPayloadSource();
52+
if (payload == null) {
53+
fail("Request message does not contain payload");
54+
}
55+
return createDiff(payload);
56+
}
57+
58+
protected Diff createDiff(Source payload) {
59+
Document expectedDocument = createDocumentFromSource(expected);
60+
Document actualDocument = createDocumentFromSource(payload);
61+
return DiffBuilder.compare(expectedDocument) //
62+
.withTest(actualDocument) //
63+
.checkForSimilar() //
64+
.build();
65+
}
66+
67+
private Document createDocumentFromSource(Source source) {
68+
try {
69+
DOMResult result = new DOMResult();
70+
transformerHelper.transform(source, result);
71+
return (Document) result.getNode();
72+
} catch (TransformerException ex) {
73+
fail("Could not transform source to DOMResult" + ex.getMessage());
74+
return null;
75+
}
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2005-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.test.support.matcher.xmlunit2;
18+
19+
import static org.springframework.ws.test.support.AssertionErrors.assertTrue;
20+
import static org.springframework.ws.test.support.AssertionErrors.fail;
21+
22+
import java.io.IOException;
23+
24+
import javax.xml.transform.Source;
25+
import javax.xml.transform.TransformerException;
26+
import javax.xml.transform.dom.DOMResult;
27+
28+
import org.springframework.util.Assert;
29+
import org.springframework.ws.soap.SoapMessage;
30+
import org.springframework.ws.test.support.matcher.AbstractSoapMessageMatcher;
31+
import org.springframework.xml.transform.TransformerHelper;
32+
import org.w3c.dom.Document;
33+
import org.xmlunit.builder.DiffBuilder;
34+
import org.xmlunit.diff.Diff;
35+
36+
/**
37+
* Matches {@link Source} SOAP envelopes.
38+
*
39+
* @author Greg Turnquist
40+
* @since 3.1
41+
*/
42+
public class SoapEnvelopeDiffMatcher extends AbstractSoapMessageMatcher {
43+
44+
private final Source expected;
45+
46+
private final TransformerHelper transformerHelper = new TransformerHelper();
47+
48+
public SoapEnvelopeDiffMatcher(Source expected) {
49+
50+
Assert.notNull(expected, "'expected' must not be null");
51+
this.expected = expected;
52+
}
53+
54+
@Override
55+
protected void match(SoapMessage soapMessage) throws IOException, AssertionError {
56+
57+
Document actualDocument = soapMessage.getDocument();
58+
Document expectedDocument = createDocumentFromSource(expected);
59+
Diff diff = DiffBuilder.compare(expectedDocument).ignoreWhitespace().withTest(actualDocument).checkForSimilar()
60+
.build();
61+
assertTrue("Envelopes are different, " + diff.toString(), !diff.hasDifferences());
62+
}
63+
64+
private Document createDocumentFromSource(Source source) {
65+
66+
try {
67+
DOMResult result = new DOMResult();
68+
transformerHelper.transform(source, result);
69+
return (Document) result.getNode();
70+
} catch (TransformerException ex) {
71+
fail("Could not transform source to DOMResult" + ex.getMessage());
72+
return null;
73+
}
74+
}
75+
76+
}

0 commit comments

Comments
 (0)