Search in sources :

Example 91 with DBBroker

use of org.exist.storage.DBBroker 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();
    }
}
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 92 with DBBroker

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

the class XQueryContextTest method prepareForExecution_setsUserFromSession.

@Test
public void prepareForExecution_setsUserFromSession() {
    // partial mock context
    XQueryContext context = EasyMock.createMockBuilder(XQueryContext.class).withConstructor().addMockedMethod("getUserFromHttpSession").addMockedMethod("getBroker").createMock();
    DBBroker mockBroker = createMock(DBBroker.class);
    Subject mockSubject = createMock(Subject.class);
    // expectations
    expect(context.getUserFromHttpSession()).andReturn(mockSubject);
    expect(context.getBroker()).andReturn(mockBroker).times(2);
    mockBroker.pushSubject(mockSubject);
    // test
    replay(context);
    context.prepareForExecution();
    verify(context);
}
Also used : DBBroker(org.exist.storage.DBBroker) Subject(org.exist.security.Subject) Test(org.junit.Test)

Example 93 with DBBroker

use of org.exist.storage.DBBroker 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);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) CollectionConfigurationManager(org.exist.collections.CollectionConfigurationManager) BrokerPool(org.exist.storage.BrokerPool)

Example 94 with DBBroker

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

the class LuceneMatchListenerTest method inlineMatchNodes_whenIndenting.

@Test
public void inlineMatchNodes_whenIndenting() throws EXistException, PermissionDeniedException, XPathException, SAXException, CollectionConfigurationException, LockException, IOException {
    configureAndStore(CONF5, XML2);
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        final String query = "declare namespace tei=\"http://www.tei-c.org/ns/1.0\";" + "//tei:p[.//tei:w[ft:query(., <query><bool><term>дознајем</term></bool></query>)]] ! util:expand(.)";
        final Sequence seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        final String result = queryResult2String(broker, seq, true);
        final String expected = "<p xmlns=\"http://www.tei-c.org/ns/1.0\">\n" + "    <s type=\"combo\">\n" + "        <w lemma=\"из\">из</w>\n" + "        <w>новина</w>\n" + "        <w lemma=\"и\">и</w>\n" + "        <w lemma=\"од\">од</w>\n" + "        <lb/>\n" + "        <pb n=\"32\"/>\n" + "        <w>других</w>\n" + "        <w lemma=\"човек\">људи</w>\n" + "        <w>" + MATCH_START + "дознајем" + MATCH_END + "</w>, <w xml:id=\"VSK.P13.t1.p4.w205\" lemma=\"ма\">ма</w>\n" + "        <w>се</w>\n" + "        <w lemma=\"не\">не</w>\n" + "        <w>прорезује</w>\n" + "        <w>право</w>\n" + "        <w lemma=\"по\">по</w>\n" + "        <w>имућству</w>, <w xml:id=\"VSK.P13.t1.p4.w219\" lemma=\"те\">те</w>\n" + "        <w>се</w>\n" + "        <w>на</w>\n" + "        <w lemma=\"то\">то</w>\n" + "        <w>видим</w>\n" + "        <w>многи</w>\n" + "        <w>љуте</w>.</s>\n" + "</p>";
        XMLAssert.assertEquals(expected, result);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 95 with DBBroker

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

the class LuceneMatchListenerTest method matchInAncestor.

@Test
public void matchInAncestor() throws EXistException, PermissionDeniedException, XPathException, SAXException, IOException, XpathException, LockException, CollectionConfigurationException {
    configureAndStore(CONF1, XML);
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        Sequence seq = xquery.execute(broker, "//para[ft:query(., 'mixed')]/hi", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        String result = queryResult2String(broker, seq);
        XMLAssert.assertXpathEvaluatesTo("1", "count(//exist:match)", result);
        seq = xquery.execute(broker, "//para[ft:query(., 'nested')]/note", null);
        assertNotNull(seq);
        assertEquals(1, seq.getItemCount());
        result = queryResult2String(broker, seq);
        XMLAssert.assertXpathEvaluatesTo("1", "count(//hi/exist:match)", result);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Aggregations

DBBroker (org.exist.storage.DBBroker)468 BrokerPool (org.exist.storage.BrokerPool)304 Txn (org.exist.storage.txn.Txn)219 Sequence (org.exist.xquery.value.Sequence)185 Test (org.junit.Test)170 XQuery (org.exist.xquery.XQuery)108 Collection (org.exist.collections.Collection)93 TransactionManager (org.exist.storage.txn.TransactionManager)70 EXistException (org.exist.EXistException)66 StringInputSource (org.exist.util.StringInputSource)66 PermissionDeniedException (org.exist.security.PermissionDeniedException)44 Source (org.exist.source.Source)42 StringSource (org.exist.source.StringSource)41 XmldbURI (org.exist.xmldb.XmldbURI)41 CompiledXQuery (org.exist.xquery.CompiledXQuery)39 IOException (java.io.IOException)38 QName (org.exist.dom.QName)37 LockedDocument (org.exist.dom.persistent.LockedDocument)36 Database (org.exist.Database)35 XPathException (org.exist.xquery.XPathException)30