Revise JdbcTestUtils method signatures to accept JdbcOperations
Closes gh-31065
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2018 the original author or authors.
|
* Copyright 2002-2023 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.
|
||||||
@@ -19,8 +19,9 @@ package org.springframework.test.jdbc;
|
|||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcOperations;
|
||||||
import org.springframework.jdbc.core.SqlParameterValue;
|
import org.springframework.jdbc.core.SqlParameterValue;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,6 +34,7 @@ import org.springframework.util.StringUtils;
|
|||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @author Chris Baldwin
|
* @author Chris Baldwin
|
||||||
* @since 2.5.4
|
* @since 2.5.4
|
||||||
|
* @see org.springframework.jdbc.core.JdbcOperations
|
||||||
* @see org.springframework.jdbc.core.JdbcTemplate
|
* @see org.springframework.jdbc.core.JdbcTemplate
|
||||||
* @see org.springframework.jdbc.datasource.init.ScriptUtils
|
* @see org.springframework.jdbc.datasource.init.ScriptUtils
|
||||||
* @see org.springframework.jdbc.datasource.init.ResourceDatabasePopulator
|
* @see org.springframework.jdbc.datasource.init.ResourceDatabasePopulator
|
||||||
@@ -45,13 +47,13 @@ public abstract class JdbcTestUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Count the rows in the given table.
|
* Count the rows in the given table.
|
||||||
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
|
* @param jdbcTemplate the {@link JdbcOperations} with which to perform JDBC
|
||||||
|
* operations
|
||||||
* @param tableName name of the table to count rows in
|
* @param tableName name of the table to count rows in
|
||||||
* @return the number of rows in the table
|
* @return the number of rows in the table
|
||||||
*/
|
*/
|
||||||
public static int countRowsInTable(JdbcTemplate jdbcTemplate, String tableName) {
|
public static int countRowsInTable(JdbcOperations jdbcTemplate, String tableName) {
|
||||||
Integer result = jdbcTemplate.queryForObject("SELECT COUNT(0) FROM " + tableName, Integer.class);
|
return countRowsInTableWhere(jdbcTemplate, tableName, null);
|
||||||
return (result != null ? result : 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -62,13 +64,16 @@ public abstract class JdbcTestUtils {
|
|||||||
* the provided where clause is {@code "name = 'Bob' and age > 25"}, the
|
* the provided where clause is {@code "name = 'Bob' and age > 25"}, the
|
||||||
* resulting SQL statement to execute will be
|
* resulting SQL statement to execute will be
|
||||||
* {@code "SELECT COUNT(0) FROM person WHERE name = 'Bob' and age > 25"}.
|
* {@code "SELECT COUNT(0) FROM person WHERE name = 'Bob' and age > 25"}.
|
||||||
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
|
* @param jdbcTemplate the {@link JdbcOperations} with which to perform JDBC
|
||||||
|
* operations
|
||||||
* @param tableName the name of the table to count rows in
|
* @param tableName the name of the table to count rows in
|
||||||
* @param whereClause the {@code WHERE} clause to append to the query
|
* @param whereClause the {@code WHERE} clause to append to the query
|
||||||
* @return the number of rows in the table that match the provided
|
* @return the number of rows in the table that match the provided
|
||||||
* {@code WHERE} clause
|
* {@code WHERE} clause
|
||||||
*/
|
*/
|
||||||
public static int countRowsInTableWhere(JdbcTemplate jdbcTemplate, String tableName, String whereClause) {
|
public static int countRowsInTableWhere(
|
||||||
|
JdbcOperations jdbcTemplate, String tableName, @Nullable String whereClause) {
|
||||||
|
|
||||||
String sql = "SELECT COUNT(0) FROM " + tableName;
|
String sql = "SELECT COUNT(0) FROM " + tableName;
|
||||||
if (StringUtils.hasText(whereClause)) {
|
if (StringUtils.hasText(whereClause)) {
|
||||||
sql += " WHERE " + whereClause;
|
sql += " WHERE " + whereClause;
|
||||||
@@ -79,11 +84,12 @@ public abstract class JdbcTestUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete all rows from the specified tables.
|
* Delete all rows from the specified tables.
|
||||||
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
|
* @param jdbcTemplate the {@link JdbcOperations} with which to perform JDBC
|
||||||
|
* operations
|
||||||
* @param tableNames the names of the tables to delete from
|
* @param tableNames the names of the tables to delete from
|
||||||
* @return the total number of rows deleted from all specified tables
|
* @return the total number of rows deleted from all specified tables
|
||||||
*/
|
*/
|
||||||
public static int deleteFromTables(JdbcTemplate jdbcTemplate, String... tableNames) {
|
public static int deleteFromTables(JdbcOperations jdbcTemplate, String... tableNames) {
|
||||||
int totalRowCount = 0;
|
int totalRowCount = 0;
|
||||||
for (String tableName : tableNames) {
|
for (String tableName : tableNames) {
|
||||||
int rowCount = jdbcTemplate.update("DELETE FROM " + tableName);
|
int rowCount = jdbcTemplate.update("DELETE FROM " + tableName);
|
||||||
@@ -105,7 +111,8 @@ public abstract class JdbcTestUtils {
|
|||||||
* {@code "DELETE FROM person WHERE name = 'Bob' and age > 25"}.
|
* {@code "DELETE FROM person WHERE name = 'Bob' and age > 25"}.
|
||||||
* <p>As an alternative to hard-coded values, the {@code "?"} placeholder can
|
* <p>As an alternative to hard-coded values, the {@code "?"} placeholder can
|
||||||
* be used within the {@code WHERE} clause, binding to the given arguments.
|
* be used within the {@code WHERE} clause, binding to the given arguments.
|
||||||
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
|
* @param jdbcTemplate the {@link JdbcOperations} with which to perform JDBC
|
||||||
|
* operations
|
||||||
* @param tableName the name of the table to delete rows from
|
* @param tableName the name of the table to delete rows from
|
||||||
* @param whereClause the {@code WHERE} clause to append to the query
|
* @param whereClause the {@code WHERE} clause to append to the query
|
||||||
* @param args arguments to bind to the query (leaving it to the PreparedStatement
|
* @param args arguments to bind to the query (leaving it to the PreparedStatement
|
||||||
@@ -115,13 +122,13 @@ public abstract class JdbcTestUtils {
|
|||||||
* @return the number of rows deleted from the table
|
* @return the number of rows deleted from the table
|
||||||
*/
|
*/
|
||||||
public static int deleteFromTableWhere(
|
public static int deleteFromTableWhere(
|
||||||
JdbcTemplate jdbcTemplate, String tableName, String whereClause, Object... args) {
|
JdbcOperations jdbcTemplate, String tableName, String whereClause, Object... args) {
|
||||||
|
|
||||||
String sql = "DELETE FROM " + tableName;
|
String sql = "DELETE FROM " + tableName;
|
||||||
if (StringUtils.hasText(whereClause)) {
|
if (StringUtils.hasText(whereClause)) {
|
||||||
sql += " WHERE " + whereClause;
|
sql += " WHERE " + whereClause;
|
||||||
}
|
}
|
||||||
int rowCount = (args.length > 0 ? jdbcTemplate.update(sql, args) : jdbcTemplate.update(sql));
|
int rowCount = jdbcTemplate.update(sql, args);
|
||||||
if (logger.isInfoEnabled()) {
|
if (logger.isInfoEnabled()) {
|
||||||
logger.info("Deleted " + rowCount + " rows from table " + tableName);
|
logger.info("Deleted " + rowCount + " rows from table " + tableName);
|
||||||
}
|
}
|
||||||
@@ -130,10 +137,10 @@ public abstract class JdbcTestUtils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Drop the specified tables.
|
* Drop the specified tables.
|
||||||
* @param jdbcTemplate the JdbcTemplate with which to perform JDBC operations
|
* @param jdbcTemplate the {@link JdbcOperations} with which to perform JDBC operations
|
||||||
* @param tableNames the names of the tables to drop
|
* @param tableNames the names of the tables to drop
|
||||||
*/
|
*/
|
||||||
public static void dropTables(JdbcTemplate jdbcTemplate, String... tableNames) {
|
public static void dropTables(JdbcOperations jdbcTemplate, String... tableNames) {
|
||||||
for (String tableName : tableNames) {
|
for (String tableName : tableNames) {
|
||||||
jdbcTemplate.execute("DROP TABLE " + tableName);
|
jdbcTemplate.execute("DROP TABLE " + tableName);
|
||||||
if (logger.isInfoEnabled()) {
|
if (logger.isInfoEnabled()) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2019 the original author or authors.
|
* Copyright 2002-2023 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.
|
||||||
@@ -31,25 +31,24 @@ import static org.mockito.BDDMockito.given;
|
|||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @since 2.5.4
|
* @since 2.5.4
|
||||||
* @see JdbcTestUtilsIntegrationTests
|
|
||||||
*/
|
*/
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
class JdbcTestUtilsTests {
|
class JdbcTestUtilsTests {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private JdbcTemplate jdbcTemplate;
|
JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteWithoutWhereClause() throws Exception {
|
void deleteWithoutWhereClause() throws Exception {
|
||||||
given(jdbcTemplate.update("DELETE FROM person")).willReturn(10);
|
given(jdbcTemplate.update("DELETE FROM person", new Object[0])).willReturn(10);
|
||||||
int deleted = JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "person", null);
|
int deleted = JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "person", null);
|
||||||
assertThat(deleted).isEqualTo(10);
|
assertThat(deleted).isEqualTo(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteWithWhereClause() throws Exception {
|
void deleteWithWhereClause() throws Exception {
|
||||||
given(jdbcTemplate.update("DELETE FROM person WHERE name = 'Bob' and age > 25")).willReturn(10);
|
given(jdbcTemplate.update("DELETE FROM person WHERE name = 'Bob' and age > 25", new Object[0])).willReturn(10);
|
||||||
int deleted = JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "person", "name = 'Bob' and age > 25");
|
int deleted = JdbcTestUtils.deleteFromTableWhere(jdbcTemplate, "person", "name = 'Bob' and age > 25");
|
||||||
assertThat(deleted).isEqualTo(10);
|
assertThat(deleted).isEqualTo(10);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user