use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class ImportModuleTest method variablesBetweenModules.
@Test
public void variablesBetweenModules() throws EXistException, PermissionDeniedException, IOException, LockException, SAXException, XPathException {
final String module1 = "xquery version \"1.0\";\n" + "module namespace mod1 = \"http://example.com/mod1\";\n" + "declare variable $mod1:var1 := \"mod1 var1\";\n" + "declare function mod1:test() {\n" + " <function name=\"mod1:test\">\n" + " <variable name=\"mod1:var1\">{$mod1:var1}</variable>\n" + " </function>\n" + "};";
final String module2 = "xquery version \"1.0\";\n" + "module namespace mod2 = \"http://example.com/mod2\";\n" + "declare variable $mod2:var1 := \"mod2 var1\";\n" + "import module namespace mod1 = \"http://example.com/mod1\" at \"xmldb:exist:///db/mod1.xqm\";\n" + "declare function mod2:test() {\n" + " <function name=\"mod2:test\">\n" + " <variable name=\"mod2:var1\">{$mod2:var1}</variable>\n" + " { mod1:test() }\n" + " <variable name=\"mod1:var1\">{$mod1:var1}</variable>\n" + " </function>\n" + "};";
final String query = "xquery version \"1.0\";\n" + " import module namespace mod2 = 'http://example.com/mod2' at 'xmldb:exist:///db/mod2.xqm';\n" + "<result>\n" + " {mod2:test()}\n" + "</result>\n";
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final Source source = new StringSource(query);
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
// store modules
storeModules(broker, transaction, "/db", Tuple("mod1.xqm", module1), Tuple("mod2.xqm", module2));
// execute query
final Tuple2<XQueryContext, Sequence> contextAndResult = withCompiledQuery(broker, source, compiledXQuery -> {
final Sequence result = executeQuery(broker, compiledXQuery);
return Tuple(compiledXQuery.getContext(), result);
});
// check that the result was correct
assertNotNull(contextAndResult._2);
assertEquals(1, contextAndResult._2.getItemCount());
final Element doc = (Element) contextAndResult._2.itemAt(0);
assertNotNull(doc);
final javax.xml.transform.Source actual = Input.fromDocument(doc.getOwnerDocument()).build();
final javax.xml.transform.Source expected = Input.fromString("<result>" + "<function name=\"mod2:test\">" + "<variable name=\"mod2:var1\">mod2 var1</variable>" + "<function name=\"mod1:test\">" + "<variable name=\"mod1:var1\">mod1 var1</variable>" + "</function>" + "<variable name=\"mod1:var1\">mod1 var1</variable>" + "</function>" + "</result>").build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
transaction.commit();
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class LuceneMatchListenerTest method configureAndStore.
private void configureAndStore(final String config, final String data) throws EXistException, PermissionDeniedException, IOException, SAXException, CollectionConfigurationException, LockException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {
final Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
assertNotNull(root);
final CollectionConfigurationManager mgr = pool.getConfigurationManager();
mgr.addConfiguration(transaction, broker, root, config);
broker.storeDocument(transaction, XmldbURI.create("test_matches.xml"), new StringInputSource(data), MimeType.XML_TYPE, root);
transact.commit(transaction);
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class LuceneMatchListenerTest method inlineMatchNodes_whenIndenting.
@Test
public void inlineMatchNodes_whenIndenting() throws EXistException, PermissionDeniedException, XPathException, SAXException, CollectionConfigurationException, LockException, IOException {
configureAndStore(CONF5, XML2);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
final String query = "declare namespace tei=\"http://www.tei-c.org/ns/1.0\";" + "//tei:p[.//tei:w[ft:query(., <query><bool><term>дознајем</term></bool></query>)]] ! util:expand(.)";
final Sequence seq = xquery.execute(broker, query, null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
final String result = queryResult2String(broker, seq, true);
final String expected = "<p xmlns=\"http://www.tei-c.org/ns/1.0\">\n" + " <s type=\"combo\">\n" + " <w lemma=\"из\">из</w>\n" + " <w>новина</w>\n" + " <w lemma=\"и\">и</w>\n" + " <w lemma=\"од\">од</w>\n" + " <lb/>\n" + " <pb n=\"32\"/>\n" + " <w>других</w>\n" + " <w lemma=\"човек\">људи</w>\n" + " <w>" + MATCH_START + "дознајем" + MATCH_END + "</w>, <w xml:id=\"VSK.P13.t1.p4.w205\" lemma=\"ма\">ма</w>\n" + " <w>се</w>\n" + " <w lemma=\"не\">не</w>\n" + " <w>прорезује</w>\n" + " <w>право</w>\n" + " <w lemma=\"по\">по</w>\n" + " <w>имућству</w>, <w xml:id=\"VSK.P13.t1.p4.w219\" lemma=\"те\">те</w>\n" + " <w>се</w>\n" + " <w>на</w>\n" + " <w lemma=\"то\">то</w>\n" + " <w>видим</w>\n" + " <w>многи</w>\n" + " <w>љуте</w>.</s>\n" + "</p>";
XMLAssert.assertEquals(expected, result);
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class LuceneMatchListenerTest method matchInAncestor.
@Test
public void matchInAncestor() throws EXistException, PermissionDeniedException, XPathException, SAXException, IOException, XpathException, LockException, CollectionConfigurationException {
configureAndStore(CONF1, XML);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "//para[ft:query(., 'mixed')]/hi", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
String result = queryResult2String(broker, seq);
XMLAssert.assertXpathEvaluatesTo("1", "count(//exist:match)", result);
seq = xquery.execute(broker, "//para[ft:query(., 'nested')]/note", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq);
XMLAssert.assertXpathEvaluatesTo("1", "count(//hi/exist:match)", result);
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class SerializeAttrMatchesTest method expandAttr.
@Test
public void expandAttr() throws CollectionConfigurationException, LockException, IOException, SAXException, PermissionDeniedException, EXistException, XPathException {
configureAndStore(COLLECTION_CONFIG, XML, "test1.xml");
// query and expand
final String query = "for $hit in collection(\"" + TestConstants.TEST_COLLECTION_URI.toString() + "\")//w[ft:query(@lemma, <query><regex>књиг.*</regex></query>)]\n" + "return util:expand($hit, \"highlight-matches=both\")";
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
final Sequence seq = xquery.execute(broker, query, null);
assertEquals(1, seq.getItemCount());
final Item item = seq.itemAt(0);
assertTrue(item instanceof ElementImpl);
assertEquals("lemma", ((ElementImpl) item).getAttribute("exist:matches"));
}
}
Aggregations