这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String> expectedTaskNames = List.of("Start the application", "Check if data table is filled");
List<String> 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<String> expectedCityNames = List.of("New York", "Hamburg", "Shanghai");
List<String> 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<String> expectedCityNames = List.of("New York", "Hamburg", "Shanghai", "Paris", "Berlin", "Tokyo");
List<String> cityNames = entityManager.createNativeQuery("SELECT NAME FROM CITY ORDER BY ID")
.getResultStream()
.map(Object::toString)
.toList();
assertEquals(expectedCityNames, cityNames);
}

}
Original file line number Diff line number Diff line change
@@ -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:
Original file line number Diff line number Diff line change
@@ -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');
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE CITY
(
ID INT PRIMARY KEY ,
NAME VARCHAR(255)
);
Original file line number Diff line number Diff line change
@@ -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');
Original file line number Diff line number Diff line change
@@ -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');