Search in sources :

Example 1 with SingleConnectionDataSource

use of cn.taketoday.jdbc.datasource.SingleConnectionDataSource in project today-infrastructure by TAKETODAY.

the class JdbcTemplateTests method testLeaveConnectionOpenOnRequest.

@Test
public void testLeaveConnectionOpenOnRequest() throws Exception {
    String sql = "SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3";
    given(this.resultSet.next()).willReturn(false);
    given(this.connection.isClosed()).willReturn(false);
    given(this.connection.createStatement()).willReturn(this.preparedStatement);
    // if close is called entire test will fail
    willThrow(new RuntimeException()).given(this.connection).close();
    SingleConnectionDataSource scf = new SingleConnectionDataSource(this.dataSource.getConnection(), false);
    this.template = new JdbcTemplate(scf, false);
    RowCountCallbackHandler rcch = new RowCountCallbackHandler();
    this.template.query(sql, rcch);
    verify(this.resultSet).close();
    verify(this.preparedStatement).close();
}
Also used : SingleConnectionDataSource(cn.taketoday.jdbc.datasource.SingleConnectionDataSource) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 2 with SingleConnectionDataSource

use of cn.taketoday.jdbc.datasource.SingleConnectionDataSource in project today-infrastructure by TAKETODAY.

the class RowMapperTests method setUp.

@BeforeEach
public void setUp() throws SQLException {
    given(connection.createStatement()).willReturn(statement);
    given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
    given(statement.executeQuery(anyString())).willReturn(resultSet);
    given(preparedStatement.executeQuery()).willReturn(resultSet);
    given(resultSet.next()).willReturn(true, true, false);
    given(resultSet.getString(1)).willReturn("tb1", "tb2");
    given(resultSet.getInt(2)).willReturn(1, 2);
    template.setDataSource(new SingleConnectionDataSource(connection, false));
    template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
    template.afterPropertiesSet();
}
Also used : SingleConnectionDataSource(cn.taketoday.jdbc.datasource.SingleConnectionDataSource) SQLStateSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with SingleConnectionDataSource

use of cn.taketoday.jdbc.datasource.SingleConnectionDataSource in project today-framework by TAKETODAY.

the class JdbcTemplateTests method testLeaveConnectionOpenOnRequest.

@Test
public void testLeaveConnectionOpenOnRequest() throws Exception {
    String sql = "SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3";
    given(this.resultSet.next()).willReturn(false);
    given(this.connection.isClosed()).willReturn(false);
    given(this.connection.createStatement()).willReturn(this.preparedStatement);
    // if close is called entire test will fail
    willThrow(new RuntimeException()).given(this.connection).close();
    SingleConnectionDataSource scf = new SingleConnectionDataSource(this.dataSource.getConnection(), false);
    this.template = new JdbcTemplate(scf, false);
    RowCountCallbackHandler rcch = new RowCountCallbackHandler();
    this.template.query(sql, rcch);
    verify(this.resultSet).close();
    verify(this.preparedStatement).close();
}
Also used : SingleConnectionDataSource(cn.taketoday.jdbc.datasource.SingleConnectionDataSource) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 4 with SingleConnectionDataSource

use of cn.taketoday.jdbc.datasource.SingleConnectionDataSource in project today-framework by TAKETODAY.

the class RowMapperTests method setUp.

@BeforeEach
public void setUp() throws SQLException {
    given(connection.createStatement()).willReturn(statement);
    given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
    given(statement.executeQuery(anyString())).willReturn(resultSet);
    given(preparedStatement.executeQuery()).willReturn(resultSet);
    given(resultSet.next()).willReturn(true, true, false);
    given(resultSet.getString(1)).willReturn("tb1", "tb2");
    given(resultSet.getInt(2)).willReturn(1, 2);
    template.setDataSource(new SingleConnectionDataSource(connection, false));
    template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
    template.afterPropertiesSet();
}
Also used : SingleConnectionDataSource(cn.taketoday.jdbc.datasource.SingleConnectionDataSource) SQLStateSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

SingleConnectionDataSource (cn.taketoday.jdbc.datasource.SingleConnectionDataSource)4 SQLStateSQLExceptionTranslator (cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Test (org.junit.jupiter.api.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2