|
| 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 com.evolvedbinary.j8fu.Either; |
| 25 | + |
| 26 | +import org.exist.EXistException; |
| 27 | +import org.exist.security.PermissionDeniedException; |
| 28 | +import org.exist.storage.BrokerPool; |
| 29 | +import org.exist.storage.DBBroker; |
| 30 | +import org.exist.test.ExistEmbeddedServer; |
| 31 | +import org.exist.xquery.value.Sequence; |
| 32 | +import org.exist.xquery.value.StringValue; |
| 33 | + |
| 34 | +import org.junit.ClassRule; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +import java.net.URISyntaxException; |
| 38 | +import java.nio.file.Path; |
| 39 | +import java.nio.file.Paths; |
| 40 | + |
| 41 | +import static com.evolvedbinary.j8fu.Either.Left; |
| 42 | +import static com.evolvedbinary.j8fu.Either.Right; |
| 43 | +import static com.ibm.icu.impl.Assert.fail; |
| 44 | +import static org.exist.test.XQueryAssertions.assertThatXQResult; |
| 45 | +import static org.exist.test.XQueryAssertions.assertXQStaticError; |
| 46 | +import static org.hamcrest.Matchers.equalTo; |
| 47 | + |
| 48 | +/** |
| 49 | + * Ensure library module imports work in one-off queries |
| 50 | + * needs functx to be installed => conf.xml => triggers => autodeploy |
| 51 | + * |
| 52 | + * @author <a href="mailto:[email protected]">Juri Leino</a> |
| 53 | + */ |
| 54 | +public class ModuleImportTest { |
| 55 | + @ClassRule |
| 56 | + public static final ExistEmbeddedServer server = new ExistEmbeddedServer(null, getConfigFile(), null, false, true); |
| 57 | + |
| 58 | + protected static Either<XPathException, CompiledXQuery> compileQuery(final String string) throws EXistException, PermissionDeniedException { |
| 59 | + final BrokerPool pool = server.getBrokerPool(); |
| 60 | + final XQuery xqueryService = pool.getXQueryService(); |
| 61 | + try (final DBBroker broker = pool.getBroker()) { |
| 62 | + try { |
| 63 | + return Right(xqueryService.compile(new XQueryContext(broker.getDatabase()), string)); |
| 64 | + } catch (final XPathException e) { |
| 65 | + return Left(e); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + protected static Either<XPathException, Sequence> executeQuery(final String string) throws EXistException, PermissionDeniedException { |
| 71 | + final BrokerPool pool = server.getBrokerPool(); |
| 72 | + final XQuery xqueryService = pool.getXQueryService(); |
| 73 | + try (final DBBroker broker = pool.getBroker()) { |
| 74 | + try { |
| 75 | + return Right(xqueryService.execute(broker, string, null)); |
| 76 | + } catch (final XPathException e) { |
| 77 | + return Left(e); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static Path getConfigFile() { |
| 83 | + final ClassLoader loader = ModuleImportTest.class.getClassLoader(); |
| 84 | + final char separator = System.getProperty("file.separator").charAt(0); |
| 85 | + final String packagePath = ModuleImportTest.class.getPackage().getName().replace('.', separator); |
| 86 | + |
| 87 | + try { |
| 88 | + return Paths.get(loader.getResource(packagePath + separator + "conf.xml").toURI()); |
| 89 | + } catch (final URISyntaxException e) { |
| 90 | + fail(e); |
| 91 | + return null; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void importLibraryWithoutLocation() throws EXistException, PermissionDeniedException { |
| 97 | + final Sequence expected = new StringValue("xs:integer"); |
| 98 | + |
| 99 | + final String query = "import module namespace functx='http://www.functx.com';" + |
| 100 | + "functx:atomic-type(4)"; |
| 101 | + final Either<XPathException, Sequence> actual = executeQuery(query); |
| 102 | + |
| 103 | + assertThatXQResult(actual, equalTo(expected)); |
| 104 | + } |
| 105 | + @Test |
| 106 | + public void importLibraryFromDbLocation() throws EXistException, PermissionDeniedException { |
| 107 | + final Sequence expected = new StringValue("xs:integer"); |
| 108 | + |
| 109 | + final String query = "import module namespace functx='http://www.functx.com'" + |
| 110 | + " at '/db/system/repo/functx-1.0.1/functx/functx.xq';" + |
| 111 | + "functx:atomic-type(4)"; |
| 112 | + final Either<XPathException, Sequence> actual = executeQuery(query); |
| 113 | + |
| 114 | + assertThatXQResult(actual, equalTo(expected)); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void importLibraryFromXMLDBLocation() throws EXistException, PermissionDeniedException { |
| 119 | + final Sequence expected = new StringValue("xs:integer"); |
| 120 | + |
| 121 | + final String query = "import module namespace functx='http://www.functx.com'" + |
| 122 | + " at 'xmldb:/db/system/repo/functx-1.0.1/functx/functx.xq';" + |
| 123 | + "functx:atomic-type(4)"; |
| 124 | + final Either<XPathException, Sequence> actual = executeQuery(query); |
| 125 | + |
| 126 | + assertThatXQResult(actual, equalTo(expected)); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void importLibraryFromXMLDBLocationDoubleSlash() throws EXistException, PermissionDeniedException { |
| 131 | + final Sequence expected = new StringValue("xs:integer"); |
| 132 | + |
| 133 | + final String query = "import module namespace functx='http://www.functx.com'" + |
| 134 | + " at 'xmldb:///db/system/repo/functx-1.0.1/functx/functx.xq';" + |
| 135 | + "functx:atomic-type(4)"; |
| 136 | + final Either<XPathException, Sequence> actual = executeQuery(query); |
| 137 | + |
| 138 | + assertThatXQResult(actual, equalTo(expected)); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void importLibraryFromExistXMLDBLocation() throws EXistException, PermissionDeniedException { |
| 143 | + final Sequence expected = new StringValue("xs:integer"); |
| 144 | + |
| 145 | + final String query = "import module namespace functx='http://www.functx.com'" + |
| 146 | + " at 'xmldb:exist:///db/system/repo/functx-1.0.1/functx/functx.xq';" + |
| 147 | + "functx:atomic-type(4)"; |
| 148 | + final Either<XPathException, Sequence> actual = executeQuery(query); |
| 149 | + |
| 150 | + assertThatXQResult(actual, equalTo(expected)); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void importLibraryFromUnknownLocation() throws EXistException, PermissionDeniedException { |
| 155 | + |
| 156 | + final String query = "import module namespace functx='http://www.functx.com'" + |
| 157 | + " at 'unknown:///db/system/repo/functx-1.0.1/functx/functx.xq';" + |
| 158 | + "functx:atomic-type(4)"; |
| 159 | + final String expectedMessage = "error found while loading module functx: Source for module 'http://www.functx.com' not found module location hint URI 'unknown:///db/system/repo/functx-1.0.1/functx/functx.xq'."; |
| 160 | + |
| 161 | + assertXQStaticError(ErrorCodes.XQST0059, -1,-1, expectedMessage, compileQuery(query)); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void importLibraryFromRelativeLocation() throws EXistException, PermissionDeniedException { |
| 166 | + final String query = "import module namespace functx='http://www.functx.com'" + |
| 167 | + " at './functx.xq';" + |
| 168 | + "functx:atomic-type(4)"; |
| 169 | + final String expectedMessage = "error found while loading module functx: Source for module 'http://www.functx.com' not found module location hint URI './functx.xq'."; |
| 170 | + |
| 171 | + assertXQStaticError(ErrorCodes.XQST0059, -1,-1, expectedMessage, compileQuery(query)); |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments