tools.assertion.AssertAssertion Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-autotest-tool Show documentation
Show all versions of java-autotest-tool Show documentation
This is an integration of autotest tools
package tools.assertion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tools.regex.RegexUtil;
import java.io.*;
import java.util.*;
public class AssertAssertion {
private static final Logger LOGGER = LoggerFactory.getLogger(AssertAssertion.class);
private static List filesOutOfScope = new ArrayList<>();
static {
filesOutOfScope.add("GeneratorTest.java");
filesOutOfScope.add("NewCoreDBTest.java");
filesOutOfScope.add("BaseConsumer.java");
filesOutOfScope.add("CCCBaseTest.java");
}
protected static Map readJavaAsMethodMap(String filePath){
Map testAssertMap = new HashMap<>();
File file = new File(filePath);
try {
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String currLine = "";
StringBuilder stringBuilder = new StringBuilder();
boolean testflag = false;
boolean methodBodyflag = false;
String testName = "";
while ((currLine = bufferedReader.readLine()) != null){
String regexForTestMothodLine = ".*(public|protected|private)( )+(void|.*)( )+(test.*)( )*\\(.*";
if (currLine.trim().startsWith("@Test") || currLine.trim().startsWith("@SofaApiInfo")){
testflag = true;
} else if (testflag && RegexUtil.match(currLine, regexForTestMothodLine)){
methodBodyflag = true;
stringBuilder.append(currLine).append("\n");
testName = RegexUtil.getMatchItemByRegex(currLine, regexForTestMothodLine, 5).trim();
LOGGER.info("testName={}", testName);
} else if (testflag && methodBodyflag) {
stringBuilder.append(currLine).append("\n");
if (judgeParentheses(stringBuilder.toString())){
methodBodyflag = false;
testflag = false;
System.out.println("*******");
System.out.println("方法体:\n"+stringBuilder.toString());
System.out.println("*******");
if (stringBuilder.toString().contains("Assert")){
LOGGER.info("{}包括Assert字样", testName);
testAssertMap.put(testName,true);
} else {
LOGGER.info("{}不包括Assert字样", testName);
testAssertMap.put(testName,false);
}
stringBuilder.delete(0, stringBuilder.length());
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return testAssertMap;
}
protected static boolean judgeParentheses(String strVal){
char[] charArray = strVal.toCharArray();
Stack charStack = new Stack<>();
for (char c : charArray){
if (c == '{' || c == '[' || c == '(' ){
charStack.push(c);
// LOGGER.info("压入{}", Character.toString(c));
// LOGGER.info("charStack size = {}", Integer.valueOf(charStack.size()));
}
if (c == '}'){
String topVal = Character.toString(charStack.peek());
// LOGGER.info("遇到{}, 最顶层的字符={}","}",topVal);
if ("{".contentEquals(topVal)){
charStack.pop();
// LOGGER.info("弹出栈顶元素={}",topVal);
// LOGGER.info("charStack size = {}", Integer.valueOf(charStack.size()));
} else {
// LOGGER.error("返回false===={}", "}");
return false;
}
}
if (c == ']'){
String topVal = Character.toString(charStack.peek());
// LOGGER.info("遇到{}, 最顶层的字符={}","]",topVal);
if ("[".contentEquals(topVal)){
charStack.pop();
// LOGGER.info("弹出栈顶元素={}",topVal);
// LOGGER.info("charStack size = {}", Integer.valueOf(charStack.size()));
} else {
// LOGGER.error("返回false===={}", "]");
return false;
}
}
if (c == ')'){
String topVal = Character.toString(charStack.peek());
// LOGGER.info("遇到{}, 最顶层的字符={}",")",topVal);
if ("(".contentEquals(topVal)){
charStack.pop();
// LOGGER.info("弹出栈顶元素={}",topVal);
// LOGGER.info("charStack size = {}", Integer.valueOf(charStack.size()));
} else {
// LOGGER.error("返回false===={}", ")");
return false;
}
}
}
if (charStack.isEmpty()){
return true;
} else {
return false;
}
}
protected static List getJavaFileList(String basePackage){
File basePackageFile = new File(basePackage);
List javaFileList = new ArrayList<>();
if (basePackageFile.isDirectory()){
File[] subFileArray = basePackageFile.listFiles();
if (subFileArray != null && subFileArray.length > 0){
for (File currFile : subFileArray){
if (currFile.isDirectory()){
javaFileList.addAll(getJavaFileList(currFile.getAbsolutePath()));
} else if (!filesOutOfScope.contains(currFile.getName())){
javaFileList.add(currFile.getAbsolutePath());
}
}
} else {
LOGGER.warn("[警告]:您传入的路径 {} 下并没有子文件夹或目录。", basePackage);
}
} else {
LOGGER.error("[错误]:您传入的路径 {} 并不是一个directory,请传入正确路径。", basePackage);
}
return javaFileList;
}
public static Map getTestAssertionInfoMap(String topFolderPath){
List list = AssertAssertion.getJavaFileList(topFolderPath);
Map assertMap = new HashMap<>();
for (String curr : list){
Map currJavaFileMap = readJavaAsMethodMap(curr);
assertMap.putAll(currJavaFileMap);
}
return assertMap;
}
public static void main(String[] args) {
Map assertMap = AssertAssertion.getTestAssertionInfoMap("src/test/java/com/sankuai/xy/ccc");;
System.out.println(assertMap.size());
System.out.println(assertMap);
}
}