1

在我的 JUnit 测试中,我将预期的 XML 文件与解析器创建的虚拟文档进行比较:

public void test() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    try (InputStream input = getClass().getResourceAsStream("expected.xml");) {
        Document expectedDocument = builder.parse(input);
        Document actualDocument = createVirtualDocument();

        XMLTestCase.assertXMLEqual(expectedDocument, actualDocument);
    }
}

问题是:即使 XML 文件相等(或最后相似),断言也会失败。XML 文件如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentScriptType="text/ecmascript" contentStyleType="text/css" preserveAspectRatio="xMidYMid meet" version="1.0" zoomAndPan="magnify">
  <g class="group">
    <rect class="shape" height="40.0" width="30.0" x="10.0" y="20.0"/>
  </g>
</svg>

还有一个:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" contentScriptType="text/ecmascript" zoomAndPan="magnify" contentStyleType="text/css" preserveAspectRatio="xMidYMid meet" version="1.0">
  <g class="group">
    <rect x="10.0" width="30.0" height="40.0" y="20.0" class="shape"/>
  </g>
</svg>

简而言之:属性顺序不同。我已经尝试过XMLUnit.setIgnoreAttributeOrder(true)and XMLUnit.setNormalize(true),但无济于事。

测试失败时我收到的消息是:

[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg contentScriptType="text/ecmascript"...> at /svg[1]/@contentScriptType to <svg contentScriptType="text/ecmascript"...> at /svg[1]/@contentScriptType
[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg contentStyleType="text/css"...> at /svg[1]/@contentStyleType to <svg contentStyleType="text/css"...> at /svg[1]/@contentStyleType
[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg preserveAspectRatio="xMidYMid meet"...> at /svg[1]/@preserveAspectRatio to <svg preserveAspectRatio="xMidYMid meet"...> at /svg[1]/@preserveAspectRatio
[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg version="1.0"...> at /svg[1]/@version to <svg version="1.0"...> at /svg[1]/@version
[not identical] Expected attribute value explicitly specified 'true' but was 'false' - comparing <svg zoomAndPan="magnify"...> at /svg[1]/@zoomAndPan to <svg zoomAndPan="magnify"...> at /svg[1]/@zoomAndPan
[different] Expected presence of child node 'g' but was 'null' - comparing <g...> at /svg[1]/g[1] to  at null

我也试过:

Diff difference = new Diff(expectedDocument, actualDocument);
difference.overrideElementQualifier(new ElementNameAndAttributeQualifier());

但差异声称不相似。那么如何正确比较这两个 XML 文档呢?

4

1 回答 1

0

那只是愚蠢的。如果我不比较Documents 而是将它们转换为字符串并比较这些代码,则该代码有效:

ByteArrayOutputStream expected = new ByteArrayOutputStream();
storeXml(expectedDocument, expected);

ByteArrayOutputStream actual = new ByteArrayOutputStream();
storeXml(actualDocument, actual);

assertXMLEqual(expected.toString(), actual.toString());

和:

protected static void storeXml(Document document, OutputStream out) throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(new DOMSource(document), new StreamResult(out));
}
于 2014-11-14T11:05:33.363 回答