Skip to content

Commit

Permalink
Refactor Contribution Guidelines and unit tests (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
linghengqian authored Sep 1, 2023
1 parent 6ba32b2 commit 08e2f0a
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 51 deletions.
14 changes: 13 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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 对应分支执行对应的单元测试。
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testAddAndRemoveDataSource() {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void main(String[] args) {

@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" +
Expand All @@ -75,7 +75,7 @@ protected Map<String, DataSourceProperty> executeStmt(Statement statement) throw
" `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')");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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;
Expand Down Expand Up @@ -69,18 +68,18 @@ public void testNest() {
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)));
}

private DataSourceProperty createDataSourceProperty(String poolName) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private DataSourceProperty createDataSourceProperty(String poolName) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public StudentService(DataSource dataSource) {

@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();
Expand All @@ -51,7 +52,7 @@ public int addStudentWithTx(String name, Integer age) {

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public TeacherService(DataSource dataSource) {
@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();
Expand All @@ -53,7 +53,7 @@ public int addTeacherWithTx(String name, Integer age) {

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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
);
Original file line number Diff line number Diff line change
@@ -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
);
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void testAddAndRemoveDataSource() {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void main(String[] args) {

@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("""
Expand All @@ -73,7 +73,7 @@ protected Map<String, DataSourceProperty> executeStmt(Statement statement) throw
`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')");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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;
Expand Down Expand Up @@ -65,18 +64,18 @@ void testNest() {
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)));
}

private DataSourceProperty createDataSourceProperty(String poolName) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private DataSourceProperty createDataSourceProperty(String poolName) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public StudentService(DataSource dataSource) {

@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();
Expand All @@ -52,7 +53,7 @@ public int addStudentWithTx(String name, Integer age) {

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public TeacherService(DataSource dataSource) {
@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();
Expand All @@ -54,7 +54,7 @@ public int addTeacherWithTx(String name, Integer age) {

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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
);
Original file line number Diff line number Diff line change
@@ -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
);

0 comments on commit 08e2f0a

Please sign in to comment.