diff --git a/persistence-modules/spring-boot-persistence-h2-2/src/main/java/com/baeldung/h2execscript/H2ExecScriptDemoApplication.java b/persistence-modules/spring-boot-persistence-h2-2/src/main/java/com/baeldung/h2execscript/H2ExecScriptDemoApplication.java new file mode 100644 index 000000000000..14c95e98aa95 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2-2/src/main/java/com/baeldung/h2execscript/H2ExecScriptDemoApplication.java @@ -0,0 +1,11 @@ +package com.baeldung.h2execscript; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class H2ExecScriptDemoApplication { + public static void main(String... args) { + SpringApplication.run(H2ExecScriptDemoApplication.class, args); + } +} \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-h2-2/src/test/java/com/baeldung/h2execscript/H2ExecSqlIntegrationTest.java b/persistence-modules/spring-boot-persistence-h2-2/src/test/java/com/baeldung/h2execscript/H2ExecSqlIntegrationTest.java new file mode 100644 index 000000000000..10b116724756 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2-2/src/test/java/com/baeldung/h2execscript/H2ExecSqlIntegrationTest.java @@ -0,0 +1,64 @@ +package com.baeldung.h2execscript; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; + +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.transaction.annotation.Transactional; + +import jakarta.persistence.EntityManager; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(classes = H2ExecScriptDemoApplication.class) +@ActiveProfiles("h2-exec-sql") +@Transactional +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class H2ExecSqlIntegrationTest { + + @Autowired + private EntityManager entityManager; + + @Test + void whenUsingRunscriptInJdbcUrl_thenSqlExecuted() { + List expectedTaskNames = List.of("Start the application", "Check if data table is filled"); + List taskNames = entityManager.createNativeQuery("SELECT NAME FROM TASK_TABLE ORDER BY ID") + .getResultStream() + .map(Object::toString) + .toList(); + assertEquals(expectedTaskNames, taskNames); + } + + @Test + @Order(1) + void whenSpringAutoDetectSchemaAndDataSql_thenSqlExecuted() { + List expectedCityNames = List.of("New York", "Hamburg", "Shanghai"); + List cityNames = entityManager.createNativeQuery("SELECT NAME FROM CITY ORDER BY ID") + .getResultStream() + .map(Object::toString) + .toList(); + assertEquals(expectedCityNames, cityNames); + } + + @Test + @Order(2) + void whenRunscriptInNativeQuery_thenSqlExecuted() { + entityManager.createNativeQuery("RUNSCRIPT FROM 'classpath:/sql/add_cities.sql'") + .executeUpdate(); + List expectedCityNames = List.of("New York", "Hamburg", "Shanghai", "Paris", "Berlin", "Tokyo"); + List cityNames = entityManager.createNativeQuery("SELECT NAME FROM CITY ORDER BY ID") + .getResultStream() + .map(Object::toString) + .toList(); + assertEquals(expectedCityNames, cityNames); + } + +} \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/application-h2-exec-sql.yml b/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/application-h2-exec-sql.yml new file mode 100644 index 000000000000..a3e516bb167c --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/application-h2-exec-sql.yml @@ -0,0 +1,6 @@ +spring: + datasource: + driverClassName: org.h2.Driver + url: jdbc:h2:mem:demodb;INIT=RUNSCRIPT FROM 'classpath:/sql/init_my_db.sql' + username: sa + password: \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/data.sql b/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/data.sql new file mode 100644 index 000000000000..f79fd44fabed --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/data.sql @@ -0,0 +1,3 @@ +INSERT INTO CITY (ID, NAME) VALUES (1, 'New York'); +INSERT INTO CITY (ID, NAME) VALUES (2, 'Hamburg'); +INSERT INTO CITY (ID, NAME) VALUES (3, 'Shanghai'); \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/schema.sql b/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/schema.sql new file mode 100644 index 000000000000..160ea4fc1751 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/schema.sql @@ -0,0 +1,5 @@ +CREATE TABLE CITY +( + ID INT PRIMARY KEY , + NAME VARCHAR(255) +); \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/sql/add_cities.sql b/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/sql/add_cities.sql new file mode 100644 index 000000000000..787f95952347 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/sql/add_cities.sql @@ -0,0 +1,3 @@ +INSERT INTO CITY (ID, NAME) VALUES (4, 'Paris'); +INSERT INTO CITY (ID, NAME) VALUES (5, 'Berlin'); +INSERT INTO CITY (ID, NAME) VALUES (6, 'Tokyo'); \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/sql/init_my_db.sql b/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/sql/init_my_db.sql new file mode 100644 index 000000000000..d3bd58a93c10 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2-2/src/test/resources/sql/init_my_db.sql @@ -0,0 +1,8 @@ +CREATE TABLE TASK_TABLE +( + ID INT PRIMARY KEY, + NAME VARCHAR(255) +); + +INSERT INTO TASK_TABLE (ID, NAME) VALUES (1, 'Start the application'); +INSERT INTO TASK_TABLE (ID, NAME) VALUES (2, 'Check if data table is filled'); \ No newline at end of file