Skip to content

Commit 9ccb1c8

Browse files
authored
improve performance of EqualToXmlPattern. (#2944)
removes use of DiffBuilder.ignoreComments which performs an xslt transform on the provided XML documents which is very memory intensive. now comments are ignored at read/parse time. as part of this change, the Xml utility class was also refactored to (hopefully) make it more reusable.
1 parent f913265 commit 9ccb1c8

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/main/java/com/github/tomakehurst/wiremock/matching/EqualToXmlPattern.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ public boolean isExactMatch() {
151151
.withTest(value)
152152
.withComparisonController(ComparisonControllers.StopWhenDifferent)
153153
.ignoreWhitespace()
154-
.ignoreComments()
155154
.withDifferenceEvaluator(diffEvaluator)
156155
.withNodeMatcher(new OrderInvariantNodeMatcher(ignoreOrderOfSameNode))
157156
.withDocumentBuilderFactory(DOCUMENT_BUILDER_FACTORY)
@@ -188,7 +187,6 @@ public double getDistance() {
188187
DiffBuilder.compare(Input.from(expectedValue))
189188
.withTest(value)
190189
.ignoreWhitespace()
191-
.ignoreComments()
192190
.withDifferenceEvaluator(diffEvaluator)
193191
.withComparisonListeners(
194192
(comparison, outcome) -> {
@@ -228,6 +226,7 @@ public double getDistance() {
228226
private static DocumentBuilderFactory newDocumentBuilderFactory() {
229227
DocumentBuilderFactory factory = Xml.newDocumentBuilderFactory();
230228
try {
229+
factory.setFeature("http://apache.org/xml/features/include-comments", false);
231230
factory.setFeature("http://xml.org/sax/features/namespaces", true);
232231
} catch (ParserConfigurationException e) {
233232
throwUnchecked(e);

src/test/java/com/github/tomakehurst/wiremock/matching/EqualToXmlPatternTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,4 +568,12 @@ void matchesIfTwoIdenticalChildNodesAreEmpty() {
568568
MatchResult result = pattern.match("<body><entry/><entry/></body>");
569569
assertTrue(result.isExactMatch());
570570
}
571+
572+
@Test
573+
void matchesIfCommentsDiffer() {
574+
EqualToXmlPattern pattern =
575+
new EqualToXmlPattern("<body><!-- Comment --><entry/><entry/></body>", false, true);
576+
MatchResult result = pattern.match("<body><entry/><entry/><!-- A different comment --></body>");
577+
assertTrue(result.isExactMatch());
578+
}
571579
}

0 commit comments

Comments
 (0)