Search in sources :

Example 11 with Diff

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

the class XMLUtils method compareXML.

/**
 * @param control
 * @param test
 * @param namespaceAware
 * @return differences found, in a format for print
 */
public static String compareXML(String control, String test, boolean namespaceAware) {
    Object controlObject = control;
    Object testObject = test;
    // DiffBuilder.withDocumentBuilderFactory(factory);
    if (!namespaceAware) {
        try {
            controlObject = xmlStringToDOM(control);
            testObject = xmlStringToDOM(test);
        } catch (Exception e) {
            throw new RuntimeException(e.getCause().getMessage(), e);
        }
    }
    StringBuilder differencesSB = new StringBuilder();
    Diff diff;
    try {
        diff = DiffBuilder.compare(controlObject).withTest(testObject).normalizeWhitespace().withDifferenceEvaluator(DifferenceEvaluators.chain(DifferenceEvaluators.Default, new PlaceholderDifferenceEvaluator("#\\{", null))).build();
    } catch (XMLUnitException e) {
        throw new RuntimeException(e.getCause().getMessage(), e);
    }
    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(org.xmlunit.placeholder.PlaceholderDifferenceEvaluator) XPathExpressionException(javax.xml.xpath.XPathExpressionException) TransformerException(javax.xml.transform.TransformerException) XMLUnitException(org.xmlunit.XMLUnitException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 12 with Diff

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

the class XMLIOTest method modifyFile.

@Test
public void modifyFile() throws ManipulationException, IOException, XPathExpressionException {
    String updatePath = "/assembly/includeBaseDirectory";
    String newBaseDirectory = "/home/MYNEWBASEDIR";
    Document doc = xmlIO.parseXML(xmlFile);
    XPath xPath = XPathFactory.newInstance().newXPath();
    Node node = (Node) xPath.evaluate(updatePath, doc, XPathConstants.NODE);
    node.setTextContent(newBaseDirectory);
    File target = tf.newFile();
    xmlIO.writeXML(target, doc);
    Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.fromFile(target)).build();
    logger.debug("Difference {} ", diff.toString());
    String targetXML = FileUtils.readFileToString(target);
    // XMLUnit only seems to support XPath 1.0 so modify the expression to find the value.
    String xpathForHamcrest = "/*/*[local-name() = '" + updatePath.substring(updatePath.lastIndexOf('/') + 1) + "']";
    assertThat(targetXML, hasXPath(xpathForHamcrest));
    assertThat(targetXML, EvaluateXPathMatcher.hasXPath(xpathForHamcrest, equalTo(newBaseDirectory)));
    assertTrue(diff.toString(), diff.hasDifferences());
}
Also used : XPath(javax.xml.xpath.XPath) HasXPathMatcher.hasXPath(org.xmlunit.matchers.HasXPathMatcher.hasXPath) Diff(org.xmlunit.diff.Diff) 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 13 with Diff

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

the class XMLIOTest method readFile.

@Test
public void readFile() throws IOException, ManipulationException, TransformerException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    Document doc = xmlIO.parseXML(xmlFile);
    String strResult = xmlIO.convert(doc);
    Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.fromString(strResult)).build();
    assertFalse(diff.toString(), diff.hasDifferences());
}
Also used : Diff(org.xmlunit.diff.Diff) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 14 with Diff

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

the class XMLManipulatorTest method alterFile.

@Test
public void alterFile() throws Exception {
    String replacementGA = "com.rebuild:servlet-api";
    String tomcatPath = "//include[starts-with(.,'org.apache.tomcat')]";
    File target = tf.newFile();
    FileUtils.copyFile(xmlFile, target);
    Project project = new Project(null, target, null);
    xmlManipulator.internalApplyChanges(project, new XMLState.XMLOperation(target.getName(), tomcatPath, replacementGA));
    Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(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, fromFile(target).build());
    int count = 0;
    for (Node anI : i) {
        count++;
        assertTrue(anI.getTextContent().startsWith("com.rebuild:servlet-api"));
    }
    assertEquals(1, count);
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) XMLState(org.commonjava.maven.ext.core.state.XMLState) Diff(org.xmlunit.diff.Diff) JAXPXPathEngine(org.xmlunit.xpath.JAXPXPathEngine) Node(org.w3c.dom.Node) Input.fromFile(org.xmlunit.builder.Input.fromFile) File(java.io.File) Test(org.junit.Test) XMLIOTest(org.commonjava.maven.ext.io.XMLIOTest)

Example 15 with Diff

use of org.xmlunit.diff.Diff in project atlasmap by atlasmap.

the class AtlasMapSMTTest method test.

@Test
public void test() throws Exception {
    File f = new File("target/target.xml");
    for (int i = 0; !f.exists() && i < 5; i++) {
        Thread.sleep(1000);
        f = new File("target/target.xml");
    }
    assertTrue(f.exists());
    try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
        String line = reader.readLine();
        for (int i = 0; line == null && i < 5; i++) {
            Thread.sleep(1000);
            line = reader.readLine();
        }
        assertNotNull(line);
        Diff d = DiffBuilder.compare(Input.fromFile("src/test/resources/target.xml").build()).withTest(Input.fromString(line).build()).ignoreWhitespace().build();
        assertFalse(d.hasDifferences(), d.toString() + ": " + line);
    }
}
Also used : Diff(org.xmlunit.diff.Diff) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) Test(org.junit.jupiter.api.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