use of org.exist.storage.txn.Txn 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.txn.Txn 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());
}
}
}
use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class ImportModuleTest method variablesBetweenModules.
@Test
public void variablesBetweenModules() throws EXistException, PermissionDeniedException, IOException, LockException, SAXException, XPathException {
final String module1 = "xquery version \"1.0\";\n" + "module namespace mod1 = \"http://example.com/mod1\";\n" + "declare variable $mod1:var1 := \"mod1 var1\";\n" + "declare function mod1:test() {\n" + " <function name=\"mod1:test\">\n" + " <variable name=\"mod1:var1\">{$mod1:var1}</variable>\n" + " </function>\n" + "};";
final String module2 = "xquery version \"1.0\";\n" + "module namespace mod2 = \"http://example.com/mod2\";\n" + "declare variable $mod2:var1 := \"mod2 var1\";\n" + "import module namespace mod1 = \"http://example.com/mod1\" at \"xmldb:exist:///db/mod1.xqm\";\n" + "declare function mod2:test() {\n" + " <function name=\"mod2:test\">\n" + " <variable name=\"mod2:var1\">{$mod2:var1}</variable>\n" + " { mod1:test() }\n" + " <variable name=\"mod1:var1\">{$mod1:var1}</variable>\n" + " </function>\n" + "};";
final String query = "xquery version \"1.0\";\n" + " import module namespace mod2 = 'http://example.com/mod2' at 'xmldb:exist:///db/mod2.xqm';\n" + "<result>\n" + " {mod2:test()}\n" + "</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("mod1.xqm", module1), Tuple("mod2.xqm", module2));
// 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>" + "<function name=\"mod2:test\">" + "<variable name=\"mod2:var1\">mod2 var1</variable>" + "<function name=\"mod1:test\">" + "<variable name=\"mod1:var1\">mod1 var1</variable>" + "</function>" + "<variable name=\"mod1:var1\">mod1 var1</variable>" + "</function>" + "</result>").build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
transaction.commit();
}
}
use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class LuceneMatchListenerTest method configureAndStore.
private void configureAndStore(final String config, final String data) throws EXistException, PermissionDeniedException, IOException, SAXException, CollectionConfigurationException, LockException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {
final Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
assertNotNull(root);
final CollectionConfigurationManager mgr = pool.getConfigurationManager();
mgr.addConfiguration(transaction, broker, root, config);
broker.storeDocument(transaction, XmldbURI.create("test_matches.xml"), new StringInputSource(data), MimeType.XML_TYPE, root);
transact.commit(transaction);
}
}
use of org.exist.storage.txn.Txn in project exist by eXist-db.
the class SerializeAttrMatchesTest method configureAndStore.
private DocumentSet configureAndStore(final String configuration, final String data, final String docName) throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, TriggerException, LockException, IOException {
final MutableDocumentSet docs = new DefaultDocumentSet();
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {
if (configuration != null) {
final CollectionConfigurationManager mgr = pool.getConfigurationManager();
mgr.addConfiguration(transaction, broker, test, configuration);
}
broker.storeDocument(transaction, XmldbURI.create(docName), new StringInputSource(data), MimeType.XML_TYPE, test);
docs.add(test.getDocument(broker, XmldbURI.create(docName)));
transact.commit(transaction);
}
return docs;
}
Aggregations