Search in sources :

Example 56 with XdmNodeWriteTrx

use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.

the class DocumentCreater method createRevisioned.

/**
 * Create revisioned document.
 *
 * @throws SirixException if shredding fails
 * @throws XMLStreamException if StAX reader couldn't be created
 * @throws IOException if reading XML string fails
 */
public static void createRevisioned(final Database database) throws SirixException, IOException, XMLStreamException {
    try (final XdmNodeWriteTrx firstWtx = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build()).beginNodeWriteTrx()) {
        final XMLShredder shredder = new XMLShredder.Builder(firstWtx, XMLShredder.createStringReader(REVXML), Insert.ASFIRSTCHILD).commitAfterwards().build();
        shredder.call();
    }
    try (final XdmNodeWriteTrx secondWtx = database.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build()).beginNodeWriteTrx()) {
        secondWtx.moveToFirstChild();
        secondWtx.moveToFirstChild();
        secondWtx.moveToFirstChild();
        secondWtx.setValue("A Contrived Test Document");
        secondWtx.moveToParent();
        secondWtx.moveToRightSibling();
        secondWtx.moveToRightSibling();
        secondWtx.moveToFirstChild();
        secondWtx.moveToRightSibling();
        final long key = secondWtx.getNodeKey();
        secondWtx.insertAttribute(new QNm("role"), "bold");
        secondWtx.moveTo(key);
        secondWtx.moveToRightSibling();
        secondWtx.setValue("changed in it.");
        secondWtx.moveToParent();
        secondWtx.insertElementAsRightSibling(new QNm("para"));
        secondWtx.insertTextAsFirstChild("This is a new para 2b.");
        secondWtx.moveToParent();
        secondWtx.moveToRightSibling();
        secondWtx.moveToRightSibling();
        secondWtx.moveToFirstChild();
        secondWtx.setValue("This is a different para 4.");
        secondWtx.moveToParent();
        secondWtx.insertElementAsRightSibling(new QNm("para"));
        secondWtx.insertTextAsFirstChild("This is a new para 4b.");
        secondWtx.moveToParent();
        secondWtx.moveToRightSibling();
        secondWtx.moveToRightSibling();
        secondWtx.remove();
        secondWtx.remove();
        secondWtx.commit();
        secondWtx.moveToDocumentRoot();
        secondWtx.moveToFirstChild();
        secondWtx.moveToFirstChild();
        secondWtx.remove();
        secondWtx.commit();
    }
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) QNm(org.brackit.xquery.atomic.QNm) ResourceManagerConfiguration(org.sirix.access.conf.ResourceManagerConfiguration) XMLShredder(org.sirix.service.xml.shredder.XMLShredder)

Example 57 with XdmNodeWriteTrx

use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.

the class XMLShredderTest method testShredIntoExisting.

@Test
public void testShredIntoExisting() throws Exception {
    final XdmNodeWriteTrx wtx = holder.getWriter();
    final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(XML), Insert.ASFIRSTCHILD).includeComments(true).commitAfterwards().build();
    shredder.call();
    assertEquals(2, wtx.getRevisionNumber());
    wtx.moveToDocumentRoot();
    wtx.moveToFirstChild();
    wtx.remove();
    final XMLShredder shredder2 = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(XML), Insert.ASFIRSTCHILD).includeComments(true).commitAfterwards().build();
    shredder2.call();
    assertEquals(3, wtx.getRevisionNumber());
    wtx.close();
    // Setup expected session.
    final Database database2 = TestHelper.getDatabase(PATHS.PATH2.getFile());
    final ResourceManager expectedSession = database2.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    final XdmNodeWriteTrx expectedTrx = expectedSession.beginNodeWriteTrx();
    DocumentCreater.create(expectedTrx);
    expectedTrx.commit();
    expectedTrx.moveToDocumentRoot();
    // Verify.
    final XdmNodeReadTrx rtx = holder.getResourceManager().beginNodeReadTrx();
    final Iterator<Long> descendants = new DescendantAxis(rtx);
    final Iterator<Long> expectedDescendants = new DescendantAxis(expectedTrx);
    while (expectedDescendants.hasNext()) {
        expectedDescendants.next();
        descendants.hasNext();
        descendants.next();
        assertEquals(expectedTrx.getName(), rtx.getName());
        assertEquals(expectedTrx.getValue(), rtx.getValue());
    }
    // expectedTrx.moveToDocumentRoot();
    // final Iterator<Long> expectedDescendants2 = new
    // DescendantAxis(expectedTrx);
    // while (expectedDescendants2.hasNext()) {
    // expectedDescendants2.next();
    // descendants.hasNext();
    // descendants.next();
    // assertEquals(expectedTrx.getQNameOfCurrentNode(),
    // rtx.getQNameOfCurrentNode());
    // }
    expectedTrx.close();
    expectedSession.close();
    rtx.close();
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) Database(org.sirix.api.Database) ResourceManager(org.sirix.api.ResourceManager) DescendantAxis(org.sirix.axis.DescendantAxis) Test(org.junit.Test)

Example 58 with XdmNodeWriteTrx

use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.

the class XMLShredderTest method testAttributesNSPrefix.

@Test
public void testAttributesNSPrefix() throws Exception {
    // Setup expected session.
    final XdmNodeWriteTrx expectedTrx2 = holder.getWriter();
    DocumentCreater.createWithoutNamespace(expectedTrx2);
    expectedTrx2.commit();
    // Setup parsed session.
    final Database database2 = TestHelper.getDatabase(PATHS.PATH2.getFile());
    final ResourceManager manager2 = database2.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
    final XdmNodeWriteTrx wtx = manager2.beginNodeWriteTrx();
    final XMLShredder shredder = new XMLShredder.Builder(wtx, XMLShredder.createFileReader(XML2), Insert.ASFIRSTCHILD).commitAfterwards().build();
    shredder.call();
    wtx.commit();
    wtx.close();
    // Verify.
    final XdmNodeReadTrx rtx = manager2.beginNodeReadTrx();
    rtx.moveToDocumentRoot();
    final Iterator<Long> expectedAttributes = new DescendantAxis(expectedTrx2);
    final Iterator<Long> attributes = new DescendantAxis(rtx);
    while (expectedAttributes.hasNext() && attributes.hasNext()) {
        expectedAttributes.next();
        attributes.next();
        if (expectedTrx2.getKind() == Kind.ELEMENT || rtx.getKind() == Kind.ELEMENT) {
            assertEquals(expectedTrx2.getNamespaceCount(), rtx.getNamespaceCount());
            assertEquals(expectedTrx2.getAttributeCount(), rtx.getAttributeCount());
            for (int i = 0; i < expectedTrx2.getAttributeCount(); i++) {
                assertEquals(expectedTrx2.getName(), rtx.getName());
            }
        }
    }
    attributes.hasNext();
    assertEquals(expectedAttributes.hasNext(), attributes.hasNext());
    expectedTrx2.close();
    rtx.close();
    manager2.close();
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) Database(org.sirix.api.Database) ResourceManager(org.sirix.api.ResourceManager) DescendantAxis(org.sirix.axis.DescendantAxis) Test(org.junit.Test)

Example 59 with XdmNodeWriteTrx

use of org.sirix.api.XdmNodeWriteTrx 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 60 with XdmNodeWriteTrx

use of org.sirix.api.XdmNodeWriteTrx in project sirix by sirixdb.

the class RevertTest method test.

@Test
public void test() throws SirixException {
    XdmNodeWriteTrx wtx = holder.getResourceManager().beginNodeWriteTrx();
    assertEquals(1L, wtx.getRevisionNumber());
    DocumentCreater.create(wtx);
    assertEquals(1L, wtx.getRevisionNumber());
    wtx.commit();
    assertEquals(2L, wtx.getRevisionNumber());
    wtx.close();
    wtx = holder.getResourceManager().beginNodeWriteTrx();
    assertEquals(2L, wtx.getRevisionNumber());
    wtx.moveToFirstChild();
    wtx.insertElementAsFirstChild(new QNm("bla"));
    wtx.commit();
    assertEquals(3L, wtx.getRevisionNumber());
    wtx.close();
    wtx = holder.getResourceManager().beginNodeWriteTrx();
    assertEquals(3L, wtx.getRevisionNumber());
    wtx.revertTo(1);
    wtx.commit();
    assertEquals(4L, wtx.getRevisionNumber());
    wtx.close();
    wtx = holder.getResourceManager().beginNodeWriteTrx();
    assertEquals(4L, wtx.getRevisionNumber());
    final long rev3MaxNodeKey = wtx.getMaxNodeKey();
    wtx.close();
    wtx = holder.getResourceManager().beginNodeWriteTrx();
    assertEquals(4L, wtx.getRevisionNumber());
    wtx.revertTo(1);
    wtx.moveToFirstChild();
    final long maxNodeKey = wtx.getMaxNodeKey();
    assertEquals(rev3MaxNodeKey, maxNodeKey);
    wtx.insertElementAsFirstChild(new QNm(""));
    assertEquals(maxNodeKey + 1, wtx.getNodeKey());
    assertEquals(maxNodeKey + 1, wtx.getMaxNodeKey());
    wtx.commit();
    wtx.close();
}
Also used : XdmNodeWriteTrx(org.sirix.api.XdmNodeWriteTrx) QNm(org.brackit.xquery.atomic.QNm) Test(org.junit.Test)

Aggregations

XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)101 Test (org.junit.Test)61 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)48 ResourceManager (org.sirix.api.ResourceManager)33 Database (org.sirix.api.Database)22 QNm (org.brackit.xquery.atomic.QNm)15 SirixException (org.sirix.exception.SirixException)15 Path (java.nio.file.Path)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 DocumentException (org.brackit.xquery.xdm.DocumentException)7 DatabaseConfiguration (org.sirix.access.conf.DatabaseConfiguration)7 DescendantAxis (org.sirix.axis.DescendantAxis)7 XMLSerializerBuilder (org.sirix.service.xml.serialize.XMLSerializer.XMLSerializerBuilder)7 IOException (java.io.IOException)5 NonStructuralWrapperAxis (org.sirix.axis.NonStructuralWrapperAxis)5 SirixIOException (org.sirix.exception.SirixIOException)5 SirixDeweyID (org.sirix.node.SirixDeweyID)5 DBNode (org.sirix.xquery.node.DBNode)5 XMLEventReader (javax.xml.stream.XMLEventReader)4 QueryException (org.brackit.xquery.QueryException)4