use of org.exist.source.Source in project exist by eXist-db.
the class ImportModuleTest method cyclic2.
/**
* Checks that XQST0093 is raised if there exists a sequence of modules M1 ... Mi ... M1.
*/
@Ignore("eXist-db does not have cyclic import checks, but it should!")
@Test
public void cyclic2() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException {
final String module1 = "xquery version \"1.0\";\n" + "module namespace impl1 = \"http://example.com/impl1\";\n" + "import module namespace impl2 = \"http://example.com/impl2\"" + " at \"xmldb:exist:///db/impl2.xqm\";\n" + "declare function impl1:f1($a as xs:string) as xs:string {\n" + " $a\n" + "};\n";
final String module2 = "xquery version \"1.0\";\n" + "module namespace impl2 = \"http://example.com/impl2\";\n" + "import module namespace impl3 = \"http://example.com/impl3\"" + " at \"xmldb:exist:///db/impl3.xqm\";\n" + "declare function impl2:f1($a as xs:string) as xs:string {\n" + " $a\n" + "};\n";
final String module3 = "xquery version \"1.0\";\n" + "module namespace impl3 = \"http://example.com/impl3\";\n" + "import module namespace impl1 = \"http://example.com/impl1\"" + " at \"xmldb:exist:///db/impl1.xqm\";\n" + "declare function impl3:f1($a as xs:string) as xs:string {\n" + " $a\n" + "};\n";
final String query = "import module namespace impl1 = \"http://example.com/impl1\"" + " at \"xmldb:exist:///db/impl1.xqm\";\n" + "<result>\n" + " <impl1>{impl1:f1(\"to impl1\")}</impl1>" + "</result>\n";
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final Source source = new StringSource(query);
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
// store modules
storeModules(broker, transaction, "/db", Tuple("impl1.xqm", module1), Tuple("impl2.xqm", module2), Tuple("impl3.xqm", module3));
// execute query
try {
final Tuple2<XQueryContext, Sequence> contextAndResult = withCompiledQuery(broker, source, compiledXQuery -> {
final Sequence result = executeQuery(broker, compiledXQuery);
return Tuple(compiledXQuery.getContext(), result);
});
transaction.commit();
fail("expected XQST0093");
} catch (final XPathException e) {
transaction.commit();
assertEquals(ErrorCodes.XQST0093, e.getErrorCode());
}
}
}
use of org.exist.source.Source in project exist by eXist-db.
the class ImportModuleTest method variableSameAsOtherModule.
/**
* Checks that XQST0049 is raised if two modules contain a variable of the same name.
*/
@Test
public void variableSameAsOtherModule() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException {
final String module1 = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare variable $impl:f1 := \"impl1\";\n";
final String module2 = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare variable $impl:f1 := \"impl2\";\n";
final String query = "import module namespace impl = \"http://example.com/impl\"" + " at \"xmldb:exist:///db/impl1.xqm\", \"xmldb:exist:///db/impl2.xqm\";\n" + "<result>\n" + " <impl1>{$impl:f1}</impl1>" + "</result>\n";
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final Source source = new StringSource(query);
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
// store modules
storeModules(broker, transaction, "/db", Tuple("impl1.xqm", module1), Tuple("impl2.xqm", module2));
// execute query
try {
final Tuple2<XQueryContext, Sequence> contextAndResult = withCompiledQuery(broker, source, compiledXQuery -> {
final Sequence result = executeQuery(broker, compiledXQuery);
return Tuple(compiledXQuery.getContext(), result);
});
transaction.commit();
fail("expected XQST0049");
} catch (final XPathException e) {
transaction.commit();
assertEquals(ErrorCodes.XQST0049, e.getErrorCode());
}
}
}
use of org.exist.source.Source in project exist by eXist-db.
the class ImportModuleTest method functionsSingleLocationHint.
/**
* Imports a single XQuery Library Module containing functions into a target namespace.
*/
@Test
public void functionsSingleLocationHint() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, XPathException {
final String module = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare function impl:f1($a as xs:string) as xs:string {\n" + " $a\n" + "};\n";
final String query = "import module namespace impl = \"http://example.com/impl\" at \"xmldb:exist:///db/impl1.xqm\";\n" + "<result>\n" + " <impl1>{impl:f1(\"to impl1\")}</impl1>" + "</result>\n";
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final Source source = new StringSource(query);
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
// store module
storeModules(broker, transaction, "/db", Tuple("impl1.xqm", module));
// execute query
final Tuple2<XQueryContext, Sequence> contextAndResult = withCompiledQuery(broker, source, compiledXQuery -> {
final Sequence result = executeQuery(broker, compiledXQuery);
return Tuple(compiledXQuery.getContext(), result);
});
// check that the result was correct
assertNotNull(contextAndResult._2);
assertEquals(1, contextAndResult._2.getItemCount());
final Element doc = (Element) contextAndResult._2.itemAt(0);
assertNotNull(doc);
final javax.xml.transform.Source actual = Input.fromDocument(doc.getOwnerDocument()).build();
final javax.xml.transform.Source expected = Input.fromString("<result><impl1>to impl1</impl1></result>").build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
transaction.commit();
}
}
use of org.exist.source.Source in project exist by eXist-db.
the class ImportModuleTest method prefixNot.
/**
* Executes an XQuery that imports a module with a specific prefix.
*
* @param prefix the prefix to use in the `import module` statement.
*
* @return the error code from executing the query, or null if there was no error.
*/
@Nullable
private ErrorCodes.ErrorCode prefixNot(final String prefix) throws EXistException, IOException, PermissionDeniedException, LockException, SAXException {
final String module = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare function impl:f1($a as xs:string) as xs:string {\n" + " $a\n" + "};\n";
final String query = "import module namespace " + prefix + " = \"http://example.com/impl\" at \"xmldb:exist:///db/impl1.xqm\";\n" + "<result>\n" + " <impl1>{" + prefix + ":f1(\"to impl1\")}</impl1>" + "</result>\n";
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final Source source = new StringSource(query);
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
// store module
storeModules(broker, transaction, "/db", Tuple("impl1.xqm", module));
// execute query
try {
final Tuple2<XQueryContext, Sequence> contextAndResult = withCompiledQuery(broker, source, compiledXQuery -> {
final Sequence result = executeQuery(broker, compiledXQuery);
return Tuple(compiledXQuery.getContext(), result);
});
transaction.commit();
return null;
} catch (final XPathException e) {
transaction.commit();
return e.getErrorCode();
}
}
}
use of org.exist.source.Source in project exist by eXist-db.
the class ImportModuleTest method functionSameAsImportingModule.
/**
* Checks that XQST0034 is raised if an imported module and the importing module contain a function of the same name and arity.
*/
@Test
public void functionSameAsImportingModule() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException {
final String module = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare function impl:f1($a as xs:string) as xs:string {\n" + " $a\n" + "};\n";
final String query = "import module namespace impl = \"http://example.com/impl\"" + " at \"xmldb:exist:///db/impl1.xqm\";\n" + "declare function impl:f1($a as xs:string) as xs:string {\n" + " $a\n" + "};\n" + "<result>\n" + " <impl1>{impl:f1(\"to impl1\")}</impl1>" + "</result>\n";
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final Source source = new StringSource(query);
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
// store module
storeModules(broker, transaction, "/db", Tuple("impl1.xqm", module));
// execute query
try {
final Tuple2<XQueryContext, Sequence> contextAndResult = withCompiledQuery(broker, source, compiledXQuery -> {
final Sequence result = executeQuery(broker, compiledXQuery);
return Tuple(compiledXQuery.getContext(), result);
});
transaction.commit();
fail("expected XQST0034");
} catch (final XPathException e) {
transaction.commit();
assertEquals(ErrorCodes.XQST0034, e.getErrorCode());
assertTrue(e.getMessage().contains("{http://example.com/impl}f1#1"));
}
}
}
Aggregations