|
| 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.DiffMatcher.elemSource; |
| 30 | +import static org.exist.test.XQueryAssertions.*; |
| 31 | + |
| 32 | +/** |
| 33 | + * Ensure function types returned in element content throws at compile time and |
| 34 | + * has location information. |
| 35 | + * See issue :<a href="https://github.com/eXist-db/exist/issues/3474">#3474</a>. |
| 36 | + * |
| 37 | + * @author <a href="mailto:[email protected]">Juri Leino</a> |
| 38 | + */ |
| 39 | +public class FunctionTypeInElementContentTest extends XQueryCompilationTest { |
| 40 | + |
| 41 | + @Test |
| 42 | + public void arrayLiteral() throws EXistException, PermissionDeniedException { |
| 43 | + final String query = "element test { [] }"; |
| 44 | + assertXQResultSimilar(elemSource("<test/>"), executeQuery(query)); |
| 45 | + } |
| 46 | + |
| 47 | + // TODO(JL): array content could be removed after https://github.com/eXist-db/exist/issues/3472 is fixed |
| 48 | + @Test |
| 49 | + public void arrayConstructor() throws EXistException, PermissionDeniedException { |
| 50 | + final String query = "element test { array { () } }"; |
| 51 | + assertXQResultSimilar(elemSource("<test/>"), executeQuery(query)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void sequenceOfItems() throws EXistException, PermissionDeniedException { |
| 56 | + final String query = "element test { (1, map {})[1] }"; |
| 57 | + assertXQResultSimilar(elemSource("<test>1</test>"), executeQuery(query)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void partialBuiltIn() throws EXistException, PermissionDeniedException { |
| 62 | + final String query = "element test { sum(?) }"; |
| 63 | + final String error = "Function types are not allowed in element content. Got function(*)"; |
| 64 | + assertXQStaticError(ErrorCodes.XQTY0105, 1, 16, error, compileQuery(query)); |
| 65 | + } |
| 66 | + |
| 67 | + // TODO(JL): Does still throw without location info |
| 68 | + @Test |
| 69 | + public void functionReference() throws EXistException, PermissionDeniedException { |
| 70 | + final String query = "element test { sum#0 }"; |
| 71 | + final String error = "Function types are not allowed in element content. Got function(*)"; |
| 72 | + assertXQStaticError(ErrorCodes.XQTY0105, 0, 0, error, compileQuery(query)); |
| 73 | + } |
| 74 | + |
| 75 | + // TODO(JL): Does not throw at compile time |
| 76 | + @Test |
| 77 | + public void functionVariable() throws EXistException, PermissionDeniedException { |
| 78 | + final String query = "let $f := function () {} return element test { $f }"; |
| 79 | + final String error = "Enclosed expression contains function item"; |
| 80 | + assertXQDynamicError(ErrorCodes.XQTY0105, 1, 49, error, executeQuery(query)); |
| 81 | + } |
| 82 | + |
| 83 | + // TODO(JL): user defined function has its location offset to a weird location |
| 84 | + @Test |
| 85 | + public void userDefinedFunction() throws EXistException, PermissionDeniedException { |
| 86 | + final String query = "element test { function () {} }"; |
| 87 | + final String error = "Function types are not allowed in element content. Got function(*)"; |
| 88 | + assertXQStaticError(ErrorCodes.XQTY0105, 1, 25, error, compileQuery(query)); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void mapConstructor() throws EXistException, PermissionDeniedException { |
| 93 | + final String query = "element test { map {} }"; |
| 94 | + final String error = "Function types are not allowed in element content. Got map(*)"; |
| 95 | + assertXQStaticError(ErrorCodes.XQTY0105, 1, 16, error, compileQuery(query)); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void mapConstructorLookup() throws EXistException, PermissionDeniedException { |
| 100 | + final String query = "element test { map {1:1}?1 }"; |
| 101 | + assertXQResultSimilar(elemSource("<test>1</test>"), executeQuery(query)); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * sequence in enclosed expression with only a function type |
| 106 | + */ |
| 107 | + @Test |
| 108 | + public void sequenceOfMaps() throws EXistException, PermissionDeniedException { |
| 109 | + final String query = "element test { (map {}) }"; |
| 110 | + final String error = "Function types are not allowed in element content. Got map(*)"; |
| 111 | + assertXQDynamicError(ErrorCodes.XQTY0105, 0, 0, error, executeQuery(query)); |
| 112 | + } |
| 113 | + |
| 114 | + // TODO(JL): add (sub-expression) location |
| 115 | + /** |
| 116 | + * This is an edge case, which would evaluate to empty sequence |
| 117 | + * but should arguably still throw. |
| 118 | + */ |
| 119 | + @Test |
| 120 | + public void sequenceOfMapsEdgeCase() throws EXistException, PermissionDeniedException { |
| 121 | + final String query = "element test { (map {})[2] }"; |
| 122 | + final String error = "Function types are not allowed in element content. Got map(*)"; |
| 123 | + assertXQStaticError(ErrorCodes.XQTY0105, 0, 0, error, compileQuery(query)); |
| 124 | + } |
| 125 | + |
| 126 | + // TODO(JL): add (sub-expression) location |
| 127 | + // TODO(JL): this could throw at compile time |
| 128 | + @Test |
| 129 | + public void arrayOfMaps() throws EXistException, PermissionDeniedException { |
| 130 | + final String query = "element test { [map {}] }"; |
| 131 | + final String error = "Enclosed expression contains function item"; |
| 132 | + assertXQDynamicError(ErrorCodes.XQTY0105, 1, 16, error, executeQuery(query)); |
| 133 | + }; |
| 134 | + |
| 135 | + // TODO(JL): add (sub-expression) location |
| 136 | + // TODO(JL): This should throw at compile time, but does not |
| 137 | + @Test |
| 138 | + public void mapConstructorInSubExpression() throws EXistException, PermissionDeniedException { |
| 139 | + final String query = "element test { \"a\", map {} }"; |
| 140 | + final String error = "Enclosed expression contains function item"; |
| 141 | + assertXQDynamicError(ErrorCodes.XQTY0105, 0, 0, error, executeQuery(query)); |
| 142 | + } |
| 143 | +} |
0 commit comments