-
Notifications
You must be signed in to change notification settings - Fork 8
DiffBuilder
With the DiffBuilder you can build the difference between two XML Sources in human readable fluent style.
This example will throw an AssertionError: "Expected attribute value 'abc' but was 'xyz'".
final String control = "<a><b attr=\"abc\"></b></a>";
final String test = "<a><b attr=\"xyz\"></b></a>";
Diff myDiff = DiffBuilder.compare(Input.fromString(control))
.withTest(Input.fromString(test))
.build();
Assert.assertTrue(myDiff.toString(), myDiff.hasDifferences());
the C# equivalent would be
string control = "<a><b attr=\"abc\"></b></a>";
string test = "<a><b attr=\"xyz\"></b></a>";
var myDiff = DiffBuilder.Compare(Input.FromString(control))
.WithTest(Input.FromString(test))
.Build();
Assert.IsTrue(myDiff.HasDifferences(), myDiff.ToString());
The name Diff
is a class and a namespace name in XMLUnit.NET which
may force you to alias the class name in using
statements. In the
examples we'll always use var
to avoid the ambiguity.
You can compare all kind of Objects with each other which can be used as a XML source.
See: Providing Input To XMLUnit - Input.from(Object)
The DiffBuilder
can be configured via fluent API:
Diff myDiff = DiffBuilder.compare(control)
.withTest(test)
.checkForSimilar().checkForIdentical() // [1]
.ignoreComments() // [2]
.ignoreWhitespace() // [3]
.normalizeWhitespace() // [4]
.withComparisonController(ComparisonController) // [5]
.withComparisonFormatter(comparisonFormatter) // [6]
.withComparisonListeners(comparisonListeners) // [7]
.withDifferenceEvaluator(differenceEvaluator) // [8]
.withDifferenceListeners(comparisonListeners) // [9]
.withNodeMatcher(nodeMatcher) // [10]
.withAttributeFilter(attributeFilter) // [11]
.withNodeFilter(nodeFilter); // [12]
.withNamespaceContext(); // [13]
-
checkForSimilar() checkForIdentical():
The Difference between identical and similar is decided by the default DifferenceEvaluator. -
ignoreComments():
will stripping all comments from the test- and control-XML before comparing. -
ignoreWhitespace():
will removing all empty text nodes and trimming the non-empty ones from the test- and control-XML before comparing. -
normalizeWhitespace():
will removing all empty text nodes and normalizing the non-empty ones from the test- and control-XML before comparing. With "normalized" in this context means all whitespace characters are replaced by space characters and consecutive whitespace characters are collapsed. -
withComparisonController():
The default ComparisonController will let the DifferenceEngine evaluate all differences between test and control XML.
If the evaluation should stop after the first found difference you must use theComparisonControllers.StopWhenDifferent
instead.
See withComparisonController() -
withComparisonFormatter():
Use a custom Formatter for the Error Messages.
See ComparisonFormatter. -
withComparisonListeners():
Registers a listener that is notified of each comparison.
See ComparisonListener. -
withDifferenceEvaluator():
Provide your own custom DifferenceEvaluator implementation.
See DifferenceEvaluator. -
withDifferenceListeners():
Registers a listener that is notified of each comparison with outcome other thanComparisonResult.EQUAL
.
See ComparisonListener. -
withNodeMatcher():
Sets the strategy for selecting nodes to compare.
See NodeMatcher. - withAttributeFilter(): Optional strategy that allows certain attributes to be ignore completely. See AttributeFilter.
- withNodeFilter(): Optional strategy that allows certain nodes to be ignore completely. See NodeFilter.
-
withNamespaceContext():
Establish a namespace context that will be used inComparison.Detail.getXPath()
.
Without a namespace context (or with an empty context) the XPath expressions will only use local names for elements and attributes.
See withNamespaceContext()
the .NET version is almost the same, only the method names are capitalized.
Assuming you compare two XMLs with two differences (attr1 and attr2):
final String control = "<a><b attr1=\"abc\" attr2=\"def\"></b></a>";
final String test = "<a><b attr1=\"uvw\" attr2=\"xyz\"></b></a>";
The default ComparisonController
will let evaluate all differences:
Diff myDiff = DiffBuilder.compare(control).withTest(test)
.build();
assertThat(Linqy.count(myDiff.getDifferences()), is(2));
With the custom ComparisonController
ComparisonControllers.StopWhenDifferent
comparison is cut short once
the first difference has been encountered and only the first
difference will be returned:
Diff myDiff = DiffBuilder.compare(control).withTest(test)
.withComparisonController(ComparisonControllers.StopWhenDifferent)
.build();
assertThat(Linqy.count(myDiff.getDifferences()), is(1));
TODO Examples?
- Overview
- General Concepts
- Comparing XML
- Validating XML
- Utilities
- Migrating from XMLUnit 1.x to 2.x
- Known Issues