public static void assertXmlEquals(String expected, String actual) throws Exception { XMLUnit.setIgnoreWhitespace(true); Diff diff = new Diff(expected, actual); Assert.assertTrue(diff.toString(), diff.similar()); } }
Diff diff = new Diff(control, test, null, new ElementNameAndAttributeQualifier("name")); assertTrue( diff.toString(), diff.similar() );
private void compareXML(String expected, String actual) throws Exception { setNormalizeWhitespace(true); setIgnoreWhitespace(true); setIgnoreComments(true); setIgnoreAttributeOrder(false); Diff diff = XMLUnit.compareXML(expected, actual); if (!(diff.similar() && diff.identical())) { System.out.println(actual); DetailedDiff detDiff = new DetailedDiff(diff); @SuppressWarnings("rawtypes") List differences = detDiff.getAllDifferences(); StringBuilder diffLines = new StringBuilder(); for (Object object : differences) { Difference difference = (Difference) object; diffLines.append(difference.toString() + '\n'); } throw new IllegalArgumentException("Actual XML differs from expected: \n" + diffLines.toString()); } } }
/** * Receives to {@link String} representation of two XML files and verify that they are semantically equivalent * * @param expected the reference content * @param actual the actual content * @throws Exception if comparison fails */ public static void compareXML(String expected, String actual) throws Exception { XMLUnit.setNormalizeWhitespace(true); XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreComments(true); XMLUnit.setIgnoreAttributeOrder(false); Diff diff = XMLUnit.compareXML(expected, actual); if (!(diff.similar() && diff.identical())) { System.out.println(actual); DetailedDiff detDiff = new DetailedDiff(diff); @SuppressWarnings("rawtypes") List differences = detDiff.getAllDifferences(); StringBuilder diffLines = new StringBuilder(); for (Object object : differences) { Difference difference = (Difference) object; diffLines.append(difference.toString() + '\n'); } throw new IllegalArgumentException("Actual XML differs from expected: \n" + diffLines.toString()); } }
... String test = xmlToString(testXml); String expected = xmlToString(expectedXml); ... Diff xmlDiff = new Diff(expected, test); assertTrue(xmlDiff.similar()); ...
String control = "<prefix1:element xmlns:prefix1=\"namespace\" prefix1:attribute=\"x\">Some text</prefix1:element>"; String test = "<prefix2:element xmlns:prefix2=\"namespace\" prefix2:attribute=\"x\">Some text</prefix2:element>"; try { Diff diff = XMLUnit.compareXML( control, test ); assertTrue( "Similar", diff.similar() ); assertTrue( "Identical", diff.identical() ); } catch ( Exception e ) { fail( e.getMessage() ); }
public static boolean compareXMLs(String xmlSource, String xmlCompareWith) throws Exception { XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreComments(true); XMLUnit.setIgnoreAttributeOrder(true); XMLUnit.setNormalizeWhitespace(true); Diff myDiff = new Diff(xmlSource, xmlCompareWith); myDiff.similar() }
/** * 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 static void assertXMLEqual(String msg, Diff diff, boolean assertion) { if (assertion != diff.similar()) { fail(getFailMessage(msg, diff)); } }
public void testIdenticalAndSimilar() throws Exception { String controlXML = "<account><id>3A-00</id><name>acme</name></account>"; String testXML = "<account><name>acme</name><id>3A-00</id></account>"; Diff diff = new Diff(controlXML, testXML); assertTrue(diff.similar()); assertFalse(diff.identical()); }
/** * 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 static void assertXMLEqual(String msg, Diff diff, boolean assertion) { if (assertion != diff.similar()) { fail(getFailMessage(msg, diff)); } }
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); assertTrue("test XML matches control skeleton XML " + myDiff, myDiff.similar()); }
public static void assertSimilarXml(String expected, String result) throws Exception { XMLUnit.setIgnoreWhitespace(true); Diff diff = compareXML(result, expected); if (!diff.similar()) { System.out.println("Expected xml is:\n"); System.out.println(prettyPrint(expected)); System.out.println("########################################\n"); System.out.println("But got:\n"); System.out.println(prettyPrint(result)); } assertThat(diff.similar(), is(true)); }
private static void doAssertXmlEquals(String expectedXml, String actualXml) throws Exception { Diff diff = new Diff(expectedXml, actualXml); diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier()); if (!diff.similar()) { fail(String.format("\nExpected the following XML\n" + formatXml(expectedXml) + "\nbut actual XML was\n\n" + formatXml(actualXml))); } }
private static void doAssertXmlEquals(String expectedXml, String actualXml) throws Exception { Diff diff = new Diff(expectedXml, actualXml); diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier()); if (!diff.similar()) { fail(String.format("\nExpected the following XML\n" + formatXml(expectedXml) + "\nbut actual XML was\n\n" + formatXml(actualXml))); } }
@Override protected void match(SoapMessage soapMessage) throws IOException, AssertionError { Document actualDocument = soapMessage.getDocument(); Document expectedDocument = createDocumentFromSource(expected); Diff diff = new Diff(expectedDocument, actualDocument); assertTrue("Envelopes are different, " + diff.toString(), diff.similar()); }
@Override protected void match(SoapMessage soapMessage) throws IOException, AssertionError { Document actualDocument = soapMessage.getDocument(); Document expectedDocument = createDocumentFromSource(expected); Diff diff = new Diff(expectedDocument, actualDocument); assertTrue("Envelopes are different, " + diff.toString(), diff.similar()); }
@Test public void zapLocalFunction() throws IOException, SAXException { Map<String, Object> dynamicEvalParams = new HashMap<>(); dynamicEvalParams.put("language", "Bulgarian"); dynamicEvalParams.put("author_name", "John"); Object[] params = new Object[] { "/xquery/string-q3.xq", dynamicEvalParams }; String zapResult = transformer.zap(source("/xquery/string.xml"), params); assertTrue(XMLUnit.compareXML(result("/xquery/string-q3.xml"), zapResult).similar()); }
@Test public void zapClasspathResolver() throws IOException, SAXException { String zapResult = transformer.zap(source("/xquery/string.xml"), "/xquery/string-q5.xq"); assertTrue(XMLUnit.compareXML(result("/xquery/string.xml"), zapResult).similar()); }
@Test public void zapWithNamespaces() throws IOException, SAXException { String zapResult = transformer.zap(source("/xquery/ns.xml"), "/xquery/ns-q1.xq"); assertTrue(XMLUnit.compareXML(result("/xquery/ns-q1.xml"), zapResult).similar()); }
@Test public void zapMainFunction() throws IOException, SAXException { String zapResult = transformer.zap(source("/xquery/string.xml"), "/xquery/string-q4.xq"); assertTrue(XMLUnit.compareXML(result("/xquery/string.xml"), zapResult).similar()); }