Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor codegen unit test #878

Merged
merged 5 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 81 additions & 71 deletions jcommon/codegen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,87 @@
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>run.mone</groupId>
<artifactId>jcommon</artifactId>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>run.mone</groupId>
<artifactId>jcommon</artifactId>
<version>1.6.0-jdk21-SNAPSHOT</version>
</parent>
<artifactId>codegen</artifactId>
<version>1.6.0-jdk21-SNAPSHOT</version>
</parent>
<artifactId>codegen</artifactId>
<version>1.6.0-jdk21-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>run.mone</groupId>
<artifactId>codegen-temlates</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.7.14</version>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-codegen</artifactId>
<version>1.7.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.29</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>run.mone</groupId>
<artifactId>infra-result</artifactId>
<version>1.6.0-jdk21-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>thrift</artifactId>
<groupId>org.apache.thrift</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>run.mone</groupId>
<artifactId>codegen-temlates</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>3.15.4.RELEASE</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mybatis-flex</groupId>
<artifactId>mybatis-flex-codegen</artifactId>
<version>1.7.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.29</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>run.mone</groupId>
<artifactId>infra-result</artifactId>
<version>1.6.0-jdk21-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>thrift</artifactId>
<groupId>org.apache.thrift</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>


</dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,6 +48,8 @@ public class CodeGenerator {

private static boolean createController = false;

private static final String SPRING_BOOT_TEST_TYPE = "springboot";

/**
* 主方法
* 判断输入参数长度是否大于 0,调用方法处理参数并提取数据
Expand All @@ -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;
Expand All @@ -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());
// 调用方法并获取结果
Expand All @@ -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) {
Expand All @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -62,6 +64,39 @@ public static String renderTemplateFromFile(String templateFileName, Map<String,
return "";
}

@SneakyThrows
public static String renderTemplateFromFileV2(String templateFileName, Map<String, ? extends Object> m) {
try {
InputStream is = TemplateUtils.class.getClassLoader().getResourceAsStream(templateFileName);
if (is == null) {
throw new IOException("Resource not found: " + templateFileName);
}

// 使用Channels和ByteBuffer读取所有字节
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
int bytesRead;
while ((bytesRead = Channels.newChannel(is).read(byteBuffer)) != -1) {
if (byteBuffer.remaining() == 0) {
ByteBuffer newBuffer = ByteBuffer.allocate(byteBuffer.capacity() * 2);
byteBuffer.flip();
newBuffer.put(byteBuffer);
byteBuffer = newBuffer;
}
}
byteBuffer.flip();
byte[] bytes = new byte[byteBuffer.limit()];
byteBuffer.get(bytes);

// 将字节数组转换为字符串
String template = new String(bytes, StandardCharsets.UTF_8);

return renderTemplate(template, m);
} catch (IOException ex) {
log.error("Error reading template file", ex);
}
return "";
}

//把String写到指定文件中(class)
public static void writeStringToFile(String content, String filePath) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
Expand Down
29 changes: 29 additions & 0 deletions jcommon/codegen/src/main/resources/tlp/testSpring.java
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;


}
Loading
Loading