import org.custommonkey.xmlunit.DetailedDiff; import org.custommonkey.xmlunit.XMLUnit; import org.junit.Assert; public class TestXml { public static void main(String[] args) throws Exception { String result = "<abc attr=\"value1\" title=\"something\"> </abc>"; // will be ok assertXMLEquals("<abc attr=\"value1\" title=\"something\"></abc>", result); } public static void assertXMLEquals(String expectedXML, String actualXML) throws Exception { XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreAttributeOrder(true); DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML)); List<?> allDifferences = diff.getAllDifferences(); Assert.assertEquals("Differences found: "+ diff.toString(), 0, allDifferences.size()); } }
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()); } }
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(); assertEquals(myDiff.toString(), 0, allDifferences.size()); }
DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(oldXml, newXml)); diff.overrideDifferenceListener(new IgnoreMyAttributeElements()); List<?> allDifferences = diff.getAllDifferences(); //do something with differences
private String buildDifferenceMessage(Diff diff) { DetailedDiff detailedDiff = new DetailedDiff(diff); @SuppressWarnings("unchecked") List<Difference> differences = detailedDiff.getAllDifferences(); StringBuilder message = new StringBuilder( "Some differences has been found between database data and expected data:"); message.append(EOL); for (Difference difference : differences) { message.append("************************"); message.append(difference); message.append("************************"); } String differenceMessage = message.toString(); return differenceMessage; }
private String buildDifferenceMessage(Diff diff) { DetailedDiff detailedDiff = new DetailedDiff(diff); @SuppressWarnings("unchecked") List<Difference> differences = detailedDiff.getAllDifferences(); StringBuilder message = new StringBuilder( "Some differences has been found between database data and expected data:"); message.append(EOL); for (Difference difference : differences) { message.append("************************"); message.append(difference); message.append("************************"); } String differenceMessage = message.toString(); return differenceMessage; }
String expectedXML = "some xml"; String actualXML = "some xml"; DetailedDiff diff1 = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML)); diff1.overrideElementQualifier(new RecursiveElementNameAndTextQualifier()); System.out.println("Differences found: " + diff1.getAllDifferences().toString());
String expectedXML = "some xml"; String actualXML = "some xml"; DetailedDiff diff1 = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML)); diff1.overrideElementQualifier(new RecursiveElementNameAndTextQualifier()); System.out.println("Differences found: " + diff1.getAllDifferences().toString());
/** * Compare the controlDOM and testDOM and save and return the differences in a list. * * @return list with differences */ @SuppressWarnings("unchecked") public List<Difference> compare() { Diff diff = new Diff(this.controlDOM, this.testDOM); DetailedDiff detDiff = new DetailedDiff(diff); return detDiff.getAllDifferences(); }
List differences = detDiff.getAllDifferences(); for (Object object : differences) { Difference difference = (Difference)object;
List differences = detDiff.getAllDifferences(); for (Object object : differences) { Difference difference = (Difference)object;
protected void assertThatXmlHaveNoDifferences(final String xmlPrettyFormatExpected, final String xmlPrettyFormatExported) throws SAXException, IOException { XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreAttributeOrder(true); final DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(xmlPrettyFormatExported, xmlPrettyFormatExpected)); final List<?> allDifferences = diff.getAllDifferences(); assertThat(allDifferences).as("should have no differences between:\n%s\n and:\n%s\n", xmlPrettyFormatExpected, xmlPrettyFormatExported).isEmpty(); }
public boolean validate(Document value) { if (value == null) return false; try { Document expectedValue = getValue(); logger.debug("Expected XML Value: {},\nActual XML Value: {}", xmlUtilities.getDocumentAsString(expectedValue).trim() , xmlUtilities.getDocumentAsString(value).trim()); DetailedDiff difference = new DetailedDiff(new Diff(expectedValue, value)); if (!difference.similar()) logger.warn("Differences exist between two documents: {}", difference.getAllDifferences()); else logger.debug("No differences exist for input"); return difference.similar(); } catch (Exception e) { throw new RuntimeException(e); } }
@SuppressWarnings("unchecked") private static DetailedDiff getDiff(StringWriter objectXML, StringBuilder exampleXML) throws SAXException, IOException { DetailedDiff myDiff = new DetailedDiff(XMLUnit.compareXML(exampleXML.toString(), objectXML.toString())); List<Difference> allDifferences = myDiff.getAllDifferences(); if (allDifferences.size() > 0) { for (Difference d : allDifferences) { System.err.println(d); } } return myDiff; }
@Test public void generateXML() throws Exception { // Marshal the test object to an XML string StringWriter objectXML = new StringWriter(); m.marshal(fsw, objectXML); // Read the example XML from src/test/resources final StringBuilder exampleXML = new StringBuilder(); File foreignSources = new File(ClassLoader.getSystemResource("foreign-sources.xml").getFile()); assertTrue("foreign-sources.xml is readable", foreignSources.canRead()); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(foreignSources), StandardCharsets.UTF_8)); String line; while (true) { line = reader.readLine(); if (line == null) { reader.close(); break; } exampleXML.append(line).append("\n"); } System.err.println("========================================================================"); System.err.println("Object XML:"); System.err.println("========================================================================"); System.err.print(objectXML.toString()); System.err.println("========================================================================"); System.err.println("Example XML:"); System.err.println("========================================================================"); System.err.print(exampleXML.toString()); DetailedDiff myDiff = getDiff(objectXML, exampleXML); assertEquals("number of XMLUnit differences between the example XML and the mock object XML is 0", 0, myDiff.getAllDifferences().size()); }
@Test public void testParseAndFormat() throws Exception { File inputFile = new File(getClass().getResource("/test-formatted-input.xml").getFile()); File outputFile = File.createTempFile("export-test-", ".xml"); OutputStream outputStream = new FileOutputStream(outputFile); XMLReader reader = saxParserFactory.newSAXParser().getXMLReader(); DataTransporter.readFormatted(reader, inputFile, outputStream); IOUtils.closeQuietly(outputStream); Reader expectedReader = new InputStreamReader(getClass().getResourceAsStream("/test-formatted-expected.xml")); Reader actualReader = new FileReader(outputFile); DetailedDiff xmlDiff = new DetailedDiff(new Diff(expectedReader, actualReader)); IOUtils.closeQuietly(expectedReader); IOUtils.closeQuietly(actualReader); outputFile.delete(); final StringBuilder diffLog = new StringBuilder(); for (Iterator iter = xmlDiff.getAllDifferences().iterator(); iter.hasNext(); ) { Difference difference = (Difference) iter.next(); diffLog.append("expected> ").append(difference.getControlNodeDetail().getValue()).append("\n"); diffLog.append("actual > ").append(difference.getTestNodeDetail().getValue()).append("\n"); } assertTrue("Document is not formatted as expected:\n" + diffLog.toString(), xmlDiff.identical()); }
public void testRoundTrip() throws Exception { StringWriter sw = new StringWriter(); InputStream original = getClass().getClassLoader().getResourceAsStream("testmodel_data.xml"); XMLUnit.setIgnoreWhitespace(true); Collection<FastPathObject> unmarshalled = (Collection<FastPathObject>) binding.unmarshal(original); setIds(unmarshalled); binding.marshal(unmarshalled, sw); // System.out.println(sw.toString()); String expected = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("testmodel_data.xml")); Diff diff = new Diff(expected, sw.toString()); DetailedDiff detail = new DetailedDiff(diff); detail.overrideElementQualifier(new ElementNameAndAttributeQualifier()); assertTrue(detail.getAllDifferences().toString() + ": Original: " + expected + ", Generated: " + sw.toString(), detail.similar()); }
private void compareFileContent(File expectedFile, File givenFile) throws Exception { final String givenFileAbsolutePath = givenFile.getAbsolutePath(); final String expectedFileExtension = getExtension(expectedFile.getName()); final String expectedFileAbsolutePath = expectedFile.getAbsolutePath(); assertThat(new ExtensionFileComparator().compare(expectedFile, givenFile)) .as(expectedFileAbsolutePath + " and " + givenFileAbsolutePath + " should have same extension").isEqualTo(ARE_EQUALS); switch (expectedFileExtension) { case PROPERTIES: assertThat(getProperties(expectedFile)) .as(expectedFileAbsolutePath + " and " + givenFileAbsolutePath + " should contain same properties") .isEqualTo(getProperties(givenFile)); break; case XML: final List allDifferences = new DetailedDiff( XMLUnit.compareXML(new FileReader(givenFile), new FileReader(expectedFile))) .getAllDifferences(); assertThat(allDifferences).as("should xml file be equals").isEmpty(); break; default: fail("unexpected file:" + expectedFile.getAbsolutePath()); break; } }
@Test public void testPack() throws IOException, ISOException, SAXException { isoMsg.setMTI("0800"); isoMsg.set(7, "7654321"); isoMsg.set(11, "12345678"); isoMsg.set(12, "20110224112759"); isoMsg.set(24, ""); byte[] data = isoMsg.pack(); // System.out.println(new String(data)); String expected = "<isomsg><!-- org.jpos.iso.packager.XMLPackager --><field id=\"0\" value=\"0800\"/>" + "<field id=\"7\" value=\"7654321\"/><field id=\"11\" value=\"12345678\"/>" + "<field id=\"12\" value=\"20110224112759\"/><field id=\"24\" value=\"\"/></isomsg>"; XMLUnit.setIgnoreWhitespace(true); // XMLAssert.assertXMLEqual(expected, new String(data)); DetailedDiff myDiff = new DetailedDiff(XMLUnit.compareXML(expected, new String(data))); List allDifferences = myDiff.getAllDifferences(); assertEquals(myDiff.toString(), 0, allDifferences.size()); }