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));
}
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());
}
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());
}
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);
}
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"));
}
}
Aggregations