FreeMarkerGeneratorTest.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. package com.xjrsoft.xjrsoftboot;
  2. import cn.dev33.satoken.secure.SaSecureUtil;
  3. import cn.hutool.core.bean.BeanUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.db.ds.simple.SimpleDataSource;
  6. import cn.hutool.db.meta.Column;
  7. import cn.hutool.db.meta.MetaUtil;
  8. import cn.hutool.db.meta.Table;
  9. import cn.hutool.http.HttpUtil;
  10. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  11. import com.xjrsoft.XjrSoftApplication;
  12. import com.xjrsoft.common.constant.GlobalConstant;
  13. import com.xjrsoft.common.utils.DatasourceUtil;
  14. import com.xjrsoft.common.utils.JdbcToJavaUtil;
  15. import com.xjrsoft.module.generator.constant.EntityConstant;
  16. import com.xjrsoft.module.generator.dto.ApiGenerateCodesDto;
  17. import com.xjrsoft.module.generator.entity.FieldConfig;
  18. import com.xjrsoft.module.generator.entity.GeneratorConfig;
  19. import com.xjrsoft.module.generator.entity.TableConfig;
  20. import com.xjrsoft.module.generator.service.IApiGeneratorService;
  21. import com.xjrsoft.module.generator.service.impl.ApiGeneratorServiceImpl;
  22. import freemarker.template.Template;
  23. import freemarker.template.TemplateException;
  24. import lombok.AllArgsConstructor;
  25. import org.junit.jupiter.api.Test;
  26. import org.junit.runner.RunWith;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.boot.test.context.SpringBootTest;
  29. import org.springframework.test.context.junit4.SpringRunner;
  30. import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
  31. import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
  32. import javax.sql.DataSource;
  33. import java.io.FileNotFoundException;
  34. import java.io.IOException;
  35. import java.time.LocalDate;
  36. import java.time.format.DateTimeFormatter;
  37. import java.util.*;
  38. import java.util.stream.Collectors;
  39. public class FreeMarkerGeneratorTest {
  40. private static final DataSource ds = new SimpleDataSource("jdbc:mysql://10.150.10.139:3306/tl?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true",
  41. "root" , "Zwr~-f6H,u6QE^]C-AD_" );
  42. @Test
  43. public void generateCodesTest() throws IOException {
  44. List<TableConfig> tableConfigs = new ArrayList<>();
  45. TableConfig mainTable = new TableConfig();
  46. mainTable.setTableName("xjr_oa_news");
  47. mainTable.setIsMain(true);
  48. mainTable.setPkField(GlobalConstant.DEFAULT_PK);
  49. mainTable.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  50. TableConfig childTable1 = new TableConfig();
  51. childTable1.setTableName("xjr_oa_news_relation");
  52. childTable1.setIsMain(false);
  53. childTable1.setPkField(GlobalConstant.DEFAULT_PK);
  54. childTable1.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  55. childTable1.setRelationField("news_id");
  56. childTable1.setRelationTableField(GlobalConstant.DEFAULT_PK);
  57. TableConfig childTable2 = new TableConfig();
  58. childTable2.setTableName("xjr_oa_news_relation_config");
  59. childTable2.setIsMain(false);
  60. childTable2.setPkField(GlobalConstant.DEFAULT_PK);
  61. childTable2.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  62. childTable2.setRelationField("news_id");
  63. childTable2.setRelationTableField(GlobalConstant.DEFAULT_PK);
  64. tableConfigs.add(mainTable);
  65. tableConfigs.add(childTable1);
  66. // tableConfigs.add(childTable2);
  67. ApiGenerateCodesDto params = new ApiGenerateCodesDto();
  68. params.setAuthor("fanxp");
  69. params.setPackageName("test");
  70. params.setTableConfigs(tableConfigs);
  71. params.setPage(true);
  72. params.setImport(true);
  73. params.setExport(true);
  74. params.setOutMainDir(false);
  75. params.setDs(ds);
  76. IApiGeneratorService apiGeneratorService = new ApiGeneratorServiceImpl();
  77. apiGeneratorService.generateCodes(params);
  78. }
  79. @Test
  80. public void generateCodesTest1() throws IOException {
  81. List<TableConfig> tableConfigs = new ArrayList<>();
  82. TableConfig mainTable = new TableConfig();
  83. mainTable.setTableName("xjr_oa_news");
  84. mainTable.setIsMain(true);
  85. mainTable.setPkField(GlobalConstant.DEFAULT_PK);
  86. mainTable.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  87. TableConfig childTable1 = new TableConfig();
  88. childTable1.setTableName("xjr_oa_news_relation");
  89. childTable1.setIsMain(false);
  90. childTable1.setPkField(GlobalConstant.DEFAULT_PK);
  91. childTable1.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  92. childTable1.setRelationField("news_id");
  93. childTable1.setRelationTableField(GlobalConstant.DEFAULT_PK);
  94. TableConfig childTable2 = new TableConfig();
  95. childTable2.setTableName("xjr_oa_news_relation_config");
  96. childTable2.setIsMain(false);
  97. childTable2.setPkField(GlobalConstant.DEFAULT_PK);
  98. childTable2.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  99. childTable2.setRelationField("news_id");
  100. childTable2.setRelationTableField(GlobalConstant.DEFAULT_PK);
  101. tableConfigs.add(mainTable);
  102. tableConfigs.add(childTable1);
  103. // tableConfigs.add(childTable2);
  104. ApiGenerateCodesDto params = new ApiGenerateCodesDto();
  105. params.setAuthor("fanxp");
  106. params.setPackageName("test");
  107. params.setTableConfigs(tableConfigs);
  108. params.setPage(true);
  109. params.setImport(true);
  110. params.setExport(true);
  111. params.setOutMainDir(false);
  112. params.setDs(ds);
  113. IApiGeneratorService apiGeneratorService = new ApiGeneratorServiceImpl();
  114. apiGeneratorService.generateCodes(params);
  115. }
  116. @Test
  117. public void gcMaterialType() throws IOException {
  118. List<TableConfig> tableConfigs = new ArrayList<>();
  119. TableConfig mainTable = new TableConfig();
  120. mainTable.setTableName("material_type");
  121. mainTable.setIsMain(true);
  122. mainTable.setPkField(GlobalConstant.DEFAULT_PK);
  123. mainTable.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  124. TableConfig childTable1 = new TableConfig();
  125. childTable1.setTableName("material_type_assign");
  126. childTable1.setIsMain(false);
  127. childTable1.setPkField(GlobalConstant.DEFAULT_PK);
  128. childTable1.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  129. childTable1.setRelationField("material_type_id");
  130. childTable1.setRelationTableField(GlobalConstant.DEFAULT_PK);
  131. tableConfigs.add(mainTable);
  132. tableConfigs.add(childTable1);
  133. ApiGenerateCodesDto params = new ApiGenerateCodesDto();
  134. params.setAuthor("baicai");
  135. params.setPackageName("material");
  136. params.setTableConfigs(tableConfigs);
  137. params.setPage(true);
  138. params.setImport(true);
  139. params.setExport(true);
  140. params.setOutMainDir(false);
  141. params.setDs(ds);
  142. IApiGeneratorService apiGeneratorService = new ApiGeneratorServiceImpl();
  143. apiGeneratorService.generateCodes(params);
  144. }
  145. @Test
  146. public void gcMaterialTask() throws IOException {
  147. List<TableConfig> tableConfigs = new ArrayList<>();
  148. TableConfig mainTable = new TableConfig();
  149. mainTable.setTableName("material_task");
  150. mainTable.setIsMain(true);
  151. mainTable.setPkField(GlobalConstant.DEFAULT_PK);
  152. mainTable.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  153. TableConfig childTable1 = new TableConfig();
  154. childTable1.setTableName("material_task_assign");
  155. childTable1.setIsMain(false);
  156. childTable1.setPkField(GlobalConstant.DEFAULT_PK);
  157. childTable1.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  158. childTable1.setRelationField("material_task_id");
  159. childTable1.setRelationTableField(GlobalConstant.DEFAULT_PK);
  160. TableConfig childTable2 = new TableConfig();
  161. childTable2.setTableName("material_task_appendix");
  162. childTable2.setIsMain(false);
  163. childTable2.setPkField(GlobalConstant.DEFAULT_PK);
  164. childTable2.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  165. childTable2.setRelationField("material_task_id");
  166. childTable2.setRelationTableField(GlobalConstant.DEFAULT_PK);
  167. tableConfigs.add(mainTable);
  168. tableConfigs.add(childTable1);
  169. tableConfigs.add(childTable2);
  170. ApiGenerateCodesDto params = new ApiGenerateCodesDto();
  171. params.setAuthor("baicai");
  172. params.setPackageName("material");
  173. params.setTableConfigs(tableConfigs);
  174. params.setPage(true);
  175. params.setImport(true);
  176. params.setExport(true);
  177. params.setOutMainDir(false);
  178. params.setDs(ds);
  179. IApiGeneratorService apiGeneratorService = new ApiGeneratorServiceImpl();
  180. apiGeneratorService.generateCodes(params);
  181. }
  182. @Test
  183. public void gcLiteFlowTest() throws IOException {
  184. List<TableConfig> tableConfigs = new ArrayList<>();
  185. TableConfig mainTable = new TableConfig();
  186. mainTable.setTableName("xjr_liteflow_chain");
  187. mainTable.setIsMain(true);
  188. mainTable.setPkField(GlobalConstant.DEFAULT_PK);
  189. mainTable.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  190. tableConfigs.add(mainTable);
  191. ApiGenerateCodesDto params = new ApiGenerateCodesDto();
  192. params.setAuthor("fanxp");
  193. params.setPackageName("liteflow");
  194. params.setTableConfigs(tableConfigs);
  195. params.setPage(false);
  196. params.setImport(false);
  197. params.setExport(false);
  198. params.setOutMainDir(false);
  199. params.setDs(ds);
  200. IApiGeneratorService apiGeneratorService = new ApiGeneratorServiceImpl();
  201. apiGeneratorService.generateCodes(params);
  202. }
  203. @Test
  204. public void gcWFPushTest() throws IOException {
  205. List<TableConfig> tableConfigs = new ArrayList<>();
  206. TableConfig mainTable = new TableConfig();
  207. mainTable.setTableName("wf_oa_push");
  208. mainTable.setIsMain(true);
  209. mainTable.setPkField(GlobalConstant.DEFAULT_PK);
  210. mainTable.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  211. tableConfigs.add(mainTable);
  212. ApiGenerateCodesDto params = new ApiGenerateCodesDto();
  213. params.setAuthor("fanxp");
  214. params.setPackageName("oa");
  215. params.setTableConfigs(tableConfigs);
  216. params.setPage(false);
  217. params.setImport(false);
  218. params.setExport(false);
  219. params.setOutMainDir(false);
  220. params.setDs(ds);
  221. IApiGeneratorService apiGeneratorService = new ApiGeneratorServiceImpl();
  222. apiGeneratorService.generateCodes(params);
  223. }
  224. @Test
  225. public void gcWFPullest() throws IOException {
  226. List<TableConfig> tableConfigs = new ArrayList<>();
  227. TableConfig mainTable = new TableConfig();
  228. mainTable.setTableName("wf_oa_pull");
  229. mainTable.setIsMain(true);
  230. mainTable.setPkField(GlobalConstant.DEFAULT_PK);
  231. mainTable.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  232. tableConfigs.add(mainTable);
  233. ApiGenerateCodesDto params = new ApiGenerateCodesDto();
  234. params.setAuthor("fanxp");
  235. params.setPackageName("oa");
  236. params.setTableConfigs(tableConfigs);
  237. params.setPage(false);
  238. params.setImport(false);
  239. params.setExport(false);
  240. params.setOutMainDir(false);
  241. params.setDs(ds);
  242. IApiGeneratorService apiGeneratorService = new ApiGeneratorServiceImpl();
  243. apiGeneratorService.generateCodes(params);
  244. }
  245. @Test
  246. public void gcfileReceiveTest() throws IOException {
  247. List<TableConfig> tableConfigs = new ArrayList<>();
  248. TableConfig mainTable = new TableConfig();
  249. mainTable.setTableName("file_receive");
  250. mainTable.setIsMain(true);
  251. mainTable.setPkField(GlobalConstant.DEFAULT_PK);
  252. mainTable.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  253. tableConfigs.add(mainTable);
  254. ApiGenerateCodesDto params = new ApiGenerateCodesDto();
  255. params.setAuthor("fanxp");
  256. params.setPackageName("oa");
  257. params.setTableConfigs(tableConfigs);
  258. params.setPage(true);
  259. params.setImport(false);
  260. params.setExport(false);
  261. params.setOutMainDir(true);
  262. params.setDs(ds);
  263. IApiGeneratorService apiGeneratorService = new ApiGeneratorServiceImpl();
  264. apiGeneratorService.generateCodes(params);
  265. }
  266. @Test
  267. public void gcAppFunction() throws IOException {
  268. List<TableConfig> tableConfigs = new ArrayList<>();
  269. TableConfig mainTable = new TableConfig();
  270. mainTable.setTableName("base_app_function");
  271. mainTable.setIsMain(true);
  272. mainTable.setPkField(GlobalConstant.DEFAULT_PK);
  273. mainTable.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  274. tableConfigs.add(mainTable);
  275. ApiGenerateCodesDto params = new ApiGenerateCodesDto();
  276. params.setAuthor("fanxp");
  277. params.setPackageName("app");
  278. params.setTableConfigs(tableConfigs);
  279. params.setPage(true);
  280. params.setImport(false);
  281. params.setExport(false);
  282. params.setOutMainDir(false);
  283. params.setDs(ds);
  284. IApiGeneratorService apiGeneratorService = new ApiGeneratorServiceImpl();
  285. apiGeneratorService.generateCodes(params);
  286. }
  287. @Test
  288. public void gcOauthFunction() throws IOException {
  289. List<TableConfig> tableConfigs = new ArrayList<>();
  290. TableConfig mainTable = new TableConfig();
  291. mainTable.setTableName("oauth_client_details");
  292. mainTable.setIsMain(true);
  293. mainTable.setPkField(GlobalConstant.DEFAULT_PK);
  294. mainTable.setPkType(GlobalConstant.DEFAULT_PK_TYPE);
  295. tableConfigs.add(mainTable);
  296. ApiGenerateCodesDto params = new ApiGenerateCodesDto();
  297. params.setAuthor("fanxp");
  298. params.setPackageName("system");
  299. params.setTableConfigs(tableConfigs);
  300. params.setPage(false);
  301. params.setImport(false);
  302. params.setExport(false);
  303. params.setOutMainDir(true);
  304. params.setDs(ds);
  305. IApiGeneratorService apiGeneratorService = new ApiGeneratorServiceImpl();
  306. apiGeneratorService.generateCodes(params);
  307. }
  308. }