Skip to content

Commit

Permalink
improve performance of EqualToXmlPattern. (#2944)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RafeArnold authored Jan 30, 2025
1 parent f913265 commit 9ccb1c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ public boolean isExactMatch() {
.withTest(value)
.withComparisonController(ComparisonControllers.StopWhenDifferent)
.ignoreWhitespace()
.ignoreComments()
.withDifferenceEvaluator(diffEvaluator)
.withNodeMatcher(new OrderInvariantNodeMatcher(ignoreOrderOfSameNode))
.withDocumentBuilderFactory(DOCUMENT_BUILDER_FACTORY)
Expand Down Expand Up @@ -188,7 +187,6 @@ public double getDistance() {
DiffBuilder.compare(Input.from(expectedValue))
.withTest(value)
.ignoreWhitespace()
.ignoreComments()
.withDifferenceEvaluator(diffEvaluator)
.withComparisonListeners(
(comparison, outcome) -> {
Expand Down Expand Up @@ -228,6 +226,7 @@ public double getDistance() {
private static DocumentBuilderFactory newDocumentBuilderFactory() {
DocumentBuilderFactory factory = Xml.newDocumentBuilderFactory();
try {
factory.setFeature("http://apache.org/xml/features/include-comments", false);
factory.setFeature("http://xml.org/sax/features/namespaces", true);
} catch (ParserConfigurationException e) {
throwUnchecked(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,4 +568,12 @@ void matchesIfTwoIdenticalChildNodesAreEmpty() {
MatchResult result = pattern.match("<body><entry/><entry/></body>");
assertTrue(result.isExactMatch());
}

@Test
void matchesIfCommentsDiffer() {
EqualToXmlPattern pattern =
new EqualToXmlPattern("<body><!-- Comment --><entry/><entry/></body>", false, true);
MatchResult result = pattern.match("<body><entry/><entry/><!-- A different comment --></body>");
assertTrue(result.isExactMatch());
}
}

0 comments on commit 9ccb1c8

Please sign in to comment.