|
| 1 | +/* |
| 2 | + * eXist-db Open Source Native XML Database |
| 3 | + * Copyright (C) 2001 The eXist-db Authors |
| 4 | + * |
| 5 | + |
| 6 | + * http://www.exist-db.org |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 2.1 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public |
| 19 | + * License along with this library; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | + */ |
| 22 | +package org.exist.xquery; |
| 23 | + |
| 24 | +import org.exist.EXistException; |
| 25 | +import org.exist.security.PermissionDeniedException; |
| 26 | +import org.exist.test.XQueryCompilationTest; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import static org.exist.test.XQueryAssertions.assertXQResultToStringEquals; |
| 30 | +import static org.exist.test.XQueryAssertions.assertXQStaticError; |
| 31 | + |
| 32 | +/** |
| 33 | + * Ensure absolute XPath expression will raise a static error |
| 34 | + * |
| 35 | + * @author <a href="mailto:[email protected]">Juri Leino</a> |
| 36 | + */ |
| 37 | +public class AbsolutePathTests extends XQueryCompilationTest { |
| 38 | + @Test |
| 39 | + public void declaredFunctionAbsoluteSlash() throws EXistException, PermissionDeniedException { |
| 40 | + final String query = "declare function local:x() { /x }; local:x()"; |
| 41 | + final String expectedMessage = "Leading '/' selects nothing, ContextItem is absent in function body"; |
| 42 | + assertXQStaticError(ErrorCodes.XPDY0002, 1,30, expectedMessage, compileQuery(query)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void declaredFunctionLoneSlash() throws EXistException, PermissionDeniedException { |
| 47 | + final String query = "declare function local:x() { / }; local:x()"; |
| 48 | + final String expectedMessage = "Leading '/' selects nothing, ContextItem is absent in function body"; |
| 49 | + assertXQStaticError(ErrorCodes.XPDY0002, 1, 30, expectedMessage, compileQuery(query)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void declaredFunctionAbsoluteDoubleSlash() throws EXistException, PermissionDeniedException { |
| 54 | + final String query = "declare function local:x() { //x }; local:x()"; |
| 55 | + final String expectedMessage = "Leading '//' selects nothing, ContextItem is absent in function body"; |
| 56 | + assertXQStaticError(ErrorCodes.XPDY0002, 1, 30, expectedMessage, compileQuery(query)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void immediateLambdaContainsSlash() throws EXistException, PermissionDeniedException { |
| 61 | + final String query = "(function() { /x })()"; |
| 62 | + assertXQStaticError(ErrorCodes.XPDY0002, 1, 15, compileQuery(query)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void immediateLambdaContainsLoneSlash() throws EXistException, PermissionDeniedException { |
| 67 | + final String query = "(function() { / })()"; |
| 68 | + assertXQStaticError(ErrorCodes.XPDY0002, 1, 15, compileQuery(query)); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void immediateLambdaContainsDoubleSlash() throws EXistException, PermissionDeniedException { |
| 73 | + final String query = "(function() { //x })()"; |
| 74 | + assertXQStaticError(ErrorCodes.XPDY0002, 1, 15, compileQuery(query)); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void immediateLambdaWithDocumentAndDoubleSlash() throws EXistException, PermissionDeniedException { |
| 79 | + final String query = "let $f := function($d) { $d ! //x }\n" + |
| 80 | + "let $d := document { <root><x/><x/><y><x/></y></root> }\n" + |
| 81 | + "return serialize($f($d))"; |
| 82 | + assertXQResultToStringEquals("<x/><x/><x/>", executeQuery(query)); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void immediateLambdaWithDocumentAndSlash() throws EXistException, PermissionDeniedException { |
| 87 | + final String query = "let $f := function($d) { $d ! /root }\n" + |
| 88 | + "let $d := document { <root/> }\n" + |
| 89 | + "return serialize($f($d))"; |
| 90 | + assertXQResultToStringEquals("<root/>", executeQuery(query)); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void immediateLambdaWithDocumentAndLoneSlash() throws EXistException, PermissionDeniedException { |
| 95 | + final String query = "let $f := function($d) { $d ! / }\n" + |
| 96 | + "let $d := document { <root/> }\n" + |
| 97 | + "return serialize($f($d))"; |
| 98 | + assertXQResultToStringEquals("<root/>", executeQuery(query)); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void topLevelAbsolutePath() throws EXistException, PermissionDeniedException { |
| 103 | + final String query = "count(//*)"; |
| 104 | + assertXQResultToStringEquals("1", executeQuery(query)); |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments