*/
@Test
public void xupdateInsert() {
DocumentSet docs = configureAndStore(COLLECTION_CONFIG2, XML2, "xupdate.xml");
DBBroker broker = null;
TransactionManager transact = null;
Txn transaction = null;
try {
broker = pool.get(pool.getSecurityManager().getSystemSubject());
transact = pool.getTransactionManager();
transaction = transact.beginTransaction();
Occurrences occur[] = checkIndex(docs, broker, new QName[] { new QName("description", "") }, "chair", 1);
assertEquals("chair", occur[0].getTerm());
checkIndex(docs, broker, new QName[] { new QName("item", "") }, null, 5);
XQuery xquery = broker.getXQueryService();
assertNotNull(xquery);
Sequence seq = xquery.execute("//item[ft:query(description, 'chair')]", null, AccessContext.TEST);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
// Append to root node
XUpdateProcessor proc = new XUpdateProcessor(broker, docs, AccessContext.TEST);
assertNotNull(proc);
proc.setBroker(broker);
proc.setDocumentSet(docs);
String xupdate =
XUPDATE_START +
" <xu:append select=\"/test\">" +
" <item id='4'><description>Armchair</description> <condition>bad</condition></item>" +
" </xu:append>" +
XUPDATE_END;
Modification[] modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
Occurrences o[] = checkIndex(docs, broker, new QName[] { new QName("condition", "") }, null, 2);
System.out.println("prices: " + o.length);
for (int i = 0; i < o.length; i++) {
System.out.println("occurance: " + o[i].getTerm() + ": " + o[i].getOccurrences());
}
checkIndex(docs, broker, new QName[] { new QName("description", "") }, null, 4);
checkIndex(docs, broker, new QName[] { new QName("item", "") }, null, 6);
o = checkIndex(docs, broker, new QName[] { new QName("condition", "") }, "bad", 1);
assertEquals("bad", o[0].getTerm());
o = checkIndex(docs, broker, new QName[] { new QName("description", "") }, "armchair", 1);
assertEquals("armchair", o[0].getTerm());
o = checkIndex(docs, broker, new QName[] { new QName("item", "") }, "bad", 1);
assertEquals("bad", o[0].getTerm());
o = checkIndex(docs, broker, new QName[] { new QName("item", "") }, "armchair", 1);
assertEquals("armchair", o[0].getTerm());
// Insert before top element
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate =
XUPDATE_START +
" <xu:insert-before select=\"//item[@id = '1']\">" +
" <item id='0'><description>Wheelchair</description> <condition>poor</condition></item>" +
" </xu:insert-before>" +
XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(docs, broker, new QName[] { new QName("condition", "") }, null, 3);
checkIndex(docs, broker, new QName[] { new QName("description", "") }, null, 5);
checkIndex(docs, broker, new QName[] { new QName("item", "") }, null, 8);
o = checkIndex(docs, broker, new QName[] { new QName("condition", "") }, "poor", 1);
assertEquals("poor", o[0].getTerm());
o = checkIndex(docs, broker, new QName[] { new QName("description", "") }, "wheelchair", 1);
assertEquals("wheelchair", o[0].getTerm());
o = checkIndex(docs, broker, new QName[] { new QName("item", "") }, "poor", 1);
assertEquals("poor", o[0].getTerm());
o = checkIndex(docs, broker, new QName[] { new QName("item", "") }, "wheelchair", 1);
assertEquals("wheelchair", o[0].getTerm());
// Insert after element
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate =
XUPDATE_START +
" <xu:insert-after select=\"//item[@id = '1']\">" +
" <item id='1.1'><description>refrigerator</description> <condition>perfect</condition></item>" +
" </xu:insert-after>" +
XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(docs, broker, new QName[] { new QName("condition", "") }, null, 4);
checkIndex(docs, broker, new QName[] { new QName("description", "") }, null, 6);
checkIndex(docs, broker, new QName[] { new QName("item", "") }, null, 10);
o = checkIndex(docs, broker, new QName[] { new QName("condition", "") }, "perfect", 1);
assertEquals("perfect", o[0].getTerm());
o = checkIndex(docs, broker, new QName[] { new QName("description", "") }, "refrigerator", 1);
assertEquals("refrigerator", o[0].getTerm());
o = checkIndex(docs, broker, new QName[] { new QName("item", "") }, "perfect", 1);
assertEquals("perfect", o[0].getTerm());
o = checkIndex(docs, broker, new QName[] { new QName("item", "") }, "refrigerator", 1);
assertEquals("refrigerator", o[0].getTerm());
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate =
XUPDATE_START +
" <xu:insert-after select=\"//item[@id = '1']/description\">" +
" <condition>average</condition>" +
" </xu:insert-after>" +
XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(docs, broker, new QName[] { new QName("condition", "") }, null, 5);
checkIndex(docs, broker, new QName[] { new QName("item", "") }, null, 11);
o = checkIndex(docs, broker, new QName[] { new QName("condition", "") }, "average", 1);
assertEquals("average", o[0].getTerm());
o = checkIndex(docs, broker, new QName[] { new QName("item", "") }, "average", 1);
assertEquals("average", o[0].getTerm());
// Insert before nested element
proc.setBroker(broker);
proc.setDocumentSet(docs);
xupdate =
XUPDATE_START +
" <xu:insert-before select=\"//item[@id = '1']/description\">" +
" <condition>awesome</condition>" +
" </xu:insert-before>" +
XUPDATE_END;
modifications = proc.parse(new InputSource(new StringReader(xupdate)));
assertNotNull(modifications);
modifications[0].process(transaction);
proc.reset();
checkIndex(docs, broker, new QName[] { new QName("condition", "") }, null, 6);
checkIndex(docs, broker, new QName[] { new QName("item", "") }, null, 12);
o = checkIndex(docs, broker, new QName[] { new QName("condition", "") }, "awesome", 1);
assertEquals("awesome", o[0].getTerm());
o = checkIndex(docs, broker, new QName[] { new QName("item", "") }, "awesome", 1);
assertEquals("awesome", o[0].getTerm());
// Overwrite attribute
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();
QName qnattr[] = { new QName("attr", "", "") };
qnattr[0].setNameType(ElementValue.ATTRIBUTE);
o = checkIndex(docs, broker, qnattr, null, 1);
assertEquals("abc", o[0].getTerm());
checkIndex(docs, broker, qnattr, "attribute", 0);
transact.commit(transaction);
} catch (Exception e) {
if (transact != null)
transact.abort(transaction);
e.printStackTrace();
fail(e.getMessage());
} finally {
if (pool != null) {
pool.release(broker);