Search in sources :

Example 21 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class ImportModuleTest method noSuchModuleWithoutLocationHint.

/**
 * Checks that XQST0059 is raised if the module to be imported cannot be found (when there is no location hint).
 */
@Test
public void noSuchModuleWithoutLocationHint() throws EXistException, IOException, PermissionDeniedException {
    final String query = "import module namespace impl = \"http://example.com/impl\";\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 22 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class ImportModuleTest method emptyNamespace.

/**
 * Checks that XQST0088 is raised if the namespace part of an `import module` statement is empty.
 */
@Test
public void emptyNamespace() 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 = \"\" 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
        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 XQST0088");
        } catch (final XPathException e) {
            transaction.commit();
            assertEquals(ErrorCodes.XQST0088, 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 23 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class ImportModuleTest method prefixSameAsOtherNamespaceDeclaration.

/**
 * Checks that XQST0033 is raised if the prefix part of an `import module` statement is the same as the prefix
 * of a namespace declaration within the same module.
 */
@Test
public void prefixSameAsOtherNamespaceDeclaration() throws EXistException, IOException, SAXException, PermissionDeniedException, LockException {
    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 = "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 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 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 24 with Source

use of org.exist.source.Source 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 25 with Source

use of org.exist.source.Source 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)

Aggregations

Source (org.exist.source.Source)83 StringSource (org.exist.source.StringSource)62 Sequence (org.exist.xquery.value.Sequence)43 BrokerPool (org.exist.storage.BrokerPool)42 DBBroker (org.exist.storage.DBBroker)42 Txn (org.exist.storage.txn.Txn)40 Test (org.junit.Test)35 StringInputSource (org.exist.util.StringInputSource)32 DBSource (org.exist.source.DBSource)23 IOException (java.io.IOException)19 PermissionDeniedException (org.exist.security.PermissionDeniedException)19 InputSource (org.xml.sax.InputSource)15 EXistException (org.exist.EXistException)12 XmldbURI (org.exist.xmldb.XmldbURI)11 XQueryPool (org.exist.storage.XQueryPool)9 XQueryContext (org.exist.xquery.XQueryContext)9 FileSource (org.exist.source.FileSource)8 CompiledXQuery (org.exist.xquery.CompiledXQuery)8 XQuery (org.exist.xquery.XQuery)8 Collection (org.exist.collections.Collection)7