Example usage for org.springframework.jdbc.object SqlFunction setResultType

List of usage examples for org.springframework.jdbc.object SqlFunction setResultType

Introduction

In this page you can find the example usage for org.springframework.jdbc.object SqlFunction setResultType.

Prototype

public void setResultType(Class<T> resultType) 

Source Link

Document

Specify the type that the result object is required to match.

Usage

From source file:org.springframework.jdbc.object.SqlFunctionTests.java

public void testFunctionWithStringConvertedResult() throws SQLException {
    ctrlResultSetMetaData = MockControl.createControl(ResultSetMetaData.class);
    mockResultSetMetaData = (ResultSetMetaData) ctrlResultSetMetaData.getMock();
    mockResultSetMetaData.getColumnCount();
    ctrlResultSetMetaData.setReturnValue(1);

    mockResultSet.getMetaData();/*from   w w  w . j av  a  2  s  .c o m*/
    ctrlResultSet.setReturnValue(mockResultSetMetaData, 1);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getString(1);
    ctrlResultSet.setReturnValue("14");
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(1), Types.INTEGER);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.setString(2, "rod");
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(FUNCTION_MIXED);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    SqlFunction function = new SqlFunction(mockDataSource, FUNCTION_MIXED);
    function.setTypes(new int[] { Types.INTEGER, Types.VARCHAR });
    function.setResultType(String.class);
    function.compile();

    String result = (String) function.runGeneric(new Object[] { new Integer(1), "rod" });
    assertTrue("Function returned value 14", "14".equals(result));
}