Java Code Examples for org.springframework.jdbc.Customer

The following examples show how to use org.springframework.jdbc.Customer. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void testQueryWithResultSetExtractor() throws SQLException {
	given(resultSet.next()).willReturn(true);
	given(resultSet.getInt("id")).willReturn(1);
	given(resultSet.getString("forename")).willReturn("rod");

	params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
	params.put("country", "UK");
	Customer cust = namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params,
			rs -> {
				rs.next();
				Customer cust1 = new Customer();
				cust1.setId(rs.getInt(COLUMN_NAMES[0]));
				cust1.setForename(rs.getString(COLUMN_NAMES[1]));
				return cust1;
			});

	assertTrue("Customer id was assigned correctly", cust.getId() == 1);
	assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
	verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
	verify(preparedStatement).setString(2, "UK");
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 2
@Test
public void testQueryWithResultSetExtractorNoParameters() throws SQLException {
	given(resultSet.next()).willReturn(true);
	given(resultSet.getInt("id")).willReturn(1);
	given(resultSet.getString("forename")).willReturn("rod");

	Customer cust = namedParameterTemplate.query(SELECT_NO_PARAMETERS,
			rs -> {
				rs.next();
				Customer cust1 = new Customer();
				cust1.setId(rs.getInt(COLUMN_NAMES[0]));
				cust1.setForename(rs.getString(COLUMN_NAMES[1]));
				return cust1;
			});

	assertTrue("Customer id was assigned correctly", cust.getId() == 1);
	assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NO_PARAMETERS);
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 3
@Test
public void testQueryWithRowCallbackHandler() 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 LinkedList<>();
	namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, rs -> {
		Customer cust = new Customer();
		cust.setId(rs.getInt(COLUMN_NAMES[0]));
		cust.setForename(rs.getString(COLUMN_NAMES[1]));
		customers.add(cust);
	});

	assertEquals(1, customers.size());
	assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
	assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
	verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
	verify(preparedStatement).setString(2, "UK");
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 4
@Test
public void testQueryWithRowCallbackHandlerNoParameters() 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 LinkedList<>();
	namedParameterTemplate.query(SELECT_NO_PARAMETERS, rs -> {
		Customer cust = new Customer();
		cust.setId(rs.getInt(COLUMN_NAMES[0]));
		cust.setForename(rs.getString(COLUMN_NAMES[1]));
		customers.add(cust);
	});

	assertEquals(1, customers.size());
	assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
	assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NO_PARAMETERS);
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 5
@Test
public void testQueryWithRowMapper() 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");
	List<Customer> customers = namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params,
			(rs, rownum) -> {
				Customer cust = new Customer();
				cust.setId(rs.getInt(COLUMN_NAMES[0]));
				cust.setForename(rs.getString(COLUMN_NAMES[1]));
				return cust;
			});
	assertEquals(1, customers.size());
	assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
	assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
	verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
	verify(preparedStatement).setString(2, "UK");
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 6
@Test
public void testQueryWithRowMapperNoParameters() throws SQLException {
	given(resultSet.next()).willReturn(true, false);
	given(resultSet.getInt("id")).willReturn(1);
	given(resultSet.getString("forename")).willReturn("rod");

	List<Customer> customers = namedParameterTemplate.query(SELECT_NO_PARAMETERS,
			(rs, rownum) -> {
				Customer cust = new Customer();
				cust.setId(rs.getInt(COLUMN_NAMES[0]));
				cust.setForename(rs.getString(COLUMN_NAMES[1]));
				return cust;
			});
	assertEquals(1, customers.size());
	assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
	assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NO_PARAMETERS);
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 7
@Test
public void testQueryForObjectWithRowMapper() 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");
	Customer cust = namedParameterTemplate.queryForObject(SELECT_NAMED_PARAMETERS, params,
			(rs, rownum) -> {
				Customer cust1 = new Customer();
				cust1.setId(rs.getInt(COLUMN_NAMES[0]));
				cust1.setForename(rs.getString(COLUMN_NAMES[1]));
				return cust1;
			});
	assertTrue("Customer id was assigned correctly", cust.getId() == 1);
	assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
	verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
	verify(preparedStatement).setString(2, "UK");
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 8
@Test
public void testQueryWithResultSetExtractor() throws SQLException {
	given(resultSet.next()).willReturn(true);
	given(resultSet.getInt("id")).willReturn(1);
	given(resultSet.getString("forename")).willReturn("rod");

	params.put("id", new SqlParameterValue(Types.DECIMAL, 1));
	params.put("country", "UK");
	Customer cust = namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params,
			rs -> {
				rs.next();
				Customer cust1 = new Customer();
				cust1.setId(rs.getInt(COLUMN_NAMES[0]));
				cust1.setForename(rs.getString(COLUMN_NAMES[1]));
				return cust1;
			});

	assertTrue("Customer id was assigned correctly", cust.getId() == 1);
	assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
	verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
	verify(preparedStatement).setString(2, "UK");
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 9
@Test
public void testQueryWithResultSetExtractorNoParameters() throws SQLException {
	given(resultSet.next()).willReturn(true);
	given(resultSet.getInt("id")).willReturn(1);
	given(resultSet.getString("forename")).willReturn("rod");

	Customer cust = namedParameterTemplate.query(SELECT_NO_PARAMETERS,
			rs -> {
				rs.next();
				Customer cust1 = new Customer();
				cust1.setId(rs.getInt(COLUMN_NAMES[0]));
				cust1.setForename(rs.getString(COLUMN_NAMES[1]));
				return cust1;
			});

	assertTrue("Customer id was assigned correctly", cust.getId() == 1);
	assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NO_PARAMETERS);
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 10
@Test
public void testQueryWithRowCallbackHandler() 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 LinkedList<>();
	namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params, rs -> {
		Customer cust = new Customer();
		cust.setId(rs.getInt(COLUMN_NAMES[0]));
		cust.setForename(rs.getString(COLUMN_NAMES[1]));
		customers.add(cust);
	});

	assertEquals(1, customers.size());
	assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
	assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
	verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
	verify(preparedStatement).setString(2, "UK");
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 11
@Test
public void testQueryWithRowCallbackHandlerNoParameters() 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 LinkedList<>();
	namedParameterTemplate.query(SELECT_NO_PARAMETERS, rs -> {
		Customer cust = new Customer();
		cust.setId(rs.getInt(COLUMN_NAMES[0]));
		cust.setForename(rs.getString(COLUMN_NAMES[1]));
		customers.add(cust);
	});

	assertEquals(1, customers.size());
	assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
	assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NO_PARAMETERS);
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 12
@Test
public void testQueryWithRowMapper() 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");
	List<Customer> customers = namedParameterTemplate.query(SELECT_NAMED_PARAMETERS, params,
			(rs, rownum) -> {
				Customer cust = new Customer();
				cust.setId(rs.getInt(COLUMN_NAMES[0]));
				cust.setForename(rs.getString(COLUMN_NAMES[1]));
				return cust;
			});
	assertEquals(1, customers.size());
	assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
	assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
	verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
	verify(preparedStatement).setString(2, "UK");
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 13
@Test
public void testQueryWithRowMapperNoParameters() throws SQLException {
	given(resultSet.next()).willReturn(true, false);
	given(resultSet.getInt("id")).willReturn(1);
	given(resultSet.getString("forename")).willReturn("rod");

	List<Customer> customers = namedParameterTemplate.query(SELECT_NO_PARAMETERS,
			(rs, rownum) -> {
				Customer cust = new Customer();
				cust.setId(rs.getInt(COLUMN_NAMES[0]));
				cust.setForename(rs.getString(COLUMN_NAMES[1]));
				return cust;
			});
	assertEquals(1, customers.size());
	assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
	assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NO_PARAMETERS);
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 14
@Test
public void testQueryForObjectWithRowMapper() 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");
	Customer cust = namedParameterTemplate.queryForObject(SELECT_NAMED_PARAMETERS, params,
			(rs, rownum) -> {
				Customer cust1 = new Customer();
				cust1.setId(rs.getInt(COLUMN_NAMES[0]));
				cust1.setForename(rs.getString(COLUMN_NAMES[1]));
				return cust1;
			});
	assertTrue("Customer id was assigned correctly", cust.getId() == 1);
	assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
	verify(preparedStatement).setObject(1, 1, Types.DECIMAL);
	verify(preparedStatement).setString(2, "UK");
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 15
@Test
public void testQueryWithRowCallbackHandlerNoParameters() 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 LinkedList<Customer>();
	namedParameterTemplate.query(SELECT_NO_PARAMETERS, new RowCallbackHandler() {
		@Override
		public void processRow(ResultSet rs) throws SQLException {
			Customer cust = new Customer();
			cust.setId(rs.getInt(COLUMN_NAMES[0]));
			cust.setForename(rs.getString(COLUMN_NAMES[1]));
			customers.add(cust);
		}
	});

	assertEquals(1, customers.size());
	assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
	assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NO_PARAMETERS);
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 16
@Test
public void testQueryWithRowMapperNoParameters() throws SQLException {
	given(resultSet.next()).willReturn(true, false);
	given(resultSet.getInt("id")).willReturn(1);
	given(resultSet.getString("forename")).willReturn("rod");

	List<Customer> customers = namedParameterTemplate.query(SELECT_NO_PARAMETERS,
			new RowMapper<Customer>() {
				@Override
				public 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;
				}
			});
	assertEquals(1, customers.size());
	assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
	assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NO_PARAMETERS);
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 17
@Test
public void testQueryWithRowCallbackHandlerNoParameters() 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 LinkedList<Customer>();
	namedParameterTemplate.query(SELECT_NO_PARAMETERS, new RowCallbackHandler() {
		@Override
		public void processRow(ResultSet rs) throws SQLException {
			Customer cust = new Customer();
			cust.setId(rs.getInt(COLUMN_NAMES[0]));
			cust.setForename(rs.getString(COLUMN_NAMES[1]));
			customers.add(cust);
		}
	});

	assertEquals(1, customers.size());
	assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
	assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NO_PARAMETERS);
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 18
@Test
public void testQueryWithRowMapperNoParameters() throws SQLException {
	given(resultSet.next()).willReturn(true, false);
	given(resultSet.getInt("id")).willReturn(1);
	given(resultSet.getString("forename")).willReturn("rod");

	List<Customer> customers = namedParameterTemplate.query(SELECT_NO_PARAMETERS,
			new RowMapper<Customer>() {
				@Override
				public 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;
				}
			});
	assertEquals(1, customers.size());
	assertTrue("Customer id was assigned correctly", customers.get(0).getId() == 1);
	assertTrue("Customer forename was assigned correctly", customers.get(0).getForename().equals("rod"));
	verify(connection).prepareStatement(SELECT_NO_PARAMETERS);
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 19
Source Project: spring-analysis-note   Source File: CustomerMapper.java    License: MIT License 5 votes vote down vote up
@Override
public 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;
}
 
Example 20
Source Project: spring-analysis-note   Source File: SqlQueryTests.java    License: MIT License 5 votes vote down vote up
@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);
	assertThatExceptionOfType(IncorrectResultSizeDataAccessException.class).isThrownBy(() ->
			query.findCustomer("rod"));
	verify(preparedStatement).setString(1, "rod");
	verify(connection).prepareStatement(SELECT_ID_FORENAME_WHERE);
	verify(resultSet).close();
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 21
Source Project: spring-analysis-note   Source File: SqlQueryTests.java    License: MIT License 5 votes vote down vote up
@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();
}
 
Example 22
Source Project: spring-analysis-note   Source File: SqlQueryTests.java    License: MIT License 5 votes vote down vote up
@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();
}
 
Example 23
Source Project: spring-analysis-note   Source File: SqlQueryTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testUnnamedParameterDeclarationWithNamedParameterQuery()
		throws SQLException {
	class CustomerQuery extends MappingSqlQuery<Customer> {

		public CustomerQuery(DataSource ds) {
			super(ds, SELECT_ID_FORENAME_WHERE);
			setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
			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;
		}

		public Customer findCustomer(int id) {
			Map<String, Integer> params = new HashMap<>();
			params.put("id", id);
			return executeByNamedParam(params).get(0);
		}
	}

	// Query should not succeed since parameter declaration did not specify parameter name
	CustomerQuery query = new CustomerQuery(dataSource);
	assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() ->
			query.findCustomer(1));
}
 
Example 24
Source Project: spring-analysis-note   Source File: SqlQueryTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testNamedParameterUsingInvalidQuestionMarkPlaceHolders()
		throws SQLException {
	given(
	connection.prepareStatement(SELECT_ID_FORENAME_WHERE_ID_REUSED_1,
			ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY)).willReturn(preparedStatement);

	class CustomerQuery extends MappingSqlQuery<Customer> {

		public CustomerQuery(DataSource ds) {
			super(ds, SELECT_ID_FORENAME_WHERE_ID_REUSED_1);
			setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
			declareParameter(new SqlParameter("id1", 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;
		}

		public List<Customer> findCustomers(Integer id1) {
			Map<String, Integer> params = new HashMap<>();
			params.put("id1", id1);
			return executeByNamedParam(params);
		}
	}

	CustomerQuery query = new CustomerQuery(dataSource);
	assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() ->
			query.findCustomers(1));
}
 
Example 25
Source Project: spring-analysis-note   Source File: SqlQueryTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testUpdateCustomers() throws SQLException {
	given(resultSet.next()).willReturn(true, true, false);
	given(resultSet.getInt("id")).willReturn(1, 2);
	given(connection.prepareStatement(SELECT_ID_FORENAME_WHERE_ID,
			ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)
		).willReturn(preparedStatement);

	class CustomerUpdateQuery extends UpdatableSqlQuery<Customer> {

		public CustomerUpdateQuery(DataSource ds) {
			super(ds, SELECT_ID_FORENAME_WHERE_ID);
			declareParameter(new SqlParameter(Types.NUMERIC));
			compile();
		}

		@Override
		protected Customer updateRow(ResultSet rs, int rownum, @Nullable Map<? ,?> context)
				throws SQLException {
			rs.updateString(2, "" + context.get(rs.getInt(COLUMN_NAMES[0])));
			return null;
		}
	}

	CustomerUpdateQuery query = new CustomerUpdateQuery(dataSource);
	Map<Integer, String> values = new HashMap<>(2);
	values.put(1, "Rod");
	values.put(2, "Thomas");
	query.execute(2, values);
	verify(resultSet).updateString(2, "Rod");
	verify(resultSet).updateString(2, "Thomas");
	verify(resultSet, times(2)).updateRow();
	verify(preparedStatement).setObject(1, 2, Types.NUMERIC);
	verify(resultSet).close();
	verify(preparedStatement).close();
	verify(connection).close();
}
 
Example 26
Source Project: java-technology-stack   Source File: CustomerMapper.java    License: MIT License 5 votes vote down vote up
@Override
public 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;
}
 
Example 27
Source Project: java-technology-stack   Source File: SqlQueryTests.java    License: MIT License 5 votes vote down vote up
@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();
	}
}
 
Example 28
Source Project: java-technology-stack   Source File: SqlQueryTests.java    License: MIT License 5 votes vote down vote up
@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();
}
 
Example 29
Source Project: java-technology-stack   Source File: SqlQueryTests.java    License: MIT License 5 votes vote down vote up
@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();
}
 
Example 30
Source Project: java-technology-stack   Source File: SqlQueryTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testUnnamedParameterDeclarationWithNamedParameterQuery()
		throws SQLException {
	class CustomerQuery extends MappingSqlQuery<Customer> {

		public CustomerQuery(DataSource ds) {
			super(ds, SELECT_ID_FORENAME_WHERE);
			setResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE);
			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;
		}

		public Customer findCustomer(int id) {
			Map<String, Integer> params = new HashMap<>();
			params.put("id", id);
			return executeByNamedParam(params).get(0);
		}
	}

	// Query should not succeed since parameter declaration did not specify parameter name
	CustomerQuery query = new CustomerQuery(dataSource);
	thrown.expect(InvalidDataAccessApiUsageException.class);
	query.findCustomer(1);
}