-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update * refactor codegen * change pom * change infra pom
- Loading branch information
Showing
5 changed files
with
231 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,17 @@ public class CodeGenerator { | |
|
||
private static String serviceName = "s"; | ||
|
||
private static String basePath = "/Users/wodiwudi/java/nr-car-account"; | ||
private static String testPackageName = "TP"; | ||
|
||
private static String testPath = ""; | ||
|
||
private static String mainClass = ""; | ||
|
||
private static String author = ""; | ||
|
||
private static String testType = ""; | ||
|
||
private static String basePath = "you base path"; | ||
|
||
private static boolean createPojo = false; | ||
|
||
|
@@ -38,6 +48,8 @@ public class CodeGenerator { | |
|
||
private static boolean createController = false; | ||
|
||
private static final String SPRING_BOOT_TEST_TYPE = "springboot"; | ||
|
||
/** | ||
* 主方法 | ||
* 判断输入参数长度是否大于 0,调用方法处理参数并提取数据 | ||
|
@@ -53,6 +65,11 @@ public static void main(String[] args) { | |
className = map.get("pojoName"); | ||
testName = map.get("testName"); | ||
serviceName = map.get("serviceName"); | ||
testPackageName = map.get("testPackageName"); | ||
testPath = map.get("testPath"); | ||
mainClass = map.get("mainClass"); | ||
author = map.get("author"); | ||
testType = map.get("testType"); | ||
} | ||
if (StringUtils.isEmpty(className)) { | ||
return; | ||
|
@@ -61,6 +78,11 @@ public static void main(String[] args) { | |
data.put("className", className); | ||
data.put("author", "[email protected]"); | ||
data.put("serviceName", serviceName); | ||
data.put("testPackageName", testPackageName); | ||
data.put("testPath", testPath); | ||
data.put("mainClass", mainClass); | ||
data.put("testAuthor", author); | ||
data.put("testType", testType); | ||
Optional<String> first = Arrays.stream(testName.split("\\.")).findFirst(); | ||
data.put("testName", first.get()); | ||
// 调用方法并获取结果 | ||
|
@@ -86,10 +108,14 @@ public static void main(String[] args) { | |
|
||
if (createTest) { | ||
String cn = testName; | ||
System.out.println("create test:" + cn); | ||
String test = TemplateUtils.renderTemplateFromFile("tlp/test.java", data); | ||
TemplateUtils.writeStringToFile(test, basePath + "/your project/src/test/java/com/xiaomi/nr/car/account/constant/" + cn); | ||
|
||
System.out.println("create test cn :" + cn + "path:" + testPath); | ||
String test; | ||
if (SPRING_BOOT_TEST_TYPE.equals(testType)) { | ||
test = TemplateUtils.renderTemplateFromFile("tlp/testSpring.java", data); | ||
} else { | ||
test = TemplateUtils.renderTemplateFromFile("tlp/test.java", data); | ||
} | ||
TemplateUtils.writeStringToFile(test, testPath + cn); | ||
} | ||
|
||
if (createController) { | ||
|
@@ -98,6 +124,48 @@ public static void main(String[] args) { | |
} | ||
} | ||
|
||
public static void createAllUnitTest(Map<String, String> map) { | ||
if (map.isEmpty()) { | ||
return; | ||
} | ||
//方便ai调用的时候,设置表名 | ||
className = map.get("pojoName"); | ||
testName = map.get("testName"); | ||
serviceName = map.get("serviceName"); | ||
testPackageName = map.get("testPackageName"); | ||
testPath = map.get("testPath"); | ||
mainClass = map.get("mainClass"); | ||
author = map.get("author"); | ||
testType = map.get("testType"); | ||
|
||
if (StringUtils.isEmpty(className)) { | ||
return; | ||
} | ||
Map<String, Object> data = new HashMap<>(); | ||
data.put("className", className); | ||
data.put("author", "[email protected]"); | ||
data.put("serviceName", serviceName); | ||
data.put("testPackageName", testPackageName); | ||
data.put("testPath", testPath); | ||
data.put("mainClass", mainClass); | ||
data.put("testAuthor", author); | ||
data.put("testType", testType); | ||
Optional<String> first = Arrays.stream(testName.split("\\.")).findFirst(); | ||
data.put("testName", first.get()); | ||
|
||
if (createTest) { | ||
String cn = testName; | ||
System.out.println("create test cn :" + cn + "path:" + testPath); | ||
String test; | ||
if (SPRING_BOOT_TEST_TYPE.equals(testType)) { | ||
test = TemplateUtils.renderTemplateFromFileV2("tlp/testSpring.java", data); | ||
} else { | ||
test = TemplateUtils.renderTemplateFromFileV2("tlp/test.java", data); | ||
} | ||
TemplateUtils.writeStringToFile(test, testPath + cn); | ||
} | ||
} | ||
|
||
|
||
private static Map<String, String> parseArgsAndExtractData(String[] args) { | ||
String jsonStr = args[0]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ${testPackageName}; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import javax.annotation.Resource; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
import static org.mockito.ArgumentMatchers.*; | ||
|
||
/** | ||
* @author ${testAuthor} | ||
*/ | ||
@ExtendWith(SpringExtension.class) | ||
@SpringBootTest(classes = ${mainClass}.class) | ||
public class ${testName} { | ||
|
||
|
||
@Resource | ||
private ${serviceName} ${strutil.toLowerCase(serviceName)}Service; | ||
|
||
|
||
} |
Oops, something went wrong.