use of org.springframework.jdbc.Customer in project spring-framework by spring-projects.
the class SqlQueryTests method testListCustomersIntInt.
@Test
public void testListCustomersIntInt() throws SQLException {
given(resultSet.next()).willReturn(true, true, false);
given(resultSet.getInt("id")).willReturn(1, 2);
given(resultSet.getString("forename")).willReturn("rod", "dave");
class CustomerQuery extends MappingSqlQuery<Customer> {
public CustomerQuery(DataSource ds) {
super(ds, SELECT_ID_WHERE);
declareParameter(new SqlParameter(Types.NUMERIC));
declareParameter(new SqlParameter(Types.NUMERIC));
compile();
}
@Override
protected Customer mapRow(ResultSet rs, int rownum) throws SQLException {
Customer cust = new Customer();
cust.setId(rs.getInt(COLUMN_NAMES[0]));
cust.setForename(rs.getString(COLUMN_NAMES[1]));
return cust;
}
}
CustomerQuery query = new CustomerQuery(dataSource);
List<Customer> list = query.execute(1, 1);
assertTrue("2 results in list", list.size() == 2);
assertThat(list.get(0).getForename(), is("rod"));
assertThat(list.get(1).getForename(), is("dave"));
verify(preparedStatement).setObject(1, 1, Types.NUMERIC);
verify(preparedStatement).setObject(2, 1, Types.NUMERIC);
verify(connection).prepareStatement(SELECT_ID_WHERE);
verify(resultSet).close();
verify(preparedStatement).close();
verify(connection).close();
}
use of org.springframework.jdbc.Customer in project spring-framework by spring-projects.
the class SqlQueryTests method doTestNamedParameterCustomerQuery.
private void doTestNamedParameterCustomerQuery(final boolean namedDeclarations) throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
given(connection.prepareStatement(SELECT_ID_FORENAME_NAMED_PARAMETERS_PARSED, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY)).willReturn(preparedStatement);
class CustomerQuery extends MappingSqlQuery<Customer> {
public CustomerQuery(DataSource ds) {
super(ds, SELECT_ID_FORENAME_NAMED_PARAMETERS);
setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
if (namedDeclarations) {
declareParameter(new SqlParameter("country", Types.VARCHAR));
declareParameter(new SqlParameter("id", Types.NUMERIC));
} else {
declareParameter(new SqlParameter(Types.NUMERIC));
declareParameter(new SqlParameter(Types.VARCHAR));
}
compile();
}
@Override
protected Customer mapRow(ResultSet rs, int rownum) throws SQLException {
Customer cust = new Customer();
cust.setId(rs.getInt(COLUMN_NAMES[0]));
cust.setForename(rs.getString(COLUMN_NAMES[1]));
return cust;
}
public Customer findCustomer(int id, String country) {
Map<String, Object> params = new HashMap<>();
params.put("id", id);
params.put("country", country);
return executeByNamedParam(params).get(0);
}
}
CustomerQuery query = new CustomerQuery(dataSource);
Customer cust = query.findCustomer(1, "UK");
assertTrue("Customer id was assigned correctly", cust.getId() == 1);
assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
verify(preparedStatement).setObject(1, 1, Types.NUMERIC);
verify(preparedStatement).setString(2, "UK");
verify(resultSet).close();
verify(preparedStatement).close();
verify(connection).close();
}
use of org.springframework.jdbc.Customer in project spring-framework by spring-projects.
the class SqlQueryTests method testFindCustomerString.
@Test
public void testFindCustomerString() throws SQLException {
given(resultSet.next()).willReturn(true, false);
given(resultSet.getInt("id")).willReturn(1);
given(resultSet.getString("forename")).willReturn("rod");
class CustomerQuery extends MappingSqlQuery<Customer> {
public CustomerQuery(DataSource ds) {
super(ds, SELECT_ID_FORENAME_WHERE);
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
@Override
protected Customer mapRow(ResultSet rs, int rownum) throws SQLException {
Customer cust = new Customer();
cust.setId(rs.getInt(COLUMN_NAMES[0]));
cust.setForename(rs.getString(COLUMN_NAMES[1]));
return cust;
}
public Customer findCustomer(String id) {
return findObject(id);
}
}
CustomerQuery query = new CustomerQuery(dataSource);
Customer cust = query.findCustomer("rod");
assertTrue("Customer id was assigned correctly", cust.getId() == 1);
assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
verify(preparedStatement).setString(1, "rod");
verify(connection).prepareStatement(SELECT_ID_FORENAME_WHERE);
verify(resultSet).close();
verify(preparedStatement).close();
verify(connection).close();
}
use of org.springframework.jdbc.Customer in project spring-framework by spring-projects.
the class SqlQueryTests method testListCustomersString.
@Test
public void testListCustomersString() throws SQLException {
given(resultSet.next()).willReturn(true, true, false);
given(resultSet.getInt("id")).willReturn(1, 2);
given(resultSet.getString("forename")).willReturn("rod", "dave");
class CustomerQuery extends MappingSqlQuery<Customer> {
public CustomerQuery(DataSource ds) {
super(ds, SELECT_ID_FORENAME_WHERE);
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
@Override
protected Customer mapRow(ResultSet rs, int rownum) throws SQLException {
Customer cust = new Customer();
cust.setId(rs.getInt(COLUMN_NAMES[0]));
cust.setForename(rs.getString(COLUMN_NAMES[1]));
return cust;
}
}
CustomerQuery query = new CustomerQuery(dataSource);
List<Customer> list = query.execute("one");
assertTrue("2 results in list", list.size() == 2);
assertThat(list.get(0).getForename(), is("rod"));
assertThat(list.get(1).getForename(), is("dave"));
verify(preparedStatement).setString(1, "one");
verify(connection).prepareStatement(SELECT_ID_FORENAME_WHERE);
verify(resultSet).close();
verify(preparedStatement).close();
verify(connection).close();
}
use of org.springframework.jdbc.Customer in project spring-framework by spring-projects.
the class SqlQueryTests method testFindTooManyCustomers.
@Test
public void testFindTooManyCustomers() throws SQLException {
given(resultSet.next()).willReturn(true, true, false);
given(resultSet.getInt("id")).willReturn(1, 2);
given(resultSet.getString("forename")).willReturn("rod", "rod");
class CustomerQuery extends MappingSqlQuery<Customer> {
public CustomerQuery(DataSource ds) {
super(ds, SELECT_ID_FORENAME_WHERE);
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
@Override
protected Customer mapRow(ResultSet rs, int rownum) throws SQLException {
Customer cust = new Customer();
cust.setId(rs.getInt(COLUMN_NAMES[0]));
cust.setForename(rs.getString(COLUMN_NAMES[1]));
return cust;
}
public Customer findCustomer(String id) {
return findObject(id);
}
}
CustomerQuery query = new CustomerQuery(dataSource);
thrown.expect(IncorrectResultSizeDataAccessException.class);
try {
query.findCustomer("rod");
} finally {
verify(preparedStatement).setString(1, "rod");
verify(connection).prepareStatement(SELECT_ID_FORENAME_WHERE);
verify(resultSet).close();
verify(preparedStatement).close();
verify(connection).close();
}
}
Aggregations