ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 02.01.2026

Просмотров: 482

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

XMLUnit Java User’s Guide

i

XMLUnit Java User’s Guide

XMLUnit Java User’s Guide

 

 

ii

 

 

 

 

 

 

 

 

 

COLLABORATORS

 

 

 

 

 

 

 

 

 

 

TITLE :

 

 

 

 

 

XMLUnit Java User’s Guide

 

 

 

 

 

 

 

 

ACTION

NAME

DATE

SIGNATURE

 

 

 

 

 

 

 

 

WRITTEN BY

Tim Bacon and

February 7, 2013

 

 

 

 

Stefan Bodewig

 

 

 

 

 

 

 

 

 

REVISION HISTORY

NUMBER

DATE

DESCRIPTION

NAME

1.0

January 2003

Documentation for XMLUnit Java 1.0

Tim Bacon

 

 

 

 

1.1

April 2007

Documentation for XMLUnit Java 1.1

 

 

 

 

 

1.2

June 2008

Documentation for XMLUnit Java 1.2

 

 

 

 

 

1.3

September 2009

Documentation for XMLUnit Java 1.3

 

 

 

 

 

1.4

February 2013

Documentation for XMLUnit Java 1.4

 

 

 

 

 


XMLUnit Java User’s Guide iii

Contents

1 A Tour of XMLUnit

1

1.1

What is XMLUnit? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1

1.2

Quick tour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1

1.3

Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1

1.4

Configuring XMLUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1

1.5

Writing XML comparison tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2

1.6

Comparing XML Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4

1.7

Validation Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

1.8

XPath Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

1.9

Testing by Tree Walking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

2 Using XMLUnit

8

2.1

Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8

2.2

Basic Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8

 

2.2.1 Comparing Pieces of XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

 

2.2.2

Validating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

 

2.2.3

XSLT Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

 

2.2.4

XPath Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

 

2.2.5

DOM Tree Walking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

2.3

Using XMLUnit With JUnit 3.x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

2.4

Common Configuration Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

 

2.4.1

JAXP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

 

2.4.2

EntityResolver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

 

2.4.3

Element Content Whitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

 

2.4.4

XSLT Stylesheet Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11

2.5

Providing Input to XMLUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11

3 Comparing Pieces of XML

11

3.1

The Difference Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11

3.2

ComparisonController . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

12

3.3

DifferenceListener . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

16

 

3.3.1

IgnoreTextAndAttributeValuesDifferenceListener . . . . . . . . . . . . . . . . . . .

17

3.4

ElementQualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

17

 

3.4.1

ElementNameQualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

 

3.4.2

ElementNameAndAttributeQualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

 

3.4.3

ElementNameAndTextQualifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

19

 

3.4.4

org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier

19

 

 

 

 


XMLUnit Java User’s Guide

iv

3.4.5org.custommonkey.xmlunit.examples.MultiLevelElementNameAndTextQualifier 19

 

3.5

Diff and DetailedDiff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

20

 

3.6

MatchTracker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

21

 

3.7

JUnit 3.x Convenience Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

21

 

3.8

Configuration Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

22

 

 

3.8.1

Whitespace Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

22

 

 

3.8.2

"Normalizing" Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

 

 

3.8.3

Ignoring Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

 

 

3.8.4

Treating CDATA Sections and Text Nodes Alike . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

 

 

3.8.5

Entity Reference Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

 

 

3.8.6

Comparison of Unmatched Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

23

4

Validating XML Documents

24

 

4.1

The Validator Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

 

 

4.1.1

DTD Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

 

 

4.1.2

XML Schema Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

25

 

4.2

JUnit 3.x Convenience Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

26

 

4.3

Configuration Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

26

 

4.4

JAXP 1.3 Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

26

5

XPath Tests

 

27

 

5.1

XPath Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

27

 

5.2

Using XML Namespaces in XPath Selectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

28

 

5.3

JUnit 3.x Convenience Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

28

 

5.4

Configuration Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

29

6

DOM Tree Walking

29

 

6.1

DocumentTraversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

29

 

6.2

NodeTest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

29

 

6.3

NodeTester . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

30

 

 

6.3.1

AbstractNodeTester . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

31

 

 

6.3.2

CountingNodeTester . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

31

 

6.4

JUnit 3.x Convenience Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

31

 

6.5

Configuration Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

32

A

Changes

 

32

 

A.1

Changes from XMLUnit 1.0 to 1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

32

 

 

A.1.1

Breaking Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

32

 

 

A.1.2

New Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

33

 

 

A.1.3

Important Bug Fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

33

 

 

 

 

 


XMLUnit Java User’s Guide

v

A.2

Changes from XMLUnit 1.1 to 1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

33

 

A.2.1

Breaking Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

33

 

A.2.2

New Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

34

 

A.2.3

Important Bug Fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

34

A.3

Changes from XMLUnit 1.2 to 1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

34

 

A.3.1

Breaking Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

34

 

A.3.2

New Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

34

 

A.3.3

Important Bug Fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

34

A.4

Changes from XMLUnit 1.3 to 1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

35

 

A.4.1

Breaking Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

35

 

A.4.2

New Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

35

 

A.4.3

Important Bug Fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

35

XMLUnit Java User’s Guide

vi

List of Tables

 

1

Document level Differences detected by DifferenceEngine . . . . . . . . . . . . . . . . . . . . . . . .

13

2

Element level Differences detected by DifferenceEngine . . . . . . . . . . . . . . . . . . . . . . . . .

14

3

Attribute level Differences detected by DifferenceEngine . . . . . . . . . . . . . . . . . . . . . . . .

14

4

Other Differences detected by DifferenceEngine . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15

5

Contents of NodeDetail.getValue() for Differences . . . . . . . . . . . . . . . . . . . . . . . . . .

15


XMLUnit Java User’s Guide

1 / 35

1 A Tour of XMLUnit

This first section contains a tour through XMLUnit’s features, the next sections will cover them in more detail.

Note that it has a strong focus on using the XMLTestCase class which is one option to use XMLUnit, but not the only one. XMLUnit’s features can be fully used without any dependency on JUnit at all.

1.1What is XMLUnit?

XMLUnit enables JUnit-style assertions to be made about the content and structure of XML1. It is an open source project hosted at http://xmlunit.sourceforge.net/ that grew out of a need to test a system that generated and received custom XML messages. The problem that we faced was how to verify that the system generated the correct message from a known set of inputs. Obviously we could use a DTD or a schema to validate the message output, but this approach wouldn’t allow us to distinguish between valid XML with correct content (e.g. element <foo>bar</foo>) and valid XML with incorrect content (e.g. element <foo>baz</foo>). What we really wanted was an assertXMLEqual() method, so we could compare the message that we expected the system to generate and the message that the system actually generated. And that was the beginning of XMLUnit.

1.2Quick tour

XMLUnit provides a single JUnit extension class, XMLTestCase, and a set of supporting classes that allow assertions to be made about:

The differences between two pieces of XML (via Diff and DetailedDiff classes)

The validity of a piece of XML (via Validator class)

The outcome of transforming a piece of XML using XSLT (via Transform class)

The evaluation of an XPath expression on a piece of XML (via classes implementing the XpathEngine interface)

Individual nodes in a piece of XML that are exposed by DOM Traversal (via NodeTest class)

XMLUnit can also treat HTML content, even badly-formed HTML, as valid XML to allow these assertions to be made about web pages (via the HTMLDocumentBuilder class).

1.3Glossary

As with many projects some words in XMLUnit have particular meanings so here is a quick overview. A piece of XML is a DOM Document, a String containing marked-up content, or a Source or Reader that allows access to marked-up content within some resource. XMLUnit compares the expected control XML to some actual test XML. The comparison can reveal that two pieces of XML are identical, similar or different. The unit of measurement used by the comparison is a difference, and differences can be either recoverable or unrecoverable. Two pieces of XML are identical if there are no differences between them, similar if there are only recoverable differences between them, and different if there are any unrecoverable differences between them.

1.4Configuring XMLUnit

There are many Java XML parsers available, and XMLUnit should work with any JAXP compliant parser library, such as Xerces- J 2 from the Apache Software Foundation. To use the XSLT and XPath features of XMLUnit a Trax (the XSLT portion of JAXP) compliant transformation engine is required, such as Xalan-J3, from the Apache Software Foundation. To configure XMLUnit to use a specific parser and transformation engine set three System properties before any tests are run, e.g.

1 For more information on JUnit see http://www.junit.org

2 http://xerces.apache.org/

3 http://xalan.apache.org/