private void convertJavaFile2Test(File javaFile) {
CompilationUnit compilationUnit = JavaParserFactory.getCompilationUnit(javaFile, sourceEncode);
CompilationUnitBuilder compilationUnitBuilder = new CompilationUnitBuilder();
compilationUnitBuilder.buildComment(FileComments.GENERATOR_COMMENT);
//process package
PackageDeclaration packageDeclaration = compilationUnit.getPackage();
String testPackageName = PackageUtils.getTestPackageNameFrom(packageDeclaration);
compilationUnitBuilder.buildPackage(testPackageName);
//process import
//process type
List typeList = compilationUnit.getTypes();
for (Object type : typeList) {
TypeDeclaration typeDeclaration = (TypeDeclaration) type;
String className = typeDeclaration.getName();
getLog().info("className:" + className);
ClassTypeBuilder classTypeBuilder = new ClassTypeBuilder(className + Consts.TEST_SUFFIX);
//process methods
List<MethodDeclaration> methodDeclarations = MembersFilter.findMethodsFrom(typeDeclaration);
for (MethodDeclaration methodDeclaration : methodDeclarations) {
//has method and add import
compilationUnitBuilder.buildImports(null);
String methodName = methodDeclaration.getName();
getLog().info("methodName:" + methodDeclaration.getName());
classTypeBuilder.buildMethod(methodName + Consts.TEST_SUFFIX, methodDeclaration);
}
compilationUnitBuilder.buildClass(classTypeBuilder.build());
}
CompilationUnit testCompilationUnit = compilationUnitBuilder.build();
//写入测试代码文件
TestGenerator.writeJavaTest(testDir, testCompilationUnit);
}