提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.infra.service.codegen.inner; |
H |
2 |
|
|
3 |
import cn.hutool.core.io.FileUtil; |
|
4 |
import cn.hutool.core.io.IoUtil; |
|
5 |
import cn.hutool.core.io.resource.ResourceUtil; |
|
6 |
import cn.hutool.core.map.MapUtil; |
|
7 |
import cn.hutool.core.util.StrUtil; |
|
8 |
import cn.hutool.core.util.ZipUtil; |
|
9 |
import com.iailab.framework.common.util.json.JsonUtils; |
|
10 |
import com.iailab.framework.test.core.ut.BaseMockitoUnitTest; |
|
11 |
import com.iailab.module.infra.dal.dataobject.codegen.CodegenColumnDO; |
|
12 |
import com.iailab.module.infra.dal.dataobject.codegen.CodegenTableDO; |
|
13 |
import com.iailab.module.infra.framework.codegen.config.CodegenProperties; |
|
14 |
import org.junit.jupiter.api.BeforeEach; |
|
15 |
import org.mockito.InjectMocks; |
|
16 |
import org.mockito.Spy; |
|
17 |
|
|
18 |
import java.io.ByteArrayInputStream; |
|
19 |
import java.io.ByteArrayOutputStream; |
|
20 |
import java.util.ArrayList; |
|
21 |
import java.util.HashMap; |
|
22 |
import java.util.List; |
|
23 |
import java.util.Map; |
|
24 |
|
|
25 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
26 |
|
|
27 |
/** |
|
28 |
* {@link CodegenEngine} 的单元测试抽象基类 |
|
29 |
* |
|
30 |
* @author iailab |
|
31 |
*/ |
|
32 |
public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest { |
|
33 |
|
|
34 |
/** |
|
35 |
* 测试文件资源目录 |
|
36 |
*/ |
|
37 |
private String resourcesPath = ""; |
|
38 |
|
|
39 |
@InjectMocks |
|
40 |
protected CodegenEngine codegenEngine; |
|
41 |
|
|
42 |
@Spy |
|
43 |
protected CodegenProperties codegenProperties = new CodegenProperties() |
|
44 |
.setBasePackage("com.iailab"); |
|
45 |
|
|
46 |
@BeforeEach |
|
47 |
public void setUp() { |
|
48 |
codegenEngine.setJakartaEnable(true); // 强制使用 jakarta,保证单测可以基于 jakarta 断言 |
|
49 |
codegenEngine.initGlobalBindingMap(); |
|
50 |
// 单测强制使用 |
|
51 |
// 获取测试文件 resources 路径 |
|
52 |
String absolutePath = FileUtil.getAbsolutePath("application-unit-test.yaml"); |
|
53 |
// 系统不一样生成的文件也有差异,那就各自生成各自的 |
|
54 |
resourcesPath = absolutePath.split("/target")[0] + "/src/test/resources/codegen/"; |
|
55 |
} |
|
56 |
|
|
57 |
protected static CodegenTableDO getTable(String name) { |
|
58 |
String content = ResourceUtil.readUtf8Str("codegen/table/" + name + ".json"); |
|
59 |
return JsonUtils.parseObject(content, "table", CodegenTableDO.class); |
|
60 |
} |
|
61 |
|
|
62 |
protected static List<CodegenColumnDO> getColumnList(String name) { |
|
63 |
String content = ResourceUtil.readUtf8Str("codegen/table/" + name + ".json"); |
|
64 |
List<CodegenColumnDO> list = JsonUtils.parseArray(content, "columns", CodegenColumnDO.class); |
|
65 |
list.forEach(column -> { |
|
66 |
if (column.getNullable() == null) { |
|
67 |
column.setNullable(false); |
|
68 |
} |
|
69 |
if (column.getCreateOperation() == null) { |
|
70 |
column.setCreateOperation(false); |
|
71 |
} |
|
72 |
if (column.getUpdateOperation() == null) { |
|
73 |
column.setUpdateOperation(false); |
|
74 |
} |
|
75 |
if (column.getListOperation() == null) { |
|
76 |
column.setListOperation(false); |
|
77 |
} |
|
78 |
if (column.getListOperationResult() == null) { |
|
79 |
column.setListOperationResult(false); |
|
80 |
} |
|
81 |
}); |
|
82 |
return list; |
|
83 |
} |
|
84 |
|
|
85 |
@SuppressWarnings("rawtypes") |
|
86 |
protected static void assertResult(Map<String, String> result, String path) { |
|
87 |
String assertContent = ResourceUtil.readUtf8Str("codegen/" + path + "/assert.json"); |
|
88 |
List<HashMap> asserts = JsonUtils.parseArray(assertContent, HashMap.class); |
|
89 |
assertEquals(asserts.size(), result.size()); |
|
90 |
// 校验每个文件 |
|
91 |
asserts.forEach(assertMap -> { |
|
92 |
String contentPath = (String) assertMap.get("contentPath"); |
|
93 |
String filePath = (String) assertMap.get("filePath"); |
|
94 |
String content = ResourceUtil.readUtf8Str("codegen/" + path + "/" + contentPath); |
|
95 |
assertEquals(content, result.get(filePath), filePath + ":不匹配"); |
|
96 |
}); |
|
97 |
} |
|
98 |
|
|
99 |
// ==================== 调试专用 ==================== |
|
100 |
|
|
101 |
/** |
|
102 |
* 【调试使用】将生成的代码,写入到文件 |
|
103 |
* |
|
104 |
* @param result 生成的代码 |
|
105 |
* @param path 写入文件的路径 |
|
106 |
*/ |
|
107 |
protected void writeFile(Map<String, String> result, String path) { |
|
108 |
// 生成压缩包 |
|
109 |
String[] paths = result.keySet().toArray(new String[0]); |
|
110 |
ByteArrayInputStream[] ins = result.values().stream().map(IoUtil::toUtf8Stream).toArray(ByteArrayInputStream[]::new); |
|
111 |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
|
112 |
ZipUtil.zip(outputStream, paths, ins); |
|
113 |
// 写入文件 |
|
114 |
FileUtil.writeBytes(outputStream.toByteArray(), path); |
|
115 |
} |
|
116 |
|
|
117 |
/** |
|
118 |
* 【调试使用】将生成的结果,写入到文件 |
|
119 |
* |
|
120 |
* @param result 生成的代码 |
|
121 |
* @param basePath 写入文件的路径(绝对路径) |
|
122 |
*/ |
|
123 |
protected void writeResult(Map<String, String> result, String basePath) { |
|
124 |
// 写入文件内容 |
|
125 |
List<Map<String, String>> asserts = new ArrayList<>(); |
|
126 |
result.forEach((filePath, fileContent) -> { |
|
127 |
String lastFilePath = StrUtil.subAfter(filePath, '/', true); |
|
128 |
String contentPath = StrUtil.subAfter(lastFilePath, '.', true) |
|
129 |
+ '/' + StrUtil.subBefore(lastFilePath, '.', true); |
|
130 |
asserts.add(MapUtil.<String, String>builder().put("filePath", filePath) |
|
131 |
.put("contentPath", contentPath).build()); |
|
132 |
FileUtil.writeUtf8String(fileContent, basePath + "/" + contentPath); |
|
133 |
}); |
|
134 |
// 写入 assert.json 文件 |
|
135 |
FileUtil.writeUtf8String(JsonUtils.toJsonPrettyString(asserts), basePath + "/assert.json"); |
|
136 |
} |
|
137 |
|
|
138 |
} |