use of org.sirix.api.XdmNodeReadTrx in project sirix by sirixdb.
the class XMLShredderTest method testSTAXShredder.
@Test
public void testSTAXShredder() throws Exception {
// Setup parsed session.
XMLShredder.main(XML.toAbsolutePath().toString(), PATHS.PATH2.getFile().toAbsolutePath().toString());
final XdmNodeReadTrx expectedTrx = holder.getWriter();
// Verify.
final Database database2 = TestHelper.getDatabase(PATHS.PATH2.getFile());
database2.createResource(new ResourceConfiguration.Builder(TestHelper.RESOURCE, PATHS.PATH2.getConfig()).build());
final ResourceManager manager = database2.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
final XdmNodeReadTrx rtx = manager.beginNodeReadTrx();
rtx.moveToDocumentRoot();
final Iterator<Long> expectedDescendants = new DescendantAxis(expectedTrx);
final Iterator<Long> descendants = new DescendantAxis(rtx);
while (expectedDescendants.hasNext() && descendants.hasNext()) {
assertEquals(expectedTrx.getNodeKey(), rtx.getNodeKey());
assertEquals(expectedTrx.getParentKey(), rtx.getParentKey());
assertEquals(expectedTrx.getFirstChildKey(), rtx.getFirstChildKey());
assertEquals(expectedTrx.getLeftSiblingKey(), rtx.getLeftSiblingKey());
assertEquals(expectedTrx.getRightSiblingKey(), rtx.getRightSiblingKey());
assertEquals(expectedTrx.getChildCount(), rtx.getChildCount());
if (expectedTrx.getKind() == Kind.ELEMENT || rtx.getKind() == Kind.ELEMENT) {
assertEquals(expectedTrx.getAttributeCount(), rtx.getAttributeCount());
assertEquals(expectedTrx.getNamespaceCount(), rtx.getNamespaceCount());
}
assertEquals(expectedTrx.getKind(), rtx.getKind());
assertEquals(expectedTrx.getName(), rtx.getName());
assertEquals(expectedTrx.getValue(), expectedTrx.getValue());
}
rtx.close();
manager.close();
database2.close();
expectedTrx.close();
}
use of org.sirix.api.XdmNodeReadTrx 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();
}
use of org.sirix.api.XdmNodeReadTrx 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();
}
use of org.sirix.api.XdmNodeReadTrx in project sirix by sirixdb.
the class XPathStringChecker method testIAxisConventions.
public static void testIAxisConventions(final Axis axis, final String[] expectedValues) {
final XdmNodeReadTrx rtx = axis.getTrx();
// IAxis Convention 1.
final long startKey = rtx.getNodeKey();
final String[] strValues = new String[expectedValues.length];
int offset = 0;
while (axis.hasNext()) {
axis.next();
// IAxis results.
if (offset >= expectedValues.length) {
fail("More nodes found than expected.");
}
if (!("".equals(rtx.getValue()))) {
strValues[offset++] = rtx.getValue();
} else {
strValues[offset++] = rtx.getName().toString();
}
// // IAxis Convention 2.
// try {
// axis.next();
// fail("Should only allow to call next() once.");
// } catch (Exception e) {
// // Must throw exception.
// }
// IAxis Convention 3.
rtx.moveToDocumentRoot();
}
// IAxis Convention 5.
assertEquals(startKey, rtx.getNodeKey());
// IAxis results.
assertArrayEquals(expectedValues, strValues);
}
use of org.sirix.api.XdmNodeReadTrx in project sirix by sirixdb.
the class NodeReadTrxImplTest method testEmptyRtx.
@Test
public void testEmptyRtx() throws SirixException {
assertFalse(Files.exists(PATHS.PATH2.getFile()));
Databases.createDatabase(PATHS.PATH2.getConfig());
try (final Database db = Databases.openDatabase(PATHS.PATH2.getFile())) {
db.createResource(new ResourceConfiguration.Builder(TestHelper.RESOURCE, PATHS.PATH2.getConfig()).build());
try (final ResourceManager resMgr = db.getResourceManager(new ResourceManagerConfiguration.Builder(TestHelper.RESOURCE).build());
final XdmNodeReadTrx rtx = resMgr.beginNodeReadTrx()) {
rtx.getRevisionNumber();
}
}
}
Aggregations