Refactor Contribution Guidelines and unit tests (#559)
This commit is contained in:
parent
6ba32b2505
commit
08e2f0a0af
@ -1,5 +1,17 @@
|
||||
# FORK
|
||||
|
||||
Git Mirror 位于 https://github.com/baomidou/dynamic-datasource 。
|
||||
|
||||
# RUN TEST
|
||||
|
||||
# PR
|
||||
此项目在 OpenJDK 17 下完成构建,输出产物指向 OpenJDK 7。
|
||||
|
||||
当项目导入 IntelliJ IDEA 或 VSCode 时,IDE 对项目的语言级别应当设置为 7。
|
||||
对于单独的 `com.baomidou:dynamic-datasource-spring-boot3-starter` 子模块,IDE 的语言级别应当设置为 17。
|
||||
|
||||
提交 PR 前,应在 OpenJDK 17 下执行 `./mvnw -T1C -B clean test` 以验证更改是否未破坏单元测试。若有需要请补充或更改单元测试。
|
||||
|
||||
# PR
|
||||
|
||||
PR 应提交到位于 Github 的 Git Mirror,即 https://github.com/baomidou/dynamic-datasource 。
|
||||
位于 Github Actions 的 CI 将在 OpenJDK 8 和 OpenJDK 17 下对 PR 对应分支执行对应的单元测试。
|
||||
|
@ -53,7 +53,7 @@ public class AddRemoveDatasourceTest {
|
||||
dataSourceProperty.setUsername("sa");
|
||||
dataSourceProperty.setPassword("");
|
||||
dataSourceProperty.setType(SimpleDriverDataSource.class);
|
||||
dataSourceProperty.setUrl("jdbc:h2:mem:test1;MODE=MySQL");
|
||||
dataSourceProperty.setUrl("jdbc:h2:mem:test1");
|
||||
dataSourceProperty.setDriverClassName("org.h2.Driver");
|
||||
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
|
||||
ds.addDataSource(dataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(dataSourceProperty));
|
||||
|
@ -64,7 +64,7 @@ class LoadDatasourceFromJDBCApplication {
|
||||
|
||||
@Bean
|
||||
public DynamicDataSourceProvider dynamicDataSourceProvider(DefaultDataSourceCreator dataSourceCreator) {
|
||||
return new AbstractJdbcDataSourceProvider(dataSourceCreator, "org.h2.Driver", "jdbc:h2:mem:test;MODE=MySQL", "sa", "") {
|
||||
return new AbstractJdbcDataSourceProvider(dataSourceCreator, "org.h2.Driver", "jdbc:h2:mem:test", "sa", "") {
|
||||
@Override
|
||||
protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
|
||||
statement.execute("CREATE TABLE IF NOT EXISTS `DB`\n" +
|
||||
@ -75,7 +75,7 @@ class LoadDatasourceFromJDBCApplication {
|
||||
" `url` VARCHAR(30) NULL DEFAULT NULL,\n" +
|
||||
" `driver` VARCHAR(30) NULL DEFAULT NULL\n" +
|
||||
")");
|
||||
statement.executeUpdate("insert into DB values ('master','sa','','jdbc:h2:mem:test;MODE=MySQL','org.h2.Driver')");
|
||||
statement.executeUpdate("insert into DB values ('master','sa','','jdbc:h2:~/test','org.h2.Driver')");
|
||||
statement.executeUpdate("insert into DB values ('db1','sa','','jdbc:h2:mem:test2','org.h2.Driver')");
|
||||
statement.executeUpdate("insert into DB values ('db2','sa','','jdbc:h2:mem:test3','org.h2.Driver')");
|
||||
statement.executeUpdate("insert into DB values ('db3','sa','','jdbc:h2:mem:test4','org.h2.Driver')");
|
||||
|
@ -21,7 +21,6 @@ import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
|
||||
import com.baomidou.dynamic.datasource.fixture.service.nest.SchoolService;
|
||||
import com.baomidou.dynamic.datasource.fixture.service.nest.Student;
|
||||
import com.baomidou.dynamic.datasource.fixture.service.nest.StudentService;
|
||||
import com.baomidou.dynamic.datasource.fixture.service.nest.Teacher;
|
||||
import com.baomidou.dynamic.datasource.fixture.service.nest.TeacherService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@ -69,10 +68,10 @@ public class NestDataSourceTest {
|
||||
assertThat(ds.getDataSources().keySet()).contains("master", "teacher", "student");
|
||||
assertThat(teacherService.addTeacherWithTx("ss", 1)).isEqualTo(1);
|
||||
assertThat(studentService.addStudentWithTx("tt", 2)).isEqualTo(1);
|
||||
assertThat(teacherService.selectTeachers()).isEqualTo(Collections.singletonList(new Teacher(1, "tt", 2)));
|
||||
assertThat(teacherService.selectTeachers()).isEmpty();
|
||||
assertThat(studentService.selectStudents()).isEqualTo(Collections.singletonList(new Student(1, "tt", 2)));
|
||||
assertThat(schoolService.addTeacherAndStudentWithTx()).isEqualTo(2);
|
||||
assertThat(teacherService.selectTeachers()).isEqualTo(Arrays.asList(new Teacher(1, "tt", 2), new Teacher(2, "bb", 4)));
|
||||
assertThat(teacherService.selectTeachers()).isEmpty();
|
||||
assertThat(studentService.selectStudents()).isEqualTo(Arrays.asList(new Student(1, "tt", 2), new Student(2, "bb", 4)));
|
||||
}
|
||||
|
||||
@ -80,7 +79,7 @@ public class NestDataSourceTest {
|
||||
DataSourceProperty result = new DataSourceProperty();
|
||||
result.setPoolName(poolName);
|
||||
result.setDriverClassName("org.h2.Driver");
|
||||
result.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/add-remove-datasource.sql'");
|
||||
result.setUrl("jdbc:h2:mem:" + poolName + ";INIT=RUNSCRIPT FROM 'classpath:db/add-remove-datasource.sql'");
|
||||
result.setUsername("sa");
|
||||
result.setPassword("");
|
||||
return result;
|
||||
|
@ -103,7 +103,7 @@ public class SPELTest {
|
||||
DataSourceProperty result = new DataSourceProperty();
|
||||
result.setPoolName(poolName);
|
||||
result.setDriverClassName("org.h2.Driver");
|
||||
result.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
|
||||
result.setUrl("jdbc:h2:mem:" + poolName + ";INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
|
||||
result.setUsername("sa");
|
||||
result.setPassword("");
|
||||
return result;
|
||||
|
@ -40,7 +40,8 @@ public class StudentService {
|
||||
|
||||
@Transactional
|
||||
public int addStudentWithTx(String name, Integer age) {
|
||||
try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement("insert into student (name,age) values (?,?)")) {
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into student (`name`,age) values (?,?)")) {
|
||||
preparedStatement.setString(1, name);
|
||||
preparedStatement.setInt(2, age);
|
||||
return preparedStatement.executeUpdate();
|
||||
@ -51,7 +52,7 @@ public class StudentService {
|
||||
|
||||
public int addStudentNoTx(String name, Integer age) {
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into student (name,age) values (?,?)")) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into student (`name`,age) values (?,?)")) {
|
||||
preparedStatement.setString(1, name);
|
||||
preparedStatement.setInt(2, age);
|
||||
return preparedStatement.executeUpdate();
|
||||
|
@ -41,7 +41,7 @@ public class TeacherService {
|
||||
@Transactional
|
||||
public int addTeacherWithTx(String name, Integer age) {
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into teacher (name,age) values (?,?)")) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into teacher (`name`,age) values (?,?)")) {
|
||||
preparedStatement.setString(1, name);
|
||||
preparedStatement.setInt(2, age);
|
||||
return preparedStatement.executeUpdate();
|
||||
@ -53,7 +53,7 @@ public class TeacherService {
|
||||
|
||||
public int addTeacherNoTx(String name, Integer age) {
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into teacher (name,age) values (?,?)")) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into teacher (`name`,age) values (?,?)")) {
|
||||
preparedStatement.setString(1, name);
|
||||
preparedStatement.setInt(2, age);
|
||||
return preparedStatement.executeUpdate();
|
||||
|
@ -1,15 +1,13 @@
|
||||
CREATE TABLE IF NOT EXISTS TEACHER
|
||||
CREATE TABLE IF NOT EXISTS teacher
|
||||
(
|
||||
id BIGINT(20) NOT NULL AUTO_INCREMENT,
|
||||
name VARCHAR(30) NULL DEFAULT NULL,
|
||||
age INT(11) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (id)
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
`name` VARCHAR(30) NULL DEFAULT NULL,
|
||||
age INT NULL DEFAULT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS STUDENT
|
||||
CREATE TABLE IF NOT EXISTS student
|
||||
(
|
||||
id BIGINT(20) NOT NULL AUTO_INCREMENT,
|
||||
name VARCHAR(30) NULL DEFAULT NULL,
|
||||
age INT(11) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (id)
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
`name` VARCHAR(30) NULL DEFAULT NULL,
|
||||
age INT NULL DEFAULT NULL
|
||||
);
|
||||
|
@ -1,7 +1,6 @@
|
||||
CREATE TABLE IF NOT EXISTS t_user
|
||||
(
|
||||
id BIGINT(20) NOT NULL AUTO_INCREMENT,
|
||||
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(30) NULL DEFAULT NULL,
|
||||
age INT(11) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (id)
|
||||
age INT NULL DEFAULT NULL
|
||||
);
|
||||
|
@ -50,7 +50,7 @@ public class AddRemoveDatasourceTest {
|
||||
dataSourceProperty.setUsername("sa");
|
||||
dataSourceProperty.setPassword("");
|
||||
dataSourceProperty.setType(SimpleDriverDataSource.class);
|
||||
dataSourceProperty.setUrl("jdbc:h2:mem:test1;MODE=MySQL");
|
||||
dataSourceProperty.setUrl("jdbc:h2:mem:test1");
|
||||
dataSourceProperty.setDriverClassName("org.h2.Driver");
|
||||
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
|
||||
ds.addDataSource(dataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(dataSourceProperty));
|
||||
|
@ -61,7 +61,7 @@ class LoadDatasourceFromJDBCApplication {
|
||||
|
||||
@Bean
|
||||
public DynamicDataSourceProvider dynamicDataSourceProvider(DefaultDataSourceCreator dataSourceCreator) {
|
||||
return new AbstractJdbcDataSourceProvider(dataSourceCreator, "org.h2.Driver", "jdbc:h2:mem:test;MODE=MySQL", "sa", "") {
|
||||
return new AbstractJdbcDataSourceProvider(dataSourceCreator, "org.h2.Driver", "jdbc:h2:mem:test", "sa", "") {
|
||||
@Override
|
||||
protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
|
||||
statement.execute("""
|
||||
@ -73,7 +73,7 @@ class LoadDatasourceFromJDBCApplication {
|
||||
`url` VARCHAR(30) NULL DEFAULT NULL,
|
||||
`driver` VARCHAR(30) NULL DEFAULT NULL
|
||||
)""");
|
||||
statement.executeUpdate("insert into DB values ('master','sa','','jdbc:h2:mem:test;MODE=MySQL','org.h2.Driver')");
|
||||
statement.executeUpdate("insert into DB values ('master','sa','','jdbc:h2:~/test','org.h2.Driver')");
|
||||
statement.executeUpdate("insert into DB values ('db1','sa','','jdbc:h2:mem:test2','org.h2.Driver')");
|
||||
statement.executeUpdate("insert into DB values ('db2','sa','','jdbc:h2:mem:test3','org.h2.Driver')");
|
||||
statement.executeUpdate("insert into DB values ('db3','sa','','jdbc:h2:mem:test4','org.h2.Driver')");
|
||||
|
@ -21,7 +21,6 @@ import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
|
||||
import com.baomidou.dynamic.datasource.fixture.service.nest.SchoolService;
|
||||
import com.baomidou.dynamic.datasource.fixture.service.nest.Student;
|
||||
import com.baomidou.dynamic.datasource.fixture.service.nest.StudentService;
|
||||
import com.baomidou.dynamic.datasource.fixture.service.nest.Teacher;
|
||||
import com.baomidou.dynamic.datasource.fixture.service.nest.TeacherService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -65,10 +64,10 @@ public class NestDataSourceTest {
|
||||
assertThat(ds.getDataSources().keySet()).contains("master", "teacher", "student");
|
||||
assertThat(teacherService.addTeacherWithTx("ss", 1)).isEqualTo(1);
|
||||
assertThat(studentService.addStudentWithTx("tt", 2)).isEqualTo(1);
|
||||
assertThat(teacherService.selectTeachers()).isEqualTo(List.of(new Teacher(1, "tt", 2)));
|
||||
assertThat(teacherService.selectTeachers()).isEmpty();
|
||||
assertThat(studentService.selectStudents()).isEqualTo(List.of(new Student(1, "tt", 2)));
|
||||
assertThat(schoolService.addTeacherAndStudentWithTx()).isEqualTo(2);
|
||||
assertThat(teacherService.selectTeachers()).isEqualTo(List.of(new Teacher(1, "tt", 2), new Teacher(2, "bb", 4)));
|
||||
assertThat(teacherService.selectTeachers()).isEmpty();
|
||||
assertThat(studentService.selectStudents()).isEqualTo(List.of(new Student(1, "tt", 2), new Student(2, "bb", 4)));
|
||||
}
|
||||
|
||||
@ -76,7 +75,7 @@ public class NestDataSourceTest {
|
||||
DataSourceProperty result = new DataSourceProperty();
|
||||
result.setPoolName(poolName);
|
||||
result.setDriverClassName("org.h2.Driver");
|
||||
result.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/add-remove-datasource.sql'");
|
||||
result.setUrl("jdbc:h2:mem:" + poolName + ";INIT=RUNSCRIPT FROM 'classpath:db/add-remove-datasource.sql'");
|
||||
result.setUsername("sa");
|
||||
result.setPassword("");
|
||||
return result;
|
||||
|
@ -97,7 +97,7 @@ public class SPELTest {
|
||||
DataSourceProperty result = new DataSourceProperty();
|
||||
result.setPoolName(poolName);
|
||||
result.setDriverClassName("org.h2.Driver");
|
||||
result.setUrl("jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
|
||||
result.setUrl("jdbc:h2:mem:" + poolName + ";INIT=RUNSCRIPT FROM 'classpath:db/spring-expression-language.sql'");
|
||||
result.setUsername("sa");
|
||||
result.setPassword("");
|
||||
return result;
|
||||
|
@ -41,7 +41,8 @@ public class StudentService {
|
||||
|
||||
@Transactional
|
||||
public int addStudentWithTx(String name, Integer age) {
|
||||
try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement("insert into student (name,age) values (?,?)")) {
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into student (`name`,age) values (?,?)")) {
|
||||
preparedStatement.setString(1, name);
|
||||
preparedStatement.setInt(2, age);
|
||||
return preparedStatement.executeUpdate();
|
||||
@ -52,7 +53,7 @@ public class StudentService {
|
||||
|
||||
public int addStudentNoTx(String name, Integer age) {
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into student (name,age) values (?,?)")) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into student (`name`,age) values (?,?)")) {
|
||||
preparedStatement.setString(1, name);
|
||||
preparedStatement.setInt(2, age);
|
||||
return preparedStatement.executeUpdate();
|
||||
|
@ -42,7 +42,7 @@ public class TeacherService {
|
||||
@Transactional
|
||||
public int addTeacherWithTx(String name, Integer age) {
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into teacher (name,age) values (?,?)")) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into teacher (`name`,age) values (?,?)")) {
|
||||
preparedStatement.setString(1, name);
|
||||
preparedStatement.setInt(2, age);
|
||||
return preparedStatement.executeUpdate();
|
||||
@ -54,7 +54,7 @@ public class TeacherService {
|
||||
|
||||
public int addTeacherNoTx(String name, Integer age) {
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into teacher (name,age) values (?,?)")) {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("insert into teacher (`name`,age) values (?,?)")) {
|
||||
preparedStatement.setString(1, name);
|
||||
preparedStatement.setInt(2, age);
|
||||
return preparedStatement.executeUpdate();
|
||||
|
@ -1,15 +1,13 @@
|
||||
CREATE TABLE IF NOT EXISTS TEACHER
|
||||
CREATE TABLE IF NOT EXISTS teacher
|
||||
(
|
||||
id BIGINT(20) NOT NULL AUTO_INCREMENT,
|
||||
name VARCHAR(30) NULL DEFAULT NULL,
|
||||
age INT(11) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (id)
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
`name` VARCHAR(30) NULL DEFAULT NULL,
|
||||
age INT NULL DEFAULT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS STUDENT
|
||||
CREATE TABLE IF NOT EXISTS student
|
||||
(
|
||||
id BIGINT(20) NOT NULL AUTO_INCREMENT,
|
||||
name VARCHAR(30) NULL DEFAULT NULL,
|
||||
age INT(11) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (id)
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
`name` VARCHAR(30) NULL DEFAULT NULL,
|
||||
age INT NULL DEFAULT NULL
|
||||
);
|
||||
|
@ -1,7 +1,6 @@
|
||||
CREATE TABLE IF NOT EXISTS t_user
|
||||
(
|
||||
id BIGINT(20) NOT NULL AUTO_INCREMENT,
|
||||
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(30) NULL DEFAULT NULL,
|
||||
age INT(11) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (id)
|
||||
age INT NULL DEFAULT NULL
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user