Search in sources :

Example 6 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, VersionedReference 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 7 with DetailedDiff

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

the class XmlUtilTest method testToString.

@Test
public void testToString() throws XmlUtilException, SAXException, IOException {
    Document doc = XmlUtil.newDocument();
    Element expected = doc.createElement("root");
    doc.appendChild(expected);
    Element child = doc.createElement("child");
    expected.appendChild(child);
    String actual = XmlUtil.toString(expected);
    DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML("<root><child/></root>", actual));
    Assert.assertTrue(diff.toString(), diff.similar());
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with DetailedDiff

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

the class XmlUtilTest method testFromString.

@Test
public void testFromString() throws XmlUtilException {
    Document expected = XmlUtil.newDocument();
    Element element = expected.createElement("root");
    expected.appendChild(element);
    Element child = expected.createElement("child");
    element.appendChild(child);
    Document actual = XmlUtil.fromString("<root><child/></root>");
    DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(expected, actual));
    Assert.assertTrue(diff.toString(), diff.similar());
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with DetailedDiff

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

the class DifferencesTest method testMkStringWhenExistingDifference.

@Test
public void testMkStringWhenExistingDifference() throws XmlUtilException, SAXException, IOException {
    differences.addDifference(new Difference("diff1"));
    differences.addDifference(new Difference("diff2"));
    String actual = differences.mkString();
    Document doc = XmlUtil.newDocument();
    Element root = doc.createElement(Differences.DIFFERENCES_NODE);
    doc.appendChild(root);
    Element diff1 = doc.createElement(Differences.DIFFERENCE_NODE);
    diff1.appendChild(doc.createTextNode("diff1"));
    root.appendChild(diff1);
    Element diff2 = doc.createElement(Differences.DIFFERENCE_NODE);
    diff2.appendChild(doc.createTextNode("diff2"));
    root.appendChild(diff2);
    String expected = XmlUtil.toString(doc);
    DetailedDiff result = new DetailedDiff(XMLUnit.compareXML(expected, actual));
    Assert.assertTrue("Differences can be correctly transforms as String", result.similar());
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Element(org.w3c.dom.Element) Difference(org.cerberus.service.xmlunit.Difference) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 10 with DetailedDiff

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

the class FMSETest method test.

/**
 * Test a folder of XML files.
 *
 * @param FOLDER path string
 * @throws Exception if any exception occurs
 */
private void test(final String FOLDER) throws Exception {
    Database database = TestHelper.getDatabase(PATHS.PATH1.getFile());
    ResourceManager resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    final Path folder = Paths.get(FOLDER);
    final List<Path> list = Files.list(folder).filter(path -> path.getFileName().endsWith(".xml")).collect(toList());
    // Sort files list according to file names.
    list.sort((first, second) -> {
        final String firstName = first.getFileName().toString().substring(0, first.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 : list) {
        if (file.getFileName().endsWith(".xml")) {
            if (first) {
                first = false;
                try (final XdmNodeWriteTrx wtx = resource.beginNodeWriteTrx()) {
                    final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(file), Insert.ASFIRSTCHILD).commitAfterwards().build();
                    shredder.call();
                }
            } else {
                FMSEImport.main(new String[] { PATHS.PATH1.getFile().toAbsolutePath().toString(), file.toAbsolutePath().toString() });
            }
            resource.close();
            resource = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
            final OutputStream out = new ByteArrayOutputStream();
            final XMLSerializer serializer = new XMLSerializerBuilder(resource, out).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.err.println("***********************");
                System.err.println(difference);
                System.err.println("***********************");
            }
            assertTrue("pieces of XML are similar " + diff, diff.similar());
            assertTrue("but are they identical? " + diff, diff.identical());
        }
    }
    database.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) IOException(java.io.IOException) Test(org.junit.Test) FMSEImport(org.sirix.diff.service.FMSEImport) File(java.io.File) XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) Insert(org.sirix.service.xml.shredder.Insert) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) XMLShredder(org.sirix.service.xml.shredder.XMLShredder) 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) ResourceManagerConfiguration(org.sirix.access.conf.ResourceManagerConfiguration) 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) XMLShredder(org.sirix.service.xml.shredder.XMLShredder)

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