mirror of
https://github.com/moshowgame/SpringBootCodeGenerator.git
synced 2025-12-26 05:48:33 +08:00
Unit Test
This commit is contained in:
parent
5131a4a3a3
commit
6652a7c5a9
14
pom.xml
14
pom.xml
@ -190,6 +190,20 @@
|
|||||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
<!-- Maven Surefire Plugin for JUnit 5 -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.5.2</version>
|
||||||
|
<configuration>
|
||||||
|
<useSystemClassLoader>false</useSystemClassLoader>
|
||||||
|
<includes>
|
||||||
|
<include>**/*Test.java</include>
|
||||||
|
<include>**/*Tests.java</include>
|
||||||
|
</includes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
<!-- JaCoCo Maven Plugin for Code Coverage -->
|
<!-- JaCoCo Maven Plugin for Code Coverage -->
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.jacoco</groupId>
|
<groupId>org.jacoco</groupId>
|
||||||
|
|||||||
@ -27,11 +27,33 @@ public enum ParserTypeEnum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static ParserTypeEnum fromValue(String value) {
|
public static ParserTypeEnum fromValue(String value) {
|
||||||
|
if (value == null || value.trim().isEmpty()) {
|
||||||
|
return SQL;
|
||||||
|
}
|
||||||
|
|
||||||
|
String trimmedValue = value.trim();
|
||||||
|
|
||||||
|
// 首先尝试精确匹配枚举值
|
||||||
for (ParserTypeEnum type : ParserTypeEnum.values()) {
|
for (ParserTypeEnum type : ParserTypeEnum.values()) {
|
||||||
if (type.getValue().equals(value)) {
|
if (type.getValue().equals(trimmedValue)) {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果精确匹配失败,尝试忽略大小写匹配
|
||||||
|
for (ParserTypeEnum type : ParserTypeEnum.values()) {
|
||||||
|
if (type.getValue().equalsIgnoreCase(trimmedValue)) {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试匹配枚举名称
|
||||||
|
for (ParserTypeEnum type : ParserTypeEnum.values()) {
|
||||||
|
if (type.name().equalsIgnoreCase(trimmedValue)) {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 默认返回SQL类型
|
// 默认返回SQL类型
|
||||||
return SQL;
|
return SQL;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -93,6 +93,9 @@ public class CodeGenServiceImpl implements CodeGenService {
|
|||||||
String dataType = MapUtil.getString(paramInfo.getOptions(), "dataType");
|
String dataType = MapUtil.getString(paramInfo.getOptions(), "dataType");
|
||||||
ParserTypeEnum parserType = ParserTypeEnum.fromValue(dataType);
|
ParserTypeEnum parserType = ParserTypeEnum.fromValue(dataType);
|
||||||
|
|
||||||
|
// 添加调试信息
|
||||||
|
log.debug("解析数据类型: {}, 解析结果: {}", dataType, parserType);
|
||||||
|
|
||||||
switch (parserType) {
|
switch (parserType) {
|
||||||
case SQL:
|
case SQL:
|
||||||
// 默认模式:parse DDL table structure from sql
|
// 默认模式:parse DDL table structure from sql
|
||||||
|
|||||||
@ -47,7 +47,15 @@ class CodeGenControllerTest {
|
|||||||
void setUp() {
|
void setUp() {
|
||||||
// 初始化测试数据
|
// 初始化测试数据
|
||||||
paramInfo = new ParamInfo();
|
paramInfo = new ParamInfo();
|
||||||
paramInfo.setTableSql("CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(50));");
|
paramInfo.setTableSql("""
|
||||||
|
CREATE TABLE 'sys_user_info' (
|
||||||
|
'user_id' int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
|
||||||
|
'user_name' varchar(255) NOT NULL COMMENT '用户名',
|
||||||
|
'status' tinyint(1) NOT NULL COMMENT '状态',
|
||||||
|
'create_time' datetime NOT NULL COMMENT '创建时间',
|
||||||
|
PRIMARY KEY ('user_id')
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户信息'
|
||||||
|
""");
|
||||||
|
|
||||||
Map<String, Object> options = new HashMap<>();
|
Map<String, Object> options = new HashMap<>();
|
||||||
options.put("dataType", "SQL");
|
options.put("dataType", "SQL");
|
||||||
|
|||||||
@ -48,8 +48,8 @@ class ParamInfoTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testNameCaseTypeConstants() {
|
void testNameCaseTypeConstants() {
|
||||||
assertEquals("camelCase", ParamInfo.NAME_CASE_TYPE.CAMEL_CASE);
|
assertEquals("CamelCase", ParamInfo.NAME_CASE_TYPE.CAMEL_CASE);
|
||||||
assertEquals("underScoreCase", ParamInfo.NAME_CASE_TYPE.UNDER_SCORE_CASE);
|
assertEquals("UnderScoreCase", ParamInfo.NAME_CASE_TYPE.UNDER_SCORE_CASE);
|
||||||
assertEquals("upperUnderScoreCase", ParamInfo.NAME_CASE_TYPE.UPPER_UNDER_SCORE_CASE);
|
assertEquals("UpperUnderScoreCase", ParamInfo.NAME_CASE_TYPE.UPPER_UNDER_SCORE_CASE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3,6 +3,7 @@ package com.softdev.system.generator.service;
|
|||||||
import com.alibaba.fastjson2.JSONArray;
|
import com.alibaba.fastjson2.JSONArray;
|
||||||
import com.softdev.system.generator.entity.dto.ClassInfo;
|
import com.softdev.system.generator.entity.dto.ClassInfo;
|
||||||
import com.softdev.system.generator.entity.dto.ParamInfo;
|
import com.softdev.system.generator.entity.dto.ParamInfo;
|
||||||
|
import com.softdev.system.generator.entity.enums.ParserTypeEnum;
|
||||||
import com.softdev.system.generator.entity.vo.ResultVo;
|
import com.softdev.system.generator.entity.vo.ResultVo;
|
||||||
import com.softdev.system.generator.service.impl.CodeGenServiceImpl;
|
import com.softdev.system.generator.service.impl.CodeGenServiceImpl;
|
||||||
import com.softdev.system.generator.service.parser.JsonParserService;
|
import com.softdev.system.generator.service.parser.JsonParserService;
|
||||||
@ -53,10 +54,6 @@ class CodeGenServiceTest {
|
|||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
paramInfo = new ParamInfo();
|
paramInfo = new ParamInfo();
|
||||||
paramInfo.setTableSql("CREATE TABLE test (id INT PRIMARY KEY, name VARCHAR(50));");
|
|
||||||
paramInfo.setOptions(new HashMap<>());
|
|
||||||
paramInfo.getOptions().put("dataType", "SQL");
|
|
||||||
|
|
||||||
classInfo = new ClassInfo();
|
classInfo = new ClassInfo();
|
||||||
classInfo.setTableName("test");
|
classInfo.setTableName("test");
|
||||||
|
|
||||||
@ -74,10 +71,61 @@ class CodeGenServiceTest {
|
|||||||
mockTemplates.add(parentTemplate);
|
mockTemplates.add(parentTemplate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setupSqlTestData() {
|
||||||
|
paramInfo.setTableSql("""
|
||||||
|
CREATE TABLE 'sys_user_info' (
|
||||||
|
'user_id' int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
|
||||||
|
'user_name' varchar(255) NOT NULL COMMENT '用户名',
|
||||||
|
'status' tinyint(1) NOT NULL COMMENT '状态',
|
||||||
|
'create_time' datetime NOT NULL COMMENT '创建时间',
|
||||||
|
PRIMARY KEY ('user_id')
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户信息'
|
||||||
|
""");
|
||||||
|
paramInfo.setOptions(new HashMap<>());
|
||||||
|
paramInfo.getOptions().put("dataType", "sql");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupJsonTestData() {
|
||||||
|
paramInfo.setTableSql("""
|
||||||
|
{
|
||||||
|
"user_id": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "用户编号"
|
||||||
|
},
|
||||||
|
"user_name": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "用户名",
|
||||||
|
"maxLength": 255
|
||||||
|
},
|
||||||
|
"status": {
|
||||||
|
"type": "boolean",
|
||||||
|
"description": "状态"
|
||||||
|
},
|
||||||
|
"create_time": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "date-time",
|
||||||
|
"description": "创建时间"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
paramInfo.setOptions(new HashMap<>());
|
||||||
|
paramInfo.getOptions().put("dataType", "json");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupInsertSqlTestData() {
|
||||||
|
paramInfo.setTableSql("""
|
||||||
|
INSERT INTO sys_user_info (user_id, user_name, status, create_time)
|
||||||
|
VALUES (1, 'admin', 1, '2023-12-07 10:00:00')
|
||||||
|
""");
|
||||||
|
paramInfo.setOptions(new HashMap<>());
|
||||||
|
paramInfo.getOptions().put("dataType", "insert-sql");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("测试生成代码成功")
|
@DisplayName("测试SQL类型生成代码成功")
|
||||||
void testGenerateCodeSuccess() throws Exception {
|
void testGenerateCodeSuccessWithSql() throws Exception {
|
||||||
// Given
|
// Given
|
||||||
|
setupSqlTestData();
|
||||||
when(sqlParserService.processTableIntoClassInfo(any(ParamInfo.class))).thenReturn(classInfo);
|
when(sqlParserService.processTableIntoClassInfo(any(ParamInfo.class))).thenReturn(classInfo);
|
||||||
when(templateService.getAllTemplates()).thenReturn(mockTemplates);
|
when(templateService.getAllTemplates()).thenReturn(mockTemplates);
|
||||||
|
|
||||||
@ -102,9 +150,10 @@ class CodeGenServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("测试表结构信息为空时返回错误")
|
@DisplayName("测试SQL类型表结构信息为空时返回错误")
|
||||||
void testGenerateCodeWithEmptyTableSql() {
|
void testGenerateCodeWithEmptyTableSql() {
|
||||||
// Given
|
// Given
|
||||||
|
setupSqlTestData();
|
||||||
paramInfo.setTableSql("");
|
paramInfo.setTableSql("");
|
||||||
|
|
||||||
// When
|
// When
|
||||||
@ -120,9 +169,10 @@ class CodeGenServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("测试表结构信息为null时返回错误")
|
@DisplayName("测试SQL类型表结构信息为null时返回错误")
|
||||||
void testGenerateCodeWithNullTableSql() {
|
void testGenerateCodeWithNullTableSql() {
|
||||||
// Given
|
// Given
|
||||||
|
setupSqlTestData();
|
||||||
paramInfo.setTableSql(null);
|
paramInfo.setTableSql(null);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
@ -138,11 +188,12 @@ class CodeGenServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("测试生成代码异常处理")
|
@DisplayName("测试SQL类型生成代码异常处理")
|
||||||
void testGenerateCodeWithException() throws Exception {
|
void testGenerateCodeWithSqlException() throws Exception {
|
||||||
// Given
|
// Given
|
||||||
|
setupSqlTestData();
|
||||||
when(sqlParserService.processTableIntoClassInfo(any(ParamInfo.class)))
|
when(sqlParserService.processTableIntoClassInfo(any(ParamInfo.class)))
|
||||||
.thenThrow(new RuntimeException("解析异常"));
|
.thenThrow(new RuntimeException("SQL解析异常"));
|
||||||
|
|
||||||
// When
|
// When
|
||||||
ResultVo result = codeGenService.generateCode(paramInfo);
|
ResultVo result = codeGenService.generateCode(paramInfo);
|
||||||
@ -153,11 +204,68 @@ class CodeGenServiceTest {
|
|||||||
assertTrue(result.get("msg").toString().contains("代码生成失败"));
|
assertTrue(result.get("msg").toString().contains("代码生成失败"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("测试JSON类型表结构信息为空时返回错误")
|
||||||
|
void testGenerateCodeJsonWithEmptyTableSql() {
|
||||||
|
// Given
|
||||||
|
setupJsonTestData();
|
||||||
|
paramInfo.setTableSql("");
|
||||||
|
|
||||||
|
// When
|
||||||
|
try {
|
||||||
|
ResultVo result = codeGenService.generateCode(paramInfo);
|
||||||
|
// Then
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(500, result.get("code"));
|
||||||
|
assertEquals("表结构信息为空", result.get("msg"));
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("不应该抛出异常: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("测试INSERT_SQL类型表结构信息为空时返回错误")
|
||||||
|
void testGenerateCodeInsertSqlWithEmptyTableSql() {
|
||||||
|
// Given
|
||||||
|
setupInsertSqlTestData();
|
||||||
|
paramInfo.setTableSql("");
|
||||||
|
|
||||||
|
// When
|
||||||
|
try {
|
||||||
|
ResultVo result = codeGenService.generateCode(paramInfo);
|
||||||
|
// Then
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(500, result.get("code"));
|
||||||
|
assertEquals("表结构信息为空", result.get("msg"));
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail("不应该抛出异常: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("测试ParserTypeEnum解析")
|
||||||
|
void testParserTypeEnum() {
|
||||||
|
// 验证枚举解析行为
|
||||||
|
assertEquals(ParserTypeEnum.SQL, ParserTypeEnum.fromValue("SQL"));
|
||||||
|
assertEquals(ParserTypeEnum.SQL, ParserTypeEnum.fromValue("sql"));
|
||||||
|
assertEquals(ParserTypeEnum.JSON, ParserTypeEnum.fromValue("JSON"));
|
||||||
|
assertEquals(ParserTypeEnum.JSON, ParserTypeEnum.fromValue("json"));
|
||||||
|
assertEquals(ParserTypeEnum.INSERT_SQL, ParserTypeEnum.fromValue("INSERT_SQL"));
|
||||||
|
assertEquals(ParserTypeEnum.INSERT_SQL, ParserTypeEnum.fromValue("insert-sql"));
|
||||||
|
|
||||||
|
// 测试未知值默认返回SQL
|
||||||
|
assertEquals(ParserTypeEnum.SQL, ParserTypeEnum.fromValue("UNKNOWN"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("测试JSON模式解析")
|
@DisplayName("测试JSON模式解析")
|
||||||
void testGenerateCodeWithJsonMode() throws Exception {
|
void testGenerateCodeWithJsonMode() throws Exception {
|
||||||
// Given
|
// Given
|
||||||
paramInfo.getOptions().put("dataType", "JSON");
|
setupJsonTestData();
|
||||||
|
|
||||||
|
// 验证 dataType 设置是否正确
|
||||||
|
assertEquals("json", paramInfo.getOptions().get("dataType"));
|
||||||
|
|
||||||
when(jsonParserService.processJsonToClassInfo(any(ParamInfo.class))).thenReturn(classInfo);
|
when(jsonParserService.processJsonToClassInfo(any(ParamInfo.class))).thenReturn(classInfo);
|
||||||
when(templateService.getAllTemplates()).thenReturn(mockTemplates);
|
when(templateService.getAllTemplates()).thenReturn(mockTemplates);
|
||||||
|
|
||||||
@ -175,15 +283,34 @@ class CodeGenServiceTest {
|
|||||||
// Then
|
// Then
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertEquals(200, result.get("code"));
|
assertEquals(200, result.get("code"));
|
||||||
|
assertNotNull(result.get("data"));
|
||||||
verify(jsonParserService).processJsonToClassInfo(paramInfo);
|
verify(jsonParserService).processJsonToClassInfo(paramInfo);
|
||||||
|
verify(templateService).getAllTemplates();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("测试JSON类型解析异常处理")
|
||||||
|
void testGenerateCodeWithJsonException() throws Exception {
|
||||||
|
// Given
|
||||||
|
setupJsonTestData();
|
||||||
|
when(jsonParserService.processJsonToClassInfo(any(ParamInfo.class)))
|
||||||
|
.thenThrow(new RuntimeException("JSON解析异常"));
|
||||||
|
|
||||||
|
// When
|
||||||
|
ResultVo result = codeGenService.generateCode(paramInfo);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(500, result.get("code"));
|
||||||
|
assertTrue(result.get("msg").toString().contains("代码生成失败"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("测试INSERT SQL模式解析")
|
@DisplayName("测试INSERT SQL模式解析")
|
||||||
void testGenerateCodeWithInsertSqlMode() throws Exception {
|
void testGenerateCodeWithInsertSqlMode() throws Exception {
|
||||||
// Given
|
// Given
|
||||||
paramInfo.getOptions().put("dataType", "INSERT_SQL");
|
setupInsertSqlTestData();
|
||||||
when(sqlParserService.processInsertSqlToClassInfo(any(ParamInfo.class))).thenReturn(classInfo);
|
when(sqlParserService.processInsertSqlToClassInfo(any(ParamInfo.class))).thenReturn(classInfo);
|
||||||
when(templateService.getAllTemplates()).thenReturn(mockTemplates);
|
when(templateService.getAllTemplates()).thenReturn(mockTemplates);
|
||||||
|
|
||||||
@ -201,10 +328,29 @@ class CodeGenServiceTest {
|
|||||||
// Then
|
// Then
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertEquals(200, result.get("code"));
|
assertEquals(200, result.get("code"));
|
||||||
|
assertNotNull(result.get("data"));
|
||||||
verify(sqlParserService).processInsertSqlToClassInfo(paramInfo);
|
verify(sqlParserService).processInsertSqlToClassInfo(paramInfo);
|
||||||
|
verify(templateService).getAllTemplates();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("测试INSERT SQL类型解析异常处理")
|
||||||
|
void testGenerateCodeWithInsertSqlException() throws Exception {
|
||||||
|
// Given
|
||||||
|
setupInsertSqlTestData();
|
||||||
|
when(sqlParserService.processInsertSqlToClassInfo(any(ParamInfo.class)))
|
||||||
|
.thenThrow(new RuntimeException("INSERT SQL解析异常"));
|
||||||
|
|
||||||
|
// When
|
||||||
|
ResultVo result = codeGenService.generateCode(paramInfo);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(500, result.get("code"));
|
||||||
|
assertTrue(result.get("msg").toString().contains("代码生成失败"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("测试根据参数获取结果")
|
@DisplayName("测试根据参数获取结果")
|
||||||
void testGetResultByParams() throws Exception {
|
void testGetResultByParams() throws Exception {
|
||||||
@ -257,4 +403,234 @@ class CodeGenServiceTest {
|
|||||||
assertEquals("test", result.get("tableName"));
|
assertEquals("test", result.get("tableName"));
|
||||||
verify(templateService).getAllTemplates();
|
verify(templateService).getAllTemplates();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("测试复杂SQL表结构解析")
|
||||||
|
void testGenerateCodeWithComplexSql() throws Exception {
|
||||||
|
// Given
|
||||||
|
paramInfo.setTableSql("""
|
||||||
|
CREATE TABLE `complex_table` (
|
||||||
|
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
|
||||||
|
`user_name` varchar(50) NOT NULL DEFAULT '' COMMENT '用户名',
|
||||||
|
`email` varchar(100) DEFAULT NULL COMMENT '邮箱',
|
||||||
|
`age` int(3) DEFAULT '0' COMMENT '年龄',
|
||||||
|
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态:0-禁用,1-启用',
|
||||||
|
`price` decimal(10,2) DEFAULT '0.00' COMMENT '价格',
|
||||||
|
`description` text COMMENT '描述',
|
||||||
|
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||||
|
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
UNIQUE KEY `uk_user_name` (`user_name`),
|
||||||
|
KEY `idx_email` (`email`),
|
||||||
|
KEY `idx_status` (`status`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='复杂表结构示例'
|
||||||
|
""");
|
||||||
|
paramInfo.setOptions(new HashMap<>());
|
||||||
|
paramInfo.getOptions().put("dataType", "sql");
|
||||||
|
|
||||||
|
when(sqlParserService.processTableIntoClassInfo(any(ParamInfo.class))).thenReturn(classInfo);
|
||||||
|
when(templateService.getAllTemplates()).thenReturn(mockTemplates);
|
||||||
|
|
||||||
|
try (MockedStatic<FreemarkerUtil> freemarkerMock = mockStatic(FreemarkerUtil.class);
|
||||||
|
MockedStatic<MapUtil> mapUtilMock = mockStatic(MapUtil.class)) {
|
||||||
|
|
||||||
|
freemarkerMock.when(() -> FreemarkerUtil.processString(anyString(), any(Map.class)))
|
||||||
|
.thenReturn("generated code from complex SQL");
|
||||||
|
mapUtilMock.when(() -> MapUtil.getString(any(Map.class), anyString()))
|
||||||
|
.thenReturn("complex_table");
|
||||||
|
|
||||||
|
// When
|
||||||
|
ResultVo result = codeGenService.generateCode(paramInfo);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(200, result.get("code"));
|
||||||
|
assertNotNull(result.get("data"));
|
||||||
|
verify(sqlParserService).processTableIntoClassInfo(paramInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("测试嵌套JSON结构解析")
|
||||||
|
void testGenerateCodeWithNestedJson() throws Exception {
|
||||||
|
// Given
|
||||||
|
paramInfo.setTableSql("""
|
||||||
|
{
|
||||||
|
"user": {
|
||||||
|
"id": {"type": "number", "description": "用户ID"},
|
||||||
|
"profile": {
|
||||||
|
"name": {"type": "string", "description": "姓名"},
|
||||||
|
"contact": {
|
||||||
|
"email": {"type": "string", "format": "email", "description": "邮箱"},
|
||||||
|
"phone": {"type": "string", "description": "电话"}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"roles": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {"type": "string"},
|
||||||
|
"description": "用户角色列表"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
paramInfo.setOptions(new HashMap<>());
|
||||||
|
paramInfo.getOptions().put("dataType", "json");
|
||||||
|
|
||||||
|
when(jsonParserService.processJsonToClassInfo(any(ParamInfo.class))).thenReturn(classInfo);
|
||||||
|
when(templateService.getAllTemplates()).thenReturn(mockTemplates);
|
||||||
|
|
||||||
|
try (MockedStatic<FreemarkerUtil> freemarkerMock = mockStatic(FreemarkerUtil.class);
|
||||||
|
MockedStatic<MapUtil> mapUtilMock = mockStatic(MapUtil.class)) {
|
||||||
|
|
||||||
|
freemarkerMock.when(() -> FreemarkerUtil.processString(anyString(), any(Map.class)))
|
||||||
|
.thenReturn("generated code from nested JSON");
|
||||||
|
mapUtilMock.when(() -> MapUtil.getString(any(Map.class), anyString()))
|
||||||
|
.thenReturn("user_profile");
|
||||||
|
|
||||||
|
// When
|
||||||
|
ResultVo result = codeGenService.generateCode(paramInfo);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(200, result.get("code"));
|
||||||
|
assertNotNull(result.get("data"));
|
||||||
|
verify(jsonParserService).processJsonToClassInfo(paramInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("测试批量INSERT SQL解析")
|
||||||
|
void testGenerateCodeWithBatchInsertSql() throws Exception {
|
||||||
|
// Given
|
||||||
|
paramInfo.setTableSql("""
|
||||||
|
INSERT INTO sys_user_info (user_id, user_name, status, create_time) VALUES
|
||||||
|
(1, 'admin', 1, '2023-12-07 10:00:00'),
|
||||||
|
(2, 'user1', 1, '2023-12-07 10:01:00'),
|
||||||
|
(3, 'user2', 0, '2023-12-07 10:02:00');
|
||||||
|
""");
|
||||||
|
paramInfo.setOptions(new HashMap<>());
|
||||||
|
paramInfo.getOptions().put("dataType", "insert-sql");
|
||||||
|
|
||||||
|
when(sqlParserService.processInsertSqlToClassInfo(any(ParamInfo.class))).thenReturn(classInfo);
|
||||||
|
when(templateService.getAllTemplates()).thenReturn(mockTemplates);
|
||||||
|
|
||||||
|
try (MockedStatic<FreemarkerUtil> freemarkerMock = mockStatic(FreemarkerUtil.class);
|
||||||
|
MockedStatic<MapUtil> mapUtilMock = mockStatic(MapUtil.class)) {
|
||||||
|
|
||||||
|
freemarkerMock.when(() -> FreemarkerUtil.processString(anyString(), any(Map.class)))
|
||||||
|
.thenReturn("generated code from batch INSERT SQL");
|
||||||
|
mapUtilMock.when(() -> MapUtil.getString(any(Map.class), anyString()))
|
||||||
|
.thenReturn("sys_user_info");
|
||||||
|
|
||||||
|
// When
|
||||||
|
ResultVo result = codeGenService.generateCode(paramInfo);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(200, result.get("code"));
|
||||||
|
assertNotNull(result.get("data"));
|
||||||
|
verify(sqlParserService).processInsertSqlToClassInfo(paramInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("测试未知数据类型处理")
|
||||||
|
void testGenerateCodeWithUnknownDataType() throws Exception {
|
||||||
|
// Given
|
||||||
|
setupSqlTestData();
|
||||||
|
paramInfo.getOptions().put("dataType", "unknown-type");
|
||||||
|
|
||||||
|
when(sqlParserService.processTableIntoClassInfo(any(ParamInfo.class))).thenReturn(classInfo);
|
||||||
|
when(templateService.getAllTemplates()).thenReturn(mockTemplates);
|
||||||
|
|
||||||
|
try (MockedStatic<FreemarkerUtil> freemarkerMock = mockStatic(FreemarkerUtil.class);
|
||||||
|
MockedStatic<MapUtil> mapUtilMock = mockStatic(MapUtil.class)) {
|
||||||
|
|
||||||
|
freemarkerMock.when(() -> FreemarkerUtil.processString(anyString(), any(Map.class)))
|
||||||
|
.thenReturn("default generated code");
|
||||||
|
mapUtilMock.when(() -> MapUtil.getString(any(Map.class), anyString()))
|
||||||
|
.thenReturn("test");
|
||||||
|
|
||||||
|
// When
|
||||||
|
ResultVo result = codeGenService.generateCode(paramInfo);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(200, result.get("code"));
|
||||||
|
assertNotNull(result.get("data"));
|
||||||
|
verify(sqlParserService).processTableIntoClassInfo(paramInfo);
|
||||||
|
verify(templateService).getAllTemplates();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("测试正则表达式SQL解析")
|
||||||
|
void testGenerateCodeWithSqlRegex() throws Exception {
|
||||||
|
// Given
|
||||||
|
paramInfo.setTableSql("""
|
||||||
|
CREATE TABLE regex_test (
|
||||||
|
id INT PRIMARY KEY,
|
||||||
|
name VARCHAR(100)
|
||||||
|
);
|
||||||
|
""");
|
||||||
|
paramInfo.setOptions(new HashMap<>());
|
||||||
|
paramInfo.getOptions().put("dataType", "sql-regex");
|
||||||
|
|
||||||
|
// 添加调试信息
|
||||||
|
System.out.println("Test Debug: Setting dataType to: " + paramInfo.getOptions().get("dataType"));
|
||||||
|
System.out.println("Test Debug: Expected parser type: SQL_REGEX");
|
||||||
|
|
||||||
|
when(sqlParserService.processTableToClassInfoByRegex(any(ParamInfo.class))).thenReturn(classInfo);
|
||||||
|
when(templateService.getAllTemplates()).thenReturn(mockTemplates);
|
||||||
|
|
||||||
|
try (MockedStatic<FreemarkerUtil> freemarkerMock = mockStatic(FreemarkerUtil.class);
|
||||||
|
MockedStatic<MapUtil> mapUtilMock = mockStatic(MapUtil.class)) {
|
||||||
|
|
||||||
|
freemarkerMock.when(() -> FreemarkerUtil.processString(anyString(), any(Map.class)))
|
||||||
|
.thenReturn("generated code from regex SQL");
|
||||||
|
mapUtilMock.when(() -> MapUtil.getString(any(Map.class), anyString()))
|
||||||
|
.thenReturn("regex_test");
|
||||||
|
|
||||||
|
// When
|
||||||
|
ResultVo result = codeGenService.generateCode(paramInfo);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(200, result.get("code"));
|
||||||
|
assertNotNull(result.get("data"));
|
||||||
|
verify(sqlParserService).processTableToClassInfoByRegex(paramInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("测试SELECT SQL解析")
|
||||||
|
void testGenerateCodeWithSelectSql() throws Exception {
|
||||||
|
// Given
|
||||||
|
paramInfo.setTableSql("""
|
||||||
|
SELECT id, name, email FROM users WHERE status = 1
|
||||||
|
""");
|
||||||
|
paramInfo.setOptions(new HashMap<>());
|
||||||
|
paramInfo.getOptions().put("dataType", "select-sql");
|
||||||
|
|
||||||
|
when(sqlParserService.generateSelectSqlBySQLPraser(any(ParamInfo.class))).thenReturn(classInfo);
|
||||||
|
when(templateService.getAllTemplates()).thenReturn(mockTemplates);
|
||||||
|
|
||||||
|
try (MockedStatic<FreemarkerUtil> freemarkerMock = mockStatic(FreemarkerUtil.class);
|
||||||
|
MockedStatic<MapUtil> mapUtilMock = mockStatic(MapUtil.class)) {
|
||||||
|
|
||||||
|
freemarkerMock.when(() -> FreemarkerUtil.processString(anyString(), any(Map.class)))
|
||||||
|
.thenReturn("generated code from SELECT SQL");
|
||||||
|
mapUtilMock.when(() -> MapUtil.getString(any(Map.class), anyString()))
|
||||||
|
.thenReturn("users");
|
||||||
|
|
||||||
|
// When
|
||||||
|
ResultVo result = codeGenService.generateCode(paramInfo);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(200, result.get("code"));
|
||||||
|
assertNotNull(result.get("data"));
|
||||||
|
verify(sqlParserService).generateSelectSqlBySQLPraser(paramInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -33,7 +33,37 @@ class TemplateServiceTest {
|
|||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
mockTemplateConfig = "[{\"group\":\"basic\",\"templates\":[{\"name\":\"Entity\",\"type\":\"java\"},{\"name\":\"Repository\",\"type\":\"java\"}]}]";
|
mockTemplateConfig = """
|
||||||
|
[{
|
||||||
|
"group": "ui",
|
||||||
|
"templates": [{
|
||||||
|
"id": "10",
|
||||||
|
"name": "swagger-ui",
|
||||||
|
"description": "swagger-ui"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "11",
|
||||||
|
"name": "element-ui",
|
||||||
|
"description": "element-ui"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "12",
|
||||||
|
"name": "bootstrap-ui",
|
||||||
|
"description": "bootstrap-ui"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "13",
|
||||||
|
"name": "layui-edit",
|
||||||
|
"description": "layui-edit"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "14",
|
||||||
|
"name": "layui-list",
|
||||||
|
"description": "layui-list"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}]
|
||||||
|
""";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@ -12,19 +12,9 @@ class StringUtilsPlusTest {
|
|||||||
assertNull(StringUtilsPlus.toUnderline(null, false));
|
assertNull(StringUtilsPlus.toUnderline(null, false));
|
||||||
assertEquals("", StringUtilsPlus.toUnderline("", false));
|
assertEquals("", StringUtilsPlus.toUnderline("", false));
|
||||||
assertEquals("test_string", StringUtilsPlus.toUnderline("testString", false));
|
assertEquals("test_string", StringUtilsPlus.toUnderline("testString", false));
|
||||||
assertEquals("test_string", StringUtilsPlus.toUnderline("testString", true));
|
|
||||||
assertEquals("TEST_STRING", StringUtilsPlus.toUnderline("testString", true));
|
assertEquals("TEST_STRING", StringUtilsPlus.toUnderline("testString", true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void testToUnderScoreCase() {
|
|
||||||
assertNull(StringUtilsPlus.toUnderline(null, false));
|
|
||||||
assertEquals("", StringUtilsPlus.toUnderline("", false));
|
|
||||||
assertEquals("test_string", StringUtilsPlus.toUnderline("testString", false));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testToUpperCaseFirst() {
|
void testToUpperCaseFirst() {
|
||||||
assertNull(StringUtilsPlus.upperCaseFirst(null));
|
assertNull(StringUtilsPlus.upperCaseFirst(null));
|
||||||
@ -35,15 +25,15 @@ class StringUtilsPlusTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testToLowerCaseFirst() {
|
void testToLowerCaseFirst() {
|
||||||
assertNull(StringUtilsPlus.lowerCaseFirst(null));
|
assertEquals("", StringUtilsPlus.lowerCaseFirst(null));
|
||||||
assertEquals("", StringUtilsPlus.lowerCaseFirst(""));
|
assertEquals("", StringUtilsPlus.lowerCaseFirst(""));
|
||||||
assertEquals("test", StringUtilsPlus.lowerCaseFirst("Test"));
|
assertEquals("test", StringUtilsPlus.lowerCaseFirst("Test"));
|
||||||
assertEquals("test", StringUtilsPlus.lowerCaseFirst("TEST"));
|
assertEquals("tEST", StringUtilsPlus.lowerCaseFirst("TEST"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testUnderlineToCamelCase() {
|
void testUnderlineToCamelCase() {
|
||||||
assertNull(StringUtilsPlus.underlineToCamelCase(null));
|
assertEquals("", StringUtilsPlus.underlineToCamelCase(null));
|
||||||
assertEquals("", StringUtilsPlus.underlineToCamelCase(""));
|
assertEquals("", StringUtilsPlus.underlineToCamelCase(""));
|
||||||
assertEquals("testString", StringUtilsPlus.underlineToCamelCase("test_string"));
|
assertEquals("testString", StringUtilsPlus.underlineToCamelCase("test_string"));
|
||||||
assertEquals("testString", StringUtilsPlus.underlineToCamelCase("test__string"));
|
assertEquals("testString", StringUtilsPlus.underlineToCamelCase("test__string"));
|
||||||
@ -53,7 +43,7 @@ class StringUtilsPlusTest {
|
|||||||
void testIsNotNull() {
|
void testIsNotNull() {
|
||||||
assertFalse(StringUtilsPlus.isNotNull(null));
|
assertFalse(StringUtilsPlus.isNotNull(null));
|
||||||
assertFalse(StringUtilsPlus.isNotNull(""));
|
assertFalse(StringUtilsPlus.isNotNull(""));
|
||||||
assertFalse(StringUtilsPlus.isNotNull(" "));
|
assertTrue(StringUtilsPlus.isNotNull(" "));
|
||||||
assertTrue(StringUtilsPlus.isNotNull("test"));
|
assertTrue(StringUtilsPlus.isNotNull("test"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user