use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class ImportModuleTest method noSuchModuleWithLocationHint.
/**
* Checks that XQST0059 is raised if the module to be imported cannot be found (when there is a location hint).
*/
@Test
public void noSuchModuleWithLocationHint() throws EXistException, IOException, PermissionDeniedException {
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()) {
// 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 XQST0059");
} catch (final XPathException e) {
transaction.commit();
assertEquals(ErrorCodes.XQST0059, e.getErrorCode());
}
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class ImportModuleTest method functionSameAsOtherModule.
/**
* Checks that XQST0034 is raised if two modules contain a function of the same name and arity.
*/
@Test
public void functionSameAsOtherModule() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException {
final String module1 = "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 module2 = "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\", \"xmldb:exist:///db/impl2.xqm\";\n" + "<result>\n" + " <impl1>{impl:f1(\"to impl1\")}</impl1>" + " <impl2>{impl:f1(\"to impl1\")}</impl2>" + "</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 XQST0034");
} catch (final XPathException e) {
transaction.commit();
assertEquals(ErrorCodes.XQST0034, e.getErrorCode());
assertTrue(e.getMessage().contains("{http://example.com/impl}f1#1"));
}
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class ImportModuleTest method functionDuplicateNsInMainModule.
/**
* Checks that XQST0034 is raised if a main module contains two functions of the same name and arity.
*/
@Test
public void functionDuplicateNsInMainModule() throws EXistException, IOException, PermissionDeniedException, LockException, TriggerException, XPathException {
final String query = "declare namespace ns1 = 'http://ns1';\n" + "declare namespace ns12 = 'http://ns1';\n" + "\n" + "declare function ns1:f1($a as xs:string) as xs:string {\n" + " <first>{$a}</first>\n" + "};\n" + "\n" + "declare function ns12:f1($a as xs:string) as xs:string {\n" + " <second>{$a}</second>\n" + "};\n" + "\n" + "<result>\n" + " <impl1>{ns1:f1(\"to impl1\")}</impl1>" + " <impl2>{ns12:f1(\"to impl1\")}</impl2>" + "</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()) {
// 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://ns1}f1#1"));
}
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class ImportModuleTest method variablesCompositeFromMultipleLocationHints.
/**
* Imports multiple XQuery Library Modules containing variables into the same target namespace.
*/
@Test
public void variablesCompositeFromMultipleLocationHints() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, XPathException {
final String module1 = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare variable $impl:v1 := \"impl1\";\n";
final String module2 = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare variable $impl:v2 := \"impl2\";\n";
final String module3 = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare variable $impl:v3 := \"impl3\";\n";
final String query = "import module namespace impl = \"http://example.com/impl\"" + " at \"xmldb:exist:///db/impl1.xqm\", \"xmldb:exist:///db/impl2.xqm\", \"xmldb:exist:///db/impl3.xqm\";\n" + "<result>\n" + " <impl1>{$impl:v1}</impl1>" + " <impl2>{$impl:v2}</impl2>" + " <impl3>{$impl:v3}</impl3>" + "</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
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>impl1</impl1>" + "<impl2>impl2</impl2>" + "<impl3>impl3</impl3>" + "</result>").build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
transaction.commit();
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class ImportModuleTest method prefixSameAsOtherImport.
/**
* Checks that XQST0033 is raised if the prefix part of an `import module` statement is the same as the prefix
* of another `import module` statement within the same module.
*/
@Test
public void prefixSameAsOtherImport() throws EXistException, IOException, SAXException, PermissionDeniedException, LockException {
final String module1 = "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 module2 = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare function impl:f1($a as xs:string, $b as xs:string) as xs:string {\n" + " fn:concat($a, ' ', $b)\n" + "};\n";
final String query = "import module namespace impl = \"http://example.com/impl\" at \"xmldb:exist:///db/impl1.xqm\";\n" + "import module namespace impl = \"http://example.com/impl\" at \"xmldb:exist:///db/impl2.xqm\";\n" + "<result>\n" + " <impl1>{impl:f1(\"to impl1\")}</impl1>" + " <impl2>{impl:f1(\"to\", \"impl1\")}</impl2>" + "</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 XQST0033");
} catch (final XPathException e) {
transaction.commit();
assertEquals(ErrorCodes.XQST0033, e.getErrorCode());
}
}
}
Aggregations