Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7967a32

Browse files
authored
Merge pull request #293 from xmlunit/make-placeholders-deal-with-xsi-type
ensure PlaceholderDifferenceEvaluator survives differences in xsi:typ…
2 parents f72dde7 + 7c2d8b3 commit 7967a32

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

RELEASE_NOTES.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
## XMLUnit for Java 2.10.1 - /not released, yet/
44

5+
* placeholders can now also be used inside of the local part of `xsi:type` attributes.
6+
PR [#293](https://github.com/xmlunit/xmlunit/pull/293)
7+
8+
* PlaceholderDifferenceEvaluator would cause ClassCastException for documents with
9+
differences in `xsi:type` attributes.
10+
Issue [#276](https://github.com/xmlunit/xmlunit/issues/276)
11+
512
* updated a bunch of Maven plugins, in particular we now create CylcloneDX files using
613
version 1.6 of the schema.
714
PR [#292](https://github.com/xmlunit/xmlunit/pull/292)

xmlunit-placeholders/src/main/java/org/xmlunit/placeholder/PlaceholderDifferenceEvaluator.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ServiceLoader;
2020
import java.util.regex.Matcher;
2121
import java.util.regex.Pattern;
22+
import javax.xml.XMLConstants;
2223
import javax.xml.namespace.QName;
2324
import org.w3c.dom.Node;
2425
import org.xmlunit.diff.Comparison;
@@ -206,10 +207,19 @@ public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome
206207
return evaluateConsideringPlaceholders(controlTarget.getNodeValue(), testTarget.getNodeValue(), outcome);
207208

208209
// comparing textual content of attributes
209-
} else if (comparison.getType() == ComparisonType.ATTR_VALUE) {
210+
} else if (comparison.getType() == ComparisonType.ATTR_VALUE
211+
&& controlDetails.getValue() instanceof String
212+
&& testDetails.getValue() instanceof String) {
210213
return evaluateConsideringPlaceholders((String) controlDetails.getValue(),
211214
(String) testDetails.getValue(), outcome);
212215

216+
// comparing QName content of xsi:type attributes
217+
} else if (comparison.getType() == ComparisonType.ATTR_VALUE
218+
&& controlDetails.getValue() instanceof QName
219+
&& testDetails.getValue() instanceof QName) {
220+
return evaluateConsideringPlaceholders((QName) controlDetails.getValue(),
221+
(QName) testDetails.getValue(), outcome);
222+
213223
// "test document has no attribute but control document has"
214224
} else if (isMissingAttributeDifference(comparison)) {
215225
return evaluateMissingAttributeConsideringPlaceholders(comparison, outcome);
@@ -303,6 +313,16 @@ private ComparisonResult evaluateAttributeListLengthConsideringPlaceholders(Comp
303313
return ComparisonResult.EQUAL;
304314
}
305315

316+
private ComparisonResult evaluateConsideringPlaceholders(QName controlQName, QName testQName,
317+
ComparisonResult outcome) {
318+
if (controlQName.getNamespaceURI().equals(testQName.getNamespaceURI())) {
319+
return evaluateConsideringPlaceholders(controlQName.getLocalPart(), testQName.getLocalPart(),
320+
outcome);
321+
}
322+
323+
return outcome;
324+
}
325+
306326
private ComparisonResult evaluateConsideringPlaceholders(String controlText, String testText,
307327
ComparisonResult outcome) {
308328
final Matcher placeholderMatcher = placeholderRegex.matcher(controlText);

xmlunit-placeholders/src/main/java/org/xmlunit/placeholder/package-info.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@
7878
* org.xmlunit.diff.DifferenceEvaluator} and add it to the builder
7979
* yourself.</p>
8080
*
81+
* <p>Placeholder sequences must appear as values inside of attribute values or nested textual content of elements -
82+
* inside the control document. As a special case they may also appear as local part of a tye name of xsi:type
83+
* attributes of control documents - i.e. <code>xsi:type="prefix:${xmlunit:ignore}"</code>. The namepace URIs of the
84+
* compared types must match with the test document. Support for xsi:types has been added with XMLUnit 2.10.1.</p>
85+
*
8186
* @since 2.6.0
8287
*/
8388
package org.xmlunit.placeholder;

xmlunit-placeholders/src/test/java/org/xmlunit/placeholder/PlaceholderDifferenceEvaluatorTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,81 @@ public void isDateTimePlaceholder_Element_IsDateTime_CustomFormat() {
472472
assertFalse(diff.hasDifferences());
473473
}
474474

475+
@Test
476+
public void canCompareDocmentsWithXsiTypes() {
477+
String control = "<element"
478+
+ " xmlns:myns=\"https://example.org/some-ns\""
479+
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
480+
+ " xsi:type=\"myns:some-type\" />";
481+
String test = "<element"
482+
+ " xmlns:myns=\"https://example.org/some-ns\""
483+
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
484+
+ " xsi:type=\"myns:some-other-type\" />";
485+
486+
Diff diff = DiffBuilder.compare(control).withTest(test)
487+
.withDifferenceEvaluator(new PlaceholderDifferenceEvaluator()).build();
488+
489+
assertTrue(diff.hasDifferences());
490+
int count = 0;
491+
Iterator it = diff.getDifferences().iterator();
492+
while (it.hasNext()) {
493+
count++;
494+
Difference difference = (Difference) it.next();
495+
assertEquals(ComparisonResult.DIFFERENT, difference.getResult());
496+
assertEquals(ComparisonType.ATTR_VALUE,
497+
difference.getComparison().getType());
498+
}
499+
assertEquals(1, count);
500+
}
501+
502+
@Test
503+
public void canIgnoreXsiTypeDifference() {
504+
String control = "<element"
505+
+ " xmlns:myns=\"https://example.org/some-ns\""
506+
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
507+
+ " xsi:type=\"myns:${xmlunit.ignore}\" />";
508+
String test = "<element"
509+
+ " xmlns:myns=\"https://example.org/some-ns\""
510+
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
511+
+ " xsi:type=\"myns:some-other-type\" />";
512+
513+
Diff diff = DiffBuilder.compare(control).withTest(test)
514+
.withDifferenceEvaluator(new PlaceholderDifferenceEvaluator()).build();
515+
516+
assertFalse(diff.hasDifferences());
517+
}
518+
519+
@Test
520+
public void cantIgnoreXsiNamespaceDifference() {
521+
String control = "<element"
522+
+ " xmlns:myns=\"https://example.org/some-other-ns\""
523+
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
524+
+ " xsi:type=\"myns:${xmlunit.ignore}\" />";
525+
String test = "<element"
526+
+ " xmlns:myns=\"https://example.org/some-ns\""
527+
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
528+
+ " xsi:type=\"myns:some-other-type\" />";
529+
530+
Diff diff = DiffBuilder.compare(control).withTest(test)
531+
.withDifferenceEvaluator(new PlaceholderDifferenceEvaluator()).build();
532+
533+
assertTrue(diff.hasDifferences());
534+
}
535+
536+
@Test
537+
public void canCompareXsiTypeWithhRegex() {
538+
String control = "<element"
539+
+ " xmlns:myns=\"https://example.org/some-ns\""
540+
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
541+
+ " xsi:type=\"myns:${xmlunit.matchesRegex(.*-type)}\" />";
542+
String test = "<element"
543+
+ " xmlns:myns=\"https://example.org/some-ns\""
544+
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
545+
+ " xsi:type=\"myns:some-other-type\" />";
546+
547+
Diff diff = DiffBuilder.compare(control).withTest(test)
548+
.withDifferenceEvaluator(new PlaceholderDifferenceEvaluator()).build();
549+
550+
assertFalse(diff.hasDifferences());
551+
}
475552
}

0 commit comments

Comments
 (0)