* BAEL-6801 (#4)

Validate string length with Java validation annotations

* Bael-6870, Executing SQL Script File in Java

* Bael-6870, Fixed formatting on pom.xml

* Bael-6870, Fixing Jenkins Build Failure

* Bael-6870, PR Review comments

* Bael-6870, PR Review comments
This commit is contained in:
parthiv39731
2023-08-21 19:23:25 +05:30
committed by GitHub
parent f091132895
commit a87c1cce8f
8 changed files with 343 additions and 0 deletions
@@ -0,0 +1,46 @@
package com.baeldung.script;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MyBatisScriptUtilityUnitTest {
private static final Log logger = LogFactory.getLog(MyBatisScriptUtilityUnitTest.class);
private static Connection connection = null;
private static final String JDBC_URL = "jdbc:h2:mem:testdb1";
private static final String USERNAME = "user";
private static final String PASSWORD = "password";
@Before
public void prepareConnection() throws Exception {
connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
}
@AfterAll
public static void closeConnection() throws Exception {
connection.close();
}
@Test
public void givenConnectionObject_whenSQLFile_thenExecute() throws Exception {
String path = new File(ClassLoader.getSystemClassLoader()
.getResource("employee.sql").getFile()).toPath().toString();
MyBatisScriptUtility.runScript(path, connection);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT COUNT(1) FROM employees");
if (resultSet.next()) {
int count = resultSet.getInt(1);
Assert.assertEquals("Incorrect number of records inserted", 20, count);
}
}
}
@@ -0,0 +1,47 @@
package com.baeldung.script;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class SpringScriptUtilityUnitTest {
private static final Log logger = LogFactory.getLog(SpringScriptUtilityUnitTest.class);
private static Connection connection = null;
private static final String JDBC_URL = "jdbc:h2:mem:testdb2";
private static final String USERNAME = "user";
private static final String PASSWORD = "password";
@Before
public void prepareConnection() throws Exception {
connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
}
@AfterAll
public static void closeConnection() throws Exception {
connection.close();
}
@Test
public void givenConnectionObject_whenSQLFile_thenExecute() throws Exception {
String path = new File(ClassLoader.getSystemClassLoader()
.getResource("employee.sql").getFile()).toPath().toString();
SpringScriptUtility.runScript(path, connection);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT COUNT(1) FROM employees");
if (resultSet.next()) {
int count = resultSet.getInt(1);
Assert.assertEquals("Incorrect number of records inserted", 20, count);
}
}
}
@@ -0,0 +1,47 @@
package com.baeldung.script;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class SqlScriptBatchExecutorUnitTest {
private static final Log logger = LogFactory.getLog(SqlScriptBatchExecutorUnitTest.class);
private static Connection connection = null;
private static final String JDBC_URL = "jdbc:h2:mem:testdb3";
private static final String USERNAME = "user";
private static final String PASSWORD = "password";
@Before
public void prepareConnection() throws Exception {
connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
}
@AfterAll
public static void closeConnection() throws Exception {
connection.close();
}
@Test
public void givenConnectionObject_whenSQLFile_thenExecute() throws Exception {
String path = new File(ClassLoader.getSystemClassLoader()
.getResource("employee.sql").getFile()).toPath().toString();
SqlScriptBatchExecutor.executeBatchedSQL(path, connection, 10);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT COUNT(1) FROM employees");
if (resultSet.next()) {
int count = resultSet.getInt(1);
Assert.assertEquals("Incorrect number of records inserted", 20, count);
}
}
}
@@ -0,0 +1,75 @@
/**
Script Name : Create employees script
Author: Parthiv Pradhan
**/
-- Create the employees table if it doesn't exist
CREATE TABLE employees (
id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
-- Insert employee records
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (1, 'John', 'Doe', 'HR', 50000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (2, 'Jane', 'Smith', 'IT', 60000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (3, 'Michael', 'Johnson', 'Finance', 55000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (4, 'Emily', 'Williams', 'Marketing', 52000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (5, 'David', 'Brown', 'IT', 65000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (6, 'Sarah', 'Miller', 'Finance', 58000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (7, 'Robert', 'Jones', 'HR', 53000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (8, 'Jessica', 'Davis', 'Marketing', 51000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (9, 'William', 'Wilson', 'IT', 59000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (10, 'Jennifer', 'Taylor', 'Finance', 57000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (11, 'Daniel', 'Anderson', 'Marketing', 54000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (12, 'Linda', 'Martinez', 'HR', 52000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (13, 'Christopher', 'Lopez', 'IT', 62000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (14, 'Karen', 'Hernandez', 'Finance', 56000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (15, 'Mark', 'Garcia', 'Marketing', 53000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (16, 'Patricia', 'Lee', 'HR', 51000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (17, 'Anthony', 'Clark', 'IT', 60000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (18, 'Maria', 'Lewis', 'Finance', 59000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (19, 'Paul', 'Walker', 'Marketing', 55000.00);
INSERT INTO employees (id, first_name, last_name, department, salary)
VALUES (20, 'Ruth', 'Young', 'HR', 54000.00);