Search in sources :

Example 66 with Source

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

the class ImportModuleTest method functionDuplicateInMainModule.

/**
 * Checks that XQST0034 is raised if a main module contains two functions of the same name and arity.
 */
@Test
public void functionDuplicateInMainModule() throws EXistException, IOException, PermissionDeniedException, LockException, TriggerException, XPathException {
    final String query = "declare function local:f1($a as xs:string) as xs:string {\n" + "    <first>{$a}</first>\n" + "};\n" + "\n" + "declare function local:f1($a as xs:string) as xs:string {\n" + "    <second>{$a}</second>\n" + "};\n" + "\n" + "<result>\n" + "    <impl1>{local:f1(\"to impl1\")}</impl1>" + "    <impl2>{local: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://www.w3.org/2005/xquery-local-functions}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 67 with Source

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

the class ImportModuleTest method namespaceSameAsOtherImport.

/**
 * Checks that XQST0047 is raised if the namespace part of an `import module` statement is the same as the namespace
 * of another `import module` statement within the same module.
 */
@Test
public void namespaceSameAsOtherImport() 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, $b as xs:string) as xs:string {\n" + "    fn:concat($a, ' ', $b)\n" + "};\n";
    final String query = "import module namespace impl1 = \"http://example.com/impl\"" + "        at \"xmldb:exist:///db/impl1.xqm\";\n" + "import module namespace impl2 = \"http://example.com/impl\"" + "        at \"xmldb:exist:///db/impl2.xqm\";\n" + "<result>\n" + "    <impl1>{impl1:f1(\"to impl1\")}</impl1>" + "    <impl2>{impl2:f1(\"to\", \"impl2\")}</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 XQST0047");
        } catch (final XPathException e) {
            transaction.commit();
            assertEquals(ErrorCodes.XQST0047, 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 Source

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

the class ImportModuleTest method functionsCompositeFromMultipleLocationHints.

/**
 * Imports multiple XQuery Library Modules containing functions into the same target namespace.
 */
@Test
public void functionsCompositeFromMultipleLocationHints() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, XPathException {
    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 module3 = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare function impl:f2($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\", \"xmldb:exist:///db/impl3.xqm\";\n" + "<result>\n" + "    <impl1>{impl:f1(\"to impl1\")}</impl1>" + "    <impl2>{impl:f1(\"to\", \"impl2\")}</impl2>" + "    <impl3>{impl:f2(\"to impl3\")}</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>to impl1</impl1>" + "<impl2>to impl2</impl2>" + "<impl3>to impl3</impl3>" + "</result>").build();
        final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
        assertFalse(diff.toString(), diff.hasDifferences());
        transaction.commit();
    }
}
Also used : Diff(org.xmlunit.diff.Diff) Element(org.w3c.dom.Element) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) StringSource(org.exist.source.StringSource) StringInputSource(org.exist.util.StringInputSource) Source(org.exist.source.Source) DBBroker(org.exist.storage.DBBroker) StringSource(org.exist.source.StringSource) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 69 with Source

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

the class ImportModuleTest method variablesSingleLocationHint.

/**
 * Imports a single XQuery Library Module containing variables into a target namespace.
 */
@Test
public void variablesSingleLocationHint() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, XPathException {
    final String module = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare variable $impl:v1 := \"impl1\";\n";
    final String query = "import module namespace impl = \"http://example.com/impl\" at \"xmldb:exist:///db/impl1.xqm\";\n" + "<result>\n" + "    <impl1>{$impl:v1}</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>impl1</impl1></result>").build();
        final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
        assertFalse(diff.toString(), diff.hasDifferences());
        transaction.commit();
    }
}
Also used : Diff(org.xmlunit.diff.Diff) Element(org.w3c.dom.Element) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) StringSource(org.exist.source.StringSource) StringInputSource(org.exist.util.StringInputSource) Source(org.exist.source.Source) DBBroker(org.exist.storage.DBBroker) StringSource(org.exist.source.StringSource) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 70 with Source

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

the class ImportModuleTest method variablesCompositeFromMultipleLocationHintsWithDifferingPrefixes.

/**
 * Imports multiple XQuery Library Modules into the same target namespace.
 */
@Test
public void variablesCompositeFromMultipleLocationHintsWithDifferingPrefixes() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, XPathException {
    final String module1 = "xquery version \"1.0\";\n" + "module namespace impl1 = \"http://example.com/impl\";\n" + "declare variable $impl1:v1 := \"impl1\";\n";
    final String module2 = "xquery version \"1.0\";\n" + "module namespace impl2 = \"http://example.com/impl\";\n" + "declare variable $impl2:v2 := \"impl2\";\n";
    final String module3 = "xquery version \"1.0\";\n" + "module namespace impl3 = \"http://example.com/impl\";\n" + "declare variable $impl3: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();
    }
}
Also used : Diff(org.xmlunit.diff.Diff) Element(org.w3c.dom.Element) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) StringSource(org.exist.source.StringSource) StringInputSource(org.exist.util.StringInputSource) Source(org.exist.source.Source) DBBroker(org.exist.storage.DBBroker) StringSource(org.exist.source.StringSource) BrokerPool(org.exist.storage.BrokerPool) 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