Skip to content

Commit 7ab1fdf

Browse files
Allow multiple locations for modules in tests (#2556)
* allow multiple locations for modules in tests * allow module import using the same prefix and URI as in the module declaration
1 parent a784619 commit 7ab1fdf

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

basex-core/src/main/java/org/basex/query/QueryContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public final class QueryContext extends Job implements Closeable {
118118
/** Parsed modules, containing the file path and library module. */
119119
public final TokenObjectMap<LibraryModule> libs = new TokenObjectMap<>();
120120
/** Pre-declared modules, containing module URI and their file paths (required for test APIs). */
121-
public final TokenObjectMap<byte[]> modDeclared = new TokenObjectMap<>();
121+
public final TokenObjectMap<TokenList> modDeclared = new TokenObjectMap<>();
122122
/** Stack of module files that are currently parsed. */
123123
public final TokenList modStack = new TokenList();
124124

basex-core/src/main/java/org/basex/query/QueryParser.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -743,17 +743,22 @@ private void moduleImport() throws QueryException {
743743

744744
// add non-default namespace
745745
if(prefix != EMPTY) {
746-
if(sc.ns.staticURI(prefix) != null) throw error(DUPLNSDECL_X, prefix);
747-
sc.ns.add(prefix, uri, info());
748-
namespaces.put(prefix, uri);
746+
final byte[] su = sc.ns.staticURI(prefix);
747+
if(su == null) {
748+
sc.ns.add(prefix, uri, info());
749+
namespaces.put(prefix, uri);
750+
} else {
751+
final byte[] mu = sc.module == null ? null : sc.module.uri();
752+
if(!Token.eq(su, uri) || !Token.eq(mu, uri)) throw error(DUPLNSDECL_X, prefix);
753+
}
749754
}
750755

751756
// check modules at specified locations
752757
final ModInfo mi = new ModInfo();
753758
if(!addLocations(mi.paths)) {
754759
// check module files that have been pre-declared by a test API
755-
final byte[] pth = qc.modDeclared.get(uri);
756-
if(pth != null) mi.paths.add(pth);
760+
final TokenList pths = qc.modDeclared.get(uri);
761+
if(pths != null) pths.forEach(mi.paths::add);
757762
}
758763
mi.uri = uri;
759764
mi.info = info();

basex-core/src/main/java/org/basex/query/QueryProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.basex.query.value.*;
1414
import org.basex.query.value.node.*;
1515
import org.basex.query.value.seq.*;
16+
import org.basex.util.list.*;
1617

1718
/**
1819
* This class is an entry point for evaluating XQuery strings.
@@ -217,7 +218,7 @@ public Serializer serializer(final OutputStream os) throws IOException, QueryExc
217218
* @param file file name
218219
*/
219220
public void module(final String uri, final String file) {
220-
qc.modDeclared.put(token(uri), token(file));
221+
qc.modDeclared.computeIfAbsent(token(uri), TokenList::new).add(token(file));
221222
}
222223

223224
@Override

basex-core/src/main/java/org/basex/query/func/fn/FnLoadXQueryModule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public XQMap item(final QueryContext qc, final InputInfo ii) throws QueryExcepti
6464
} else {
6565
final StringList locs = new StringList().add(hints);
6666
if(locs.isEmpty()) {
67-
final byte[] loc = qc.modDeclared.get(modUri);
68-
if(loc != null) {
69-
locs.add(Token.string(loc));
67+
final TokenList files = qc.modDeclared.get(modUri);
68+
if(files != null) {
69+
for(final byte[] file : files) locs.add(Token.string(file));
7070
} else {
7171
final String path = repoFilePath(modUri, qc.context);
7272
if(path != null) locs.add(path);

0 commit comments

Comments
 (0)