Skip to content

Commit 10cdb7b

Browse files
test: Add sqlite3 test (#2161)
CLose #1131 Signed-off-by: unknowIfGuestInDream <liang.tang.cx@gmail.com>
1 parent d546295 commit 10cdb7b

File tree

10 files changed

+900
-0
lines changed

10 files changed

+900
-0
lines changed

core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,11 @@
455455
<artifactId>qrcodegen</artifactId>
456456
<scope>test</scope>
457457
</dependency>
458+
<dependency>
459+
<groupId>org.xerial</groupId>
460+
<artifactId>sqlite-jdbc</artifactId>
461+
<scope>test</scope>
462+
</dependency>
458463
</dependencies>
459464

460465
<build>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2025 unknowIfGuestInDream.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of unknowIfGuestInDream, any associated website, nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL UNKNOWIFGUESTINDREAM BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
package com.tlcsdm.core.database;
29+
30+
import cn.hutool.core.io.resource.ResourceUtil;
31+
import com.tlcsdm.core.database.sqlite.UserDao;
32+
import org.junit.jupiter.api.Assertions;
33+
import org.junit.jupiter.api.BeforeAll;
34+
import org.junit.jupiter.api.BeforeEach;
35+
import org.junit.jupiter.api.Test;
36+
37+
import java.io.File;
38+
import java.nio.file.Paths;
39+
import java.sql.SQLException;
40+
import java.util.List;
41+
42+
/**
43+
* @author unknowIfGuestInDream
44+
*/
45+
public class Sqlite1Test {
46+
47+
private static String dbParentPath;
48+
private static String TEST_DB_URL;
49+
private UserDao userDao;
50+
51+
@BeforeAll
52+
static void init() {
53+
dbParentPath = new File(ResourceUtil.getResource("database/init.sql").getPath()).getParent();
54+
TEST_DB_URL = "jdbc:sqlite:" + Paths.get(dbParentPath).resolve("usersqlite.db");
55+
System.out.println(TEST_DB_URL);
56+
}
57+
58+
@BeforeEach
59+
void setUp() throws SQLException {
60+
userDao = new UserDao(TEST_DB_URL);
61+
userDao.deleteAllUsers();
62+
}
63+
64+
@Test
65+
void testInsertAndGetUsers() throws SQLException {
66+
// 插入测试数据
67+
userDao.insertUser("Alice", "alice@example.com");
68+
userDao.insertUser("Bob", "bob@example.com");
69+
70+
// 验证数据
71+
List<String> names = userDao.getAllUserNames();
72+
Assertions.assertEquals(2, names.size());
73+
Assertions.assertTrue(names.contains("Alice"));
74+
Assertions.assertTrue(names.contains("Bob"));
75+
}
76+
77+
@Test
78+
void testEmptyDatabase() throws SQLException {
79+
List<String> names = userDao.getAllUserNames();
80+
Assertions.assertTrue(names.isEmpty());
81+
}
82+
83+
@Test
84+
void testDuplicateEmail() {
85+
// 测试唯一约束
86+
Assertions.assertDoesNotThrow(() -> userDao.insertUser("Alice", "alice@example.com"));
87+
SQLException exception = Assertions.assertThrows(SQLException.class,
88+
() -> userDao.insertUser("Alice", "alice@example.com"));
89+
Assertions.assertTrue(exception.getMessage().contains("UNIQUE constraint failed"));
90+
}
91+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2025 unknowIfGuestInDream.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of unknowIfGuestInDream, any associated website, nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL UNKNOWIFGUESTINDREAM BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
package com.tlcsdm.core.database;
29+
30+
import com.tlcsdm.core.database.sqlite.UserDao;
31+
import org.junit.jupiter.api.Assertions;
32+
import org.junit.jupiter.api.BeforeAll;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.api.io.TempDir;
36+
37+
import java.nio.file.Path;
38+
import java.sql.SQLException;
39+
import java.util.List;
40+
41+
/**
42+
* @author unknowIfGuestInDream
43+
*/
44+
public class Sqlite2Test {
45+
@TempDir
46+
static Path tempDir;
47+
private static String TEST_DB_URL;
48+
private UserDao userDao;
49+
50+
@BeforeAll
51+
static void init() {
52+
TEST_DB_URL = "jdbc:sqlite:" + tempDir.resolve("test.db");
53+
System.out.println(TEST_DB_URL);
54+
}
55+
56+
@BeforeEach
57+
void setUp() throws SQLException {
58+
userDao = new UserDao(TEST_DB_URL);
59+
userDao.deleteAllUsers();
60+
}
61+
62+
@Test
63+
void testInsertAndGetUsers() throws SQLException {
64+
// 插入测试数据
65+
userDao.insertUser("Alice", "alice@example.com");
66+
userDao.insertUser("Bob", "bob@example.com");
67+
68+
// 验证数据
69+
List<String> names = userDao.getAllUserNames();
70+
Assertions.assertEquals(2, names.size());
71+
Assertions.assertTrue(names.contains("Alice"));
72+
Assertions.assertTrue(names.contains("Bob"));
73+
}
74+
75+
@Test
76+
void testEmptyDatabase() throws SQLException {
77+
List<String> names = userDao.getAllUserNames();
78+
Assertions.assertTrue(names.isEmpty());
79+
}
80+
81+
@Test
82+
void testDuplicateEmail() {
83+
// 测试唯一约束
84+
Assertions.assertDoesNotThrow(() -> userDao.insertUser("Alice", "alice@example.com"));
85+
SQLException exception = Assertions.assertThrows(SQLException.class,
86+
() -> userDao.insertUser("Alice", "alice@example.com"));
87+
Assertions.assertTrue(exception.getMessage().contains("UNIQUE constraint failed"));
88+
}
89+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (c) 2025 unknowIfGuestInDream.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of unknowIfGuestInDream, any associated website, nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL UNKNOWIFGUESTINDREAM BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
package com.tlcsdm.core.database;
29+
30+
import cn.hutool.core.io.resource.ResourceUtil;
31+
import org.junit.jupiter.api.BeforeAll;
32+
import org.junit.jupiter.api.Test;
33+
34+
import java.io.File;
35+
import java.nio.file.Paths;
36+
import java.sql.Connection;
37+
import java.sql.DriverManager;
38+
import java.sql.ResultSet;
39+
import java.sql.SQLException;
40+
import java.sql.Statement;
41+
42+
/**
43+
* @author unknowIfGuestInDream
44+
*/
45+
public class SqliteAccountTest {
46+
/**
47+
* 以嵌入式(本地)连接方式连接数据库
48+
*/
49+
private static final String DRIVER_CLASS = "org.sqlite.JDBC";
50+
private static final String USER = "root";
51+
private static final String PASSWORD = "root";
52+
private static String dbParentPath;
53+
private static String TEST_DB_URL;
54+
55+
@BeforeAll
56+
static void init() {
57+
dbParentPath = new File(ResourceUtil.getResource("database/init.sql").getPath()).getParent();
58+
TEST_DB_URL = "jdbc:sqlite:" + Paths.get(dbParentPath).resolve("sqliteDB.db");
59+
System.out.println(TEST_DB_URL);
60+
}
61+
62+
@Test
63+
void testInsertAndGetUsers() throws ClassNotFoundException, SQLException {
64+
//与数据库建立连接
65+
Class.forName(DRIVER_CLASS);
66+
Connection conn = DriverManager.getConnection(TEST_DB_URL, USER, PASSWORD);
67+
Statement statement = conn.createStatement();
68+
69+
//删除表
70+
statement.execute("DROP TABLE IF EXISTS USER_INF");
71+
//创建表
72+
statement.execute(
73+
"CREATE TABLE USER_INF(id VARCHAR(50) PRIMARY KEY, name VARCHAR(50) NOT NULL, sex VARCHAR(50) NOT NULL)");
74+
75+
//插入数据
76+
statement.executeUpdate("INSERT INTO USER_INF VALUES('1', '程咬金', '男') ");
77+
statement.executeUpdate("INSERT INTO USER_INF VALUES('2', '孙尚香', '女') ");
78+
statement.executeUpdate("INSERT INTO USER_INF VALUES('3', '猴子', '男') ");
79+
80+
//查询数据
81+
ResultSet resultSet = statement.executeQuery("select * from USER_INF");
82+
while (resultSet.next()) {
83+
System.out.println(
84+
resultSet.getInt("id") + ", " + resultSet.getString("name") + ", " + resultSet.getString("sex"));
85+
}
86+
//关闭连接
87+
statement.close();
88+
conn.close();
89+
}
90+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2025 unknowIfGuestInDream.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of unknowIfGuestInDream, any associated website, nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
* DISCLAIMED. IN NO EVENT SHALL UNKNOWIFGUESTINDREAM BE LIABLE FOR ANY
20+
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
*/
27+
28+
package com.tlcsdm.core.database;
29+
30+
import cn.hutool.core.io.resource.ResourceUtil;
31+
import org.junit.jupiter.api.AfterAll;
32+
import org.junit.jupiter.api.Assertions;
33+
import org.junit.jupiter.api.BeforeAll;
34+
import org.junit.jupiter.api.Test;
35+
36+
import java.io.BufferedReader;
37+
import java.io.IOException;
38+
import java.io.InputStream;
39+
import java.io.InputStreamReader;
40+
import java.sql.Connection;
41+
import java.sql.DriverManager;
42+
import java.sql.ResultSet;
43+
import java.sql.SQLException;
44+
import java.sql.Statement;
45+
46+
/**
47+
* 初始化脚本.
48+
*
49+
* @author unknowIfGuestInDream
50+
*/
51+
public class SqliteInitSQLTest {
52+
53+
private static Connection conn;
54+
55+
@BeforeAll
56+
static void setup() throws Exception {
57+
// 创建内存数据库连接
58+
conn = DriverManager.getConnection("jdbc:sqlite::memory:");
59+
// 执行初始化脚本
60+
initializeInMemoryDatabase(conn);
61+
}
62+
63+
@Test
64+
void testUserCount() throws SQLException {
65+
try (Statement stmt = conn.createStatement();
66+
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM users")) {
67+
rs.next();
68+
Assertions.assertEquals(3, rs.getInt(1));
69+
}
70+
}
71+
72+
@AfterAll
73+
static void cleanup() throws SQLException {
74+
if (conn != null) {
75+
conn.close();
76+
}
77+
}
78+
79+
private static void initializeInMemoryDatabase(Connection conn) throws SQLException, IOException {
80+
try (InputStream is = ResourceUtil.getResource("database/init.sql").openStream();
81+
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
82+
Statement stmt = conn.createStatement()) {
83+
84+
String line;
85+
StringBuilder sb = new StringBuilder();
86+
while ((line = reader.readLine()) != null) {
87+
// 跳过注释和空行
88+
if (!line.trim().startsWith("--") && !line.trim().isEmpty()) {
89+
sb.append(line);
90+
// 检查是否以分号结束(完整SQL语句)
91+
if (line.trim().endsWith(";")) {
92+
String sql = sb.toString();
93+
stmt.execute(sql);
94+
sb = new StringBuilder();
95+
}
96+
}
97+
}
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)