use of org.exist.xquery.XQuery in project exist by eXist-db.
the class CustomIndexTest method xupdateInsert.
@Test
public void xupdateInsert() throws EXistException, LockException, XPathException, PermissionDeniedException, SAXException, IOException, ParserConfigurationException {
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()) {
checkIndex(broker, docs, "cha", 1);
checkIndex(broker, docs, "le8", 1);
XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "//item[ngram:contains(., 'cha')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
assertNotNull(proc);
proc.setBroker(broker);
proc.setDocumentSet(docs);
String xupdate = XUPDATE_START + " <xu:append select=\"/test\">" + " <item id='4'><description>Armchair</description><price>340</price></item>" + " </xu:append>" + XUPDATE_END;
Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "arm", 1);
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate = XUPDATE_START + " <xu:insert-before select=\"//item[@id = '1']\">" + " <item id='0'><description>Wheelchair</description><price>1230</price></item>" + " </xu:insert-before>" + XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "hee", 1);
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate = XUPDATE_START + " <xu:insert-after select=\"//item[@id = '1']\">" + " <item id='1.1'><description>refrigerator</description><price>777</price></item>" + " </xu:insert-after>" + XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "ref", 1);
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate = XUPDATE_START + " <xu:insert-after select=\"//item[@id = '1']/description\">" + " <price>999</price>" + " </xu:insert-after>" + XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "999", 1);
checkIndex(broker, docs, "cha", 1);
checkIndex(broker, docs, "ir9", 1);
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate = XUPDATE_START + " <xu:insert-before select=\"//item[@id = '1']/description\">" + " <price>888</price>" + " </xu:insert-before>" + XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "999", 1);
checkIndex(broker, docs, "888", 1);
checkIndex(broker, docs, "88c", 1);
checkIndex(broker, docs, "att", 1);
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate = XUPDATE_START + " <xu:append select=\"//item[@id = '1']\">" + " <xu:attribute name=\"attr\">abc</xu:attribute>" + " </xu:append>" + XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "att", 0);
checkIndex(broker, docs, "abc", 1);
transact.commit(transaction);
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class CustomIndexTest method xupdateReplace.
@Test
public void xupdateReplace() throws LockException, XPathException, PermissionDeniedException, SAXException, EXistException, IOException, ParserConfigurationException {
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()) {
checkIndex(broker, docs, "cha", 1);
checkIndex(broker, docs, "le8", 1);
XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "//item[ngram:contains(., 'cha')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
XUpdateProcessor proc = new XUpdateProcessor(broker, docs);
assertNotNull(proc);
proc.setBroker(broker);
proc.setDocumentSet(docs);
String xupdate = XUPDATE_START + " <xu:replace select=\"//item[@id = '1']\">" + " <item id='4'><description>Wheelchair</description><price>809.50</price></item>" + " </xu:replace>" + XUPDATE_END;
Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "whe", 1);
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate = XUPDATE_START + " <xu:replace select=\"//item[@id = '4']/description\">" + " <description>Armchair</description>" + " </xu:replace>" + XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(broker, docs, "whe", 0);
checkIndex(broker, docs, "arm", 1);
transact.commit(transaction);
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class CustomIndexTest method query.
@Test
public void query() throws PermissionDeniedException, XPathException, EXistException {
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, "//item[ngram:contains(., 'cha')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
seq = xquery.execute(broker, "//section[ngram:contains(*, '123')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
seq = xquery.execute(broker, "//section[ngram:contains(para, '123')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
seq = xquery.execute(broker, "//*[ngram:contains(., '567')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class MatchListenerTest method nestedIndex.
@Test
public void nestedIndex() throws PermissionDeniedException, IOException, LockException, CollectionConfigurationException, SAXException, EXistException, XPathException {
configureAndStore(CONF1, XML);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute(broker, "//para[ngram:contains(term, 'term')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
String result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>a third paragraph with <term>" + MATCH_START + "term" + MATCH_END + "</term>.</para>", result);
seq = xquery.execute(broker, "//term[ngram:contains(., 'term')]/..", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>a third paragraph with <term>" + MATCH_START + "term" + MATCH_END + "</term>.</para>", result);
seq = xquery.execute(broker, "//term[ngram:contains(., 'term')]/ancestor::para", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
result = queryResult2String(broker, seq, 0);
XMLAssert.assertEquals("<para>a third paragraph with <term>" + MATCH_START + "term" + MATCH_END + "</term>.</para>", result);
}
}
use of org.exist.xquery.XQuery in project exist by eXist-db.
the class MatchListenerTest method smallStrings.
@Test
public void smallStrings() throws PermissionDeniedException, IOException, LockException, CollectionConfigurationException, SAXException, EXistException, XPathException, XpathException {
configureAndStore(CONF3, 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[] strings = new String[] { "龍", "龍護", "曰龍護", "名曰龍護" };
for (int i = 0; i < strings.length; i++) {
final Sequence seq = xquery.execute(broker, "declare namespace tei=\"http://www.tei-c.org/ns/1.0\";\n" + "//tei:p[ngram:contains(., '" + strings[i] + "')]", null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
final String result = queryResult2String(broker, seq, 0);
XMLAssert.assertXpathEvaluatesTo(i < 2 ? "2" : "1", "count(//exist:match)", result);
XMLAssert.assertXpathExists("//exist:match[text() = '" + strings[i] + "']", result);
}
}
}
Aggregations