Skip to content

Commit 7a76ccc

Browse files
gabriele-tomassettiadamretter
authored andcommitted
[test] Add new tests found in reference
1 parent 59c682b commit 7a76ccc

File tree

1 file changed

+155
-2
lines changed

1 file changed

+155
-2
lines changed

exist-core/src/test/java/org/exist/xquery/update/XQueryUpdate3Test.java

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public void transformWith() throws EXistException, RecognitionException, XPathEx
245245
public void copyModifyExprTest() throws EXistException, RecognitionException, XPathException, TokenStreamException, PermissionDeniedException
246246
{
247247
String query = "copy $je := $e\n" +
248-
" modify $je\n" +
248+
" modify delete node $je/salary\n" +
249249
" return $je";
250250

251251
BrokerPool pool = BrokerPool.getInstance();
@@ -264,11 +264,50 @@ public void copyModifyExprTest() throws EXistException, RecognitionException, XP
264264

265265
XQueryTreeParser treeParser = new XQueryTreeParser(context);
266266
PathExpr expr = new PathExpr(context);
267-
treeParser.expr(ast, expr);
267+
Expression ret = treeParser.expr(ast, expr);
268268
if (treeParser.foundErrors()) {
269269
fail(treeParser.getErrorMessage());
270270
return;
271271
}
272+
273+
assertTrue(ret instanceof CopyModifyExpression);
274+
assertEquals("$je",((CopyModifyExpression) ret).getReturnExpr().toString());
275+
}
276+
}
277+
278+
@Test
279+
public void copyModifyExprTestComplexModify() throws EXistException, RecognitionException, XPathException, TokenStreamException, PermissionDeniedException
280+
{
281+
String query = "copy $newx := $oldx\n" +
282+
" modify (rename node $newx as \"newx\", \n" +
283+
" replace value of node $newx with $newx * 2)\n" +
284+
" return ($oldx, $newx)";
285+
286+
BrokerPool pool = BrokerPool.getInstance();
287+
try(final DBBroker broker = pool.getBroker()) {
288+
// parse the query into the internal syntax tree
289+
XQueryContext context = new XQueryContext(broker.getBrokerPool());
290+
XQueryLexer lexer = new XQueryLexer(context, new StringReader(query));
291+
XQueryParser xparser = new XQueryParser(lexer);
292+
xparser.expr();
293+
if (xparser.foundErrors()) {
294+
fail(xparser.getErrorMessage());
295+
return;
296+
}
297+
298+
XQueryAST ast = (XQueryAST) xparser.getAST();
299+
300+
XQueryTreeParser treeParser = new XQueryTreeParser(context);
301+
PathExpr expr = new PathExpr(context);
302+
Expression ret = treeParser.expr(ast, expr);
303+
if (treeParser.foundErrors()) {
304+
fail(treeParser.getErrorMessage());
305+
return;
306+
}
307+
308+
assertTrue(ret instanceof CopyModifyExpression);
309+
assertEquals("( replace $newx \"newx\", replace $newx VALUE $newx * 2 )",((CopyModifyExpression) ret).getModifyExpr().toString());
310+
assertEquals("( $oldx, $newx )",((CopyModifyExpression) ret).getReturnExpr().toString());
272311
}
273312
}
274313

@@ -339,6 +378,48 @@ public void insertExpr() throws EXistException, RecognitionException, XPathExcep
339378

340379
assertTrue(expr.getFirst() instanceof InsertExpr);
341380
assertEquals(Expression.Category.UPDATING, expr.getFirst().getCategory());
381+
assertEquals(InsertExpr.Choice.AFTER, ((InsertExpr) expr.getFirst()).getChoice());
382+
}
383+
}
384+
385+
@Test
386+
public void insertExprAsLast() throws EXistException, RecognitionException, XPathException, TokenStreamException, PermissionDeniedException
387+
{
388+
String query =
389+
"insert node $new-police-report\n" +
390+
" as last into fn:doc(\"insurance.xml\")/policies\n" +
391+
" /policy[id = $pid]\n" +
392+
" /driver[license = $license]\n" +
393+
" /accident[date = $accdate]\n" +
394+
" /police-reports";
395+
396+
BrokerPool pool = BrokerPool.getInstance();
397+
try(final DBBroker broker = pool.getBroker()) {
398+
// parse the query into the internal syntax tree
399+
XQueryContext context = new XQueryContext(broker.getBrokerPool());
400+
XQueryLexer lexer = new XQueryLexer(context, new StringReader(query));
401+
XQueryParser xparser = new XQueryParser(lexer);
402+
xparser.expr();
403+
if (xparser.foundErrors()) {
404+
fail(xparser.getErrorMessage());
405+
return;
406+
}
407+
408+
XQueryAST ast = (XQueryAST) xparser.getAST();
409+
410+
XQueryTreeParser treeParser = new XQueryTreeParser(context);
411+
PathExpr expr = new PathExpr(context);
412+
treeParser.expr(ast, expr);
413+
414+
if (treeParser.foundErrors()) {
415+
fail(treeParser.getErrorMessage());
416+
return;
417+
}
418+
419+
assertTrue(expr.getFirst() instanceof InsertExpr);
420+
assertEquals(Expression.Category.UPDATING, expr.getFirst().getCategory());
421+
assertEquals("$new-police-report", ((InsertExpr) expr.getFirst()).getSourceExpr().toString());
422+
assertEquals(InsertExpr.Choice.LAST, ((InsertExpr) expr.getFirst()).getChoice());
342423
}
343424
}
344425

@@ -348,6 +429,42 @@ public void deleteExpr() throws EXistException, RecognitionException, XPathExcep
348429
String query =
349430
"delete node fn:doc(\"bib.xml\")/books/book[1]/author[last()]";
350431

432+
BrokerPool pool = BrokerPool.getInstance();
433+
try(final DBBroker broker = pool.getBroker()) {
434+
// parse the query into the internal syntax tree
435+
XQueryContext context = new XQueryContext(broker.getBrokerPool());
436+
XQueryLexer lexer = new XQueryLexer(context, new StringReader(query));
437+
XQueryParser xparser = new XQueryParser(lexer);
438+
xparser.expr();
439+
if (xparser.foundErrors()) {
440+
fail(xparser.getErrorMessage());
441+
return;
442+
}
443+
444+
XQueryAST ast = (XQueryAST) xparser.getAST();
445+
446+
XQueryTreeParser treeParser = new XQueryTreeParser(context);
447+
PathExpr expr = new PathExpr(context);
448+
treeParser.expr(ast, expr);
449+
450+
if (treeParser.foundErrors()) {
451+
fail(treeParser.getErrorMessage());
452+
return;
453+
}
454+
455+
assertTrue(expr.getFirst() instanceof DeleteExpr);
456+
assertEquals(Expression.Category.UPDATING, expr.getFirst().getCategory());
457+
assertEquals("doc(\"bib.xml\")/child::{}books/child::{}book[1]/child::{}author[last()]", ((DeleteExpr) expr.getFirst()).getTargetExpr().toString());
458+
}
459+
}
460+
461+
@Test
462+
public void deleteExprComplex() throws EXistException, RecognitionException, XPathException, TokenStreamException, PermissionDeniedException
463+
{
464+
String query =
465+
"delete nodes /email/message\n" +
466+
" [date > xs:dayTimeDuration(\"P365D\")]";
467+
351468
BrokerPool pool = BrokerPool.getInstance();
352469
try(final DBBroker broker = pool.getBroker()) {
353470
// parse the query into the internal syntax tree
@@ -485,4 +602,40 @@ public void renameExpr() throws EXistException, RecognitionException, XPathExcep
485602
assertEquals("\"principal-author\"",((RenameExpr) expr.getFirst()).getNewNameExpr().toString());
486603
}
487604
}
605+
606+
@Test
607+
public void renameExprWithExpr() throws EXistException, RecognitionException, XPathException, TokenStreamException, PermissionDeniedException
608+
{
609+
String query =
610+
"rename node fn:doc(\"bib.xml\")/books/book[1]/author[1]\n" +
611+
"as $newname";
612+
613+
BrokerPool pool = BrokerPool.getInstance();
614+
try(final DBBroker broker = pool.getBroker()) {
615+
// parse the query into the internal syntax tree
616+
XQueryContext context = new XQueryContext(broker.getBrokerPool());
617+
XQueryLexer lexer = new XQueryLexer(context, new StringReader(query));
618+
XQueryParser xparser = new XQueryParser(lexer);
619+
xparser.expr();
620+
if (xparser.foundErrors()) {
621+
fail(xparser.getErrorMessage());
622+
return;
623+
}
624+
625+
XQueryAST ast = (XQueryAST) xparser.getAST();
626+
627+
XQueryTreeParser treeParser = new XQueryTreeParser(context);
628+
PathExpr expr = new PathExpr(context);
629+
treeParser.expr(ast, expr);
630+
631+
if (treeParser.foundErrors()) {
632+
fail(treeParser.getErrorMessage());
633+
return;
634+
}
635+
636+
assertTrue(expr.getFirst() instanceof RenameExpr);
637+
assertEquals("doc(\"bib.xml\")/child::{}books/child::{}book[1]/child::{}author[1]",((RenameExpr) expr.getFirst()).getTargetExpr().toString());
638+
assertEquals("$newname",((RenameExpr) expr.getFirst()).getNewNameExpr().toString());
639+
}
640+
}
488641
}

0 commit comments

Comments
 (0)