From 07b519921913fcd01121c4ac7d94471b197463d1 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 28 May 2025 20:27:32 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95=E4=B8=AD=E7=9A=84=E8=A1=A8=E5=92=8C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将表结构和初始数据的SQL放在 *.sql 文件中,使单元测试代码更清晰。 Reviewed-on: http://zhouxy.xyz:3000/ZhouXY108/simple-jdbc/pulls/4 Co-authored-by: ZhouXY108 Co-committed-by: ZhouXY108 --- .../jdbc/test/SimpleJdbcTemplateTests.java | 106 ++++++++---------- src/test/resources/data.sql | 23 ++++ src/test/resources/schema.sql | 10 ++ 3 files changed, 80 insertions(+), 59 deletions(-) create mode 100644 src/test/resources/data.sql create mode 100644 src/test/resources/schema.sql diff --git a/src/test/java/xyz/zhouxy/jdbc/test/SimpleJdbcTemplateTests.java b/src/test/java/xyz/zhouxy/jdbc/test/SimpleJdbcTemplateTests.java index 703cf38..ce13fb3 100644 --- a/src/test/java/xyz/zhouxy/jdbc/test/SimpleJdbcTemplateTests.java +++ b/src/test/java/xyz/zhouxy/jdbc/test/SimpleJdbcTemplateTests.java @@ -1,10 +1,27 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package xyz.zhouxy.jdbc.test; import static org.junit.jupiter.api.Assertions.*; -import static xyz.zhouxy.jdbc.ParamBuilder.*; +import static xyz.zhouxy.jdbc.ParamBuilder.buildParams; +import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.sql.SQLException; -import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date; import java.util.List; @@ -18,8 +35,9 @@ import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; +import com.google.common.io.Resources; import xyz.zhouxy.jdbc.RowMapper; import xyz.zhouxy.jdbc.SimpleJdbcTemplate; @@ -31,83 +49,53 @@ class SimpleJdbcTemplateTests { private static final Logger log = LoggerFactory.getLogger(SimpleJdbcTemplateTests.class); - private static final SimpleJdbcTemplate jdbcTemplate; + private static SimpleJdbcTemplate jdbcTemplate; - static { + @BeforeAll + static void initH2() throws IOException, SQLException { JdbcDataSource dataSource = new JdbcDataSource(); dataSource.setURL("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=FALSE;MODE=MySQL"); dataSource.setUser("sa"); dataSource.setPassword(""); jdbcTemplate = new SimpleJdbcTemplate(dataSource); - } - @BeforeAll - static void createTable() throws SQLException { - jdbcTemplate.update("CREATE TABLE sys_account (" - + "\n" + " id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY" - + "\n" + " ,username VARCHAR(255) NOT NULL" - + "\n" + " ,account_status VARCHAR(2) NOT NULL" - + "\n" + " ,create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP" - + "\n" + " ,created_by BIGINT NOT NULL" - + "\n" + " ,update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP" - + "\n" + " ,updated_by BIGINT DEFAULT NULL" - + "\n" + " ,version BIGINT NOT NULL DEFAULT 0" - + "\n" + ")"); + // 建表 + executeSqlFile("schema.sql"); } @BeforeEach - void initData() throws SQLException { - jdbcTemplate.update("truncate table sys_account"); - jdbcTemplate.batchUpdate("INSERT INTO sys_account(id, username, account_status, created_by) VALUES (?, ?, ?, ?)", Lists.newArrayList( - buildParams(2L, "zhouxy2", "0", 108L), - buildParams(3L, "zhouxy3", "0", 108L), - buildParams(4L, "zhouxy4", "0", 108L), - buildParams(5L, "zhouxy5", "0", 108L), - buildParams(6L, "zhouxy6", "0", 108L), - buildParams(7L, "zhouxy7", "0", 108L), - buildParams(8L, "zhouxy8", "0", 108L), - buildParams(9L, "zhouxy9", "0", 108L) - ), 10); - jdbcTemplate.batchUpdate("INSERT INTO sys_account(id, username, account_status, created_by, create_time, update_time, version) VALUES (?, ?, ?, ?, ?, ?, ?)", Lists.newArrayList( - buildParams(10L, "zhouxy10", "1", 118L, LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 29), 31), - buildParams(11L, "zhouxy11", "1", 118L, LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 29), 28), - buildParams(12L, "zhouxy12", "1", 118L, LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 29), 25), - buildParams(13L, "zhouxy13", "1", 118L, LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 29), 22), - buildParams(14L, "zhouxy14", "1", 118L, LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 29), 19), - buildParams(15L, "zhouxy15", "1", 118L, LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 29), 16), - buildParams(16L, "zhouxy16", "1", 118L, LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 29), 13), - buildParams(17L, "zhouxy17", "1", 118L, LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 29), 10), - buildParams(18L, "zhouxy18", "1", 118L, LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 29), 7), - buildParams(19L, "zhouxy19", "1", 118L, LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 29), 0) - ), 10); - jdbcTemplate.update("INSERT INTO sys_account(id, username, account_status, created_by, create_time, updated_by, update_time, version) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", - buildParams(20L, "zhouxy20", "2", 118L, LocalDateTime.of(2008, 8, 8, 20, 8), 31L, LocalDateTime.now(), 88L)); + void initData() throws IOException, SQLException { + // 初始化数据 + executeSqlFile("data.sql"); + } + + static void executeSqlFile(String filePath) throws IOException, SQLException { + String[] sqls = Resources + .toString(Resources.getResource(filePath), StandardCharsets.UTF_8) + .split(";"); + for (String sql : sqls) { + jdbcTemplate.update(sql); + } } @Test void testQuery() throws SQLException { Object[] ids = buildParams(5, 9, 13, 14, 17, 20, 108); - String sql = "SELECT id, username, account_status" - + "\n FROM sys_account" - + "\n WHERE id IN (" - + "\n ?, ?, ?, ?, ?, ?, ?" - + "\n )"; + String sql = "SELECT id, username, account_status FROM sys_account WHERE id IN (?, ?, ?, ?, ?, ?, ?)"; log.info(sql); List> rs = jdbcTemplate.queryList(sql, ids); for (Map dbRecord : rs) { log.info("{}", dbRecord); } - assertEquals( - Lists.newArrayList( - ImmutableMap.of("id", 5L, "account_status", "0", "username", "zhouxy5"), - ImmutableMap.of("id", 9L, "account_status", "0", "username", "zhouxy9"), - ImmutableMap.of("id", 13L, "account_status", "1", "username", "zhouxy13"), - ImmutableMap.of("id", 14L, "account_status", "1", "username", "zhouxy14"), - ImmutableMap.of("id", 17L, "account_status", "1", "username", "zhouxy17"), - ImmutableMap.of("id", 20L, "account_status", "2", "username", "zhouxy20") - ), - rs + List> expected = ImmutableList.of( + ImmutableMap.of("id", 5L, "account_status", "0", "username", "zhouxy5"), + ImmutableMap.of("id", 9L, "account_status", "0", "username", "zhouxy9"), + ImmutableMap.of("id", 13L, "account_status", "1", "username", "zhouxy13"), + ImmutableMap.of("id", 14L, "account_status", "1", "username", "zhouxy14"), + ImmutableMap.of("id", 17L, "account_status", "1", "username", "zhouxy17"), + ImmutableMap.of("id", 20L, "account_status", "2", "username", "zhouxy20") ); + assertEquals(expected, rs); } @Test diff --git a/src/test/resources/data.sql b/src/test/resources/data.sql new file mode 100644 index 0000000..f667fb3 --- /dev/null +++ b/src/test/resources/data.sql @@ -0,0 +1,23 @@ +truncate table sys_account; + +INSERT INTO sys_account(id, username, account_status, created_by) VALUES (2, 'zhouxy2', '0', 108); +INSERT INTO sys_account(id, username, account_status, created_by) VALUES (3, 'zhouxy3', '0', 108); +INSERT INTO sys_account(id, username, account_status, created_by) VALUES (4, 'zhouxy4', '0', 108); +INSERT INTO sys_account(id, username, account_status, created_by) VALUES (5, 'zhouxy5', '0', 108); +INSERT INTO sys_account(id, username, account_status, created_by) VALUES (6, 'zhouxy6', '0', 108); +INSERT INTO sys_account(id, username, account_status, created_by) VALUES (7, 'zhouxy7', '0', 108); +INSERT INTO sys_account(id, username, account_status, created_by) VALUES (8, 'zhouxy8', '0', 108); +INSERT INTO sys_account(id, username, account_status, created_by) VALUES (9, 'zhouxy9', '0', 108); + +INSERT INTO sys_account(id, username, account_status, created_by, create_time, update_time, `version`) VALUES (10, 'zhouxy10', '1', 118, '2000-01-01', '2000-01-29', 31); +INSERT INTO sys_account(id, username, account_status, created_by, create_time, update_time, `version`) VALUES (11, 'zhouxy11', '1', 118, '2000-01-01', '2000-01-29', 28); +INSERT INTO sys_account(id, username, account_status, created_by, create_time, update_time, `version`) VALUES (12, 'zhouxy12', '1', 118, '2000-01-01', '2000-01-29', 25); +INSERT INTO sys_account(id, username, account_status, created_by, create_time, update_time, `version`) VALUES (13, 'zhouxy13', '1', 118, '2000-01-01', '2000-01-29', 22); +INSERT INTO sys_account(id, username, account_status, created_by, create_time, update_time, `version`) VALUES (14, 'zhouxy14', '1', 118, '2000-01-01', '2000-01-29', 19); +INSERT INTO sys_account(id, username, account_status, created_by, create_time, update_time, `version`) VALUES (15, 'zhouxy15', '1', 118, '2000-01-01', '2000-01-29', 16); +INSERT INTO sys_account(id, username, account_status, created_by, create_time, update_time, `version`) VALUES (16, 'zhouxy16', '1', 118, '2000-01-01', '2000-01-29', 13); +INSERT INTO sys_account(id, username, account_status, created_by, create_time, update_time, `version`) VALUES (17, 'zhouxy17', '1', 118, '2000-01-01', '2000-01-29', 10); +INSERT INTO sys_account(id, username, account_status, created_by, create_time, update_time, `version`) VALUES (18, 'zhouxy18', '1', 118, '2000-01-01', '2000-01-29', 7 ); +INSERT INTO sys_account(id, username, account_status, created_by, create_time, update_time, `version`) VALUES (19, 'zhouxy19', '1', 118, '2000-01-01', '2000-01-29', 0 ); + +INSERT INTO sys_account(id, username, account_status, created_by, create_time, updated_by, update_time, version) VALUES (20, 'zhouxy20', '2', 118, '2008-08-08 20:08:00', 31, now(), 88); diff --git a/src/test/resources/schema.sql b/src/test/resources/schema.sql new file mode 100644 index 0000000..e6f5aa7 --- /dev/null +++ b/src/test/resources/schema.sql @@ -0,0 +1,10 @@ +CREATE TABLE sys_account ( + id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY + ,username VARCHAR(255) NOT NULL + ,account_status VARCHAR(2) NOT NULL + ,create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP + ,created_by BIGINT NOT NULL + ,update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP + ,updated_by BIGINT DEFAULT NULL + ,version BIGINT NOT NULL DEFAULT 0 +)