From 14fa8f236ff2df338b31b4a89ee043e7c0a68fb0 Mon Sep 17 00:00:00 2001 From: Mickael Jeanroy Date: Tue, 14 Nov 2023 15:35:20 +0100 Subject: [PATCH] feat: batch sql queries --- .../{core => commons}/jdbc/JdbcUtils.java | 71 ++++++++++++++----- .../commons/jdbc/ResultSetMapFunction.java | 44 ++++++++++++ ...stractJdbcDropCreateForeignKeyManager.java | 6 +- .../InformationSchemaForeignKeyManager.java | 4 +- .../jdbc/JdbcDefaultConnectionFactory.java | 1 + .../mjeanroy/dbunit/core/jdbc/JdbcDriver.java | 1 + .../core/jdbc/MsSQLForeignKeyManager.java | 4 +- .../core/jdbc/MySQLForeignKeyManager.java | 4 +- .../core/jdbc/OracleForeignKeyManager.java | 5 +- .../core/jdbc/PostgresForeignKeyManager.java | 4 +- .../dbunit/core/runner/SqlScriptExecutor.java | 2 +- .../dbunit/core/sql/SqlScriptParser.java | 14 +--- .../runner/SqlScriptRunnerFunctionTest.java | 55 +++++++------- .../dbunit/core/sql/SqlScriptParserTest.java | 52 ++++++-------- 14 files changed, 161 insertions(+), 106 deletions(-) rename src/main/java/com/github/mjeanroy/dbunit/{core => commons}/jdbc/JdbcUtils.java (60%) create mode 100644 src/main/java/com/github/mjeanroy/dbunit/commons/jdbc/ResultSetMapFunction.java diff --git a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/JdbcUtils.java b/src/main/java/com/github/mjeanroy/dbunit/commons/jdbc/JdbcUtils.java similarity index 60% rename from src/main/java/com/github/mjeanroy/dbunit/core/jdbc/JdbcUtils.java rename to src/main/java/com/github/mjeanroy/dbunit/commons/jdbc/JdbcUtils.java index df40dbdd..6a01adee 100644 --- a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/JdbcUtils.java +++ b/src/main/java/com/github/mjeanroy/dbunit/commons/jdbc/JdbcUtils.java @@ -22,7 +22,7 @@ * SOFTWARE. */ -package com.github.mjeanroy.dbunit.core.jdbc; +package com.github.mjeanroy.dbunit.commons.jdbc; import com.github.mjeanroy.dbunit.exception.JdbcException; import com.github.mjeanroy.dbunit.loggers.Logger; @@ -36,14 +36,23 @@ import java.util.Collection; import java.util.List; -final class JdbcUtils { +/** + * Static JDBC Utilities, should only be used internally. + */ +public final class JdbcUtils { private static final Logger log = Loggers.getLogger(JdbcUtils.class); private JdbcUtils() { } - static void loadDriver(String driverClassName) { + /** + * Load JDBC Driver. + * + * @param driverClassName Driver classname. + * @throws JdbcException If JDBC driver cannot be loaded. + */ + public static void loadDriver(String driverClassName) { log.info("Loading driver: {}", driverClassName); try { @@ -54,7 +63,15 @@ static void loadDriver(String driverClassName) { } } - static ResultSet executeQuery(Connection connection, String query) { + /** + * Execute SQL Query and returns result. + * + * @param connection JDBC Connection. + * @param query SQL Query. + * @return JDBC Result Set. + * @throws JdbcException If the SQL cannot be executed. + */ + public static ResultSet executeQuery(Connection connection, String query) { log.debug("Executing query: {}", query); try { @@ -65,7 +82,16 @@ static ResultSet executeQuery(Connection connection, String query) { } } - static List executeQuery(Connection connection, String query, ResultSetMapFunction mapFunction) { + /** + * Execute SQL Query and returns mapped result. + * + * @param connection JDBC Connection. + * @param query SQL Query. + * @param mapFunction The resultset mapping function, executed for each returned rows. + * @return Results. + * @param Type of results being returned. + */ + public static List executeQuery(Connection connection, String query, ResultSetMapFunction mapFunction) { log.debug("Executing query: {}", query); try (ResultSet resultSet = connection.createStatement().executeQuery(query)) { @@ -82,24 +108,33 @@ static List executeQuery(Connection connection, String query, ResultSetMa } } - static void executeUpdates(Connection connection, Collection queries) { - log.debug("Executing queries: {}", queries); - for (String query : queries) { - executeUpdate(connection, query); + /** + * Execute SQL Queries in a batch statement. + * + * @param connection JDBC Connection. + * @param queries SQL Queries. + */ + public static void executeQueries(Connection connection, Collection queries) { + if (queries.isEmpty()) { + return; + } + + if (queries.size() == 1) { + executeQuery(connection, queries.iterator().next()); + return; } - } - static void executeUpdate(Connection connection, String query) { - log.debug("Executing update: {}", query); + log.debug("Executing batched queries: {}", queries); try (Statement statement = connection.createStatement()) { - statement.executeUpdate(query); + for (String query : queries) { + log.debug("Adding batched query: {}", query); + statement.addBatch(query); + } + + statement.executeBatch(); } catch (SQLException ex) { - throw new JdbcException("Cannot execute query: " + query, ex); + throw new JdbcException("Cannot execute queries: " + queries, ex); } } - - interface ResultSetMapFunction { - T apply(ResultSet resultSet) throws Exception; - } } diff --git a/src/main/java/com/github/mjeanroy/dbunit/commons/jdbc/ResultSetMapFunction.java b/src/main/java/com/github/mjeanroy/dbunit/commons/jdbc/ResultSetMapFunction.java new file mode 100644 index 00000000..44d2b4c7 --- /dev/null +++ b/src/main/java/com/github/mjeanroy/dbunit/commons/jdbc/ResultSetMapFunction.java @@ -0,0 +1,44 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015-2023 Mickael Jeanroy + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.github.mjeanroy.dbunit.commons.jdbc; + +import java.sql.ResultSet; + +/** + * Functional interface used to map {@link ResultSet} to another data structure. + * + * @param Mapped type. + */ +public interface ResultSetMapFunction { + + /** + * Map {@code resultSet} to another type. + * + * @param resultSet Input. + * @return Output. + * @throws Exception If an error occurred during mapping. + */ + T apply(ResultSet resultSet) throws Exception; +} diff --git a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/AbstractJdbcDropCreateForeignKeyManager.java b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/AbstractJdbcDropCreateForeignKeyManager.java index 86f4727a..130ffcd0 100644 --- a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/AbstractJdbcDropCreateForeignKeyManager.java +++ b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/AbstractJdbcDropCreateForeignKeyManager.java @@ -32,7 +32,7 @@ import java.util.ArrayList; import java.util.List; -import static com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.executeUpdates; +import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQueries; abstract class AbstractJdbcDropCreateForeignKeyManager implements JdbcForeignKeyManager { @@ -95,7 +95,7 @@ private void dropForeignKeys(Connection connection) { } try { - executeUpdates(connection, queries); + executeQueries(connection, queries); } catch (Exception ex) { log.error(ex.getMessage(), ex); @@ -119,7 +119,7 @@ private void reCreateForeignKeys(Connection connection) { } try { - executeUpdates(connection, queries); + executeQueries(connection, queries); } catch (Exception ex) { log.error(ex.getMessage(), ex); diff --git a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/InformationSchemaForeignKeyManager.java b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/InformationSchemaForeignKeyManager.java index fc6aad89..9713ca6b 100644 --- a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/InformationSchemaForeignKeyManager.java +++ b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/InformationSchemaForeignKeyManager.java @@ -25,7 +25,7 @@ package com.github.mjeanroy.dbunit.core.jdbc; import com.github.mjeanroy.dbunit.commons.lang.ToStringBuilder; -import com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.ResultSetMapFunction; +import com.github.mjeanroy.dbunit.commons.jdbc.ResultSetMapFunction; import java.sql.Connection; import java.sql.ResultSet; @@ -33,7 +33,7 @@ import java.util.Objects; import static com.github.mjeanroy.dbunit.commons.lang.PreConditions.notEmpty; -import static com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.executeQuery; +import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQuery; import static java.util.Collections.singletonList; /** diff --git a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/JdbcDefaultConnectionFactory.java b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/JdbcDefaultConnectionFactory.java index b1eb0907..c6e6dc32 100644 --- a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/JdbcDefaultConnectionFactory.java +++ b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/JdbcDefaultConnectionFactory.java @@ -24,6 +24,7 @@ package com.github.mjeanroy.dbunit.core.jdbc; +import com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils; import com.github.mjeanroy.dbunit.commons.lang.ToStringBuilder; import java.sql.Connection; diff --git a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/JdbcDriver.java b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/JdbcDriver.java index 3dc8bc5f..4697dfc0 100644 --- a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/JdbcDriver.java +++ b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/JdbcDriver.java @@ -24,6 +24,7 @@ package com.github.mjeanroy.dbunit.core.jdbc; +import com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils; import com.github.mjeanroy.dbunit.exception.JdbcException; import java.util.ArrayList; diff --git a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/MsSQLForeignKeyManager.java b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/MsSQLForeignKeyManager.java index 2d4f8cae..9f7cad81 100644 --- a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/MsSQLForeignKeyManager.java +++ b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/MsSQLForeignKeyManager.java @@ -25,7 +25,7 @@ package com.github.mjeanroy.dbunit.core.jdbc; import com.github.mjeanroy.dbunit.commons.lang.ToStringBuilder; -import com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.ResultSetMapFunction; +import com.github.mjeanroy.dbunit.commons.jdbc.ResultSetMapFunction; import java.sql.Connection; import java.sql.ResultSet; @@ -33,7 +33,7 @@ import java.util.Objects; import static com.github.mjeanroy.dbunit.commons.lang.PreConditions.notEmpty; -import static com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.executeQuery; +import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQuery; import static java.util.Collections.singletonList; /** diff --git a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/MySQLForeignKeyManager.java b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/MySQLForeignKeyManager.java index f3be2817..8040f418 100644 --- a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/MySQLForeignKeyManager.java +++ b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/MySQLForeignKeyManager.java @@ -25,7 +25,7 @@ package com.github.mjeanroy.dbunit.core.jdbc; import com.github.mjeanroy.dbunit.commons.lang.ToStringBuilder; -import com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.ResultSetMapFunction; +import com.github.mjeanroy.dbunit.commons.jdbc.ResultSetMapFunction; import java.sql.Connection; import java.sql.ResultSet; @@ -33,7 +33,7 @@ import java.util.Objects; import static com.github.mjeanroy.dbunit.commons.lang.PreConditions.notEmpty; -import static com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.executeQuery; +import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQuery; import static java.util.Collections.singletonList; /** diff --git a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/OracleForeignKeyManager.java b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/OracleForeignKeyManager.java index 335f6736..59401bf5 100644 --- a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/OracleForeignKeyManager.java +++ b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/OracleForeignKeyManager.java @@ -24,6 +24,7 @@ package com.github.mjeanroy.dbunit.core.jdbc; +import com.github.mjeanroy.dbunit.commons.jdbc.ResultSetMapFunction; import com.github.mjeanroy.dbunit.commons.lang.ToStringBuilder; import java.sql.Connection; @@ -32,7 +33,7 @@ import java.util.Objects; import static com.github.mjeanroy.dbunit.commons.lang.PreConditions.notEmpty; -import static com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.executeQuery; +import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQuery; import static java.util.Collections.singletonList; /** @@ -133,7 +134,7 @@ public String toString() { } } - private static final class ForeignKeyMapFunction implements JdbcUtils.ResultSetMapFunction { + private static final class ForeignKeyMapFunction implements ResultSetMapFunction { @Override public ForeignKey apply(ResultSet resultSet) throws Exception { diff --git a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/PostgresForeignKeyManager.java b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/PostgresForeignKeyManager.java index 10022dfa..74216203 100644 --- a/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/PostgresForeignKeyManager.java +++ b/src/main/java/com/github/mjeanroy/dbunit/core/jdbc/PostgresForeignKeyManager.java @@ -25,7 +25,7 @@ package com.github.mjeanroy.dbunit.core.jdbc; import com.github.mjeanroy.dbunit.commons.lang.ToStringBuilder; -import com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.ResultSetMapFunction; +import com.github.mjeanroy.dbunit.commons.jdbc.ResultSetMapFunction; import java.sql.Connection; import java.sql.ResultSet; @@ -33,7 +33,7 @@ import java.util.Objects; import static com.github.mjeanroy.dbunit.commons.lang.PreConditions.notEmpty; -import static com.github.mjeanroy.dbunit.core.jdbc.JdbcUtils.executeQuery; +import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQuery; import static java.util.Collections.singletonList; /** diff --git a/src/main/java/com/github/mjeanroy/dbunit/core/runner/SqlScriptExecutor.java b/src/main/java/com/github/mjeanroy/dbunit/core/runner/SqlScriptExecutor.java index ab3f6a21..6560e76e 100644 --- a/src/main/java/com/github/mjeanroy/dbunit/core/runner/SqlScriptExecutor.java +++ b/src/main/java/com/github/mjeanroy/dbunit/core/runner/SqlScriptExecutor.java @@ -32,8 +32,8 @@ import java.sql.Connection; import java.sql.SQLException; +import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQueries; import static com.github.mjeanroy.dbunit.commons.lang.PreConditions.notNull; -import static com.github.mjeanroy.dbunit.core.sql.SqlScriptParser.executeQueries; /** * Function to execute SQL scripts against SQL connection. diff --git a/src/main/java/com/github/mjeanroy/dbunit/core/sql/SqlScriptParser.java b/src/main/java/com/github/mjeanroy/dbunit/core/sql/SqlScriptParser.java index 6198fc48..4fe0b073 100644 --- a/src/main/java/com/github/mjeanroy/dbunit/core/sql/SqlScriptParser.java +++ b/src/main/java/com/github/mjeanroy/dbunit/core/sql/SqlScriptParser.java @@ -37,6 +37,7 @@ import java.util.List; import static com.github.mjeanroy.dbunit.commons.io.Io.readLines; +import static com.github.mjeanroy.dbunit.commons.jdbc.JdbcUtils.executeQueries; import static com.github.mjeanroy.dbunit.commons.lang.Objects.firstNonNull; /** @@ -144,17 +145,4 @@ public static void executeScript(Connection connection, String sqlFilePath, SqlS List queries = parseScript(sqlFilePath, configuration); executeQueries(connection, queries); } - - /** - * Execute SQL Queries. - * - * @param connection SQL Connection. - * @param queries SQL Queries. - * @throws SQLException If an error occurred while executing queries. - */ - public static void executeQueries(Connection connection, List queries) throws SQLException { - for (String query : queries) { - connection.prepareStatement(query).execute(); - } - } } diff --git a/src/test/java/com/github/mjeanroy/dbunit/core/runner/SqlScriptRunnerFunctionTest.java b/src/test/java/com/github/mjeanroy/dbunit/core/runner/SqlScriptRunnerFunctionTest.java index cba9abca..6d21a673 100644 --- a/src/test/java/com/github/mjeanroy/dbunit/core/runner/SqlScriptRunnerFunctionTest.java +++ b/src/test/java/com/github/mjeanroy/dbunit/core/runner/SqlScriptRunnerFunctionTest.java @@ -25,7 +25,7 @@ package com.github.mjeanroy.dbunit.core.runner; import com.github.mjeanroy.dbunit.core.jdbc.JdbcConnectionFactory; -import com.github.mjeanroy.dbunit.exception.DbUnitException; +import com.github.mjeanroy.dbunit.exception.JdbcException; import com.github.mjeanroy.dbunit.tests.jupiter.EmbeddedDatabaseTest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -33,15 +33,15 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; import java.sql.Connection; -import java.sql.SQLException; import static com.github.mjeanroy.dbunit.tests.db.TestDbUtils.countMovies; import static com.github.mjeanroy.dbunit.tests.db.TestDbUtils.countUsers; import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -49,24 +49,23 @@ @EmbeddedDatabaseTest class SqlScriptRunnerFunctionTest { - private SqlScript sqlScript; private JdbcConnectionFactory factory; + private Connection connection; @BeforeEach - void setUp(EmbeddedDatabase db) { + void setUp(EmbeddedDatabase db) throws Exception { initFactory(db); - initSqlScript(); } - private void initFactory(EmbeddedDatabase db) { - factory = mock(JdbcConnectionFactory.class); - when(factory.getConnection()).thenAnswer((Answer) invocationOnMock -> - db.getConnection() - ); - } + @Test + void it_should_load_script(EmbeddedDatabase db) throws Exception { + SqlScriptExecutor executor = new SqlScriptExecutor(factory); + Connection connection = db.getConnection(); + + assertThat(countUsers(connection)).isZero(); + assertThat(countMovies(connection)).isZero(); - private void initSqlScript() { - sqlScript = new SqlScript(asList( + SqlScript sqlScript = new SqlScript(asList( "INSERT INTO users VALUES(1, 'John Doe');", "INSERT INTO users VALUES(2, 'Jane Doe');", @@ -74,15 +73,6 @@ private void initSqlScript() { "INSERT INTO movies VALUES(2, 'Lord Of The Rings', NULL);", "INSERT INTO movies VALUES(3, 'Back To The Future', 'The story of Marty MacFly');" )); - } - - @Test - void it_should_load_script(EmbeddedDatabase db) throws Exception { - SqlScriptExecutor executor = new SqlScriptExecutor(factory); - Connection connection = db.getConnection(); - - assertThat(countUsers(connection)).isZero(); - assertThat(countMovies(connection)).isZero(); executor.execute(sqlScript); @@ -93,14 +83,21 @@ void it_should_load_script(EmbeddedDatabase db) throws Exception { } @Test - void it_should_wrap_sql_exception() throws Exception { - Connection connection = mock(Connection.class); + void it_should_wrap_sql_exception(EmbeddedDatabase db) throws Exception { SqlScriptExecutor executor = new SqlScriptExecutor(factory); + SqlScript sqlScript = new SqlScript( + singletonList("INVALID SQL QUERY") + ); - when(connection.prepareStatement(anyString())).thenThrow(new SQLException("Fail Test")); - when(factory.getConnection()).thenReturn(connection); - - assertThatThrownBy(() -> executor.execute(sqlScript)).isExactlyInstanceOf(DbUnitException.class); + assertThatThrownBy(() -> executor.execute(sqlScript)).isExactlyInstanceOf(JdbcException.class); verify(connection).close(); } + + private void initFactory(EmbeddedDatabase db) throws Exception { + factory = mock(JdbcConnectionFactory.class); + connection = spy(db.getConnection()); + when(factory.getConnection()).thenAnswer((Answer) invocationOnMock -> + connection + ); + } } diff --git a/src/test/java/com/github/mjeanroy/dbunit/core/sql/SqlScriptParserTest.java b/src/test/java/com/github/mjeanroy/dbunit/core/sql/SqlScriptParserTest.java index 9b63750d..3a696fea 100644 --- a/src/test/java/com/github/mjeanroy/dbunit/core/sql/SqlScriptParserTest.java +++ b/src/test/java/com/github/mjeanroy/dbunit/core/sql/SqlScriptParserTest.java @@ -40,7 +40,6 @@ import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -193,12 +192,11 @@ void it_should_execute_sql_file() throws Exception { Resource resource = new ResourceMockBuilder().fromClasspath("/sql/schema.sql").build(); Connection connection = mock(Connection.class); PreparedStatement statement = mock(PreparedStatement.class); - - when(connection.prepareStatement(anyString())).thenReturn(statement); + when(connection.createStatement()).thenReturn(statement); SqlScriptParser.executeScript(connection, resource, configuration); - verifyExecutedQueries(connection, statement); + verifyExecutedQueries(statement); } @Test @@ -206,12 +204,11 @@ void it_should_execute_sql_file_path() throws Exception { String script = "/sql/schema.sql"; Connection connection = mock(Connection.class); PreparedStatement statement = mock(PreparedStatement.class); - - when(connection.prepareStatement(anyString())).thenReturn(statement); + when(connection.createStatement()).thenReturn(statement); SqlScriptParser.executeScript(connection, script, configuration); - verifyExecutedQueries(connection, statement); + verifyExecutedQueries(statement); } @Test @@ -220,11 +217,11 @@ void it_should_execute_sql_file_path_from_classpath() throws Exception { Connection connection = mock(Connection.class); PreparedStatement statement = mock(PreparedStatement.class); - when(connection.prepareStatement(anyString())).thenReturn(statement); + when(connection.createStatement()).thenReturn(statement); SqlScriptParser.executeScript(connection, script, configuration); - verifyExecutedQueries(connection, statement); + verifyExecutedQueries(statement); } @Test @@ -244,18 +241,17 @@ void it_should_execute_all_queries() throws Exception { Connection connection = mock(Connection.class); PreparedStatement statement = mock(PreparedStatement.class); - - when(connection.prepareStatement(anyString())).thenReturn(statement); + when(connection.createStatement()).thenReturn(statement); InputStream stream = createStream(query); SqlScriptParser.executeScript(connection, stream, configuration); - InOrder inOrder = inOrder(connection, statement); - inOrder.verify(connection).prepareStatement(q1); - inOrder.verify(statement).execute(); - inOrder.verify(connection).prepareStatement(q2); - inOrder.verify(statement).execute(); + InOrder inOrder = inOrder(statement); + inOrder.verify(statement).addBatch(q1); + inOrder.verify(statement).addBatch(q2); + inOrder.verify(statement).executeBatch(); + inOrder.verify(statement).close(); inOrder.verifyNoMoreInteractions(); } @@ -280,18 +276,18 @@ private static void verifyParsedQueries(List queries) { ); } - private static void verifyExecutedQueries(Connection connection, PreparedStatement statement) throws SQLException { - InOrder inOrder = inOrder(connection, statement); + private static void verifyExecutedQueries(PreparedStatement statement) throws SQLException { + InOrder inOrder = inOrder(statement); - verifyQueryExecution(inOrder, connection, statement, + inOrder.verify(statement).addBatch( "CREATE TABLE users (id INT PRIMARY KEY, name varchar(100));" ); - verifyQueryExecution(inOrder, connection, statement, + inOrder.verify(statement).addBatch( "CREATE TABLE movies (id INT PRIMARY KEY, title varchar(100), synopsys varchar(200));" ); - verifyQueryExecution(inOrder, connection, statement, + inOrder.verify(statement).addBatch( "CREATE TABLE users_movies ( " + " user_id INT, " + " movie_id INT, " + @@ -301,7 +297,7 @@ private static void verifyExecutedQueries(Connection connection, PreparedStateme ");" ); - verifyQueryExecution(inOrder, connection, statement, + inOrder.verify(statement).addBatch( "CREATE TABLE users_movies_events ( " + " user_id INT, " + " movie_id INT, " + @@ -311,19 +307,11 @@ private static void verifyExecutedQueries(Connection connection, PreparedStateme ");" ); + inOrder.verify(statement).executeBatch(); + inOrder.verify(statement).close(); inOrder.verifyNoMoreInteractions(); } - private static void verifyQueryExecution( - InOrder inOrder, - Connection connection, - PreparedStatement statement, - String query - ) throws SQLException { - inOrder.verify(connection).prepareStatement(query); - inOrder.verify(statement).execute(); - } - private static InputStream createStream(String query) { return new ByteArrayInputStream(query.getBytes(Charset.defaultCharset())); }