Skip to content

Commit

Permalink
Refactor dynamic-datasource-spring-boot-starter unit test and Contr…
Browse files Browse the repository at this point in the history
…ibutor Guides to fix CI
  • Loading branch information
linghengqian committed Sep 5, 2023
1 parent 289e860 commit bd6791b
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 84 deletions.
60 changes: 55 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
# FORK
# 1. FORK

Git Mirror 位于 https://github.com/baomidou/dynamic-datasource

# RUN TEST
# 2. RUN TEST

此项目在 OpenJDK 17 下完成构建,输出产物指向 OpenJDK 7
此项目在 OpenJDK 17 下完成构建,输出产物指向 OpenJDK 8

当项目导入 IntelliJ IDEA 或 VSCode 时,IDE 对项目的语言级别应当设置为 7
当项目导入 IntelliJ IDEA 或 VSCode 时,IDE 对项目的语言级别应当设置为 8
对于单独的 `com.baomidou:dynamic-datasource-spring-boot3-starter` 子模块,IDE 的语言级别应当设置为 17。

提交 PR 前,应在 OpenJDK 17 下执行 `./mvnw -T1C -B clean test` 以验证更改是否未破坏单元测试。若有需要请补充或更改单元测试。

# PR
假设贡献者处于新的 Ubuntu 22.04.3 LTS 实例下,其可通过如下 bash 命令来执行单元测试。

```bash
sudo apt install unzip zip curl sed -y
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install java 17.0.8-ms
sdk use java 17.0.8-ms

git clone [email protected]:baomidou/dynamic-datasource.git
cd ./dynamic-datasource/
./mvnw -T1C -B clean test
```

## 2.1. Execute NativeTest under GraalVM Native Image

项目的输出产物对在 GraalVM Native Image 下的可用性的验证,是通过 GraalVM Native Build Tools 的 Maven Plugin 子项目来完成的。
通过在 JVM 下运行单元测试,为单元测试打上 unique Id,此后构建为 GraalVM Native Image 进行 nativeTest 来测试在 GraalVM Native Image
下的单元测试覆盖率。

项目定义了 `generateMetadata` 的 Maven Profile 用于在普通 JVM 下携带 GraalVM Tracing Agent 执行单元测试,并在特定目录下生成或合并
已有的 GraalVM Reachability Metadata 文件。

项目定义了 `nativeTestInSpringBoot` 的 Maven Profile 用于为 `dynamic-datasource-spring-boot3-starter` 模块执行 nativeTest。

假设贡献者处于新的 Ubuntu 22.04.3 LTS 实例下,其可通过如下 bash 命令为 `dynamic-datasource-spring-boot3-starter` 子模块执行 nativeTest。

```bash
sudo apt install unzip zip curl sed -y
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install java 17.0.8-graalce
sdk use java 17.0.8-graalce
sudo apt-get install build-essential libz-dev zlib1g-dev -y

git clone [email protected]:baomidou/dynamic-datasource.git
cd ./dynamic-datasource/
./mvnw -T1C -B -PgenerateMetadata -DskipNativeTests clean test
./mvnw -am -pl dynamic-datasource-spring-boot3-starter -PnativeTestInSpringBoot -T1C -B clean test
```

贡献者在提交 PR 后,位于 Github Actions 的 CI 将进行此验证。相关 CI 的失败行为是现阶段是可接受的。

请不要为 SPEL 功能编写可能的 nativeTest,参考 https://github.com/spring-projects/spring-framework/issues/29548 。如有需要,
请使用 `org.graalvm.nativeimage.imagecode` 的 System Property 屏蔽相关测试在 GraalVM Native Image 下运行。

当贡献者发现缺少与 `dynamic-datasource` 无关的第三方库的 GraalVM Reachability Metadata 时,应当在
https://github.com/oracle/graalvm-reachability-metadata 打开新的 issue, 并提交包含依赖的第三方库缺失的 GraalVM Reachability
Metadata 的 PR。

# 3. PR

PR 应提交到位于 Github 的 Git Mirror,即 https://github.com/baomidou/dynamic-datasource
位于 Github Actions 的 CI 将在 OpenJDK 8 和 OpenJDK 17 下对 PR 对应分支执行对应的单元测试。
2 changes: 1 addition & 1 deletion dynamic-datasource-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,19 @@
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.hikaricp.HikariCpConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.test.context.junit4.SpringRunner;

import javax.sql.DataSource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@SpringBootTest(classes = AddRemoveDatasourceApplication.class, webEnvironment = RANDOM_PORT)
@RunWith(SpringRunner.class)
public class AddRemoveDatasourceTest {

@Autowired
Expand All @@ -44,7 +41,7 @@ public class AddRemoveDatasourceTest {
DefaultDataSourceCreator dataSourceCreator;

@Test
public void testAddAndRemoveDataSource() {
void testAddAndRemoveDataSource() {
HikariCpConfig hikariCpConfig = new HikariCpConfig();
hikariCpConfig.setConnectionTestQuery("select 1");
DataSourceProperty dataSourceProperty = new DataSourceProperty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.provider.AbstractJdbcDataSourceProvider;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.test.context.junit4.SpringRunner;

import javax.sql.DataSource;
import java.sql.ResultSet;
Expand All @@ -41,14 +39,13 @@
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@SpringBootTest(classes = LoadDatasourceFromJDBCApplication.class, webEnvironment = RANDOM_PORT)
@RunWith(SpringRunner.class)
public class LoadDatasourceFromJDBCTest {

@Autowired
DataSource dataSource;

@Test
public void testExistDataSource() {
void testExistDataSource() {
DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
assertThat(ds.getDataSources().keySet()).contains("master", "db1", "db2", "db3");
}
Expand All @@ -68,13 +65,13 @@ public DynamicDataSourceProvider dynamicDataSourceProvider(DefaultDataSourceCrea
@Override
protected Map<String, DataSourceProperty> executeStmt(Statement statement) throws SQLException {
statement.execute("CREATE TABLE IF NOT EXISTS `DB`\n" +
"(\n" +
" `name` VARCHAR(30) NULL DEFAULT NULL,\n" +
" `username` VARCHAR(30) NULL DEFAULT NULL,\n" +
" `password` VARCHAR(30) NULL DEFAULT NULL,\n" +
" `url` VARCHAR(30) NULL DEFAULT NULL,\n" +
" `driver` VARCHAR(30) NULL DEFAULT NULL\n" +
")");
"(\n" +
" `name` VARCHAR(30) NULL DEFAULT NULL,\n" +
" `username` VARCHAR(30) NULL DEFAULT NULL,\n" +
" `password` VARCHAR(30) NULL DEFAULT NULL,\n" +
" `url` VARCHAR(30) NULL DEFAULT NULL,\n" +
" `driver` VARCHAR(30) NULL DEFAULT NULL\n" +
")");
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')");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@
import com.baomidou.dynamic.datasource.fixture.v1.service.nest.Student;
import com.baomidou.dynamic.datasource.fixture.v1.service.nest.StudentService;
import com.baomidou.dynamic.datasource.fixture.v1.service.nest.TeacherService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.sql.DataSource;
import java.util.Arrays;
Expand All @@ -38,7 +36,6 @@
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@SpringBootTest(classes = NestApplication.class, webEnvironment = RANDOM_PORT)
@RunWith(SpringRunner.class)
public class NestDataSourceTest {

@Autowired
Expand All @@ -57,7 +54,7 @@ public class NestDataSourceTest {
SchoolService schoolService;

@Test
public void testNest() {
void testNest() {
DataSourceProperty masterDataSourceProperty = createDataSourceProperty("master");
DataSourceProperty teacherDataSourceProperty = createDataSourceProperty("teacher");
DataSourceProperty studentDataSourceProperty = createDataSourceProperty("student");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.fixture.v1.service.spel.CustomResultHandler;
import com.baomidou.dynamic.datasource.fixture.v1.service.spel.User;
import com.baomidou.dynamic.datasource.fixture.v1.service.spel.UserService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.web.context.WebApplicationContext;
Expand All @@ -38,21 +35,18 @@
import java.nio.charset.StandardCharsets;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@SpringBootTest(classes = SPELApplication.class, webEnvironment = RANDOM_PORT)
@RunWith(SpringRunner.class)
public class SPELTest {

MockMvc mockMvc;

@Autowired
private WebApplicationContext webApplicationContext;

@Autowired
DataSource dataSource;

Expand All @@ -62,15 +56,13 @@ public class SPELTest {
@Autowired
UserService userService;

@Before
public void setup() {
this.mockMvc = webAppContextSetup(webApplicationContext)
.alwaysDo(new CustomResultHandler())
.build();
@BeforeEach
void setup(WebApplicationContext webApplicationContext) {
this.mockMvc = webAppContextSetup(webApplicationContext).defaultResponseCharacterEncoding(StandardCharsets.UTF_8).build();
}

@Test
public void testSPEL() throws Exception {
void testSPEL() {
DataSourceProperty masterDataSourceProperty = createDataSourceProperty("master");
DataSourceProperty tenant1_1DataSourceProperty = createDataSourceProperty("tenant1_1");
DataSourceProperty tenant1_2DataSourceProperty = createDataSourceProperty("tenant1_2");
Expand All @@ -83,18 +75,20 @@ public void testSPEL() throws Exception {
ds.addDataSource(tenant2_1DataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(tenant2_1DataSourceProperty));
ds.addDataSource(tenant2_2DataSourceProperty.getPoolName(), dataSourceCreator.createDataSource(tenant2_2DataSourceProperty));
assertThat(ds.getDataSources().keySet()).contains("master", "tenant1_1", "tenant1_2", "tenant2_1", "tenant2_2");
mockMvc.perform(MockMvcRequestBuilders.get("/users/session").characterEncoding(StandardCharsets.UTF_8.name()))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().encoding(StandardCharsets.UTF_8.name()))
.andReturn().getResponse().getContentAsString();
mockMvc.perform(MockMvcRequestBuilders.get("/users/header").contentType(MediaType.APPLICATION_FORM_URLENCODED)
.header("tenantName", "tenant1")
.characterEncoding(StandardCharsets.UTF_8.name())
).andDo(print())
.andExpect(status().isOk())
.andExpect(content().encoding(StandardCharsets.UTF_8.name()))
.andReturn().getResponse().getContentAsString();
assertDoesNotThrow(() -> {
mockMvc.perform(MockMvcRequestBuilders.get("/users/session").characterEncoding(StandardCharsets.UTF_8))
.andDo(print()).andExpectAll(
status().isOk(),
content().encoding(StandardCharsets.UTF_8)
).andReturn().getResponse().getContentAsString();
mockMvc.perform(MockMvcRequestBuilders.get("/users/header").contentType(MediaType.APPLICATION_FORM_URLENCODED)
.header("tenantName", "tenant1")
.characterEncoding(StandardCharsets.UTF_8)
).andDo(print()).andExpectAll(
status().isOk(),
content().encoding(StandardCharsets.UTF_8)
).andReturn().getResponse().getContentAsString();
});
assertThat(userService.selectSpelByKey("tenant1")).isEqualTo("tenant1");
assertThat(userService.selecSpelByTenant(new User("tenant2"))).isEqualTo("tenant2");
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"pattern":"\\QMETA-INF/druid-filter.properties\\E"
}, {
"pattern":"\\QMETA-INF/services/ch.qos.logback.classic.spi.Configurator\\E"
}, {
"pattern":"\\QMETA-INF/services/io.undertow.servlet.ServletExtension\\E"
}, {
"pattern":"\\QMETA-INF/services/io.undertow.websockets.jsr.WebsocketClientSslProvider\\E"
}, {
"pattern":"\\QMETA-INF/services/java.sql.Driver\\E"
}, {
Expand All @@ -16,6 +20,8 @@
"pattern":"\\QMETA-INF/services/org.junit.platform.launcher.TestExecutionListener\\E"
}, {
"pattern":"\\QMETA-INF/services/org.slf4j.spi.SLF4JServiceProvider\\E"
}, {
"pattern":"\\QMETA-INF/services/org.xnio.XnioProvider\\E"
}, {
"pattern":"\\QMETA-INF/spring-autoconfigure-metadata.properties\\E"
}, {
Expand Down Expand Up @@ -79,15 +85,11 @@
}, {
"pattern":"\\Qdb/add-remove-datasource.sql\\E"
}, {
"pattern":"\\Qorg/apache/catalina/core/RestrictedFilters.properties\\E"
}, {
"pattern":"\\Qorg/apache/catalina/core/RestrictedListeners.properties\\E"
}, {
"pattern":"\\Qorg/apache/catalina/core/RestrictedServlets.properties\\E"
}, {
"pattern":"\\Qorg/apache/catalina/util/ServerInfo.properties\\E"
"pattern":"\\Qio/undertow/version.properties\\E"
}, {
"pattern":"\\Qorg/h2/util/data.zip\\E"
}, {
"pattern":"\\Qorg/jboss/threads/Version.properties\\E"
}, {
"pattern":"\\Qorg/json/JSONObject.class\\E"
}, {
Expand Down Expand Up @@ -378,6 +380,8 @@
"pattern":"\\Qorg/springframework/boot/context/properties/EnableConfigurationPropertiesRegistrar.class\\E"
}, {
"pattern":"\\Qorg/springframework/boot/sql/init/dependency/DatabaseInitializationDependencyConfigurer.class\\E"
}, {
"pattern":"\\Qorg/springframework/boot/web/server/mime-mappings.properties\\E"
}, {
"pattern":"\\Qorg/springframework/context/ApplicationContextAware.class\\E"
}, {
Expand Down Expand Up @@ -438,6 +442,10 @@
"pattern":"\\Qorg/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.class\\E"
}, {
"pattern":"\\Qorg/springframework/web/servlet/config/annotation/WebMvcConfigurer.class\\E"
}, {
"pattern":"\\Qorg/xnio/Version.properties\\E"
}, {
"pattern":"\\Qorg/xnio/nio/Version.properties\\E"
}]},
"bundles":[]
}
Loading

0 comments on commit bd6791b

Please sign in to comment.