Skip to content

Commit fe95525

Browse files
authored
Use MySQL instead of H2Database in ShardingSphere integration (#43)
1 parent 5840c1b commit fe95525

File tree

25 files changed

+323
-205
lines changed

25 files changed

+323
-205
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
cache: 'maven'
2626
- name: Build all test with Maven
2727
run: |
28-
./mvnw -T1C -B -e clean test
28+
./mvnw -T1C -e clean test
2929
test-minimum-hotspot-jdk-ci:
3030
name: Test CI - JDK ${{ matrix.java-version }} on ${{ matrix.os }}
3131
runs-on: ${{ matrix.os }}
@@ -50,6 +50,6 @@ jobs:
5050
./mvnw -am -pl orm-samples/mybatis-sample -T1C -B -e clean test
5151
- name: Build third-part test with Maven
5252
run: |
53-
./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample -T1C -B -e clean test
54-
./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample -T1C -B -e clean test
55-
./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample -T1C -B -e clean test
53+
./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample -T1C -e clean test
54+
./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-core-sample -T1C -e clean test
55+
./mvnw -am -pl third-part-samples/shardingsphere-sample/shardingsphere-jdbc-5.x-spring-sample -T1C -e clean test
Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
# Licensed to the Apache Software Foundation (ASF) under one
2-
# or more contributor license agreements. See the NOTICE file
3-
# distributed with this work for additional information
4-
# regarding copyright ownership. The ASF licenses this file
5-
# to you under the Apache License, Version 2.0 (the
6-
# "License"); you may not use this file except in compliance
7-
# with the License. You may obtain a copy of the License at
8-
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
11-
# Unless required by applicable law or agreed to in writing,
12-
# software distributed under the License is distributed on an
13-
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14-
# KIND, either express or implied. See the License for the
15-
# specific language governing permissions and limitations
16-
# under the License.
17-
wrapperVersion=3.3.2
1+
wrapperVersion=3.3.4
182
distributionType=only-script
19-
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
3+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.11/apache-maven-3.9.11-bin.zip

README.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# 演示例子
22

3-
大部分数据库连接为 H2Database,仅供测试。
3+
大部分数据库连接为 `h2database` 或通过 `testcontainers-java` 启动的 `mysql-server`,仅供测试。讨论不同情况,
44

5-
除开 `com.baomidou:springboot3-sample`, 其他子模块均可在 [OpenJDK 8, OpenJDK 21] 的 JDK 范围及其下游发行版下执行单元测试。
5+
1. `com.baomidou:springboot3-sample` 可在 [OpenJDK 17, OpenJDK 21] 的 JDK 范围及其下游发行版下执行单元测试
6+
2. 除开 `com.baomidou:springboot3-sample`, 其他子模块均可在 [OpenJDK 8, OpenJDK 21] 的 JDK 范围及其下游发行版下执行单元测试
67

78
所有单元测试在 Github Actions 完成验证。你可能希望参考 [位于 Github Actions 的 CI 文件](./.github/workflows/ci.yml)
89

@@ -29,10 +30,15 @@
2930
## Contributing
3031

3132
我们欢迎社区的贡献。围绕此 git 的讨论与协作应通过 https://github.com/baomidou/dynamic-datasource/issues 进行。
32-
33-
针对 IDE,项目的语言级别应设置为 JDK 8,对于单独的 `com.baomidou:springboot3-sample` 子模块,语言级别应设置为 JDK 17 。
3433
在提交 Pull Request 之前, 请在本地通过 [OpenJDK 17, OpenJDK 21] 的 JDK 范围下完成此命令的验证。
35-
我们鼓励通过 `SDKMAN!` 切换到 `21.0.2-graalce` 来验证。
34+
我们鼓励通过 `SDKMAN!``version-fox/vfox` 切换到 `21.0.2-graalce` 来验证。
35+
36+
针对 IntelliJ IDEA 等 IDE,项目的语言级别应设置为 JDK 8,对于单独的 `com.baomidou:springboot3-sample` 子模块,语言级别应设置为 JDK 17。
37+
下文讨论不同情况下可能的测试流程,
38+
39+
### Ubuntu 24.04
40+
41+
假设 `SDKMAN!``Docker Engine` 已安装,在 Bash 执行如下命令,
3642

3743
```shell
3844
sdk install java 21.0.2-graalce
@@ -42,3 +48,19 @@ git clone [email protected]:dynamic-datasource/dynamic-datasource-samples.git
4248
cd ./dynamic-datasource-samples/
4349
./mvnw -T1C -e clean test
4450
```
51+
52+
### Windows 11
53+
54+
假设 `PowerShell/PowerShell`, `version-fox/vfox`, `git-for-windows/git``rancher-sandbox/rancher-desktop` 已安装,
55+
`PowerShell 7` 执行如下命令,
56+
57+
```shell
58+
rdctl start --application.start-in-background --container-engine.name=moby --kubernetes.enabled=false
59+
vfox add java
60+
vfox install [email protected]
61+
vfox use --global [email protected]
62+
63+
git clone [email protected]:dynamic-datasource/dynamic-datasource-samples.git
64+
cd ./dynamic-datasource-samples/
65+
./mvnw -T1C -e clean test
66+
```

mvnw

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# ----------------------------------------------------------------------------
2020

2121
# ----------------------------------------------------------------------------
22-
# Apache Maven Wrapper startup batch script, version 3.3.2
22+
# Apache Maven Wrapper startup batch script, version 3.3.4
2323
#
2424
# Optional ENV vars
2525
# -----------------
@@ -105,14 +105,17 @@ trim() {
105105
printf "%s" "${1}" | tr -d '[:space:]'
106106
}
107107

108+
scriptDir="$(dirname "$0")"
109+
scriptName="$(basename "$0")"
110+
108111
# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties
109112
while IFS="=" read -r key value; do
110113
case "${key-}" in
111114
distributionUrl) distributionUrl=$(trim "${value-}") ;;
112115
distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;;
113116
esac
114-
done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties"
115-
[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties"
117+
done <"$scriptDir/.mvn/wrapper/maven-wrapper.properties"
118+
[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties"
116119

117120
case "${distributionUrl##*/}" in
118121
maven-mvnd-*bin.*)
@@ -130,7 +133,7 @@ maven-mvnd-*bin.*)
130133
distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip"
131134
;;
132135
maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;;
133-
*) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
136+
*) MVN_CMD="mvn${scriptName#mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;;
134137
esac
135138

136139
# apply MVNW_REPOURL and calculate MAVEN_HOME
@@ -227,7 +230,7 @@ if [ -n "${distributionSha256Sum-}" ]; then
227230
echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2
228231
exit 1
229232
elif command -v sha256sum >/dev/null; then
230-
if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then
233+
if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c - >/dev/null 2>&1; then
231234
distributionSha256Result=true
232235
fi
233236
elif command -v shasum >/dev/null; then
@@ -252,8 +255,41 @@ if command -v unzip >/dev/null; then
252255
else
253256
tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar"
254257
fi
255-
printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url"
256-
mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME"
258+
259+
# Find the actual extracted directory name (handles snapshots where filename != directory name)
260+
actualDistributionDir=""
261+
262+
# First try the expected directory name (for regular distributions)
263+
if [ -d "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" ]; then
264+
if [ -f "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/bin/$MVN_CMD" ]; then
265+
actualDistributionDir="$distributionUrlNameMain"
266+
fi
267+
fi
268+
269+
# If not found, search for any directory with the Maven executable (for snapshots)
270+
if [ -z "$actualDistributionDir" ]; then
271+
# enable globbing to iterate over items
272+
set +f
273+
for dir in "$TMP_DOWNLOAD_DIR"/*; do
274+
if [ -d "$dir" ]; then
275+
if [ -f "$dir/bin/$MVN_CMD" ]; then
276+
actualDistributionDir="$(basename "$dir")"
277+
break
278+
fi
279+
fi
280+
done
281+
set -f
282+
fi
283+
284+
if [ -z "$actualDistributionDir" ]; then
285+
verbose "Contents of $TMP_DOWNLOAD_DIR:"
286+
verbose "$(ls -la "$TMP_DOWNLOAD_DIR")"
287+
die "Could not find Maven distribution directory in extracted archive"
288+
fi
289+
290+
verbose "Found extracted Maven distribution directory: $actualDistributionDir"
291+
printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$actualDistributionDir/mvnw.url"
292+
mv -- "$TMP_DOWNLOAD_DIR/$actualDistributionDir" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME"
257293

258294
clean || :
259295
exec_maven "$@"

mvnw.cmd

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@REM ----------------------------------------------------------------------------
2020

2121
@REM ----------------------------------------------------------------------------
22-
@REM Apache Maven Wrapper startup batch script, version 3.3.2
22+
@REM Apache Maven Wrapper startup batch script, version 3.3.4
2323
@REM
2424
@REM Optional ENV vars
2525
@REM MVNW_REPOURL - repo url base for downloading maven distribution
@@ -40,7 +40,7 @@
4040
@SET __MVNW_ARG0_NAME__=
4141
@SET MVNW_USERNAME=
4242
@SET MVNW_PASSWORD=
43-
@IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*)
43+
@IF NOT "%__MVNW_CMD__%"=="" ("%__MVNW_CMD__%" %*)
4444
@echo Cannot start maven from wrapper >&2 && exit /b 1
4545
@GOTO :EOF
4646
: end batch / begin powershell #>
@@ -73,16 +73,30 @@ switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) {
7373
# apply MVNW_REPOURL and calculate MAVEN_HOME
7474
# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-<version>,maven-mvnd-<version>-<platform>}/<hash>
7575
if ($env:MVNW_REPOURL) {
76-
$MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" }
77-
$distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')"
76+
$MVNW_REPO_PATTERN = if ($USE_MVND -eq $False) { "/org/apache/maven/" } else { "/maven/mvnd/" }
77+
$distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace "^.*$MVNW_REPO_PATTERN",'')"
7878
}
7979
$distributionUrlName = $distributionUrl -replace '^.*/',''
8080
$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$',''
81-
$MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain"
81+
82+
$MAVEN_M2_PATH = "$HOME/.m2"
8283
if ($env:MAVEN_USER_HOME) {
83-
$MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain"
84+
$MAVEN_M2_PATH = "$env:MAVEN_USER_HOME"
85+
}
86+
87+
if (-not (Test-Path -Path $MAVEN_M2_PATH)) {
88+
New-Item -Path $MAVEN_M2_PATH -ItemType Directory | Out-Null
89+
}
90+
91+
$MAVEN_WRAPPER_DISTS = $null
92+
if ((Get-Item $MAVEN_M2_PATH).Target[0] -eq $null) {
93+
$MAVEN_WRAPPER_DISTS = "$MAVEN_M2_PATH/wrapper/dists"
94+
} else {
95+
$MAVEN_WRAPPER_DISTS = (Get-Item $MAVEN_M2_PATH).Target[0] + "/wrapper/dists"
8496
}
85-
$MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join ''
97+
98+
$MAVEN_HOME_PARENT = "$MAVEN_WRAPPER_DISTS/$distributionUrlNameMain"
99+
$MAVEN_HOME_NAME = ([System.Security.Cryptography.SHA256]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join ''
86100
$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME"
87101

88102
if (Test-Path -Path "$MAVEN_HOME" -PathType Container) {
@@ -134,7 +148,33 @@ if ($distributionSha256Sum) {
134148

135149
# unzip and move
136150
Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null
137-
Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null
151+
152+
# Find the actual extracted directory name (handles snapshots where filename != directory name)
153+
$actualDistributionDir = ""
154+
155+
# First try the expected directory name (for regular distributions)
156+
$expectedPath = Join-Path "$TMP_DOWNLOAD_DIR" "$distributionUrlNameMain"
157+
$expectedMvnPath = Join-Path "$expectedPath" "bin/$MVN_CMD"
158+
if ((Test-Path -Path $expectedPath -PathType Container) -and (Test-Path -Path $expectedMvnPath -PathType Leaf)) {
159+
$actualDistributionDir = $distributionUrlNameMain
160+
}
161+
162+
# If not found, search for any directory with the Maven executable (for snapshots)
163+
if (!$actualDistributionDir) {
164+
Get-ChildItem -Path "$TMP_DOWNLOAD_DIR" -Directory | ForEach-Object {
165+
$testPath = Join-Path $_.FullName "bin/$MVN_CMD"
166+
if (Test-Path -Path $testPath -PathType Leaf) {
167+
$actualDistributionDir = $_.Name
168+
}
169+
}
170+
}
171+
172+
if (!$actualDistributionDir) {
173+
Write-Error "Could not find Maven distribution directory in extracted archive"
174+
}
175+
176+
Write-Verbose "Found extracted Maven distribution directory: $actualDistributionDir"
177+
Rename-Item -Path "$TMP_DOWNLOAD_DIR/$actualDistributionDir" -NewName $MAVEN_HOME_NAME | Out-Null
138178
try {
139179
Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null
140180
} catch {

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<h2.version>2.2.224</h2.version>
2222
<spring-boot-dependencies.version>2.7.18</spring-boot-dependencies.version>
2323
<lombok.version>1.18.30</lombok.version>
24+
<!--TODO We need to change the project's `spring-boot-dependencies.version` to 4.0.0, see https://github.com/testcontainers/testcontainers-java/issues/11236 -->
25+
<testcontainers-bom.version>1.21.3</testcontainers-bom.version>
2426
</properties>
2527

2628
<modules>
@@ -53,6 +55,13 @@
5355
<type>pom</type>
5456
<scope>import</scope>
5557
</dependency>
58+
<dependency>
59+
<groupId>org.testcontainers</groupId>
60+
<artifactId>testcontainers-bom</artifactId>
61+
<version>${testcontainers-bom.version}</version>
62+
<type>pom</type>
63+
<scope>import</scope>
64+
</dependency>
5665
<dependency>
5766
<groupId>org.springdoc</groupId>
5867
<artifactId>springdoc-openapi-ui</artifactId>

third-part-samples/shardingsphere-sample/shardingsphere-jdbc-4.x-spring-sample/src/test/java/com/baomidou/samples/shardingsphere/jdbc/v4/spring/MapperLayerTests.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
/**
3535
* TODO Unit tests for ShardingSphere 5.4.0 need to be synchronized.
36-
* Tracked on <a href="https://github.com/apache/shardingsphere/issues/27955">When a logic database uses both `SHARDING` and `READWRITE_SPLITTING` features, CRUD operations on table throw `NoSuchTableException`</a>
3736
*/
3837
@SpringBootTest
3938
@Disabled("TODO wait for com.baomidou:dynamic-datasource-spring-boot-starter:4.2.0 release")
@@ -54,21 +53,21 @@ void after() {
5453

5554
@Test
5655
void whenRequestToFindAll() {
57-
assertEquals(tOrderMapper.findAll().size(), 0);
56+
assertEquals(0, tOrderMapper.findAll().size());
5857
}
5958

6059
@Test
6160
void whenRequestToAddByNameAndUserId() {
6261
int firstNumberOfAffectedRows = tOrderMapper.addAll(101L, "Bright", 114514L);
6362
int secondNumberOfAffectedRows = tOrderMapper.addAll(102L, "Jordan", 114515L);
64-
assertEquals(firstNumberOfAffectedRows + secondNumberOfAffectedRows, 2);
65-
assertEquals(tOrderMapper.findAll().size(), 2);
63+
assertEquals(2, firstNumberOfAffectedRows + secondNumberOfAffectedRows);
64+
assertEquals(2, tOrderMapper.findAll().size());
6665
}
6766

6867
@Test
6968
void whenRequestToAddByNameAndUserIdWithPrimaryKey() {
7069
List<TOrder> emptyState = tOrderMapper.findAll();
71-
assertEquals(emptyState.size(), 0);
70+
assertEquals(0, emptyState.size());
7271
assertThrows(UncategorizedSQLException.class, () -> {
7372
tOrderMapper.addByNameAndUserId("Bright", 114514L);
7473
tOrderMapper.addByNameAndUserId("Jordan", 114515L);
@@ -84,8 +83,8 @@ void whenRequestToDeleteByIdTest() {
8483
tOrderMapper.addAll(105L, "Michael", 114518L);
8584
tOrderMapper.addAll(106L, "Tony", 114519L);
8685
int numberOfAffectedRows = tOrderMapper.deleteById(114514L);
87-
assertEquals(numberOfAffectedRows, 1);
88-
assertEquals(tOrderMapper.findAll().size(), 5);
86+
assertEquals(1, numberOfAffectedRows);
87+
assertEquals(5, tOrderMapper.findAll().size());
8988
}
9089

9190
@Test
@@ -97,7 +96,7 @@ void whenRequestToDeleteAll() {
9796
tOrderMapper.addAll(105L, "Michael", 114518L);
9897
tOrderMapper.addAll(106L, "Tony", 114519L);
9998
int numberOfAffectedRows = tOrderMapper.deleteAll();
100-
assertEquals(numberOfAffectedRows, 6);
101-
assertEquals(tOrderMapper.findAll().size(), 0);
99+
assertEquals(6, numberOfAffectedRows);
100+
assertEquals(0, tOrderMapper.findAll().size());
102101
}
103102
}

0 commit comments

Comments
 (0)