-
-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow module imports in one-off xqueries #5529
Open
line-o
wants to merge
1
commit into
eXist-db:develop
Choose a base branch
from
line-o:fix/5525
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,17 +33,22 @@ | |
import org.exist.xquery.value.Sequence; | ||
import org.junit.ClassRule; | ||
|
||
import java.net.URISyntaxException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import static com.evolvedbinary.j8fu.Either.Left; | ||
import static com.evolvedbinary.j8fu.Either.Right; | ||
import static com.ibm.icu.impl.Assert.fail; | ||
|
||
/** | ||
* Base class for test suites testing XQuery compilation | ||
* @author <a href="mailto:[email protected]">Juri Leino</a> | ||
*/ | ||
public abstract class XQueryCompilationTest { | ||
@ClassRule | ||
public static final ExistEmbeddedServer server = new ExistEmbeddedServer(true, true); | ||
|
||
@ClassRule | ||
public static ExistEmbeddedServer server = new ExistEmbeddedServer(true, true); | ||
protected static Either<XPathException, CompiledXQuery> compileQuery(final String string) throws EXistException, PermissionDeniedException { | ||
final BrokerPool pool = server.getBrokerPool(); | ||
final XQuery xqueryService = pool.getXQueryService(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
exist-core/src/test/java/org/exist/xquery/ModuleImportTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* eXist-db Open Source Native XML Database | ||
* Copyright (C) 2001 The eXist-db Authors | ||
* | ||
* [email protected] | ||
* http://www.exist-db.org | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
package org.exist.xquery; | ||
|
||
import com.evolvedbinary.j8fu.Either; | ||
|
||
import org.exist.EXistException; | ||
import org.exist.security.PermissionDeniedException; | ||
import org.exist.storage.BrokerPool; | ||
import org.exist.storage.DBBroker; | ||
import org.exist.test.ExistEmbeddedServer; | ||
import org.exist.xquery.value.Sequence; | ||
import org.exist.xquery.value.StringValue; | ||
|
||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
|
||
import java.net.URISyntaxException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
import static com.evolvedbinary.j8fu.Either.Left; | ||
import static com.evolvedbinary.j8fu.Either.Right; | ||
import static com.ibm.icu.impl.Assert.fail; | ||
import static org.exist.test.XQueryAssertions.assertThatXQResult; | ||
import static org.exist.test.XQueryAssertions.assertXQStaticError; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
/** | ||
* Ensure library module imports work in one-off queries | ||
* needs functx to be installed => conf.xml => triggers => autodeploy | ||
* | ||
* @author <a href="mailto:[email protected]">Juri Leino</a> | ||
*/ | ||
public class ModuleImportTest { | ||
@ClassRule | ||
public static ExistEmbeddedServer server = new ExistEmbeddedServer(null, getConfigFile(), null, false, true); | ||
|
||
protected static Either<XPathException, CompiledXQuery> compileQuery(final String string) throws EXistException, PermissionDeniedException { | ||
final BrokerPool pool = server.getBrokerPool(); | ||
final XQuery xqueryService = pool.getXQueryService(); | ||
try (final DBBroker broker = pool.getBroker()) { | ||
try { | ||
return Right(xqueryService.compile(new XQueryContext(broker.getDatabase()), string)); | ||
} catch (final XPathException e) { | ||
return Left(e); | ||
} | ||
} | ||
} | ||
|
||
protected static Either<XPathException, Sequence> executeQuery(final String string) throws EXistException, PermissionDeniedException { | ||
final BrokerPool pool = server.getBrokerPool(); | ||
final XQuery xqueryService = pool.getXQueryService(); | ||
try (final DBBroker broker = pool.getBroker()) { | ||
try { | ||
return Right(xqueryService.execute(broker, string, null)); | ||
} catch (final XPathException e) { | ||
return Left(e); | ||
} | ||
} | ||
} | ||
|
||
private static Path getConfigFile() { | ||
final ClassLoader loader = ModuleImportTest.class.getClassLoader(); | ||
final char separator = System.getProperty("file.separator").charAt(0); | ||
final String packagePath = ModuleImportTest.class.getPackage().getName().replace('.', separator); | ||
|
||
try { | ||
return Paths.get(loader.getResource(packagePath + separator + "conf.xml").toURI()); | ||
} catch (final URISyntaxException e) { | ||
fail(e); | ||
return null; | ||
} | ||
} | ||
|
||
@Test | ||
public void importLibraryWithoutLocation() throws EXistException, PermissionDeniedException { | ||
final Sequence expected = new StringValue("xs:integer"); | ||
|
||
final String query = "import module namespace functx='http://www.functx.com';" + | ||
"functx:atomic-type(4)"; | ||
final Either<XPathException, Sequence> actual = executeQuery(query); | ||
|
||
assertThatXQResult(actual, equalTo(expected)); | ||
} | ||
@Test | ||
public void importLibraryFromDbLocation() throws EXistException, PermissionDeniedException { | ||
final Sequence expected = new StringValue("xs:integer"); | ||
|
||
final String query = "import module namespace functx='http://www.functx.com'" + | ||
" at '/db/system/repo/functx-1.0.1/functx/functx.xq';" + | ||
"functx:atomic-type(4)"; | ||
final Either<XPathException, Sequence> actual = executeQuery(query); | ||
|
||
assertThatXQResult(actual, equalTo(expected)); | ||
} | ||
|
||
@Test | ||
public void importLibraryFromXMLDBLocation() throws EXistException, PermissionDeniedException { | ||
final Sequence expected = new StringValue("xs:integer"); | ||
|
||
final String query = "import module namespace functx='http://www.functx.com'" + | ||
" at 'xmldb:///db/system/repo/functx-1.0.1/functx/functx.xq';" + | ||
"functx:atomic-type(4)"; | ||
final Either<XPathException, Sequence> actual = executeQuery(query); | ||
|
||
assertThatXQResult(actual, equalTo(expected)); | ||
} | ||
|
||
@Test | ||
public void importLibraryFromExistXMLDBLocation() throws EXistException, PermissionDeniedException { | ||
final Sequence expected = new StringValue("xs:integer"); | ||
|
||
final String query = "import module namespace functx='http://www.functx.com'" + | ||
" at 'xmldb:exist:///db/system/repo/functx-1.0.1/functx/functx.xq';" + | ||
"functx:atomic-type(4)"; | ||
final Either<XPathException, Sequence> actual = executeQuery(query); | ||
|
||
assertThatXQResult(actual, equalTo(expected)); | ||
} | ||
|
||
@Test | ||
public void importLibraryFromUnknownLocation() throws EXistException, PermissionDeniedException { | ||
|
||
final String query = "import module namespace functx='http://www.functx.com'" + | ||
" at 'unknown:///db/system/repo/functx-1.0.1/functx/functx.xq';" + | ||
"functx:atomic-type(4)"; | ||
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'."; | ||
|
||
assertXQStaticError(ErrorCodes.XQST0059, -1,-1, expectedMessage, compileQuery(query)); | ||
} | ||
|
||
@Test | ||
public void importLibraryFromRelativeLocation() throws EXistException, PermissionDeniedException { | ||
final String query = "import module namespace functx='http://www.functx.com'" + | ||
" at './functx.xq';" + | ||
"functx:atomic-type(4)"; | ||
final String expectedMessage = "error found while loading module functx: Source for module 'http://www.functx.com' not found module location hint URI './functx.xq'."; | ||
|
||
assertXQStaticError(ErrorCodes.XQST0059, -1,-1, expectedMessage, compileQuery(query)); | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to upset anyone, but I just wanted to point out that this is the wrong approach to fixing this issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fixing an additional bug I found while creating tests:
see
importLibraryFromDbLocation
andimportLibraryFromXMLDBLocation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I understood that, but it still is the wrong approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adamretter Could you explain why you think this is the wrong approach and how do you think it should be solved correctly?