Search in sources :

Example 1 with Diff

use of org.xmlunit.diff.Diff in project irontest by zheng-wang.

the class XMLUtils method compareXML.

/**
 * @param control
 * @param test
 * @return differences found, in a format for print
 */
public static String compareXML(String control, String test) {
    StringBuilder differencesSB = new StringBuilder();
    Diff diff;
    try {
        diff = DiffBuilder.compare(control).withTest(test).normalizeWhitespace().withDifferenceEvaluator(DifferenceEvaluators.chain(DifferenceEvaluators.Default, new PlaceholderDifferenceEvaluator())).build();
    } catch (XMLUnitException e) {
        throw new RuntimeException(e.getCause().getMessage());
    }
    if (diff.hasDifferences()) {
        Iterator it = diff.getDifferences().iterator();
        while (it.hasNext()) {
            Difference difference = (Difference) it.next();
            if (difference.getResult() == ComparisonResult.DIFFERENT) {
                // ignore SIMILAR comparison results
                if (differencesSB.length() > 0) {
                    differencesSB.append("\n");
                }
                differencesSB.append(difference.getComparison().toString());
            }
        }
    }
    return differencesSB.toString();
}
Also used : XMLUnitException(org.xmlunit.XMLUnitException) Diff(org.xmlunit.diff.Diff) Iterator(java.util.Iterator) Difference(org.xmlunit.diff.Difference) PlaceholderDifferenceEvaluator(io.irontest.core.assertion.PlaceholderDifferenceEvaluator)

Example 2 with Diff

use of org.xmlunit.diff.Diff in project pom-manipulation-ext by release-engineering.

the class XMLIOTest method removePartFile.

@Test
public void removePartFile() throws ManipulationException, IOException, XPathExpressionException {
    String tomcatPath = "//include[starts-with(.,'org.apache.tomcat')]";
    Document doc = xmlIO.parseXML(xmlFile);
    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xPath.evaluate(tomcatPath, doc, XPathConstants.NODESET);
    logger.debug("Found node {} with size {} ", nodeList, nodeList.getLength());
    assertTrue(nodeList.getLength() == 1);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        logger.debug("Found node {} with type {} and value {}", node.getNodeName(), node.getNodeType(), node.getTextContent());
        node.getParentNode().removeChild(node);
    }
    File target = tf.newFile();
    xmlIO.writeXML(target, doc);
    Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.fromFile(target)).build();
    assertTrue(diff.toString(), diff.hasDifferences());
    String xpathForHamcrest = "/*/*/*/*/*[starts-with(.,'org.apache.tomcat') and local-name() = 'include']";
    Iterable<Node> i = new JAXPXPathEngine().selectNodes(xpathForHamcrest, Input.fromFile(target).build());
    int count = 0;
    for (Node ignored : i) {
        count++;
    }
    assertEquals(0, count);
}
Also used : XPath(javax.xml.xpath.XPath) HasXPathMatcher.hasXPath(org.xmlunit.matchers.HasXPathMatcher.hasXPath) Diff(org.xmlunit.diff.Diff) JAXPXPathEngine(org.xmlunit.xpath.JAXPXPathEngine) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Input.fromFile(org.xmlunit.builder.Input.fromFile) File(java.io.File) Test(org.junit.Test)

Example 3 with Diff

use of org.xmlunit.diff.Diff in project pom-manipulation-ext by release-engineering.

the class XMLIOTest method writeFile.

@Test
public void writeFile() throws ManipulationException, IOException {
    Document doc = xmlIO.parseXML(xmlFile);
    File target = tf.newFile();
    xmlIO.writeXML(target, doc);
    Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.fromFile(target)).build();
    assertFalse(diff.toString(), diff.hasDifferences());
}
Also used : Diff(org.xmlunit.diff.Diff) Document(org.w3c.dom.Document) Input.fromFile(org.xmlunit.builder.Input.fromFile) File(java.io.File) Test(org.junit.Test)

Example 4 with Diff

use of org.xmlunit.diff.Diff in project pom-manipulation-ext by release-engineering.

the class XMLIOTest method modifyMultiple.

@Test
public void modifyMultiple() throws ManipulationException, IOException, XPathExpressionException {
    String updatePath = "/assembly/formats/format";
    Document doc = xmlIO.parseXML(xmlFile);
    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xPath.evaluate(updatePath, doc, XPathConstants.NODESET);
    logger.debug("Found node {} with size {} ", nodeList, nodeList.getLength());
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        logger.debug("Found node {} with type {} and value {} ", node.getNodeName(), node.getNodeType(), node.getTextContent());
        node.setTextContent("NEW-FORMAT-" + i);
    }
    File target = tf.newFile();
    xmlIO.writeXML(target, doc);
    Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.fromFile(target)).build();
    assertTrue(diff.toString(), diff.hasDifferences());
    String xpathForHamcrest = "/*/*/*[starts-with(.,'NEW-FORMAT') and local-name() = '" + updatePath.substring(updatePath.lastIndexOf('/') + 1) + "']";
    Iterable<Node> i = new JAXPXPathEngine().selectNodes(xpathForHamcrest, Input.fromFile(target).build());
    int count = 0;
    for (Node anI : i) {
        count++;
        assertTrue(anI.getTextContent().startsWith("NEW-FORMAT"));
    }
    assertEquals(3, count);
}
Also used : XPath(javax.xml.xpath.XPath) HasXPathMatcher.hasXPath(org.xmlunit.matchers.HasXPathMatcher.hasXPath) Diff(org.xmlunit.diff.Diff) JAXPXPathEngine(org.xmlunit.xpath.JAXPXPathEngine) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Input.fromFile(org.xmlunit.builder.Input.fromFile) File(java.io.File) Test(org.junit.Test)

Example 5 with Diff

use of org.xmlunit.diff.Diff in project pom-manipulation-ext by release-engineering.

the class XMLIOTest method modifyPartialFile.

@Test
public void modifyPartialFile() throws ManipulationException, IOException, XPathExpressionException {
    String replacementGA = "com.rebuild:servlet-api";
    String tomcatPath = "//include[starts-with(.,'org.apache.tomcat')]";
    Document doc = xmlIO.parseXML(xmlFile);
    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList nodeList = (NodeList) xPath.evaluate(tomcatPath, doc, XPathConstants.NODESET);
    logger.debug("Found node {} with size {} ", nodeList, nodeList.getLength());
    assertTrue(nodeList.getLength() == 1);
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        logger.debug("Found node {} with type {} and value {}", node.getNodeName(), node.getNodeType(), node.getTextContent());
        node.setTextContent(replacementGA);
    }
    File target = tf.newFile();
    xmlIO.writeXML(target, doc);
    Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.fromFile(target)).build();
    assertTrue(diff.toString(), diff.hasDifferences());
    String xpathForHamcrest = "/*/*/*/*/*[starts-with(.,'com.rebuild') and local-name() = 'include']";
    Iterable<Node> i = new JAXPXPathEngine().selectNodes(xpathForHamcrest, Input.fromFile(target).build());
    int count = 0;
    for (Node anI : i) {
        count++;
        assertTrue(anI.getTextContent().startsWith("com.rebuild:servlet-api"));
    }
    assertEquals(1, count);
}
Also used : XPath(javax.xml.xpath.XPath) HasXPathMatcher.hasXPath(org.xmlunit.matchers.HasXPathMatcher.hasXPath) Diff(org.xmlunit.diff.Diff) JAXPXPathEngine(org.xmlunit.xpath.JAXPXPathEngine) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Input.fromFile(org.xmlunit.builder.Input.fromFile) File(java.io.File) Test(org.junit.Test)

Aggregations

Diff (org.xmlunit.diff.Diff)22 Test (org.junit.Test)15 File (java.io.File)13 Node (org.w3c.dom.Node)10 Document (org.w3c.dom.Document)7 Input.fromFile (org.xmlunit.builder.Input.fromFile)6 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)5 FileWriter (java.io.FileWriter)5 InputStreamReader (java.io.InputStreamReader)5 Files (java.nio.file.Files)5 Arrays (java.util.Arrays)5 HashSet (java.util.HashSet)5 Set (java.util.Set)5 QName (javax.xml.namespace.QName)5 Source (javax.xml.transform.Source)5 StreamSource (javax.xml.transform.stream.StreamSource)5 Assert.assertFalse (org.junit.Assert.assertFalse)5 Assert.assertTrue (org.junit.Assert.assertTrue)5 DMNMarshaller (org.kie.dmn.api.marshalling.DMNMarshaller)5