|
| 1 | +package org.exist.xquery; |
| 2 | + |
| 3 | +import com.evolvedbinary.j8fu.Either; |
| 4 | +import org.exist.EXistException; |
| 5 | +import org.exist.security.PermissionDeniedException; |
| 6 | +import org.exist.storage.BrokerPool; |
| 7 | +import org.exist.storage.DBBroker; |
| 8 | +import org.exist.test.ExistEmbeddedServer; |
| 9 | +import org.exist.xquery.value.Sequence; |
| 10 | +import org.junit.ClassRule; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import static com.evolvedbinary.j8fu.Either.Left; |
| 14 | +import static com.evolvedbinary.j8fu.Either.Right; |
| 15 | +import static org.junit.Assert.assertEquals; |
| 16 | +import static org.junit.Assert.assertTrue; |
| 17 | + |
| 18 | +public class ContextItemTests { |
| 19 | + |
| 20 | + @ClassRule |
| 21 | + public static final ExistEmbeddedServer server = new ExistEmbeddedServer(true, true); |
| 22 | + |
| 23 | + @Test |
| 24 | + public void staticFunctionNoContextItem() throws EXistException, PermissionDeniedException { |
| 25 | + final String query = |
| 26 | + "declare function local:x() {" + |
| 27 | + " //x" + |
| 28 | + "};" + |
| 29 | + "local:x()"; |
| 30 | + |
| 31 | + final Either<XPathException, Sequence> result = executeQuery(query); |
| 32 | + assertXQueryError(ErrorCodes.XPDY0002, result); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void staticFunctionNoInheritContextItem() throws EXistException, PermissionDeniedException { |
| 37 | + final String query = |
| 38 | + "declare function local:x() {" + |
| 39 | + " //x" + |
| 40 | + "};" + |
| 41 | + "<doc><x>1</x><x>2</x></doc>/local:x()"; |
| 42 | + |
| 43 | + final Either<XPathException, Sequence> result = executeQuery(query); |
| 44 | + assertXQueryError(ErrorCodes.XPDY0002, result); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void dynamicFunctionNoContextItem() throws EXistException, PermissionDeniedException { |
| 49 | + final String query = |
| 50 | + "declare function local:x() {" + |
| 51 | + " //x" + |
| 52 | + "};" + |
| 53 | + "let $ref := local:x#0 return" + |
| 54 | + " $ref()"; |
| 55 | + |
| 56 | + final Either<XPathException, Sequence> result = executeQuery(query); |
| 57 | + assertXQueryError(ErrorCodes.XPDY0002, result); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void dynamicFunctionNoInheritContextItem() throws EXistException, PermissionDeniedException { |
| 62 | + final String query = |
| 63 | + "declare function local:x() {" + |
| 64 | + " //x" + |
| 65 | + "};" + |
| 66 | + "let $ref := local:x#0 return" + |
| 67 | + " <doc><x>1</x><x>2</x></doc>/$ref()"; |
| 68 | + |
| 69 | + final Either<XPathException, Sequence> result = executeQuery(query); |
| 70 | + assertXQueryError(ErrorCodes.XPDY0002, result); |
| 71 | + } |
| 72 | + |
| 73 | + private static void assertXQueryError(final ErrorCodes.ErrorCode expected, final Either<XPathException, Sequence> actual) { |
| 74 | + assertTrue("Expected: " + expected.getErrorQName() + ", but got result: " + actual.toString(), actual.isLeft()); |
| 75 | + final XPathException xpe = actual.left().get(); |
| 76 | + assertEquals(expected, xpe.getErrorCode()); |
| 77 | + } |
| 78 | + |
| 79 | + private Either<XPathException, Sequence> executeQuery(final String string) throws EXistException, PermissionDeniedException { |
| 80 | + final BrokerPool pool = server.getBrokerPool(); |
| 81 | + final XQuery xqueryService = pool.getXQueryService(); |
| 82 | + try (final DBBroker broker = pool.getBroker()) { |
| 83 | + try { |
| 84 | + return Right(xqueryService.execute(broker, string, null)); |
| 85 | + } catch (final XPathException e) { |
| 86 | + return Left(e); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments