use of org.exist.xquery.XQuery in project exist by eXist-db.
the class DLNStorageTest method nodeStorage.
@Test
public void nodeStorage() throws Exception {
BrokerPool pool = BrokerPool.getInstance();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
// test element ids
Sequence seq = xquery.execute(broker, "doc('/db/test/test_string.xml')/test/para", null);
assertEquals(3, seq.getItemCount());
NodeProxy comment = (NodeProxy) seq.itemAt(0);
assertEquals("1.1", comment.getNodeId().toString());
comment = (NodeProxy) seq.itemAt(1);
assertEquals("1.3", comment.getNodeId().toString());
comment = (NodeProxy) seq.itemAt(2);
assertEquals("1.5", comment.getNodeId().toString());
seq = xquery.execute(broker, "doc('/db/test/test_string.xml')/test//a", null);
assertEquals(1, seq.getItemCount());
NodeProxy a = (NodeProxy) seq.itemAt(0);
assertEquals("1.3.2", a.getNodeId().toString());
// test attribute id
seq = xquery.execute(broker, "doc('/db/test/test_string.xml')/test//a/@href", null);
assertEquals(1, seq.getItemCount());
NodeProxy href = (NodeProxy) seq.itemAt(0);
StorageAddress.toString(href);
assertEquals("1.3.2.1", href.getNodeId().toString());
// test Attr deserialization
Attr attr = (Attr) href.getNode();
StorageAddress.toString(((NodeHandle) attr));
// test Attr fields
assertEquals("href", attr.getNodeName());
assertEquals("href", attr.getName());
assertEquals("#", attr.getValue());
// test DOMFile.getNodeValue()
assertEquals("#", href.getStringValue());
// test text node
seq = xquery.execute(broker, "doc('/db/test/test_string.xml')/test//b/text()", null);
assertEquals(1, seq.getItemCount());
NodeProxy text = (NodeProxy) seq.itemAt(0);
assertEquals("1.5.2.1", text.getNodeId().toString());
// test DOMFile.getNodeValue()
assertEquals("paragraph", text.getStringValue());
// test Text deserialization
Text node = (Text) text.getNode();
assertEquals("paragraph", node.getNodeValue());
assertEquals("paragraph", node.getData());
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class BasicNodeSetTest method executeQuery.
private static Sequence executeQuery(final DBBroker broker, final String query, final int expected, final String expectedResult) throws XPathException, SAXException, PermissionDeniedException {
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final Sequence seq = xquery.execute(broker, query, null);
assertEquals(expected, seq.getItemCount());
if (expectedResult != null) {
final Item item = seq.itemAt(0);
final String value = serialize(broker, item);
assertEquals(expectedResult, value);
}
return seq;
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class RecoveryTest method verify.
private void verify(final BrokerPool pool) throws EXistException, PermissionDeniedException, SAXException, XPathException, IOException, BTreeException, LockException {
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final Serializer serializer = broker.borrowSerializer();
try {
try (final LockedDocument lockedDoc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/hamlet.xml"), LockMode.READ_LOCK)) {
assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/hamlet.xml' should not be null", lockedDoc);
final String data = serializer.serialize(lockedDoc.getDocument());
assertNotNull(data);
}
try (final LockedDocument lockedDoc = broker.getXMLResource(XmldbURI.ROOT_COLLECTION_URI.append("test/test2/test_string.xml"), LockMode.READ_LOCK)) {
assertNotNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/test_string.xml' should not be null", lockedDoc);
final String data = serializer.serialize(lockedDoc.getDocument());
assertNotNull(data);
}
final String lastSampleName = SAMPLES.getShakespeareXmlSampleNames()[SAMPLES.getShakespeareXmlSampleNames().length - 1];
try (final LockedDocument lockedDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(lastSampleName), LockMode.READ_LOCK)) {
assertNull("Document '" + XmldbURI.ROOT_COLLECTION + "/test/test2/'" + lastSampleName + " should not exist anymore", lockedDoc);
}
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
final Sequence seq = xquery.execute(broker, "//SPEECH[contains(LINE, 'king')]", null);
assertNotNull(seq);
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
final Item next = i.nextItem();
final String value = serializer.serialize((NodeValue) next);
}
} finally {
broker.returnSerializer(serializer);
}
try (final LockedDocument lockedBinDoc = broker.getXMLResource(TestConstants.TEST_COLLECTION_URI2.append(TestConstants.TEST_BINARY_URI), LockMode.READ_LOCK)) {
assertNotNull("Binary document is null", lockedBinDoc);
final BinaryDocument binDoc = (BinaryDocument) lockedBinDoc.getDocument();
try (final InputStream is = broker.getBinaryResource(binDoc)) {
final byte[] bdata = new byte[(int) binDoc.getContentLength()];
is.read(bdata);
final String data = new String(bdata);
assertNotNull(data);
}
}
final DOMFile domDb = ((NativeBroker) broker).getDOMFile();
assertNotNull(domDb);
try (final Writer writer = new StringWriter()) {
domDb.dump(writer);
}
final TransactionManager transact = pool.getTransactionManager();
try (final Txn transaction = transact.beginTransaction()) {
try (final Collection root = broker.openCollection(TestConstants.TEST_COLLECTION_URI, LockMode.WRITE_LOCK)) {
assertNotNull(root);
transaction.acquireCollectionLock(() -> broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(root.getURI()));
broker.removeCollection(transaction, root);
}
transact.commit(transaction);
}
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class DocTest method docAvailable_dynamicallyAvailableDocument_relativeUri.
@Test
public void docAvailable_dynamicallyAvailableDocument_relativeUri() throws XPathException, EXistException, PermissionDeniedException, URISyntaxException {
final BrokerPool pool = BrokerPool.getInstance();
final String doc = "<timestamp>" + System.currentTimeMillis() + "</timestamp>";
final String baseUri = "http://from-dynamic-context/";
final String docRelativeUri = "doc1";
final String query = "fn:doc-available('" + docRelativeUri + "')";
try (final DBBroker broker = pool.getBroker()) {
final XQueryContext context = new XQueryContext(pool);
context.setBaseURI(new AnyURIValue(new URI(baseUri)));
context.addDynamicallyAvailableDocument(baseUri + docRelativeUri, (broker2, transaction, uri) -> asInMemoryDocument(doc));
final XQuery xqueryService = pool.getXQueryService();
final CompiledXQuery compiled = xqueryService.compile(context, query);
final Sequence result = xqueryService.execute(broker, compiled, null);
assertFalse(result.isEmpty());
assertEquals(1, result.getItemCount());
assertTrue(result.itemAt(0).toJavaObject(Boolean.class).booleanValue());
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class DocTest method doc_dynamicallyAvailableDocument_absoluteUri.
@Test
public void doc_dynamicallyAvailableDocument_absoluteUri() throws XPathException, EXistException, PermissionDeniedException {
final BrokerPool pool = BrokerPool.getInstance();
final String doc = "<timestamp>" + System.currentTimeMillis() + "</timestamp>";
final String docUri = "http://from-dynamic-context/doc1";
final String query = "fn:doc('" + docUri + "')";
try (final DBBroker broker = pool.getBroker()) {
final XQueryContext context = new XQueryContext(pool);
context.addDynamicallyAvailableDocument(docUri, (broker2, transaction, uri) -> asInMemoryDocument(doc));
final XQuery xqueryService = pool.getXQueryService();
final CompiledXQuery compiled = xqueryService.compile(context, query);
final Sequence result = xqueryService.execute(broker, compiled, null);
assertFalse(result.isEmpty());
assertEquals(1, result.getItemCount());
assertTrue(result.itemAt(0) instanceof Node);
final Source expectedSource = Input.fromString(doc).build();
final Source actualSource = Input.fromNode((Node) result.itemAt(0)).build();
final Diff diff = DiffBuilder.compare(expectedSource).withTest(actualSource).checkForIdentical().checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
}
}
Aggregations