Search in sources :

Example 6 with ElementImpl

use of org.exist.dom.memtree.ElementImpl in project exist by eXist-db.

the class InspectModuleTest method xqDoc_onAnnotatedFunction.

@Test
public void xqDoc_onAnnotatedFunction() throws PermissionDeniedException, XPathException, EXistException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final String query = "import module namespace inspect = \"http://exist-db.org/xquery/inspection\";\n" + "inspect:inspect-module(xs:anyURI(\"xmldb:exist://" + TEST_COLLECTION.append(TEST_MODULE).toCollectionPathURI() + "\"))\n" + "/function[@name eq \"x:fun4\"]";
        final Sequence result = xqueryService.execute(broker, query, null);
        assertNotNull(result);
        assertEquals(1, result.getItemCount());
        final Item item1 = result.itemAt(0);
        assertTrue(item1 instanceof ElementImpl);
        final Element function = (Element) item1;
        final NodeList descriptions = function.getElementsByTagName("description");
        assertEquals(1, descriptions.getLength());
        assertEquals("An annotated function.", descriptions.item(0).getFirstChild().getNodeValue());
        final NodeList annotations = function.getElementsByTagName("annotation");
        assertEquals(2, annotations.getLength());
        assertEquals("public", ((Element) annotations.item(0)).getAttribute("name"));
        assertEquals("x:path", ((Element) annotations.item(1)).getAttribute("name"));
        assertEquals("/x/y/z", annotations.item(1).getFirstChild().getFirstChild().getNodeValue());
        final NodeList arguments = function.getElementsByTagName("argument");
        assertEquals(0, arguments.getLength());
        final NodeList returns = function.getElementsByTagName("returns");
        assertEquals(1, returns.getLength());
        assertEquals("another result", returns.item(0).getFirstChild().getNodeValue());
        transaction.commit();
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) ElementImpl(org.exist.dom.memtree.ElementImpl) XQuery(org.exist.xquery.XQuery) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 7 with ElementImpl

use of org.exist.dom.memtree.ElementImpl in project exist by eXist-db.

the class InspectModuleTest method xqDoc_multilineDesciption.

@Test
public void xqDoc_multilineDesciption() throws PermissionDeniedException, XPathException, EXistException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final String query = "import module namespace inspect = \"http://exist-db.org/xquery/inspection\";\n" + "inspect:inspect-module(xs:anyURI(\"xmldb:exist://" + TEST_COLLECTION.append(TEST_MODULE).toCollectionPathURI() + "\"))\n" + "/function[@name eq \"x:fun3\"]";
        final Sequence result = xqueryService.execute(broker, query, null);
        assertNotNull(result);
        assertEquals(1, result.getItemCount());
        final Item item1 = result.itemAt(0);
        assertTrue(item1 instanceof ElementImpl);
        final Element function = (Element) item1;
        final NodeList descriptions = function.getElementsByTagName("description");
        assertEquals(1, descriptions.getLength());
        assertEquals("This is a multiline description and therefore\n spans multiple\n lines.", descriptions.item(0).getFirstChild().getNodeValue());
        final NodeList arguments = function.getElementsByTagName("argument");
        assertEquals(0, arguments.getLength());
        final NodeList returns = function.getElementsByTagName("returns");
        assertEquals(1, returns.getLength());
        assertEquals("another result", returns.item(0).getFirstChild().getNodeValue());
        transaction.commit();
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) ElementImpl(org.exist.dom.memtree.ElementImpl) XQuery(org.exist.xquery.XQuery) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 8 with ElementImpl

use of org.exist.dom.memtree.ElementImpl in project exist by eXist-db.

the class ExecuteFunctionTest method testEmptyParameters.

@Test
public void testEmptyParameters() throws SQLException, XPathException {
    // mocks a simple SQL prepared statement with one parameter
    // is filled with an empty xsl:param element
    XQueryContext context = new XQueryContextStub();
    ExecuteFunction execute = new ExecuteFunction(context, signatureByArity(ExecuteFunction.FS_EXECUTE, functionName, 3));
    // this is what an empty xsl:param element of type varchar should use to fill prepared statement parameters
    final String emptyStringValue = "";
    final Integer emptyIntValue = null;
    final String sql = "SELECT ? AS COL1, ? AS COL2";
    // create mock objects
    Connection connection = mock(Connection.class);
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    ResultSet rs = mock(ResultSet.class);
    ResultSetMetaData rsmd = mock(ResultSetMetaData.class);
    Object[] mocks = new Object[] { connection, preparedStatement, rs, rsmd };
    // register mocked connection and prepared statement
    final long connId = SQLModule.storeConnection(context, connection);
    final long stmtId = SQLModule.storePreparedStatement(context, new PreparedStatementWithSQL(sql, preparedStatement));
    // mock behavior
    preparedStatement.setObject(1, emptyStringValue, Types.VARCHAR);
    preparedStatement.setObject(2, emptyIntValue, Types.INTEGER);
    expect(preparedStatement.getConnection()).andReturn(connection);
    expect(preparedStatement.execute()).andReturn(true);
    expect(preparedStatement.getResultSet()).andReturn(rs);
    expect(preparedStatement.getUpdateCount()).andReturn(-1);
    expect(rs.next()).andReturn(true).andReturn(false);
    expect(rs.getRow()).andReturn(1);
    expect(rs.getString(1)).andReturn(emptyStringValue);
    expect(rs.wasNull()).andReturn(emptyStringValue == null);
    expect(rs.getString(2)).andReturn(null);
    expect(rs.wasNull()).andReturn(emptyIntValue == null);
    expect(rs.getMetaData()).andStubReturn(rsmd);
    expect(rsmd.getColumnCount()).andStubReturn(2);
    expect(rsmd.getColumnLabel(1)).andStubReturn("COL1");
    expect(rsmd.getColumnLabel(2)).andStubReturn("COL2");
    expect(rsmd.getColumnTypeName(1)).andStubReturn("VARCHAR");
    expect(rsmd.getColumnTypeName(2)).andStubReturn("INTEGER");
    expect(rsmd.getColumnType(1)).andStubReturn(Types.VARCHAR);
    expect(rsmd.getColumnType(2)).andStubReturn(Types.INTEGER);
    rs.close();
    replay(mocks);
    // execute function
    MemTreeBuilder paramBuilder = new MemTreeBuilder(context);
    paramBuilder.startDocument();
    paramBuilder.startElement(new QName("parameters", SQLModule.NAMESPACE_URI), null);
    paramBuilder.startElement(new QName("param", SQLModule.NAMESPACE_URI), null);
    paramBuilder.addAttribute(new QName("type", SQLModule.NAMESPACE_URI), "varchar");
    paramBuilder.endElement();
    paramBuilder.startElement(new QName("param", SQLModule.NAMESPACE_URI), null);
    paramBuilder.addAttribute(new QName("type", SQLModule.NAMESPACE_URI), "integer");
    paramBuilder.endElement();
    paramBuilder.endElement();
    paramBuilder.endDocument();
    final ElementImpl sqlParams = (ElementImpl) paramBuilder.getDocument().getFirstChild();
    execute.eval(new Sequence[] { new IntegerValue(connId), new IntegerValue(stmtId), sqlParams, new BooleanValue(false) }, Sequence.EMPTY_SEQUENCE);
    // assert expectations
    verify(preparedStatement);
}
Also used : QName(org.exist.dom.QName) XQueryContext(org.exist.xquery.XQueryContext) ElementImpl(org.exist.dom.memtree.ElementImpl) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) Test(org.junit.Test)

Example 9 with ElementImpl

use of org.exist.dom.memtree.ElementImpl in project exist by eXist-db.

the class ExecuteFunctionTest method testMissingParamType.

@Test
public void testMissingParamType() throws SQLException {
    // mocks a simple SQL prepared statement with one parameter that lacks a type attribute.
    // This should throw an informative error.
    XQueryContext context = new XQueryContextStub();
    ExecuteFunction execute = new ExecuteFunction(context, signatureByArity(ExecuteFunction.FS_EXECUTE, functionName, 3));
    final String sql = "SELECT ?";
    // create mock objects
    Connection connection = mock(Connection.class);
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    Object[] mocks = new Object[] { connection, preparedStatement };
    // register mocked connection and prepared statement
    final long connId = SQLModule.storeConnection(context, connection);
    final long stmtId = SQLModule.storePreparedStatement(context, new PreparedStatementWithSQL(sql, preparedStatement));
    // mock behavior
    expect(preparedStatement.getConnection()).andReturn(connection);
    // no behavior necessary - error should be thrown before first call
    replay(mocks);
    // execute function
    MemTreeBuilder paramBuilder = new MemTreeBuilder(context);
    paramBuilder.startDocument();
    paramBuilder.startElement(new QName("parameters", SQLModule.NAMESPACE_URI), null);
    paramBuilder.startElement(new QName("param", SQLModule.NAMESPACE_URI), null);
    paramBuilder.endElement();
    paramBuilder.endElement();
    paramBuilder.endDocument();
    final ElementImpl sqlParams = (ElementImpl) paramBuilder.getDocument().getFirstChild();
    try {
        execute.eval(new Sequence[] { new IntegerValue(connId), new IntegerValue(stmtId), sqlParams, new BooleanValue(false) }, Sequence.EMPTY_SEQUENCE);
        fail("This should have thrown");
    } catch (XPathException e) {
        assertTrue(e.getMessage().contains("<sql:param> must contain attribute sql:type"));
    }
}
Also used : XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) XQueryContext(org.exist.xquery.XQueryContext) ElementImpl(org.exist.dom.memtree.ElementImpl) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) Test(org.junit.Test)

Example 10 with ElementImpl

use of org.exist.dom.memtree.ElementImpl in project exist by eXist-db.

the class ExecuteFunctionTest method testSQLException.

@Test
public void testSQLException() throws SQLException, XPathException {
    // mocks a simple SQL prepared statement with one parameter that fails on execution
    // and verifies the error message
    // the parameter is filled with an empty sql:param element
    XQueryContext context = new XQueryContextStub();
    ExecuteFunction execute = new ExecuteFunction(context, signatureByArity(ExecuteFunction.FS_EXECUTE, functionName, 3));
    final String sql = "SELECT ?";
    final String test_message = "SQL ERROR";
    final String test_sqlState = "SQL STATE";
    // create mock objects
    Connection connection = mock(Connection.class);
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    Object[] mocks = new Object[] { connection, preparedStatement };
    // register mocked connection and prepared statement
    final long connId = SQLModule.storeConnection(context, connection);
    final long stmtId = SQLModule.storePreparedStatement(context, new PreparedStatementWithSQL(sql, preparedStatement));
    // mock behavior
    expect(preparedStatement.getConnection()).andReturn(connection);
    preparedStatement.setObject(1, "", Types.VARCHAR);
    expect(preparedStatement.execute()).andThrow(new SQLException(test_message, test_sqlState));
    replay(mocks);
    // execute function
    MemTreeBuilder paramBuilder = new MemTreeBuilder(context);
    paramBuilder.startDocument();
    paramBuilder.startElement(new QName("parameters", SQLModule.NAMESPACE_URI), null);
    paramBuilder.startElement(new QName("param", SQLModule.NAMESPACE_URI), null);
    paramBuilder.addAttribute(new QName("type", SQLModule.NAMESPACE_URI), "varchar");
    paramBuilder.endElement();
    paramBuilder.endElement();
    paramBuilder.endDocument();
    final ElementImpl sqlParams = (ElementImpl) paramBuilder.getDocument().getFirstChild();
    Sequence res = execute.eval(new Sequence[] { new IntegerValue(connId), new IntegerValue(stmtId), sqlParams, new BooleanValue(false) }, Sequence.EMPTY_SEQUENCE);
    // assert expectations
    verify(mocks);
    assertEquals(1, res.getItemCount());
    assertEquals(Type.ELEMENT, res.getItemType());
    Node root = ((NodeValue) res.itemAt(0)).getNode();
    assertEquals("sql:exception", root.getNodeName());
    assertEquals(6, root.getChildNodes().getLength());
    Node node = root.getFirstChild();
    Node state = node;
    assertEquals("sql:state", state.getNodeName());
    assertEquals(test_sqlState, state.getTextContent());
    node = node.getNextSibling();
    Node message = node;
    assertEquals("sql:message", message.getNodeName());
    assertEquals(test_message, message.getTextContent());
    node = node.getNextSibling();
    Node stackTrace = node;
    assertEquals("sql:stack-trace", stackTrace.getNodeName());
    node = node.getNextSibling();
    Node sqlErr = node;
    assertEquals("sql:sql", sqlErr.getNodeName());
    assertEquals(sql, sqlErr.getTextContent());
    node = node.getNextSibling();
    Node parameters = node;
    assertEquals("sql:parameters", parameters.getNodeName());
    Node param1 = parameters.getFirstChild();
    assertEquals("sql:param", param1.getNodeName());
    assertEquals("varchar", param1.getAttributes().getNamedItemNS(SQLModule.NAMESPACE_URI, "type").getTextContent());
    assertEquals("", param1.getTextContent());
    node = node.getNextSibling();
    Node xquery = node;
    assertEquals("sql:xquery", xquery.getNodeName());
}
Also used : QName(org.exist.dom.QName) Node(org.w3c.dom.Node) XQueryContext(org.exist.xquery.XQueryContext) ElementImpl(org.exist.dom.memtree.ElementImpl) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) Test(org.junit.Test)

Aggregations

ElementImpl (org.exist.dom.memtree.ElementImpl)16 BrokerPool (org.exist.storage.BrokerPool)7 DBBroker (org.exist.storage.DBBroker)7 Sequence (org.exist.xquery.value.Sequence)7 QName (org.exist.dom.QName)6 XQuery (org.exist.xquery.XQuery)6 NodeList (org.w3c.dom.NodeList)6 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)5 Item (org.exist.xquery.value.Item)5 Test (org.junit.Test)5 Element (org.w3c.dom.Element)5 Txn (org.exist.storage.txn.Txn)4 XQueryContext (org.exist.xquery.XQueryContext)3 Properties (java.util.Properties)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 Collection (org.exist.collections.Collection)2 PermissionDeniedException (org.exist.security.PermissionDeniedException)2 XmldbURI (org.exist.xmldb.XmldbURI)2 Node (org.w3c.dom.Node)2 InputSource (org.xml.sax.InputSource)2