feat: 启动时检查数据库连接与AI评估表字符集,Druid固定characterEncoding防按平台默认GBK写库
parent
247669f648
commit
179d18e754
@ -0,0 +1,75 @@
|
||||
package com.sztzjy.linkCommerce.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class DatabaseCharsetCheckerTest {
|
||||
|
||||
@Test
|
||||
void reportsNothingWhenConnectionAndColumnAreUtf8mb4() {
|
||||
DatabaseCharsetChecker checker = checker("utf8mb4", "utf8mb4", Collections.singletonList("utf8mb4"));
|
||||
|
||||
assertTrue(checker.findProblems().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportsConnectionCharsetThatIsNotUtf8mb4() {
|
||||
DatabaseCharsetChecker checker = checker("gbk", "gbk", Collections.singletonList("utf8mb4"));
|
||||
|
||||
List<String> problems = checker.findProblems();
|
||||
|
||||
assertEquals(2, problems.size());
|
||||
assertTrue(problems.get(0).contains("character_set_client=gbk"));
|
||||
assertTrue(problems.get(1).contains("character_set_connection=gbk"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reportsRawResponseColumnThatIsNotUtf8mb4() {
|
||||
DatabaseCharsetChecker checker = checker("utf8mb4", "utf8mb4", Collections.singletonList("latin1"));
|
||||
|
||||
List<String> problems = checker.findProblems();
|
||||
|
||||
assertEquals(1, problems.size());
|
||||
assertTrue(problems.get(0).contains("assessment_raw_response"));
|
||||
assertTrue(problems.get(0).contains("latin1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void skipsColumnCheckWhenTableDoesNotExistYet() {
|
||||
DatabaseCharsetChecker checker = checker("utf8mb4", "utf8mb4", Collections.<String>emptyList());
|
||||
|
||||
assertTrue(checker.findProblems().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void neverBlocksStartupWhenDatabaseIsUnavailable() {
|
||||
JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class);
|
||||
when(jdbcTemplate.queryForMap(anyString())).thenThrow(new DataAccessResourceFailureException("db down"));
|
||||
|
||||
assertDoesNotThrow(() -> new DatabaseCharsetChecker(jdbcTemplate).check());
|
||||
}
|
||||
|
||||
private static DatabaseCharsetChecker checker(String client, String connection, List<String> columnCharsets) {
|
||||
JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class);
|
||||
Map<String, Object> session = new HashMap<String, Object>();
|
||||
session.put("client", client);
|
||||
session.put("conn", connection);
|
||||
when(jdbcTemplate.queryForMap(anyString())).thenReturn(session);
|
||||
when(jdbcTemplate.queryForList(anyString(), org.mockito.ArgumentMatchers.<Class<String>>any()))
|
||||
.thenReturn(columnCharsets);
|
||||
return new DatabaseCharsetChecker(jdbcTemplate);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue