|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2012 the original author or authors. |
|
|
|
* Copyright 2002-2013 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -16,15 +16,6 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.jdbc.core.namedparam; |
|
|
|
package org.springframework.jdbc.core.namedparam; |
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
|
|
|
|
import static org.mockito.BDDMockito.given; |
|
|
|
|
|
|
|
import static org.mockito.Matchers.anyString; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.atLeastOnce; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.times; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.sql.Connection; |
|
|
|
import java.sql.Connection; |
|
|
|
import java.sql.DatabaseMetaData; |
|
|
|
import java.sql.DatabaseMetaData; |
|
|
|
import java.sql.PreparedStatement; |
|
|
|
import java.sql.PreparedStatement; |
|
|
|
@ -36,13 +27,13 @@ import java.util.HashMap; |
|
|
|
import java.util.LinkedList; |
|
|
|
import java.util.LinkedList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
import javax.sql.DataSource; |
|
|
|
import javax.sql.DataSource; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Rule; |
|
|
|
import org.junit.Rule; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.rules.ExpectedException; |
|
|
|
import org.junit.rules.ExpectedException; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.dao.DataAccessException; |
|
|
|
import org.springframework.dao.DataAccessException; |
|
|
|
import org.springframework.jdbc.Customer; |
|
|
|
import org.springframework.jdbc.Customer; |
|
|
|
import org.springframework.jdbc.core.JdbcOperations; |
|
|
|
import org.springframework.jdbc.core.JdbcOperations; |
|
|
|
@ -53,6 +44,14 @@ import org.springframework.jdbc.core.RowCallbackHandler; |
|
|
|
import org.springframework.jdbc.core.RowMapper; |
|
|
|
import org.springframework.jdbc.core.RowMapper; |
|
|
|
import org.springframework.jdbc.core.SqlParameterValue; |
|
|
|
import org.springframework.jdbc.core.SqlParameterValue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.*; |
|
|
|
|
|
|
|
import static org.mockito.BDDMockito.*; |
|
|
|
|
|
|
|
import static org.mockito.Matchers.anyString; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.atLeastOnce; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.times; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @author Rick Evans |
|
|
|
* @author Rick Evans |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Juergen Hoeller |
|
|
|
@ -64,6 +63,8 @@ public class NamedParameterJdbcTemplateTests { |
|
|
|
"select id, forename from custmr where id = :id and country = :country"; |
|
|
|
"select id, forename from custmr where id = :id and country = :country"; |
|
|
|
private static final String SELECT_NAMED_PARAMETERS_PARSED = |
|
|
|
private static final String SELECT_NAMED_PARAMETERS_PARSED = |
|
|
|
"select id, forename from custmr where id = ? and country = ?"; |
|
|
|
"select id, forename from custmr where id = ? and country = ?"; |
|
|
|
|
|
|
|
private static final String SELECT_NO_PARAMETERS = |
|
|
|
|
|
|
|
"select id, forename from custmr"; |
|
|
|
|
|
|
|
|
|
|
|
private static final String UPDATE_NAMED_PARAMETERS = |
|
|
|
private static final String UPDATE_NAMED_PARAMETERS = |
|
|
|
"update seat_status set booking_id = null where performance_id = :perfId and price_band_id = :priceId"; |
|
|
|
"update seat_status set booking_id = null where performance_id = :perfId and price_band_id = :priceId"; |
|
|
|
@ -161,33 +162,23 @@ public class NamedParameterJdbcTemplateTests { |
|
|
|
verify(connection).close(); |
|
|
|
verify(connection).close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test public void testUpdate() throws SQLException { |
|
|
|
|
|
|
|
given(preparedStatement.executeUpdate()).willReturn(1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
params.put("perfId", 1); |
|
|
|
|
|
|
|
params.put("priceId", 1); |
|
|
|
|
|
|
|
int rowsAffected = namedParameterTemplate.update(UPDATE_NAMED_PARAMETERS, params); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(1, rowsAffected); |
|
|
|
|
|
|
|
verify(connection).prepareStatement(UPDATE_NAMED_PARAMETERS_PARSED); |
|
|
|
|
|
|
|
verify(preparedStatement).setObject(1, 1); |
|
|
|
|
|
|
|
verify(preparedStatement).setObject(2, 1); |
|
|
|
|
|
|
|
verify(preparedStatement).close(); |
|
|
|
|
|
|
|
verify(connection).close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testUpdateWithTypedParameters() throws SQLException { |
|
|
|
public void testExecuteNoParameters() throws SQLException { |
|
|
|
given(preparedStatement.executeUpdate()).willReturn(1); |
|
|
|
given(preparedStatement.executeUpdate()).willReturn(1); |
|
|
|
|
|
|
|
|
|
|
|
params.put("perfId", new SqlParameterValue(Types.DECIMAL, 1)); |
|
|
|
Object result = namedParameterTemplate.execute(SELECT_NO_PARAMETERS, |
|
|
|
params.put("priceId", new SqlParameterValue(Types.INTEGER, 1)); |
|
|
|
new PreparedStatementCallback<Object>() { |
|
|
|
int rowsAffected = namedParameterTemplate.update(UPDATE_NAMED_PARAMETERS, params); |
|
|
|
@Override |
|
|
|
|
|
|
|
public Object doInPreparedStatement(PreparedStatement ps) |
|
|
|
|
|
|
|
throws SQLException { |
|
|
|
|
|
|
|
assertEquals(preparedStatement, ps); |
|
|
|
|
|
|
|
ps.executeQuery(); |
|
|
|
|
|
|
|
return "result"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(1, rowsAffected); |
|
|
|
assertEquals("result", result); |
|
|
|
verify(connection).prepareStatement(UPDATE_NAMED_PARAMETERS_PARSED); |
|
|
|
verify(connection).prepareStatement(SELECT_NO_PARAMETERS); |
|
|
|
verify(preparedStatement).setObject(1, 1, Types.DECIMAL); |
|
|
|
|
|
|
|
verify(preparedStatement).setObject(2, 1, Types.INTEGER); |
|
|
|
|
|
|
|
verify(preparedStatement).close(); |
|
|
|
verify(preparedStatement).close(); |
|
|
|
verify(connection).close(); |
|
|
|
verify(connection).close(); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -222,6 +213,32 @@ public class NamedParameterJdbcTemplateTests { |
|
|
|
verify(connection).close(); |
|
|
|
verify(connection).close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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, |
|
|
|
|
|
|
|
new ResultSetExtractor<Customer>() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public Customer extractData(ResultSet rs) throws SQLException, |
|
|
|
|
|
|
|
DataAccessException { |
|
|
|
|
|
|
|
rs.next(); |
|
|
|
|
|
|
|
Customer cust = new Customer(); |
|
|
|
|
|
|
|
cust.setId(rs.getInt(COLUMN_NAMES[0])); |
|
|
|
|
|
|
|
cust.setForename(rs.getString(COLUMN_NAMES[1])); |
|
|
|
|
|
|
|
return cust; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testQueryWithRowCallbackHandler() throws SQLException { |
|
|
|
public void testQueryWithRowCallbackHandler() throws SQLException { |
|
|
|
given(resultSet.next()).willReturn(true, false); |
|
|
|
given(resultSet.next()).willReturn(true, false); |
|
|
|
@ -251,6 +268,31 @@ public class NamedParameterJdbcTemplateTests { |
|
|
|
verify(connection).close(); |
|
|
|
verify(connection).close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testQueryWithRowMapper() throws SQLException { |
|
|
|
public void testQueryWithRowMapper() throws SQLException { |
|
|
|
given(resultSet.next()).willReturn(true, false); |
|
|
|
given(resultSet.next()).willReturn(true, false); |
|
|
|
@ -279,6 +321,30 @@ public class NamedParameterJdbcTemplateTests { |
|
|
|
verify(connection).close(); |
|
|
|
verify(connection).close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testQueryForObjectWithRowMapper() throws SQLException { |
|
|
|
public void testQueryForObjectWithRowMapper() throws SQLException { |
|
|
|
given(resultSet.next()).willReturn(true, false); |
|
|
|
given(resultSet.next()).willReturn(true, false); |
|
|
|
@ -307,8 +373,39 @@ public class NamedParameterJdbcTemplateTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testBatchUpdateWithPlainMap() throws Exception { |
|
|
|
public void testUpdate() throws SQLException { |
|
|
|
|
|
|
|
given(preparedStatement.executeUpdate()).willReturn(1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
params.put("perfId", 1); |
|
|
|
|
|
|
|
params.put("priceId", 1); |
|
|
|
|
|
|
|
int rowsAffected = namedParameterTemplate.update(UPDATE_NAMED_PARAMETERS, params); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(1, rowsAffected); |
|
|
|
|
|
|
|
verify(connection).prepareStatement(UPDATE_NAMED_PARAMETERS_PARSED); |
|
|
|
|
|
|
|
verify(preparedStatement).setObject(1, 1); |
|
|
|
|
|
|
|
verify(preparedStatement).setObject(2, 1); |
|
|
|
|
|
|
|
verify(preparedStatement).close(); |
|
|
|
|
|
|
|
verify(connection).close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testUpdateWithTypedParameters() throws SQLException { |
|
|
|
|
|
|
|
given(preparedStatement.executeUpdate()).willReturn(1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
params.put("perfId", new SqlParameterValue(Types.DECIMAL, 1)); |
|
|
|
|
|
|
|
params.put("priceId", new SqlParameterValue(Types.INTEGER, 1)); |
|
|
|
|
|
|
|
int rowsAffected = namedParameterTemplate.update(UPDATE_NAMED_PARAMETERS, params); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertEquals(1, rowsAffected); |
|
|
|
|
|
|
|
verify(connection).prepareStatement(UPDATE_NAMED_PARAMETERS_PARSED); |
|
|
|
|
|
|
|
verify(preparedStatement).setObject(1, 1, Types.DECIMAL); |
|
|
|
|
|
|
|
verify(preparedStatement).setObject(2, 1, Types.INTEGER); |
|
|
|
|
|
|
|
verify(preparedStatement).close(); |
|
|
|
|
|
|
|
verify(connection).close(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void testBatchUpdateWithPlainMap() throws Exception { |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
final Map<String, Integer>[] ids = new Map[2]; |
|
|
|
final Map<String, Integer>[] ids = new Map[2]; |
|
|
|
ids[0] = Collections.singletonMap("id", 100); |
|
|
|
ids[0] = Collections.singletonMap("id", 100); |
|
|
|
@ -335,7 +432,7 @@ public class NamedParameterJdbcTemplateTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testBatchUpdateWithSqlParameterSource() throws Exception { |
|
|
|
public void testBatchUpdateWithSqlParameterSource() throws Exception { |
|
|
|
final SqlParameterSource[] ids = new SqlParameterSource[2]; |
|
|
|
SqlParameterSource[] ids = new SqlParameterSource[2]; |
|
|
|
ids[0] = new MapSqlParameterSource("id", 100); |
|
|
|
ids[0] = new MapSqlParameterSource("id", 100); |
|
|
|
ids[1] = new MapSqlParameterSource("id", 200); |
|
|
|
ids[1] = new MapSqlParameterSource("id", 200); |
|
|
|
final int[] rowsAffected = new int[] { 1, 2 }; |
|
|
|
final int[] rowsAffected = new int[] { 1, 2 }; |
|
|
|
@ -360,8 +457,7 @@ public class NamedParameterJdbcTemplateTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testBatchUpdateWithSqlParameterSourcePlusTypeInfo() throws Exception { |
|
|
|
public void testBatchUpdateWithSqlParameterSourcePlusTypeInfo() throws Exception { |
|
|
|
|
|
|
|
SqlParameterSource[] ids = new SqlParameterSource[2]; |
|
|
|
final SqlParameterSource[] ids = new SqlParameterSource[2]; |
|
|
|
|
|
|
|
ids[0] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC); |
|
|
|
ids[0] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC); |
|
|
|
ids[1] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC); |
|
|
|
ids[1] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC); |
|
|
|
final int[] rowsAffected = new int[] { 1, 2 }; |
|
|
|
final int[] rowsAffected = new int[] { 1, 2 }; |
|
|
|
|