Search in sources :

Example 26 with DetailedDiff

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

the class PersistenceSerializationTest method generateXML.

@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());
}
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 27 with DetailedDiff

use of org.custommonkey.xmlunit.DetailedDiff in project sirix by sirixdb.

the class XMLUpdateShredderTest method test.

// /** Not working anymore due to text merging on deletes. */
// @Ignore
// @Test
// public void testAllEighth() throws Exception {
// test(XMLALLEIGHTH);
// }
// 
// /** Not working anymore due to text merging on deletes. */
// @Ignore
// @Test
// public void testAllNineth() throws Exception {
// test(XMLALLNINETH);
// }
// @Test
// public void testLinguistics() throws Exception {
// test(XMLLINGUISTICS);
// }
private void test(final Path folder) throws Exception {
    final Database database = TestHelper.getDatabase(PATHS.PATH1.getFile());
    database.createResource(new ResourceConfiguration.Builder(TestHelper.RESOURCE, PATHS.PATH1.getConfig()).build());
    final ResourceManager manager = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    int i = 2;
    final List<Path> files = Files.list(folder).filter(file -> file.getFileName().endsWith(".xml")).collect(Collectors.toList());
    // Sort files array according to file names.
    files.sort((first, second) -> {
        final String firstName = first.getFileName().toString().substring(0, second.getFileName().toString().indexOf('.'));
        final String secondName = second.getFileName().toString().substring(0, second.getFileName().toString().indexOf('.'));
        if (Integer.parseInt(firstName) < Integer.parseInt(secondName)) {
            return -1;
        } else if (Integer.parseInt(firstName) > Integer.parseInt(secondName)) {
            return +1;
        } else {
            return 0;
        }
    });
    boolean first = true;
    // Shredder files.
    for (final Path file : files) {
        if (file.endsWith(".xml")) {
            final XdmNodeWriteTrx wtx = manager.beginNodeWriteTrx();
            if (first) {
                final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD).commitAfterwards().build();
                shredder.call();
                first = false;
            } else {
                @SuppressWarnings("deprecation") final XMLUpdateShredder shredder = new XMLUpdateShredder(wtx, XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD, file, ShredderCommit.COMMIT);
                shredder.call();
            }
            assertEquals(i, wtx.getRevisionNumber());
            i++;
            final OutputStream out = new ByteArrayOutputStream();
            final XMLSerializer serializer = new XMLSerializerBuilder(manager, out).prettyPrint().build();
            serializer.call();
            final StringBuilder sBuilder = TestHelper.readFile(file, false);
            final Diff diff = new Diff(sBuilder.toString(), out.toString());
            final DetailedDiff detDiff = new DetailedDiff(diff);
            @SuppressWarnings("unchecked") final List<Difference> differences = detDiff.getAllDifferences();
            for (final Difference difference : differences) {
                System.out.println("***********************");
                System.out.println(difference);
                System.out.println("***********************");
            }
            assertTrue("pieces of XML are similar " + diff, diff.similar());
            assertTrue("but are they identical? " + diff, diff.identical());
            wtx.close();
        }
    }
}
Also used : Path(java.nio.file.Path) XMLUnit(org.custommonkey.xmlunit.XMLUnit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Difference(org.custommonkey.xmlunit.Difference) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) XMLTestCase(org.custommonkey.xmlunit.XMLTestCase) PATHS(org.sirix.TestHelper.PATHS) ResourceManagerConfiguration(org.sirix.access.conf.ResourceManagerConfiguration) TestHelper(org.sirix.TestHelper) After(org.junit.After) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Path(java.nio.file.Path) Before(org.junit.Before) OutputStream(java.io.OutputStream) SirixException(org.sirix.exception.SirixException) Files(java.nio.file.Files) Test(org.junit.Test) Collectors(java.util.stream.Collectors) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) List(java.util.List) ResourceConfiguration(org.sirix.access.conf.ResourceConfiguration) ResourceManager(org.sirix.api.ResourceManager) Paths(java.nio.file.Paths) Database(org.sirix.api.Database) Diff(org.custommonkey.xmlunit.Diff) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XMLSerializer(org.sirix.service.xml.serialize.XMLSerializer) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Diff(org.custommonkey.xmlunit.Diff) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ResourceManager(org.sirix.api.ResourceManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Difference(org.custommonkey.xmlunit.Difference) XMLSerializerBuilder(org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder) Database(org.sirix.api.Database)

Example 28 with DetailedDiff

use of org.custommonkey.xmlunit.DetailedDiff in project archiva by apache.

the class MetadataTransferTest method assertMetadataEquals.

private void assertMetadataEquals(String expectedMetadataXml, Path actualFile) throws Exception {
    assertNotNull("Actual File should not be null.", actualFile);
    assertTrue("Actual file exists.", Files.exists(actualFile));
    StringWriter actualContents = new StringWriter();
    ArchivaRepositoryMetadata metadata = MavenMetadataReader.read(actualFile);
    RepositoryMetadataWriter.write(metadata, actualContents);
    DetailedDiff detailedDiff = new DetailedDiff(new Diff(expectedMetadataXml, actualContents.toString()));
    if (!detailedDiff.similar()) {
        assertEquals(expectedMetadataXml, actualContents);
    }
// assertEquals( "Check file contents.", expectedMetadataXml, actualContents );
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) StringWriter(java.io.StringWriter) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Diff(org.custommonkey.xmlunit.Diff) ArchivaRepositoryMetadata(org.apache.archiva.model.ArchivaRepositoryMetadata)

Example 29 with DetailedDiff

use of org.custommonkey.xmlunit.DetailedDiff in project archiva by apache.

the class MetadataToolsTest method assertMetadata.

private void assertMetadata(String expectedMetadata, ManagedRepositoryContent repository, ProjectReference reference) throws LayoutException, IOException, SAXException, ParserConfigurationException {
    Path metadataFile = Paths.get(repository.getRepoRoot(), tools.toPath(reference));
    String actualMetadata = org.apache.archiva.common.utils.FileUtils.readFileToString(metadataFile, Charset.defaultCharset());
    DetailedDiff detailedDiff = new DetailedDiff(new Diff(expectedMetadata, actualMetadata));
    if (!detailedDiff.similar()) {
        // If it isn't similar, dump the difference.
        assertEquals(expectedMetadata, actualMetadata);
    }
}
Also used : Path(java.nio.file.Path) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Diff(org.custommonkey.xmlunit.Diff)

Example 30 with DetailedDiff

use of org.custommonkey.xmlunit.DetailedDiff in project cerberus-source by cerberustesting.

the class XmlUnitService method getDifferencesFromXml.

@Override
public String getDifferencesFromXml(String left, String right) {
    try {
        // Gets the detailed diff between left and right argument
        Document leftDocument = inputTranslator.translate(left);
        Document rightDocument = inputTranslator.translate(right);
        DetailedDiff diffs = new DetailedDiff(XMLUnit.compareXML(leftDocument, rightDocument));
        // Creates the result structure which will contain difference list
        Differences resultDiff = new Differences();
        // Add each difference to our result structure
        for (Object diff : diffs.getAllDifferences()) {
            if (!(diff instanceof Difference)) {
                LOG.warn("Unable to handle no XMLUnit Difference " + diff);
                continue;
            }
            Difference wellTypedDiff = (Difference) diff;
            String xPathLocation = wellTypedDiff.getControlNodeDetail().getXpathLocation();
            // Then we retrieve XPath from the right structure.
            if (xPathLocation == null) {
                xPathLocation = wellTypedDiff.getTestNodeDetail().getXpathLocation();
            }
            // This case should never happen
            if (xPathLocation == null) {
                LOG.warn("Null left and right differences found");
                xPathLocation = NULL_XPATH;
            }
            resultDiff.addDifference(new org.cerberus.service.xmlunit.Difference(xPathLocation));
        }
        // Finally returns the String representation of our result structure
        return resultDiff.mkString();
    } catch (InputTranslatorException e) {
        LOG.warn("Unable to get differences from XML", e);
    }
    return null;
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) InputTranslatorException(org.cerberus.service.xmlunit.InputTranslatorException) Difference(org.custommonkey.xmlunit.Difference) Document(org.w3c.dom.Document) Differences(org.cerberus.service.xmlunit.Differences)

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