xmlUnit1.5 Comparison

xmlunit 用来对比不同的xml content是否相同。你可以ignore withspace和order.
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreAttributeOrder(true);

DifferenceListener myDifferenceListener = new IgnoreTextAndAttributeValuesDifferenceListener();
Diff myDiff = new Diff(myControlXML, myTestXML);
myDiff.overrideDifferenceListener(myDifferenceListener);


下面是一个例子:
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import org.custommonkey.xmlunit.AbstractNodeTester;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.NodeTest;
import org.custommonkey.xmlunit.NodeTester;
import org.custommonkey.xmlunit.Validator;
import org.custommonkey.xmlunit.XMLAssert;
import org.custommonkey.xmlunit.XMLUnit;
import org.custommonkey.xmlunit.XpathEngine;
import org.custommonkey.xmlunit.exceptions.ConfigurationException;
import org.custommonkey.xmlunit.exceptions.XpathException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XMLTestCase {
/**
* Compare XML documents provided by two InputSource classes
* @param control Control document
* @param test Document to test
* @return Diff object describing differences in documents
* @throws SAXException
* @throws IOException
*/
public Diff compareXML(InputSource control, InputSource test)
throws SAXException, IOException {
return XMLUnit.compareXML(control, test);
}

/**
* Compare XML documents provided by two Reader classes
* @param control Control document
* @param test Document to test
* @return Diff object describing differences in documents
* @throws SAXException
* @throws IOException
*/
public Diff compareXML(Reader control, Reader test)
throws SAXException, IOException {
return XMLUnit.compareXML(control, test);
}

/**
* Compare XML documents provided by two Reader classes
* @param control Control document
* @param test Document to test
* @return Diff object describing differences in documents
* @throws SAXException
* @throws IOException
*/
public Diff compareXML(String control, Reader test)
throws SAXException, IOException {
return XMLUnit.compareXML(new StringReader(control), test);
}

/**
* Compare XML documents provided by two Reader classes
* @param control Control document
* @param test Document to test
* @return Diff object describing differences in documents
* @throws SAXException
* @throws IOException
*/
public Diff compareXML(Reader control, String test)
throws SAXException, IOException {
return XMLUnit.compareXML(control, new StringReader(test));
}

/**
* Compare two XML documents provided as strings
* @param control Control document
* @param test Document to test
* @return Diff object describing differences in documents
* @throws SAXException
* @throws IOException
*/
public Diff compareXML(String control, String test)
throws SAXException, IOException {
return XMLUnit.compareXML(control, test);
}

/**
* Compare two XML documents provided as strings
* @param control Control document
* @param test Document to test
* @return Diff object describing differences in documents
*/
public Diff compareXML(Document control, Document test) {
return XMLUnit.compareXML(control, test);
}

/**
* Assert that the result of an XML comparison is or is not similar.
* @param diff the result of an XML comparison
* @param assertion true if asserting that result is similar
*/
public void assertXMLEqual(Diff diff, boolean assertion) {
XMLAssert.assertXMLEqual(diff, assertion);
}

/**
* Assert that the result of an XML comparison is or is not similar.
* @param msg additional message to display if assertion fails
* @param diff the result of an XML comparison
* @param assertion true if asserting that result is similar
*/
public void assertXMLEqual(String msg, Diff diff, boolean assertion) {
XMLAssert.assertXMLEqual(msg, diff, assertion);
}

/**
* Assert that the result of an XML comparison is or is not identical
* @param diff the result of an XML comparison
* @param assertion true if asserting that result is identical
*/
public void assertXMLIdentical(Diff diff, boolean assertion) {
XMLAssert.assertXMLIdentical(diff.toString(), diff, assertion);
}

/**
* Assert that the result of an XML comparison is or is not identical
* @param msg Message to display if assertion fails
* @param diff the result of an XML comparison
* @param assertion true if asserting that result is identical
*/
public void assertXMLIdentical(String msg, Diff diff, boolean assertion) {
XMLAssert.assertXMLIdentical(msg, diff, assertion);
}

/**
* Assert that two XML documents are similar
* @param control XML to be compared against
* @param test XML to be tested
* @throws SAXException
* @throws IOException
*/
public void assertXMLEqual(InputSource control, InputSource test)
throws SAXException, IOException {
XMLAssert.assertXMLEqual(control, test);
}

/**
* Assert that two XML documents are similar
* @param control XML to be compared against
* @param test XML to be tested
* @throws SAXException
* @throws IOException
*/
public void assertXMLEqual(String control, String test)
throws SAXException, IOException {
XMLAssert.assertXMLEqual(control, test);
}

/**
* Assert that two XML documents are similar
* @param control XML to be compared against
* @param test XML to be tested
*/
public void assertXMLEqual(Document control, Document test) {
XMLAssert.assertXMLEqual(control, test);
}

/**
* Assert that two XML documents are similar
* @param control XML to be compared against
* @param test XML to be tested
* @throws SAXException
* @throws IOException
*/
public void assertXMLEqual(Reader control, Reader test)
throws SAXException, IOException {
XMLAssert.assertXMLEqual(control, test);
}

/**
* Assert that two XML documents are similar
* @param err Message to be displayed on assertion failure
* @param control XML to be compared against
* @param test XML to be tested
* @throws SAXException
* @throws IOException
*/
public void assertXMLEqual(String err, String control, String test)
throws SAXException, IOException {
XMLAssert.assertXMLEqual(err, control, test);
}

/**
* Assert that two XML documents are similar
* @param err Message to be displayed on assertion failure
* @param control XML to be compared against
* @param test XML to be tested
* @throws SAXException
* @throws IOException
*/
public void assertXMLEqual(String err, InputSource control,
InputSource test)
throws SAXException, IOException {
XMLAssert.assertXMLEqual(err, control, test);
}

/**
* Assert that two XML documents are similar
* @param err Message to be displayed on assertion failure
* @param control XML to be compared against
* @param test XML to be tested
*/
public void assertXMLEqual(String err, Document control, Document test) {
XMLAssert.assertXMLEqual(err, control, test);
}

/**
* Assert that two XML documents are similar
* @param err Message to be displayed on assertion failure
* @param control XML to be compared against
* @param test XML to be tested
* @throws SAXException
* @throws IOException
*/
public void assertXMLEqual(String err, Reader control, Reader test)
throws SAXException, IOException {
XMLAssert.assertXMLEqual(err, control, test);
}

/**
* Assert that two XML documents are NOT similar
* @param control XML to be compared against
* @param test XML to be tested
* @throws SAXException
* @throws IOException
*/
public void assertXMLNotEqual(InputSource control, InputSource test)
throws SAXException, IOException {
XMLAssert.assertXMLNotEqual(control, test);
}

/**
* Assert that two XML documents are NOT similar
* @param control XML to be compared against
* @param test XML to be tested
* @throws SAXException
* @throws IOException
*/
public void assertXMLNotEqual(String control, String test)
throws SAXException, IOException {
XMLAssert.assertXMLNotEqual(control, test);
}

/**
* Assert that two XML documents are NOT similar
* @param control XML to be compared against
* @param test XML to be tested
*/
public void assertXMLNotEqual(Document control, Document test) {
XMLAssert.assertXMLNotEqual(control, test);
}

/**
* Assert that two XML documents are NOT similar
* @param control XML to be compared against
* @param test XML to be tested
* @throws SAXException
* @throws IOException
*/
public void assertXMLNotEqual(Reader control, Reader test)
throws SAXException, IOException {
XMLAssert.assertXMLNotEqual(control, test);
}

/**
* Assert that two XML documents are NOT similar
* @param err Message to be displayed on assertion failure
* @param control XML to be compared against
* @param test XML to be tested
* @throws SAXException
* @throws IOException
*/
public void assertXMLNotEqual(String err, InputSource control,
InputSource test)
throws SAXException, IOException {
XMLAssert.assertXMLNotEqual(err, control, test);
}

/**
* Assert that two XML documents are NOT similar
* @param err Message to be displayed on assertion failure
* @param control XML to be compared against
* @param test XML to be tested
* @throws SAXException
* @throws IOException
*/
public void assertXMLNotEqual(String err, String control, String test)
throws SAXException, IOException {
XMLAssert.assertXMLNotEqual(err, control, test);
}

/**
* Assert that two XML documents are NOT similar
* @param err Message to be displayed on assertion failure
* @param control XML to be compared against
* @param test XML to be tested
*/
public void assertXMLNotEqual(String err, Document control, Document test) {
XMLAssert.assertXMLNotEqual(err, control, test);
}

/**
* Assert that two XML documents are NOT similar
* @param err Message to be displayed on assertion failure
* @param control XML to be compared against
* @param test XML to be tested
* @throws SAXException
* @throws IOException
*/
public void assertXMLNotEqual(String err, Reader control, Reader test)
throws SAXException, IOException {
XMLAssert.assertXMLNotEqual(err, control, test);
}

/**
* Assert that the node lists of two Xpaths in the same document are equal
* @param xpathOne
* @param xpathTwo
* @param document
* @see XpathEngine
*/
public void assertXpathsEqual(String controlXpath, String testXpath,
InputSource document)
throws SAXException, IOException, XpathException {
XMLAssert.assertXpathsEqual(controlXpath, testXpath, document);
}

/**
* Assert that the node lists of two Xpaths in the same document are equal
* @param xpathOne
* @param xpathTwo
* @param document
* @see XpathEngine
*/
public void assertXpathsEqual(String controlXpath, String testXpath,
Document document)
throws XpathException {
XMLAssert.assertXpathsEqual(controlXpath, testXpath, document);
}

/**
* Assert that the node lists of two Xpaths in the same XML string are
* equal
* @param xpathOne
* @param xpathTwo
* @param inXMLString
* @throws SAXException
* @throws IOException
*/
public void assertXpathsEqual(String controlXpath, String testXpath,
String inXMLString)
throws SAXException, IOException, XpathException {
XMLAssert.assertXpathsEqual(controlXpath, testXpath, inXMLString);
}

/**
* Assert that the node lists of two Xpaths in two XML pieces are equal
* @param xpathOne
* @param control
* @param xpathTwo
* @param test
* @throws SAXException
* @throws IOException
*/
public void assertXpathsEqual(String controlXpath, InputSource control,
String testXpath, InputSource test)
throws SAXException, IOException, XpathException {
XMLAssert.assertXpathsEqual(controlXpath, control,
testXpath, test);
}

/**
* Assert that the node lists of two Xpaths in two XML strings are equal
* @param xpathOne
* @param inControlXMLString
* @param xpathTwo
* @param inTestXMLString
* @throws SAXException
* @throws IOException
*/
public void assertXpathsEqual(String controlXpath,
String inControlXMLString, String testXpath,
String inTestXMLString)
throws SAXException, IOException,
XpathException {
XMLAssert.assertXpathsEqual(controlXpath, inControlXMLString,
testXpath, inTestXMLString);
}

/**
* Assert that the node lists of two Xpaths in two documents are equal
* @param xpathOne
* @param xpathTwo
* @param document
* @see XpathEngine
*/
public void assertXpathsEqual(String controlXpath,
Document controlDocument,
String testXpath, Document testDocument)
throws XpathException {
XMLAssert.assertXpathsEqual(controlXpath, controlDocument, testXpath,
testDocument);
}

/**
* Assert that the node lists of two Xpaths in the same document are NOT equal
* @param xpathOne
* @param xpathTwo
* @param document
* @see XpathEngine
*/
public void assertXpathsNotEqual(String controlXpath, String testXpath,
Document document)
throws XpathException {
XMLAssert.assertXpathsNotEqual(controlXpath, testXpath, document);
}

/**
* Assert that the node lists of two Xpaths in the same XML are NOT
* equal
* @param xpathOne
* @param xpathTwo
* @param control
* @throws SAXException
* @throws IOException
*/
public void assertXpathsNotEqual(String controlXpath, String testXpath,
InputSource control)
throws SAXException, IOException, XpathException {
XMLAssert.assertXpathsNotEqual(controlXpath, testXpath, control);
}

/**
* Assert that the node lists of two Xpaths in the same XML string are NOT
* equal
* @param xpathOne
* @param xpathTwo
* @param inXMLString
* @throws SAXException
* @throws IOException
*/
public void assertXpathsNotEqual(String controlXpath, String testXpath,
String inXMLString)
throws SAXException,
IOException, XpathException {
XMLAssert.assertXpathsNotEqual(controlXpath, testXpath, inXMLString);
}

/**
* Assert that the node lists of two Xpaths in two pieces of XML
* are NOT equal
* @param xpathOne
* @param control
* @param xpathTwo
* @param test
* @throws SAXException
* @throws IOException
*/
public void assertXpathsNotEqual(String controlXpath, InputSource control,
String testXpath, InputSource test)
throws SAXException, IOException, XpathException {
XMLAssert.assertXpathsNotEqual(controlXpath, control, testXpath, test);
}

/**
* Assert that the node lists of two Xpaths in two XML strings are NOT equal
* @param xpathOne
* @param inControlXMLString
* @param xpathTwo
* @param inTestXMLString
* @throws SAXException
* @throws IOException
*/
public void assertXpathsNotEqual(String controlXpath,
String inControlXMLString,
String testXpath, String inTestXMLString)
throws SAXException, IOException,
XpathException {
XMLAssert.assertXpathsNotEqual(controlXpath, inControlXMLString,
testXpath, inTestXMLString);
}

/**
* Assert that the node lists of two Xpaths in two documents are NOT equal
* @param xpathOne
* @param xpathTwo
* @param document
* @see XpathEngine
*/
public void assertXpathsNotEqual(String controlXpath,
Document controlDocument,
String testXpath, Document testDocument)
throws XpathException {
XMLAssert.assertXpathsNotEqual(controlXpath, controlDocument,
testXpath, testDocument);
}

/**
* Assert that the evaluation of two Xpaths in the same document are equal
* @param xpathOne
* @param xpathTwo
* @param document
* @see XpathEngine
*/
public void assertXpathValuesEqual(String controlXpath, String testXpath,
Document document)
throws XpathException {
XMLAssert.assertXpathValuesEqual(controlXpath, testXpath, document);
}

/**
* Assert that the evaluation of two Xpaths in the same XML are
* equal
* @param xpathOne
* @param xpathTwo
* @param control
* @throws SAXException
* @throws IOException
*/
public void assertXpathValuesEqual(String controlXpath, String testXpath,
InputSource control)
throws SAXException, IOException, XpathException {
XMLAssert.assertXpathValuesEqual(controlXpath, testXpath, control);
}

/**
* Assert that the evaluation of two Xpaths in the same XML string are
* equal
* @param xpathOne
* @param xpathTwo
* @param inXMLString
* @throws SAXException
* @throws IOException
*/
public void assertXpathValuesEqual(String controlXpath, String testXpath,
String inXMLString)
throws SAXException, IOException,
XpathException {
XMLAssert.assertXpathValuesEqual(controlXpath, testXpath, inXMLString);
}

/**
* Assert that the evaluation of two Xpaths in two XML strings are equal
* @param xpathOne
* @param control
* @param xpathTwo
* @param test
* @throws SAXException
* @throws IOException
*/
public void assertXpathValuesEqual(String controlXpath,
InputSource control,
String testXpath,
InputSource test)
throws SAXException, IOException, XpathException {
XMLAssert.assertXpathValuesEqual(controlXpath, control,
testXpath, test);
}

/**
* Assert that the evaluation of two Xpaths in two XML strings are equal
* @param xpathOne
* @param inControlXMLString
* @param xpathTwo
* @param inTestXMLString
* @throws SAXException
* @throws IOException
*/
public void assertXpathValuesEqual(String controlXpath,
String inControlXMLString,
String testXpath,
String inTestXMLString)
throws SAXException, IOException,
XpathException {
XMLAssert.assertXpathValuesEqual(controlXpath, inControlXMLString,
testXpath, inTestXMLString);
}

/**
* Assert that the evaluation of two Xpaths in two documents are equal
* @param xpathOne
* @param xpathTwo
* @param document
* @see XpathEngine
*/
public void assertXpathValuesEqual(String controlXpath,
Document controlDocument,
String testXpath, Document testDocument)
throws XpathException {
XMLAssert.assertXpathValuesEqual(controlXpath, controlDocument,
testXpath, testDocument);
}

/**
* Assert that the evaluation of two Xpaths in the same XML string are
* NOT equal
* @param xpathOne
* @param xpathTwo
* @param control
* @throws SAXException
* @throws IOException
*/
public void assertXpathValuesNotEqual(String controlXpath,
String testXpath,
InputSource control)
throws SAXException, IOException, XpathException {
XMLAssert.assertXpathValuesNotEqual(controlXpath, testXpath,
control);
}

/**
* Assert that the evaluation of two Xpaths in the same XML string are
* NOT equal
* @param xpathOne
* @param xpathTwo
* @param inXMLString
* @throws SAXException
* @throws IOException
*/
public void assertXpathValuesNotEqual(String controlXpath, String testXpath,
String inXMLString)
throws SAXException, IOException,
XpathException {
XMLAssert.assertXpathValuesNotEqual(controlXpath, testXpath,
inXMLString);
}

/**
* Assert that the evaluation of two Xpaths in the same document are
* NOT equal
* @param xpathOne
* @param xpathTwo
* @param document
*/
public void assertXpathValuesNotEqual(String controlXpath, String testXpath,
Document document)
throws XpathException {
XMLAssert.assertXpathValuesNotEqual(controlXpath, testXpath, document);
}

/**
* Assert that the evaluation of two Xpaths in two XML strings are
* NOT equal
* @param xpathOne
* @param control
* @param xpathTwo
* @param test
* @throws SAXException
* @throws IOException
*/
public void assertXpathValuesNotEqual(String controlXpath,
InputSource control,
String testXpath,
InputSource test)
throws SAXException, IOException, XpathException {
XMLAssert.assertXpathValuesNotEqual(controlXpath, control,
testXpath, test);
}

/**
* Assert that the evaluation of two Xpaths in two XML strings are
* NOT equal
* @param xpathOne
* @param inControlXMLString
* @param xpathTwo
* @param inTestXMLString
* @throws SAXException
* @throws IOException
*/
public void assertXpathValuesNotEqual(String controlXpath,
String inControlXMLString,
String testXpath,
String inTestXMLString)
throws SAXException, IOException,
XpathException {
XMLAssert.assertXpathValuesNotEqual(controlXpath, inControlXMLString,
testXpath, inTestXMLString);
}

/**
* Assert that the evaluation of two Xpaths in two documents are
* NOT equal
* @param xpathOne
* @param xpathTwo
* @param document
*/
public void assertXpathValuesNotEqual(String controlXpath,
Document controlDocument,
String testXpath,
Document testDocument)
throws XpathException {
XMLAssert.assertXpathValuesNotEqual(controlXpath, controlDocument,
testXpath, testDocument);
}

/**
* Assert the value of an Xpath expression in an XML String
* @param expectedValue
* @param xpathExpression
* @param control
* @throws SAXException
* @throws IOException
* @see XpathEngine which provides the underlying evaluation mechanism
*/
public void assertXpathEvaluatesTo(String expectedValue,
String xpathExpression,
InputSource control)
throws SAXException, IOException, XpathException {
XMLAssert.assertXpathEvaluatesTo(expectedValue, xpathExpression,
control);
}

/**
* Assert the value of an Xpath expression in an XML String
* @param expectedValue
* @param xpathExpression
* @param inXMLString
* @throws SAXException
* @throws IOException
* @see XpathEngine which provides the underlying evaluation mechanism
*/
public void assertXpathEvaluatesTo(String expectedValue,
String xpathExpression,
String inXMLString)
throws SAXException, IOException,
XpathException {
XMLAssert.assertXpathEvaluatesTo(expectedValue, xpathExpression,
inXMLString);
}

/**
* Assert the value of an Xpath expression in an DOM Document
* @param expectedValue
* @param xpathExpression
* @param inDocument
* @param ctx
* @see XpathEngine which provides the underlying evaluation mechanism
*/
public void assertXpathEvaluatesTo(String expectedValue,
String xpathExpression,
Document inDocument)
throws XpathException {
XMLAssert.assertXpathEvaluatesTo(expectedValue, xpathExpression,
inDocument);
}

/**
* Assert that a specific XPath exists in some given XML
* @param inXpathExpression
* @param xml
* @see XpathEngine which provides the underlying evaluation mechanism
*/
public void assertXpathExists(String xPathExpression,
InputSource xml)
throws IOException, SAXException, XpathException {
XMLAssert.assertXpathExists(xPathExpression, xml);
}

/**
* Assert that a specific XPath exists in some given XML
* @param inXpathExpression
* @param inXMLString
* @see XpathEngine which provides the underlying evaluation mechanism
*/
public void assertXpathExists(String xPathExpression,
String inXMLString)
throws IOException, SAXException, XpathException {
XMLAssert.assertXpathExists(xPathExpression, inXMLString);
}

/**
* Assert that a specific XPath exists in some given XML
* @param inXpathExpression
* @param inDocument
* @param ctx
* @see XpathEngine which provides the underlying evaluation mechanism
*/
public void assertXpathExists(String xPathExpression, Document inDocument)
throws XpathException {
XMLAssert.assertXpathExists(xPathExpression, inDocument);
}

/**
* Assert that a specific XPath does NOT exist in some given XML
* @param inXpathExpression
* @param xml
* @see XpathEngine which provides the underlying evaluation mechanism
*/
public void assertXpathNotExists(String xPathExpression,
InputSource xml)
throws IOException, SAXException, XpathException {
XMLAssert.assertXpathNotExists(xPathExpression, xml);
}

/**
* Assert that a specific XPath does NOT exist in some given XML
* @param inXpathExpression
* @param inXMLString
* @see XpathEngine which provides the underlying evaluation mechanism
*/
public void assertXpathNotExists(String xPathExpression,
String inXMLString)
throws IOException, SAXException, XpathException {
XMLAssert.assertXpathNotExists(xPathExpression, inXMLString);
}

/**
* Assert that a specific XPath does NOT exist in some given XML
* @param inXpathExpression
* @param inDocument
* @see XpathEngine which provides the underlying evaluation mechanism
*/
public void assertXpathNotExists(String xPathExpression,
Document inDocument)
throws XpathException {
XMLAssert.assertXpathNotExists(xPathExpression, inDocument);
}

/**
* Assert that a piece of XML contains valid XML: the input must
* contain a DOCTYPE declaration to be validated
* @param xml
* @throws SAXException
* @throws ConfigurationException if validation could not be turned on
* @see Validator
*/
public void assertXMLValid(InputSource xml)
throws SAXException, ConfigurationException {
XMLAssert.assertXMLValid(xml);
}

/**
* Assert that a String containing XML contains valid XML: the String must
* contain a DOCTYPE declaration to be validated
* @param xmlString
* @throws SAXException
* @throws ConfigurationException if validation could not be turned on
* @see Validator
*/
public void assertXMLValid(String xmlString)
throws SAXException, ConfigurationException {
XMLAssert.assertXMLValid(xmlString);
}

/**
* Assert that a piece of XML contains valid XML: the document must
* contain a DOCTYPE to be validated, but the validation will use the
* systemId to obtain the DTD
* @param xml
* @param systemId
* @throws SAXException
* @throws ConfigurationException if validation could not be turned on
* @see Validator
*/
public void assertXMLValid(InputSource xml, String systemId)
throws SAXException, ConfigurationException {
XMLAssert.assertXMLValid(xml, systemId);
}

/**
* Assert that a String containing XML contains valid XML: the String must
* contain a DOCTYPE to be validated, but the validation will use the
* systemId to obtain the DTD
* @param xmlString
* @param systemId
* @throws SAXException
* @throws ConfigurationException if validation could not be turned on
* @see Validator
*/
public void assertXMLValid(String xmlString, String systemId)
throws SAXException, ConfigurationException {
XMLAssert.assertXMLValid(xmlString, systemId);
}

/**
* Assert that a piece of XML contains valid XML: the document
* will be given a DOCTYPE to be validated with the name and
* systemId specified regardless of whether it already contains a
* doctype declaration.
* @param xml
* @param systemId
* @param doctype
* @throws SAXException
* @throws ConfigurationException if validation could not be turned on
* @see Validator
*/
public void assertXMLValid(InputSource xml, String systemId,
String doctype)
throws SAXException, ConfigurationException {
XMLAssert.assertXMLValid(xml, systemId, doctype);
}

/**
* Assert that a String containing XML contains valid XML: the String will
* be given a DOCTYPE to be validated with the name and systemId specified
* regardless of whether it already contains a doctype declaration.
* @param xmlString
* @param systemId
* @param doctype
* @throws SAXException
* @throws ConfigurationException if validation could not be turned on
* @see Validator
*/
public void assertXMLValid(String xmlString, String systemId, String doctype)
throws SAXException, ConfigurationException {
XMLAssert.assertXMLValid(xmlString, systemId, doctype);
}

/**
* Assert that a Validator instance returns <code>isValid() == true</code>
* @param validator
*/
public void assertXMLValid(Validator validator) {
XMLAssert.assertXMLValid(validator);
}

/**
* Execute a <code>NodeTest<code> for a single node type
* and assert that it passes
* @param xml XML to be tested
* @param tester The test strategy
* @param nodeType The node type to be tested: constants defined
* in {@link Node org.w3c.dom.Node} e.g. <code>Node.ELEMENT_NODE</code>
* @throws SAXException
* @throws IOException
* @see AbstractNodeTester
* @see CountingNodeTester
*/
public void assertNodeTestPasses(InputSource xml, NodeTester tester,
short nodeType)
throws SAXException, IOException {
XMLAssert.assertNodeTestPasses(xml, tester, nodeType);
}

/**
* Execute a <code>NodeTest<code> for a single node type
* and assert that it passes
* @param xmlString XML to be tested
* @param tester The test strategy
* @param nodeType The node type to be tested: constants defined
* in {@link Node org.w3c.dom.Node} e.g. <code>Node.ELEMENT_NODE</code>
* @throws SAXException
* @throws IOException
* @see AbstractNodeTester
* @see CountingNodeTester
*/
public void assertNodeTestPasses(String xmlString, NodeTester tester,
short nodeType)
throws SAXException, IOException {
XMLAssert.assertNodeTestPasses(xmlString, tester, nodeType);
}

/**
* Execute a <code>NodeTest<code> for multiple node types and make an
* assertion about it whether it is expected to pass
* @param test a NodeTest instance containing the XML source to be tested
* @param tester The test strategy
* @param nodeTypes The node types to be tested: constants defined
* in {@link Node org.w3c.dom.Node} e.g. <code>Node.ELEMENT_NODE</code>
* @param assertion true if the test is expected to pass, false otherwise
* @see AbstractNodeTester
* @see CountingNodeTester
*/
public void assertNodeTestPasses(NodeTest test, NodeTester tester,
short[] nodeTypes, boolean assertion) {
XMLAssert.assertNodeTestPasses(test, tester, nodeTypes, assertion);
}
}



import java.io.File;
import java.io.FileReader;
import java.util.List;

import javax.xml.transform.stream.StreamSource;

import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.DifferenceListener;
import org.custommonkey.xmlunit.ElementNameAndTextQualifier;
import org.custommonkey.xmlunit.HTMLDocumentBuilder;
import org.custommonkey.xmlunit.IgnoreTextAndAttributeValuesDifferenceListener;
import org.custommonkey.xmlunit.TolerantSaxDocumentBuilder;
import org.custommonkey.xmlunit.Transform;
import org.custommonkey.xmlunit.Validator;
import org.custommonkey.xmlunit.XMLUnit;
import org.custommonkey.xmlunit.XpathEngine;
import org.custommonkey.xmlunit.examples.CountingNodeTester;
import org.junit.Assert;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ComparisonTest extends XMLTestCase {
public static void main(String[] args) {
}

@Test
public void testForEquality() throws Exception {
String myControlXML = "<msg><uuid>0x00435A8C</uuid></msg>";
String myTestXML = "<msg><localId>2376</localId></msg>";
// assertXMLEqual("comparing test xml to control xml", myControlXML,
// myTestXML);

assertXMLNotEqual("test xml not similar to control xml", myControlXML,
myTestXML);
}

@Test
public void testIdentical() throws Exception {
String myControlXML = "<struct><int>3</int><boolean>false</boolean></struct>";
String myTestXML = "<struct><boolean>false</boolean><int>3</int></struct>";
Diff myDiff = new Diff(myControlXML, myTestXML);
Assert.assertTrue("pieces of XML are similar " + myDiff,
myDiff.similar());
Assert.assertFalse("but are they identical? " + myDiff,
myDiff.identical());
}

@Test
public void testAllDifferences() throws Exception {
String myControlXML = "<news><item id=\"1\">War</item>"
+ "<item id=\"2\">Plague</item><item id=\"3\">Famine</item></news>";
String myTestXML = "<news><item id=\"1\">Peace</item>"
+ "<item id=\"2\">Health</item><item id=\"3\">Plenty</item></news>";
DetailedDiff myDiff = new DetailedDiff(compareXML(myControlXML,
myTestXML));
List allDifferences = myDiff.getAllDifferences();
Assert.assertNotEquals(myDiff.toString(), 0, allDifferences.size());
}

@Test
public void testCompareToSkeletonXML() throws Exception {
String myControlXML = "<location><street-address>22 any street</street-address><postcode>XY00 99Z</postcode></location>";
String myTestXML = "<location><street-address>20 east cheap</street-address><postcode>EC3M 1EB</postcode></location>";
DifferenceListener myDifferenceListener = new IgnoreTextAndAttributeValuesDifferenceListener();
Diff myDiff = new Diff(myControlXML, myTestXML);
myDiff.overrideDifferenceListener(myDifferenceListener);
Assert.assertTrue("test XML matches control skeleton XML " + myDiff,
myDiff.similar());
}

@Test
public void testRepeatedChildElements() throws Exception {
String myControlXML = "<suite><test status=\"pass\">FirstTestCase</test><test status=\"pass\">SecondTestCase</test></suite>";
String myTestXML = "<suite><test status=\"pass\">SecondTestCase</test><test status=\"pass\">FirstTestCase</test></suite>";

assertXMLNotEqual(
"Repeated child elements in different sequence order are not equal by default",
myControlXML, myTestXML);

Diff myDiff = new Diff(myControlXML, myTestXML);
myDiff.overrideElementQualifier(new ElementNameAndTextQualifier());
assertXMLEqual(
"But they are equal when an ElementQualifier controls which test element is compared with each control element",
myDiff, true);
}

public void testXSLTransformation() throws Exception {
String myInputXML = "...";
File myStylesheetFile = new File("...");
Transform myTransform = new Transform(myInputXML, myStylesheetFile);
String myExpectedOutputXML = "...";
Diff myDiff = new Diff(myExpectedOutputXML, myTransform);
Assert.assertTrue("XSL transformation worked as expected " + myDiff,
myDiff.similar());
}

public void testAnotherXSLTransformation() throws Exception {
File myInputXMLFile = new File("...");
File myStylesheetFile = new File("...");
Transform myTransform = new Transform(new StreamSource(myInputXMLFile),
new StreamSource(myStylesheetFile));
Document myExpectedOutputXML = XMLUnit.buildDocument(
XMLUnit.getControlParser(), new FileReader("..."));
Diff myDiff = new Diff(myExpectedOutputXML,
myTransform.getResultDocument());
Assert.assertTrue("XSL transformation worked as expected " + myDiff,
myDiff.similar());
}

public void testValidation() throws Exception {
XMLUnit.getTestDocumentBuilderFactory().setValidating(true);
// As the document is parsed it is validated against its referenced DTD
Document myTestDocument = XMLUnit.buildTestDocument("...");
String mySystemId = "...";
String myDTDUrl = new File("...").toURL().toExternalForm();
Validator myValidator = new Validator(myTestDocument, mySystemId,
myDTDUrl);
Assert.assertTrue("test document validates against unreferenced DTD",
myValidator.isValid());
}

@Test
public void testXPaths() throws Exception {
String mySolarSystemXML = "<solar-system><planet name='Earth' position='3' supportsLife='yes'/>"
+ "<planet name='Venus' position='4'/></solar-system>";

XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine();
NodeList list = simpleXpathEngine.getMatchingNodes("//planet",
XMLUnit.buildControlDocument(mySolarSystemXML));
Assert.assertTrue(list.getLength() == 2);

assertXpathExists("//planet[@name='Earth']", mySolarSystemXML);
assertXpathNotExists("//star[@name='alpha centauri']", mySolarSystemXML);
assertXpathsEqual("//planet[@name='Earth']", "//planet[@position='3']",
mySolarSystemXML);
assertXpathsNotEqual("//planet[@name='Venus']",
"//planet[@supportsLife='yes']", mySolarSystemXML);
}

public void testXPathValues() throws Exception {
String myJavaFlavours = "<java-flavours><jvm current='some platforms'>1.1.x</jvm>"
+ "<jvm current='no'>1.2.x</jvm><jvm current='yes'>1.3.x</jvm>"
+ "<jvm current='yes' latest='yes'>1.4.x</jvm></java-flavours>";
assertXpathEvaluatesTo("1.4.x", "//jvm[@latest='yes']", myJavaFlavours);
assertXpathEvaluatesTo("2", "count(//jvm[@current='yes'])",
myJavaFlavours);
assertXpathValuesEqual("//jvm[4]/@latest", "//jvm[4]/@current",
myJavaFlavours);
assertXpathValuesNotEqual("//jvm[2]/@current", "//jvm[3]/@current",
myJavaFlavours);
}

public void testXpathsInHTML() throws Exception {
String someBadlyFormedHTML = "<html><title>Ugh</title><body><h1>Heading<ul><li id='1'>Item One<li id='2'>Item Two";
TolerantSaxDocumentBuilder tolerantSaxDocumentBuilder = new TolerantSaxDocumentBuilder(
XMLUnit.getTestParser());
HTMLDocumentBuilder htmlDocumentBuilder = new HTMLDocumentBuilder(
tolerantSaxDocumentBuilder);
Document wellFormedDocument = htmlDocumentBuilder
.parse(someBadlyFormedHTML);
assertXpathEvaluatesTo("Item One", "/html/body//li[@id='1']",
wellFormedDocument);
}

public void testCountingNodeTester() throws Exception {
String testXML = "<fibonacci><val>1</val><val>2</val><val>3</val>"
+ "<val>5</val><val>9</val></fibonacci>";
CountingNodeTester countingNodeTester = new CountingNodeTester(4);
assertNodeTestPasses(testXML, countingNodeTester, Node.TEXT_NODE);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值