Search in sources :

Example 41 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class BaseConversionFunctionsTest method octalToInt.

@Test
public void octalToInt() throws XPathException {
    final XQueryContext mckContext = EasyMock.createMock(XQueryContext.class);
    final BaseConversionFunctions baseConversionFunctions = new BaseConversionFunctions(mckContext, BaseConversionFunctions.FNS_OCTAL_TO_INT);
    Sequence[] args = { new StringValue("0777") };
    final Sequence result = baseConversionFunctions.eval(args, null);
    assertEquals(1, result.getItemCount());
    assertEquals(511, (int) result.itemAt(0).toJavaObject(int.class));
}
Also used : XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue) Test(org.junit.Test)

Example 42 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class BaseConversionFunctionsTest method intToOctal.

/**
 * Test of eval method, of class PermissionsFunctions.
 */
@Test
public void intToOctal() throws XPathException {
    final XQueryContext mckContext = EasyMock.createMock(XQueryContext.class);
    final BaseConversionFunctions baseConversionFunctions = new BaseConversionFunctions(mckContext, BaseConversionFunctions.FNS_INT_TO_OCTAL);
    Sequence[] args = { new IntegerValue(511) };
    final Sequence result = baseConversionFunctions.eval(args, null);
    assertEquals(1, result.getItemCount());
    assertEquals("0777", result.itemAt(0).toString());
}
Also used : IntegerValue(org.exist.xquery.value.IntegerValue) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) Test(org.junit.Test)

Example 43 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class ExecuteFunctionTest method testStringEncoding.

@Test
public void testStringEncoding() throws SQLException, XPathException {
    // mocks a simple SQL query returning a single string and checks the result
    XQueryContext context = new XQueryContextStub();
    ExecuteFunction execute = new ExecuteFunction(context, signatureByArity(ExecuteFunction.FS_EXECUTE, functionName, 3));
    final String sql = "SELECT NAME FROM BLA";
    final String testValue = "<&>";
    // create mock objects
    Connection connection = mock(Connection.class);
    Statement stmt = mock(Statement.class);
    ResultSet rs = mock(ResultSet.class);
    ResultSetMetaData rsmd = mock(ResultSetMetaData.class);
    Object[] mocks = new Object[] { connection, stmt, rs, rsmd };
    // mock behavior
    expect(connection.createStatement()).andReturn(stmt);
    expect(stmt.execute(sql)).andReturn(true);
    expect(stmt.getResultSet()).andReturn(rs);
    expect(stmt.getUpdateCount()).andReturn(-1);
    stmt.close();
    expect(rs.getMetaData()).andReturn(rsmd);
    expect(rs.next()).andReturn(true).andReturn(false);
    expect(rs.getRow()).andReturn(1);
    expect(rs.getString(1)).andReturn(testValue);
    expect(rs.wasNull()).andReturn(false);
    rs.close();
    expect(rsmd.getColumnCount()).andStubReturn(1);
    expect(rsmd.getColumnLabel(1)).andStubReturn("NAME");
    expect(rsmd.getColumnTypeName(1)).andStubReturn("VARCHAR(100)");
    expect(rsmd.getColumnType(1)).andStubReturn(Types.VARCHAR);
    replay(mocks);
    // register mocked connection
    final long connId = SQLModule.storeConnection(context, connection);
    // execute function
    Sequence res = execute.eval(new Sequence[] { new IntegerValue(connId), new StringValue(sql), 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:result", root.getNodeName());
    Node row = root.getFirstChild();
    assertEquals("sql:row", row.getNodeName());
    Node col = row.getFirstChild();
    assertEquals("sql:field", col.getNodeName());
    assertEquals(testValue, col.getTextContent());
}
Also used : Node(org.w3c.dom.Node) XQueryContext(org.exist.xquery.XQueryContext) Test(org.junit.Test)

Example 44 with XQueryContext

use of org.exist.xquery.XQueryContext 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 45 with XQueryContext

use of org.exist.xquery.XQueryContext 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)

Aggregations

XQueryContext (org.exist.xquery.XQueryContext)70 CompiledXQuery (org.exist.xquery.CompiledXQuery)37 Sequence (org.exist.xquery.value.Sequence)34 XQuery (org.exist.xquery.XQuery)33 DBBroker (org.exist.storage.DBBroker)24 Test (org.junit.Test)23 XPathException (org.exist.xquery.XPathException)18 BrokerPool (org.exist.storage.BrokerPool)17 IOException (java.io.IOException)11 Source (org.exist.source.Source)10 XQueryPool (org.exist.storage.XQueryPool)10 Node (org.w3c.dom.Node)10 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)8 PermissionDeniedException (org.exist.security.PermissionDeniedException)8 AnyURIValue (org.exist.xquery.value.AnyURIValue)8 URI (java.net.URI)7 StringValue (org.exist.xquery.value.StringValue)7 InputSource (org.xml.sax.InputSource)7 EXistException (org.exist.EXistException)6 QName (org.exist.dom.QName)6