Search in sources :

Example 66 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class ImportModuleTest method cyclic1.

/**
 * 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 cyclic1() 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 impl1 = \"http://example.com/impl1\"" + "        at \"xmldb:exist:///db/impl1.xqm\";\n" + "declare function impl2: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));
        // 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());
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringSource(org.exist.source.StringSource) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) StringSource(org.exist.source.StringSource) StringInputSource(org.exist.util.StringInputSource) Source(org.exist.source.Source) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 67 with Txn

use of org.exist.storage.txn.Txn in project exist by eXist-db.

the class ImportModuleTest method prefixSameAsModuleDeclaration.

/**
 * Checks that XQST0033 is raised if the prefix part of an `import module` statement is the same as the prefix
 * of the library module in which it resides.
 */
@Test
public void prefixSameAsModuleDeclaration() throws EXistException, IOException, SAXException, PermissionDeniedException, LockException {
    final String module1 = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "import module namespace impl = \"http://example.com/impl\" at \"xmldb:exist:///db/impl2.xqm\";\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 = "declare namespace impl = \"http://example.com/impl\";\n" + "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 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());
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringSource(org.exist.source.StringSource) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) StringSource(org.exist.source.StringSource) StringInputSource(org.exist.util.StringInputSource) Source(org.exist.source.Source) Test(org.junit.Test)

Example 68 with Txn

use of org.exist.storage.txn.Txn 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());
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringSource(org.exist.source.StringSource) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) StringSource(org.exist.source.StringSource) StringInputSource(org.exist.util.StringInputSource) Source(org.exist.source.Source) Test(org.junit.Test)

Example 69 with Txn

use of org.exist.storage.txn.Txn 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"));
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringSource(org.exist.source.StringSource) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) StringSource(org.exist.source.StringSource) StringInputSource(org.exist.util.StringInputSource) Source(org.exist.source.Source) Test(org.junit.Test)

Example 70 with Txn

use of org.exist.storage.txn.Txn 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"));
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringSource(org.exist.source.StringSource) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) StringSource(org.exist.source.StringSource) StringInputSource(org.exist.util.StringInputSource) Source(org.exist.source.Source) Test(org.junit.Test)

Aggregations

Txn (org.exist.storage.txn.Txn)358 DBBroker (org.exist.storage.DBBroker)215 BrokerPool (org.exist.storage.BrokerPool)179 Collection (org.exist.collections.Collection)162 TransactionManager (org.exist.storage.txn.TransactionManager)129 Sequence (org.exist.xquery.value.Sequence)84 StringInputSource (org.exist.util.StringInputSource)83 Test (org.junit.Test)80 XmldbURI (org.exist.xmldb.XmldbURI)55 EXistException (org.exist.EXistException)50 PermissionDeniedException (org.exist.security.PermissionDeniedException)38 Source (org.exist.source.Source)37 StringSource (org.exist.source.StringSource)37 DocumentImpl (org.exist.dom.persistent.DocumentImpl)35 InputSource (org.xml.sax.InputSource)33 XQuery (org.exist.xquery.XQuery)32 IOException (java.io.IOException)31 LockedDocument (org.exist.dom.persistent.LockedDocument)28 InputStream (java.io.InputStream)27 Path (java.nio.file.Path)24