|
| 1 | +package com.baeldung.sql; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import java.sql.Connection; |
| 7 | +import java.sql.DriverManager; |
| 8 | +import java.sql.ResultSet; |
| 9 | +import java.sql.SQLException; |
| 10 | +import java.sql.Statement; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +// Please note, this test requires a MySQL server running on localhost:3306 with database users_db already created. |
| 18 | +// We have added creation of the table it in the setup() method. |
| 19 | +public class MultipleSQLExecutionLiveTest { |
| 20 | + |
| 21 | + private static Connection connection; |
| 22 | + |
| 23 | + @Before |
| 24 | + public void setup() throws ClassNotFoundException, SQLException { |
| 25 | + Class.forName("com.mysql.cj.jdbc.Driver"); |
| 26 | + String url = "jdbc:mysql://localhost:3306/user_db?allowMultiQueries=true"; |
| 27 | + String username = "username"; |
| 28 | + String password = "password"; |
| 29 | + connection = DriverManager.getConnection(url, username, password); |
| 30 | + |
| 31 | + Statement statement = connection.createStatement(); |
| 32 | + String createUsersSql = "CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, email VARCHAR(100) NOT NULL UNIQUE );"; |
| 33 | + statement.execute(createUsersSql); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void givenMultipleStatements_whenExecuting_thenRecordsAreInserted() throws SQLException { |
| 38 | + MultipleSQLExecution execution = new MultipleSQLExecution(connection); |
| 39 | + boolean result = execution.executeMultipleStatements(); |
| 40 | + assertTrue(result, "The statements should execute successfully."); |
| 41 | + |
| 42 | + try (Statement statement = connection.createStatement(); |
| 43 | + ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) AS count FROM users WHERE name IN ('Alice', 'Bob')")) { |
| 44 | + resultSet.next(); |
| 45 | + int count = resultSet.getInt("count"); |
| 46 | + assertEquals(2, count, "Two records should have been inserted."); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void givenBatchProcessing_whenExecuting_thenRecordsAreInserted() throws SQLException { |
| 52 | + MultipleSQLExecution execution = new MultipleSQLExecution(connection); |
| 53 | + int[] updateCounts = execution.executeBatchProcessing(); |
| 54 | + assertEquals(2, updateCounts.length, "Batch processing should execute two statements."); |
| 55 | + |
| 56 | + try (Statement statement = connection.createStatement(); |
| 57 | + ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) AS count FROM users WHERE name IN ('Charlie', 'Diana')")) { |
| 58 | + resultSet.next(); |
| 59 | + int count = resultSet.getInt("count"); |
| 60 | + assertEquals(2, count, "Two records should have been inserted via batch."); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void givenStoredProcedure_whenCalling_thenRecordsAreInserted() throws SQLException { |
| 66 | + MultipleSQLExecution execution = new MultipleSQLExecution(connection); |
| 67 | + boolean result = execution.callStoredProcedure(); |
| 68 | + assertTrue(result, "The stored procedure should execute successfully."); |
| 69 | + |
| 70 | + try (Statement statement = connection.createStatement(); |
| 71 | + ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) AS count FROM users WHERE name IN ('Eve', 'Frank')")) { |
| 72 | + resultSet.next(); |
| 73 | + int count = resultSet.getInt("count"); |
| 74 | + assertEquals(2, count, "Stored procedure should have inserted two records."); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void givenMultipleSelectStatements_whenExecuting_thenCorrectUsersAreFetched() throws SQLException { |
| 80 | + MultipleSQLExecution execution = new MultipleSQLExecution(connection); |
| 81 | + execution.executeMultipleStatements(); |
| 82 | + |
| 83 | + List<User> users = execution.executeMultipleSelectStatements(); |
| 84 | + |
| 85 | + // Here we verify that exactly two users are fetched and their names match the expected ones |
| 86 | + assertThat(users) |
| 87 | + .hasSize(2) |
| 88 | + .extracting(User::getName) |
| 89 | + .containsExactlyInAnyOrder("Alice", "Bob"); |
| 90 | + } |
| 91 | +} |
| 92 | + |
0 commit comments