Search in sources :

Example 21 with DetailedDiff

use of org.custommonkey.xmlunit.DetailedDiff in project opennms by OpenNMS.

the class AccessPointMonitorConfigTest method generateXML.

@Test
public void generateXML() throws Exception {
    // Marshal the test object to an XML string
    StringWriter objectXML = new StringWriter();
    JaxbUtils.marshal(apmc, objectXML);
    // Read the example XML from src/test/resources
    StringBuffer exampleXML = new StringBuffer();
    File apmConfig = new File(ClassLoader.getSystemResource("access-point-monitor-configuration.xml").getFile());
    assertTrue("access-point-monitor-configuration.xml is readable", apmConfig.canRead());
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(apmConfig), "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());
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 22 with DetailedDiff

use of org.custommonkey.xmlunit.DetailedDiff in project jPOS by jpos.

the class XMLPackagerTest method testPack.

@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());
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) List(java.util.List) String(java.lang.String) Test(org.junit.Test)

Example 23 with DetailedDiff

use of org.custommonkey.xmlunit.DetailedDiff in project opennms by OpenNMS.

the class TcaDataCollectionConfigTest method getDiff.

/**
 * Gets the diff.
 *
 * @param objectXML the object XML
 * @param exampleXML the example XML
 * @return the detailed diff
 * @throws SAXException the sAX exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
@SuppressWarnings("unchecked")
private DetailedDiff getDiff(StringWriter objectXML, StringBuffer 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;
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Difference(org.custommonkey.xmlunit.Difference)

Example 24 with DetailedDiff

use of org.custommonkey.xmlunit.DetailedDiff in project opennms by OpenNMS.

the class XmlTest method getDifferences.

public static List<Difference> getDifferences(final String xmlA, final String xmlB, final Predicate<String> ignoreNamespace, final Predicate<String> ignorePrefix, final Predicate<Difference> ignoreDifference) {
    DetailedDiff myDiff;
    try {
        myDiff = new DetailedDiff(XMLUnit.compareXML(xmlA, xmlB));
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
    final List<Difference> retDifferences = new ArrayList<>();
    @SuppressWarnings("unchecked") final List<Difference> allDifferences = myDiff.getAllDifferences();
    if (allDifferences.size() > 0) {
        DIFFERENCES: for (final Difference d : allDifferences) {
            final NodeDetail controlNodeDetail = d.getControlNodeDetail();
            final String control = controlNodeDetail.getValue();
            final NodeDetail testNodeDetail = d.getTestNodeDetail();
            final String test = testNodeDetail.getValue();
            if (d.getDescription().equals("namespace URI")) {
                if (control != null && !"null".equals(control)) {
                    if (ignoreNamespace.test(control.toLowerCase())) {
                        LOG.trace("Ignoring {}: {}", d.getDescription(), d);
                        continue DIFFERENCES;
                    }
                }
                if (test != null && !"null".equals(test)) {
                    if (ignoreNamespace.test(test.toLowerCase())) {
                        LOG.trace("Ignoring {}: {}", d.getDescription(), d);
                        continue DIFFERENCES;
                    }
                }
            } else if (d.getDescription().equals("namespace prefix")) {
                if (control != null && !"null".equals(control)) {
                    if (ignorePrefix.test(control.toLowerCase())) {
                        LOG.trace("Ignoring {}: {}", d.getDescription(), d);
                        continue DIFFERENCES;
                    }
                }
                if (test != null && !"null".equals(test)) {
                    if (ignorePrefix.test(test.toLowerCase())) {
                        LOG.trace("Ignoring {}: {}", d.getDescription(), d);
                        continue DIFFERENCES;
                    }
                }
            } else if (d.getDescription().equals("xsi:schemaLocation attribute")) {
                LOG.debug("Schema location '{}' does not match.  Ignoring.", controlNodeDetail.getValue() == null ? testNodeDetail.getValue() : controlNodeDetail.getValue());
                continue DIFFERENCES;
            }
            if (ignoreDifference.test(d)) {
                LOG.debug("ignoreDifference matched.  Ignoring difference: {}: {}", d.getDescription(), d);
                continue DIFFERENCES;
            } else {
                LOG.warn("Found difference: {}: {}", d.getDescription(), d);
                retDifferences.add(d);
            }
        }
    }
    return retDifferences;
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) NodeDetail(org.custommonkey.xmlunit.NodeDetail) ArrayList(java.util.ArrayList) Difference(org.custommonkey.xmlunit.Difference) XPathExpressionException(javax.xml.xpath.XPathExpressionException) IOException(java.io.IOException)

Example 25 with DetailedDiff

use of org.custommonkey.xmlunit.DetailedDiff in project opennms by OpenNMS.

the class JdbcDataCollectionConfigTest method getDiff.

@SuppressWarnings("unchecked")
private 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;
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Difference(org.custommonkey.xmlunit.Difference)

Aggregations

DetailedDiff (org.custommonkey.xmlunit.DetailedDiff)39 Diff (org.custommonkey.xmlunit.Diff)16 Difference (org.custommonkey.xmlunit.Difference)16 Test (org.junit.Test)16 Document (org.w3c.dom.Document)7 File (java.io.File)6 List (java.util.List)6 IOException (java.io.IOException)5 StringWriter (java.io.StringWriter)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Element (org.w3c.dom.Element)5 BufferedInputStream (java.io.BufferedInputStream)4 BufferedReader (java.io.BufferedReader)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 InputStreamReader (java.io.InputStreamReader)4 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)4 AssertionFailedError (junit.framework.AssertionFailedError)4