Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Sep 10, 2023
1 parent 588b5a4 commit 6ca01e1
Showing 1 changed file with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
* @author Juergen Hoeller
* @since 6.1
*/
public class JdbcClientNamedParameterTests {
class JdbcClientNamedParameterTests {

private static final String SELECT_NAMED_PARAMETERS =
"select id, forename from custmr where id = :id and country = :country";
Expand All @@ -71,7 +71,7 @@ public class JdbcClientNamedParameterTests {
private static final String INSERT_GENERATE_KEYS_PARSED =
"insert into show (name) values(?)";

private static final String[] COLUMN_NAMES = new String[] {"id", "forename"};
private static final String[] COLUMN_NAMES = {"id", "forename"};


private Connection connection = mock();
Expand All @@ -94,7 +94,7 @@ public class JdbcClientNamedParameterTests {


@BeforeEach
public void setup() throws Exception {
void setup() throws Exception {
given(dataSource.getConnection()).willReturn(connection);
given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
given(preparedStatement.getConnection()).willReturn(connection);
Expand All @@ -105,7 +105,7 @@ public void setup() throws Exception {


@Test
public void testQueryWithResultSetExtractor() throws SQLException {
void queryWithResultSetExtractor() throws SQLException {
given(resultSet.next()).willReturn(true);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
Expand All @@ -132,7 +132,7 @@ public void testQueryWithResultSetExtractor() throws SQLException {
}

@Test
public void testQueryWithResultSetExtractorParameterSource() throws SQLException {
void queryWithResultSetExtractorParameterSource() throws SQLException {
given(resultSet.next()).willReturn(true);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
Expand All @@ -159,7 +159,7 @@ public void testQueryWithResultSetExtractorParameterSource() throws SQLException
}

@Test
public void testQueryWithResultSetExtractorNoParameters() throws SQLException {
void queryWithResultSetExtractorNoParameters() throws SQLException {
given(resultSet.next()).willReturn(true);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
Expand All @@ -182,20 +182,21 @@ public void testQueryWithResultSetExtractorNoParameters() throws SQLException {
}

@Test
public void testQueryWithRowCallbackHandler() throws SQLException {
void queryWithRowCallbackHandler() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");

params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
params.put("country", "UK");
final List<Customer> customers = new ArrayList<>();
client.sql(SELECT_NAMED_PARAMETERS).params(params).query(rs -> {
Customer cust = new Customer();
cust.setId(rs.getInt(COLUMN_NAMES[0]));
cust.setForename(rs.getString(COLUMN_NAMES[1]));
customers.add(cust);
});
client.sql(SELECT_NAMED_PARAMETERS).params(params).query(
rs -> {
Customer cust = new Customer();
cust.setId(rs.getInt(COLUMN_NAMES[0]));
cust.setForename(rs.getString(COLUMN_NAMES[1]));
customers.add(cust);
});

assertThat(customers).hasSize(1);
assertThat(customers.get(0).getId()).as("Customer id was assigned correctly").isEqualTo(1);
Expand All @@ -209,20 +210,21 @@ public void testQueryWithRowCallbackHandler() throws SQLException {
}

@Test
public void testQueryWithRowCallbackHandlerParameterSource() throws SQLException {
void queryWithRowCallbackHandlerParameterSource() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");

paramSource.addValue("id", new SqlParameterValue(Types.DECIMAL, 1));
paramSource.addValue("country", "UK");
final List<Customer> customers = new ArrayList<>();
client.sql(SELECT_NAMED_PARAMETERS).paramSource(paramSource).query(rs -> {
Customer cust = new Customer();
cust.setId(rs.getInt(COLUMN_NAMES[0]));
cust.setForename(rs.getString(COLUMN_NAMES[1]));
customers.add(cust);
});
client.sql(SELECT_NAMED_PARAMETERS).paramSource(paramSource).query(
rs -> {
Customer cust = new Customer();
cust.setId(rs.getInt(COLUMN_NAMES[0]));
cust.setForename(rs.getString(COLUMN_NAMES[1]));
customers.add(cust);
});

assertThat(customers).hasSize(1);
assertThat(customers.get(0).getId()).as("Customer id was assigned correctly").isEqualTo(1);
Expand All @@ -236,18 +238,19 @@ public void testQueryWithRowCallbackHandlerParameterSource() throws SQLException
}

@Test
public void testQueryWithRowCallbackHandlerNoParameters() throws SQLException {
void queryWithRowCallbackHandlerNoParameters() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");

final List<Customer> customers = new ArrayList<>();
client.sql(SELECT_NO_PARAMETERS).query(rs -> {
Customer cust = new Customer();
cust.setId(rs.getInt(COLUMN_NAMES[0]));
cust.setForename(rs.getString(COLUMN_NAMES[1]));
customers.add(cust);
});
client.sql(SELECT_NO_PARAMETERS).query(
rs -> {
Customer cust = new Customer();
cust.setId(rs.getInt(COLUMN_NAMES[0]));
cust.setForename(rs.getString(COLUMN_NAMES[1]));
customers.add(cust);
});

assertThat(customers).hasSize(1);
assertThat(customers.get(0).getId()).as("Customer id was assigned correctly").isEqualTo(1);
Expand All @@ -259,7 +262,7 @@ public void testQueryWithRowCallbackHandlerNoParameters() throws SQLException {
}

@Test
public void testQueryWithRowMapper() throws SQLException {
void queryWithRowMapper() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
Expand Down Expand Up @@ -287,7 +290,7 @@ public void testQueryWithRowMapper() throws SQLException {
}

@Test
public void testQueryWithRowMapperNoParameters() throws SQLException {
void queryWithRowMapperNoParameters() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
Expand All @@ -311,7 +314,7 @@ public void testQueryWithRowMapperNoParameters() throws SQLException {
}

@Test
public void testQueryForObjectWithRowMapper() throws SQLException {
void queryForObjectWithRowMapper() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
Expand All @@ -338,7 +341,7 @@ public void testQueryForObjectWithRowMapper() throws SQLException {
}

@Test
public void testQueryForStreamWithRowMapper() throws SQLException {
void queryForStreamWithRowMapper() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
Expand Down Expand Up @@ -371,7 +374,7 @@ public void testQueryForStreamWithRowMapper() throws SQLException {
}

@Test
public void testUpdate() throws SQLException {
void update() throws SQLException {
given(preparedStatement.executeUpdate()).willReturn(1);

params.put("perfId", 1);
Expand All @@ -387,7 +390,7 @@ public void testUpdate() throws SQLException {
}

@Test
public void testUpdateWithTypedParameters() throws SQLException {
void updateWithTypedParameters() throws SQLException {
given(preparedStatement.executeUpdate()).willReturn(1);

params.put("perfId", new SqlParameterValue(Types.DECIMAL, 1));
Expand All @@ -403,7 +406,7 @@ public void testUpdateWithTypedParameters() throws SQLException {
}

@Test
public void testUpdateAndGeneratedKeys() throws SQLException {
void updateAndGeneratedKeys() throws SQLException {
given(resultSetMetaData.getColumnCount()).willReturn(1);
given(resultSetMetaData.getColumnLabel(1)).willReturn("1");
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
Expand Down

0 comments on commit 6ca01e1

Please sign in to comment.