将 XML 与 xmlunit 进行比较时忽略文本差异
Ignore text differences when comparing XML with xmlunit
我在比较 xml 个字符串时遇到问题。
我的比较功能是
public static boolean compareXMLs(String xmlSource, String xmlCompareWith){
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreAttributeOrder(true);
XMLUnit.setNormalizeWhitespace(true);
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);
Diff myDiff = new Diff(xmlSource, xmlCompareWith);
myDiff.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener());
return myDiff.similar();
}
我的单元测试是
String xml1 = "<a>f</a>";
String xml2 = "<a>1</a>";
assertEquals(true, RestCommonUtility.compareXMLs(xml1, xml2));
xml1 = "<a></a>";
xml2 = "<a>1</a>";
assertEquals(true, RestCommonUtility.compareXMLs(xml1, xml2));
我的单元测试在第一个断言中通过,但在第二个断言中失败。我设置了 IgnoreTextAndAttributeValuesDifferenceListener
但我的第二个断言仍然失败。有什么办法可以解决这个问题。?或者是否有任何其他框架可以帮助进行此类比较?
如果报告的错误是 CHILD_NODELIST_LENGTH,则创建一个自定义 IgnoreTextAndAttributeValuesDifferenceListener,它也会忽略 CHILD_NODELIST_LENGTH。 Id 运行 它通过调试器确认差异是什么然后排除它
您可以创建自己的 DifferenceListener
,当一个节点有单个文本子节点而另一个没有时忽略差异。这是一个例子:
public class CompareStructureOnlyListener implements DifferenceListener {
private IgnoreTextAndAttributeValuesDifferenceListener delegate =
new IgnoreTextAndAttributeValuesDifferenceListener();
@Override
public int differenceFound(Difference difference) {
int delegateResult = delegate.differenceFound(difference);
if (delegateResult == DifferenceListener.RETURN_ACCEPT_DIFFERENCE) {
// Delegate thinks there is a difference, let's confirm
if (difference.getId() == DifferenceConstants.HAS_CHILD_NODES_ID) {
Node controlNode = difference.getControlNodeDetail().getNode();
Node testNode = difference.getTestNodeDetail().getNode();
Node nodeToTest = (controlNode.hasChildNodes()) ? controlNode : testNode;
// If there is only a difference of one node and that node is a text
// node, then ignore it
if (nodeToTest.getChildNodes().getLength() == 1
&& nodeToTest.getFirstChild() instanceof Text) {
return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
} else {
return DifferenceListener.RETURN_ACCEPT_DIFFERENCE;
}
}
}
return delegateResult;
}
@Override
public void skippedComparison(Node control, Node test) {
// Does nothing
}
}
下面是一些通过的测试:
String xml1 = "<a>a</a>";
String xml2 = "<a>1</a>";
assertTrue(compareXMLs(xml1, xml2));
xml1 = "<a></a>";
xml2 = "<a>1</a>";
assertTrue(compareXMLs(xml1, xml2));
xml1 = "<a></a>";
xml2 = "<a><f></f></a>";
assertFalse(compareXMLs(xml1, xml2));
旁注:将您的 compareXMLs
方法重命名为 xmlsAreSimilar
之类的名称,或者可以轻松指示布尔 return 值含义的名称。
我在比较 xml 个字符串时遇到问题。
我的比较功能是
public static boolean compareXMLs(String xmlSource, String xmlCompareWith){
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreAttributeOrder(true);
XMLUnit.setNormalizeWhitespace(true);
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);
Diff myDiff = new Diff(xmlSource, xmlCompareWith);
myDiff.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener());
return myDiff.similar();
}
我的单元测试是
String xml1 = "<a>f</a>";
String xml2 = "<a>1</a>";
assertEquals(true, RestCommonUtility.compareXMLs(xml1, xml2));
xml1 = "<a></a>";
xml2 = "<a>1</a>";
assertEquals(true, RestCommonUtility.compareXMLs(xml1, xml2));
我的单元测试在第一个断言中通过,但在第二个断言中失败。我设置了 IgnoreTextAndAttributeValuesDifferenceListener
但我的第二个断言仍然失败。有什么办法可以解决这个问题。?或者是否有任何其他框架可以帮助进行此类比较?
如果报告的错误是 CHILD_NODELIST_LENGTH,则创建一个自定义 IgnoreTextAndAttributeValuesDifferenceListener,它也会忽略 CHILD_NODELIST_LENGTH。 Id 运行 它通过调试器确认差异是什么然后排除它
您可以创建自己的 DifferenceListener
,当一个节点有单个文本子节点而另一个没有时忽略差异。这是一个例子:
public class CompareStructureOnlyListener implements DifferenceListener {
private IgnoreTextAndAttributeValuesDifferenceListener delegate =
new IgnoreTextAndAttributeValuesDifferenceListener();
@Override
public int differenceFound(Difference difference) {
int delegateResult = delegate.differenceFound(difference);
if (delegateResult == DifferenceListener.RETURN_ACCEPT_DIFFERENCE) {
// Delegate thinks there is a difference, let's confirm
if (difference.getId() == DifferenceConstants.HAS_CHILD_NODES_ID) {
Node controlNode = difference.getControlNodeDetail().getNode();
Node testNode = difference.getTestNodeDetail().getNode();
Node nodeToTest = (controlNode.hasChildNodes()) ? controlNode : testNode;
// If there is only a difference of one node and that node is a text
// node, then ignore it
if (nodeToTest.getChildNodes().getLength() == 1
&& nodeToTest.getFirstChild() instanceof Text) {
return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
} else {
return DifferenceListener.RETURN_ACCEPT_DIFFERENCE;
}
}
}
return delegateResult;
}
@Override
public void skippedComparison(Node control, Node test) {
// Does nothing
}
}
下面是一些通过的测试:
String xml1 = "<a>a</a>";
String xml2 = "<a>1</a>";
assertTrue(compareXMLs(xml1, xml2));
xml1 = "<a></a>";
xml2 = "<a>1</a>";
assertTrue(compareXMLs(xml1, xml2));
xml1 = "<a></a>";
xml2 = "<a><f></f></a>";
assertFalse(compareXMLs(xml1, xml2));
旁注:将您的 compareXMLs
方法重命名为 xmlsAreSimilar
之类的名称,或者可以轻松指示布尔 return 值含义的名称。