From: <bo...@us...> - 2007-03-28 04:07:35
|
Revision: 159 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=159&view=rev Author: bodewig Date: 2007-03-27 21:07:35 -0700 (Tue, 27 Mar 2007) Log Message: ----------- More overrides that allow InputSource as source Modified Paths: -------------- trunk/xmlunit/src/java/org/custommonkey/xmlunit/DoctypeReader.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/Validator.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLAssert.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLTestCase.java trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Validator.java Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/DoctypeReader.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/DoctypeReader.java 2007-03-28 04:06:22 UTC (rev 158) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/DoctypeReader.java 2007-03-28 04:07:35 UTC (rev 159) @@ -38,6 +38,8 @@ import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; @@ -79,6 +81,32 @@ } /** + * Create a Reader whose XML content is provided by the originalSource with + * the exception of the DOCTYPE which is provided by the doctypeName + * and systemID. + * @param originalSource + * @param doctypeName + * @param systemID + */ + public DoctypeReader(InputStream originalSource, String encoding, + String doctypeName, String systemID) + throws IOException { + this(encoding != null + ? new InputStreamReader(originalSource, encoding) + : xmlStreamToReader(originalSource), + doctypeName, systemID); + } + + // XXX - we are cheating here, we should read into the source + // stream to see whether the XML decl (if any) specifies the + // encoding and then return an InputStreamReader using the proper + // encoding + private static Reader xmlStreamToReader(InputStream originalSource) + throws IOException { + return new InputStreamReader(originalSource); + } + + /** * @return the content of the original source, without amendments or * substitutions. Safe to call multiple times. * @throws IOException if thrown while reading from the original source Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/Validator.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/Validator.java 2007-03-28 04:06:22 UTC (rev 158) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/Validator.java 2007-03-28 04:07:35 UTC (rev 159) @@ -240,6 +240,29 @@ /** * Full constructor. + * Validates the contents of the InputSource using the DTD + * specified with the systemID and named with the doctype name. + * + * @param sourceForValidation + * @param systemID + * @param doctype + * @throws SAXException + * @throws ConfigurationException if validation could not be turned on + */ + public Validator(InputSource sourceForValidation, String systemID, + String doctype) + throws SAXException, IOException, ConfigurationException { + this(sourceForValidation.getCharacterStream() != null + ? new DoctypeReader(sourceForValidation.getCharacterStream(), + doctype, systemID) + : new DoctypeReader(sourceForValidation.getByteStream(), + sourceForValidation.getEncoding(), + doctype, systemID), + systemID); + } + + /** + * Full constructor. * Validates the contents of the Reader using the DTD specified with the * systemID and named with the doctype name. * @@ -249,9 +272,12 @@ * @throws SAXException * @throws ConfigurationException if validation could not be turned on */ - public Validator(Reader readerForValidation, String systemID, String doctype) + public Validator(Reader readerForValidation, String systemID, + String doctype) throws SAXException, ConfigurationException { - this(new DoctypeReader(readerForValidation, doctype, systemID), + this(readerForValidation instanceof DoctypeReader + ? readerForValidation + : new DoctypeReader(readerForValidation, doctype, systemID), systemID); } Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLAssert.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLAssert.java 2007-03-28 04:06:22 UTC (rev 158) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLAssert.java 2007-03-28 04:07:35 UTC (rev 159) @@ -49,6 +49,7 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -109,9 +110,7 @@ * @param assertion true if asserting that result is similar */ public static void assertXMLEqual(Diff diff, boolean assertion) { - if (assertion != diff.similar()) { - fail(diff.toString()); - } + assertXMLEqual(null, diff, assertion); } /** @@ -120,21 +119,28 @@ * @param diff the result of an XML comparison * @param assertion true if asserting that result is similar */ - public static void assertXMLEqual(String msg, Diff diff, boolean assertion) { + public static void assertXMLEqual(String msg, Diff diff, + boolean assertion) { if (assertion != diff.similar()) { - fail(msg + ", " + diff.toString()); + fail(getFailMessage(msg, diff)); } } + private static String getFailMessage(String msg, Diff diff) { + StringBuffer sb = new StringBuffer(); + if (msg != null && msg.length() > 0) { + sb.append(msg).append(", "); + } + return sb.append(diff.toString()).toString(); + } + /** * 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 static void assertXMLIdentical(Diff diff, boolean assertion) { - if (assertion != diff.identical()) { - fail(diff.toString()); - } + assertXMLIdentical(null, diff, assertion); } /** @@ -156,7 +162,7 @@ */ public static void assertXMLIdentical(String msg, Diff diff, boolean assertion) { if (assertion != diff.identical()) { - fail(msg + ", " + diff.toString()); + fail(getFailMessage(msg, diff)); } } @@ -167,10 +173,21 @@ * @throws SAXException * @throws IOException */ + public static void assertXMLEqual(InputSource control, InputSource test) + throws SAXException, IOException { + assertXMLEqual(null, 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 static void assertXMLEqual(String control, String test) throws SAXException, IOException { - Diff diff = new Diff(control, test); - assertXMLEqual(diff, true); + assertXMLEqual(null, control, test); } /** @@ -179,8 +196,7 @@ * @param test XML to be tested */ public static void assertXMLEqual(Document control, Document test) { - Diff diff = new Diff(control, test); - assertXMLEqual(diff, true); + assertXMLEqual(null, control, test); } /** @@ -192,8 +208,22 @@ */ public static void assertXMLEqual(Reader control, Reader test) throws SAXException, IOException { + assertXMLEqual(null, 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 static void assertXMLEqual(String err, InputSource control, + InputSource test) + throws SAXException, IOException { Diff diff = new Diff(control, test); - assertXMLEqual(diff, true); + assertXMLEqual(err, diff, true); } /** @@ -243,24 +273,21 @@ * @throws SAXException * @throws IOException */ - public static void assertXMLNotEqual(String control, String test) + public static void assertXMLNotEqual(InputSource control, InputSource test) throws SAXException, IOException { - Diff diff = new Diff(control, test); - assertXMLEqual(diff, false); + assertXMLNotEqual(null, 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 static void assertXMLNotEqual(String err, String control, String test) + public static void assertXMLNotEqual(String control, String test) throws SAXException, IOException { - Diff diff = new Diff(control, test); - assertXMLEqual(err, diff, false); + assertXMLNotEqual(null, control, test); } /** @@ -269,33 +296,48 @@ * @param test XML to be tested */ public static void assertXMLNotEqual(Document control, Document test) { - Diff diff = new Diff(control, test); - assertXMLEqual(diff, false); + assertXMLNotEqual(null, 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 static void assertXMLNotEqual(Reader control, Reader test) + throws SAXException, IOException { + assertXMLNotEqual(null, 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 static void assertXMLNotEqual(String err, Document control, - Document test) { + public static void assertXMLNotEqual(String err, InputSource control, + InputSource test) + throws SAXException, IOException { Diff diff = new Diff(control, test); assertXMLEqual(err, diff, false); } /** * 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 static void assertXMLNotEqual(Reader control, Reader test) + public static void assertXMLNotEqual(String err, String control, String test) throws SAXException, IOException { Diff diff = new Diff(control, test); - assertXMLEqual(diff, false); + assertXMLEqual(err, diff, false); } /** @@ -303,6 +345,18 @@ * @param err Message to be displayed on assertion failure * @param control XML to be compared against * @param test XML to be tested + */ + public static void assertXMLNotEqual(String err, Document control, + Document test) { + Diff diff = new Diff(control, test); + assertXMLEqual(err, diff, false); + } + + /** + * 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 */ @@ -326,6 +380,20 @@ } /** + * Assert that the node lists of two Xpaths in the same document are equal + * @param xpathOne + * @param xpathTwo + * @param document + * @see XpathEngine + */ + public static void assertXpathsEqual(String controlXpath, String testXpath, + InputSource document) + throws SAXException, IOException, XpathException { + assertXpathsEqual(controlXpath, testXpath, + XMLUnit.buildControlDocument(document)); + } + + /** * Assert that the node lists of two Xpaths in the same XML string are * equal * @param xpathOne @@ -342,6 +410,25 @@ } /** + * Assert that the node lists of two Xpaths in two documents are equal + * @param xpathOne + * @param xpathTwo + * @param controlDocument + * @param testDocument + * @see XpathEngine + */ + public static void assertXpathsEqual(String controlXpath, + InputSource controlDocument, + String testXpath, + InputSource testDocument) + throws SAXException, IOException, XpathException { + assertXpathsEqual(controlXpath, + XMLUnit.buildControlDocument(controlDocument), + testXpath, + XMLUnit.buildTestDocument(testDocument)); + } + + /** * Assert that the node lists of two Xpaths in two XML strings are equal * @param xpathOne * @param inControlXMLString @@ -365,7 +452,8 @@ * Assert that the node lists of two Xpaths in two documents are equal * @param xpathOne * @param xpathTwo - * @param document + * @param controlDocument + * @param testDocument * @see XpathEngine */ public static void assertXpathsEqual(String controlXpath, @@ -398,6 +486,21 @@ } /** + * Assert that the node lists of two Xpaths in the same document are NOT equal + * @param xpathOne + * @param xpathTwo + * @param document + * @see XpathEngine + */ + public static void assertXpathsNotEqual(String controlXpath, + String testXpath, + InputSource document) + throws SAXException, IOException, XpathException { + assertXpathsNotEqual(controlXpath, testXpath, + XMLUnit.buildControlDocument(document)); + } + + /** * Assert that the node lists of two Xpaths in the same XML string are NOT * equal * @param xpathOne @@ -435,6 +538,27 @@ } /** + * Assert that the node lists of two Xpaths in two XML strings are + * NOT equal + * @param xpathOne + * @param controlDocument + * @param xpathTwo + * @param testDocument + * @throws SAXException + * @throws IOException + */ + public static void assertXpathsNotEqual(String controlXpath, + InputSource controlDocument, + String testXpath, + InputSource testDocument) + throws SAXException, IOException, XpathException { + assertXpathsNotEqual(controlXpath, + XMLUnit.buildControlDocument(controlDocument), + testXpath, + XMLUnit.buildTestDocument(testDocument)); + } + + /** * Assert that the node lists of two Xpaths in two documents are NOT equal * @param xpathOne * @param xpathTwo @@ -475,6 +599,23 @@ * equal * @param xpathOne * @param xpathTwo + * @param document + * @throws SAXException + * @throws IOException + */ + public static void assertXpathValuesEqual(String controlXpath, + String testXpath, + InputSource document) + throws SAXException, IOException, XpathException { + assertXpathValuesEqual(controlXpath, testXpath, + XMLUnit.buildControlDocument(document)); + } + + /** + * Assert that the evaluation of two Xpaths in the same XML string are + * equal + * @param xpathOne + * @param xpathTwo * @param inXMLString * @throws SAXException * @throws IOException @@ -491,6 +632,26 @@ /** * 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 static void assertXpathValuesEqual(String controlXpath, + InputSource control, + String testXpath, + InputSource test) + throws SAXException, IOException, XpathException { + assertXpathValuesEqual(controlXpath, + XMLUnit.buildControlDocument(control), + testXpath, + XMLUnit.buildTestDocument(test)); + } + + /** + * Assert that the evaluation of two Xpaths in two XML strings are equal + * @param xpathOne * @param inControlXMLString * @param xpathTwo * @param inTestXMLString @@ -531,6 +692,23 @@ * NOT equal * @param xpathOne * @param xpathTwo + * @param control + * @throws SAXException + * @throws IOException + */ + public static void assertXpathValuesNotEqual(String controlXpath, + String testXpath, + InputSource control) + throws SAXException, IOException, XpathException { + assertXpathValuesNotEqual(controlXpath, testXpath, + XMLUnit.buildControlDocument(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 @@ -562,10 +740,30 @@ * 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 static void assertXpathValuesNotEqual(String controlXpath, + InputSource control, + String testXpath, + InputSource test) + throws SAXException, IOException, XpathException { + assertXpathValuesNotEqual(controlXpath, + XMLUnit.buildControlDocument(control), + testXpath, + XMLUnit.buildTestDocument(test)); + } + + /** + * Assert that the evaluation of two Xpaths in two XML strings are + * NOT equal + * @param xpathOne * @param inControlXMLString * @param xpathTwo * @param inTestXMLString - * @param ctx * @throws SAXException * @throws IOException */ @@ -608,6 +806,24 @@ } /** + * Assert the value of an Xpath expression in an XML document. + * @param expectedValue + * @param xpathExpression + * @param control + * @throws SAXException + * @throws IOException + * @see XpathEngine which provides the underlying evaluation mechanism + */ + public static void assertXpathEvaluatesTo(String expectedValue, + String xpathExpression, + InputSource control) + throws SAXException, IOException, + XpathException { + Document document = XMLUnit.buildControlDocument(control); + assertXpathEvaluatesTo(expectedValue, xpathExpression, document); + } + + /** * Assert the value of an Xpath expression in an XML String * @param expectedValue * @param xpathExpression @@ -644,6 +860,19 @@ /** * Assert that a specific XPath exists in some given XML * @param inXpathExpression + * @param control + * @see XpathEngine which provides the underlying evaluation mechanism + */ + public static void assertXpathExists(String xPathExpression, + InputSource control) + throws IOException, SAXException, XpathException { + Document inDocument = XMLUnit.buildControlDocument(control); + assertXpathExists(xPathExpression, inDocument); + } + + /** + * Assert that a specific XPath exists in some given XML + * @param inXpathExpression * @param inXMLString * @see XpathEngine which provides the underlying evaluation mechanism */ @@ -674,6 +903,19 @@ /** * Assert that a specific XPath does NOT exist in some given XML * @param inXpathExpression + * @param control + * @see XpathEngine which provides the underlying evaluation mechanism + */ + public static void assertXpathNotExists(String xPathExpression, + InputSource control) + throws IOException, SAXException, XpathException { + Document inDocument = XMLUnit.buildControlDocument(control); + assertXpathNotExists(xPathExpression, inDocument); + } + + /** + * Assert that a specific XPath does NOT exist in some given XML + * @param inXpathExpression * @param inXMLString * @see XpathEngine which provides the underlying evaluation mechanism */ @@ -702,6 +944,19 @@ } /** + * Assert that an InputSource containing XML contains valid XML: + * the document must contain a DOCTYPE declaration to be validated + * @param xml + * @throws SAXException + * @throws ConfigurationException if validation could not be turned on + * @see Validator + */ + public static void assertXMLValid(InputSource xml) + throws SAXException, ConfigurationException { + assertXMLValid(new Validator(xml)); + } + + /** * Assert that a String containing XML contains valid XML: the String must * contain a DOCTYPE declaration to be validated * @param xmlString @@ -711,10 +966,25 @@ */ public static void assertXMLValid(String xmlString) throws SAXException, ConfigurationException { - assertXMLValid(new Validator(new StringReader(xmlString))); + assertXMLValid(new Validator(xmlString)); } /** + * Assert that an InputSource containing 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 static void assertXMLValid(InputSource xml, String systemId) + throws SAXException, ConfigurationException { + assertXMLValid(new Validator(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 @@ -726,10 +996,28 @@ */ public static void assertXMLValid(String xmlString, String systemId) throws SAXException, ConfigurationException { - assertXMLValid(new Validator(new StringReader(xmlString), systemId)); + assertXMLValid(new Validator(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 static void assertXMLValid(InputSource xml, String systemId, + String doctype) + throws SAXException, ConfigurationException { + assertXMLValid(new Validator(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. @@ -756,6 +1044,25 @@ /** * 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 static void assertNodeTestPasses(InputSource xml, NodeTester tester, + short nodeType) + throws SAXException, IOException { + NodeTest test = new NodeTest(xml); + assertNodeTestPasses(test, tester, new short[] {nodeType}, true); + } + + /** + * 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 Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLTestCase.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLTestCase.java 2007-03-28 04:06:22 UTC (rev 158) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLTestCase.java 2007-03-28 04:07:35 UTC (rev 159) @@ -45,6 +45,7 @@ import junit.framework.TestCase; import org.w3c.dom.Document; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -105,6 +106,19 @@ } /** + * 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 @@ -233,6 +247,18 @@ * @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); @@ -277,7 +303,21 @@ * @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); } @@ -302,22 +342,21 @@ * @throws SAXException * @throws IOException */ - public void assertXMLNotEqual(String control, String test) + public void assertXMLNotEqual(InputSource control, InputSource 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, String control, String test) + public void assertXMLNotEqual(String control, String test) throws SAXException, IOException { - XMLAssert.assertXMLNotEqual(err, control, test); + XMLAssert.assertXMLNotEqual(control, test); } /** @@ -331,24 +370,41 @@ /** * 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, Document control, Document test) { + 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(Reader control, Reader test) + public void assertXMLNotEqual(String err, String control, String test) throws SAXException, IOException { - XMLAssert.assertXMLNotEqual(control, test); + XMLAssert.assertXMLNotEqual(err, control, test); } /** @@ -356,6 +412,16 @@ * @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 */ @@ -372,6 +438,19 @@ * @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); @@ -393,6 +472,22 @@ } /** + * 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 @@ -439,6 +534,21 @@ } /** + * 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 @@ -455,6 +565,22 @@ } /** + * 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 @@ -501,6 +627,21 @@ } /** + * 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 @@ -519,6 +660,24 @@ /** * 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 @@ -555,6 +714,23 @@ * 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 @@ -584,6 +760,25 @@ * 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 @@ -620,6 +815,23 @@ * 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 @@ -653,8 +865,19 @@ /** * 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 - * @param ctx * @see XpathEngine which provides the underlying evaluation mechanism */ public void assertXpathExists(String xPathExpression, @@ -678,6 +901,18 @@ /** * 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 */ @@ -724,6 +959,19 @@ } /** + * 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 @@ -737,6 +985,21 @@ } /** + * 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 @@ -752,6 +1015,24 @@ } /** + * 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. @@ -778,6 +1059,24 @@ /** * 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 Modified: trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java =================================================================== --- trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java 2007-03-28 04:06:22 UTC (rev 158) +++ trunk/xmlunit/src/java/org/custommonkey/xmlunit/XMLUnit.java 2007-03-28 04:07:35 UTC (rev 159) @@ -507,6 +507,19 @@ return "1.1alpha"; } + /** + * 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 static Diff compareXML(InputSource control, InputSource test) + throws SAXException, IOException { + return new Diff(control, test); + } + /** * Compare XML documents provided by two Reader classes * @param control Control document Modified: trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Validator.java =================================================================== --- trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Validator.java 2007-03-28 04:06:22 UTC (rev 158) +++ trunk/xmlunit/tests/java/org/custommonkey/xmlunit/test_Validator.java 2007-03-28 04:07:35 UTC (rev 159) @@ -39,10 +39,13 @@ import junit.framework.AssertionFailedError; import junit.framework.TestSuite; import org.w3c.dom.Document; +import org.xml.sax.InputSource; import java.io.File; +import java.io.FileInputStream; import java.io.FileReader; import java.io.FileWriter; +import java.io.StringBufferInputStream; import java.io.StringReader; /** @@ -56,15 +59,18 @@ public void testXSchema() throws Exception{ File xsdFile = new File(test_Constants.BASEDIR + "/tests/etc/Book.xsd"); - assertTrue("xsdFile " + xsdFile.getAbsolutePath() + " exists", xsdFile.exists()); + assertTrue("xsdFile " + xsdFile.getAbsolutePath() + " exists", + xsdFile.exists()); - File xmlFile = new File(test_Constants.BASEDIR + "/tests/etc/BookXsdGenerated.xml"); - assertTrue("xmlFile " + xmlFile.getAbsolutePath() + " exists", xmlFile.exists()); - validator = new Validator(new FileReader(xmlFile)); + File xmlFile = new File(test_Constants.BASEDIR + + "/tests/etc/BookXsdGenerated.xml"); + assertTrue("xmlFile " + xmlFile.getAbsolutePath() + " exists", + xmlFile.exists()); + validator = + new Validator(new InputSource(new FileInputStream(xmlFile))); validator.useXMLSchema(true); - - validator.assertIsValid(); + assertTrue("Schema " + validator.toString(), validator.isValid()); } public void testIsValidGood() throws Exception { @@ -72,8 +78,7 @@ + test_Constants.CHUCK_JONES_RIP_DTD_DECL + test_Constants.CHUCK_JONES_RIP_XML; validator = new Validator(new StringReader(toonXML)); - assertEquals("toonXML " + validator.toString(), - true, validator.isValid()); + assertTrue("toonXML " + validator.toString(), validator.isValid()); // test XMLTestCase passXMLTestCaseTest(toonXML); passXMLTestCaseTest(validator); @@ -90,7 +95,7 @@ public void testIsValidExternalSystemId() throws Exception { writeTempDTDFile(); - assertEquals(tempDTDFile.getAbsolutePath(), true, tempDTDFile.exists()); + assertTrue(tempDTDFile.getAbsolutePath(), tempDTDFile.exists()); String externalDTD = test_Constants.XML_DECLARATION + test_Constants.DOCUMENT_WITH_GOOD_EXTERNAL_DTD; @@ -98,8 +103,7 @@ validator = new Validator(new StringReader(externalDTD), tempDTDUrl); - assertEquals("externalDTD " + validator.toString(), - true, validator.isValid()); + assertTrue("externalDTD " + validator.toString(), validator.isValid()); // test XMLTestCase passXMLTestCaseTest(externalDTD, tempDTDFile.toURL().toExternalForm()); passXMLTestCaseTest(validator); @@ -109,8 +113,7 @@ validator = new Validator(new StringReader(noDTD), tempDTDFile.toURL().toExternalForm()); - assertEquals("noDTD " + validator.toString(), - false, validator.isValid()); + assertFalse("noDTD " + validator.toString(), validator.isValid()); // test XMLTestCase failXMLTestCaseTest(noDTD, tempDTDFile.toURL().toExternalForm()); failXMLTestCaseTest(validator); @@ -118,7 +121,7 @@ public void testIsValidNoDTD() throws Exception { writeTempDTDFile(); - assertEquals(tempDTDFile.getAbsolutePath(), true, tempDTDFile.exists()); + assertTrue(tempDTDFile.getAbsolutePath(), tempDTDFile.exists()); String noDTD = test_Constants.CHUCK_JONES_RIP_XML; String systemid = tempDTDFile.toURL().toExternalForm(); @@ -126,39 +129,33 @@ String notDoctype = "anima"; validator = new Validator(new StringReader(noDTD), systemid, doctype); - assertEquals(validator.toString(), true, validator.isValid()); + assertTrue(validator.toString(), validator.isValid()); // test XMLTestCase passXMLTestCaseTest(noDTD, systemid, doctype); passXMLTestCaseTest(validator); + // and Document constructor + Document document = getDocument(noDTD); + validator = new Validator(document, systemid, doctype); + assertTrue("Document " + validator.toString(), validator.isValid()); validator = new Validator(new StringReader(noDTD), systemid, notDoctype); - assertEquals(validator.toString(), false, validator.isValid()); + assertFalse(validator.toString(), validator.isValid()); // test XMLTestCase failXMLTestCaseTest(noDTD, systemid, notDoctype); failXMLTestCaseTest(validator); - - Document document = getDocument(noDTD); - validator = new Validator(document, systemid, doctype); - assertEquals("Document " + validator.toString(), - true, validator.isValid()); - // test XMLTestCase - passXMLTestCaseTest(validator); - + // and Document constructor validator = new Validator(document, systemid, notDoctype); - assertEquals("Document " + validator.toString(), - false, validator.isValid()); - // test XMLTestCase - failXMLTestCaseTest(validator); + assertFalse("Document " + validator.toString(), validator.isValid()); } public void testIsValidBad() throws Exception { String noDTD = test_Constants.XML_DECLARATION + test_Constants.CHUCK_JONES_RIP_XML; validator = new Validator(new StringReader(noDTD)); - assertEquals("noDTD " + validator.toString(), - false, validator.isValid()); + assertFalse("noDTD " + validator.toString(), validator.isValid()); // test XMLTestCase + failXMLTestCaseTest(noDTD); failXMLTestCaseTest(validator); String dtdTwice = test_Constants.XML_DECLARATION @@ -166,18 +163,18 @@ + test_Constants.CHUCK_JONES_RIP_DTD_DECL + test_Constants.CHUCK_JONES_RIP_XML; validator = new Validator(new StringReader(dtdTwice)); - assertEquals("dtdTwice " + validator.toString(), - false, validator.isValid()); + assertFalse("dtdTwice " + validator.toString(), validator.isValid()); // test XMLTestCase + failXMLTestCaseTest(dtdTwice); failXMLTestCaseTest(validator); String invalidXML = test_Constants.XML_DECLARATION + test_Constants.CHUCK_JONES_RIP_DTD_DECL + test_Constants.CHUCK_JONES_SPINNING_IN_HIS_GRAVE_XML; validator = new Validator(new StringReader(invalidXML)); - assertEquals("invalidXML " + validator.toString(), - false, validator.isValid()); + assertFalse("invalidXML " + validator.toString(), validator.isValid()); // test XMLTestCase + failXMLTestCaseTest(invalidXML); failXMLTestCaseTest(validator); } @@ -209,13 +206,22 @@ // ---- XMLTestCase methods ---- private void passXMLTestCaseTest(String xml) throws Exception { assertXMLValid(xml); + assertXMLValid(new InputSource(new StringReader(xml))); + assertXMLValid(new InputSource(new StringBufferInputStream(xml))); } private void passXMLTestCaseTest(String xml, String systemId) throws Exception { assertXMLValid(xml, systemId); + assertXMLValid(new InputSource(new StringReader(xml)), systemId); + assertXMLValid(new InputSource(new StringBufferInputStream(xml)), + systemId); } private void passXMLTestCaseTest(String xml, String systemId, String doctype) throws Exception { assertXMLValid(xml, systemId, doctype); + assertXMLValid(new InputSource(new StringReader(xml)), systemId, + doctype); + assertXMLValid(new InputSource(new StringBufferInputStream(xml)), + systemId, doctype); } private void passXMLTestCaseTest(Validator validator) throws Exception { assertXMLValid(validator); @@ -227,7 +233,40 @@ } catch (AssertionFailedError e) { // expecting this } + try { + assertXMLValid(new InputSource(new StringReader(xml)), systemId); + fail("Expected assertion to fail!"); + } catch (AssertionFailedError e) { + // expecting this + } + try { + assertXMLValid(new InputSource(new StringBufferInputStream(xml)), + systemId); + fail("Expected assertion to fail!"); + } catch (AssertionFailedError e) { + // expecting this + } } + private void failXMLTestCaseTest(String xml) throws Exception { + try { + assertXMLValid(xml); + fail("Expected assertion to fail!"); + } catch (AssertionFailedError e) { + // expecting this + } + try { + assertXMLValid(new InputSource(new StringReader(xml))); + fail("Expected assertion to fail!"); + } catch (AssertionFailedError e) { + // expecting this + } + try { + assertXMLValid(new InputSource(new StringBufferInputStream(xml))); + fail("Expected assertion to fail!"); + } catch (AssertionFailedError e) { + // expecting this + } + } private void failXMLTestCaseTest(String xml, String systemId, String doctype) throws Exception { try { @@ -236,6 +275,20 @@ } catch (AssertionFailedError e) { // expecting this } + try { + assertXMLValid(new InputSource(new StringReader(xml)), systemId, + doctype); + fail("Expected assertion to fail!"); + } catch (AssertionFailedError e) { + // expecting this + } + try { + assertXMLValid(new InputSource(new StringBufferInputStream(xml)), + systemId, doctype); + fail("Expected assertion to fail!"); + } catch (AssertionFailedError e) { + // expecting this + } } private void failXMLTestCaseTest(Validator validator) throws Exception { try { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |