• 8-12接口测试进阶-2源码分析


    目的:获取接口的地址、请求方法、请求参数
    方法:先确定cu树中所需元素的地址和类型

    代码

    package DateDriver812;
    
    import com.github.javaparser.JavaParser;
    import com.github.javaparser.ast.CompilationUnit;
    import com.github.javaparser.ast.Node;
    import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
    import com.github.javaparser.ast.body.MethodDeclaration;
    import com.github.javaparser.ast.body.Parameter;
    import com.github.javaparser.ast.expr.AnnotationExpr;
    import com.github.javaparser.ast.expr.MemberValuePair;
    import com.github.javaparser.ast.expr.NormalAnnotationExpr;
    import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
    import org.testng.annotations.Test;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    public class JavaParseDemo2  extends VoidVisitorAdapter {
        private Set<MethodDeclaration> allElement = new HashSet<>();
        private Set<MethodDeclaration> getAllMethod(CompilationUnit cu){
            this.visit(cu,null);
            return allElement;
        }
        @Override
        public void visit(MethodDeclaration n, Object args){
            allElement.add(n);
        }
        @Test
        public void testjavacode() throws IOException{
            FileInputStream in = new FileInputStream("file/BaseController.java");
            CompilationUnit cu = JavaParser.parse(in);
            in.close();
            List<Node> culist = cu.getChildNodes();
            for(Node cuunit:culist){
    
                if(cuunit instanceof ClassOrInterfaceDeclaration){
                    ClassOrInterfaceDeclaration classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) cuunit;
                    List<Node> listNode = classOrInterfaceDeclaration.getChildNodes();
                    for(Node n:listNode){
                        if(n instanceof NormalAnnotationExpr){
                            NormalAnnotationExpr normalAnnotationExpr = (NormalAnnotationExpr) n;
    
                            for(Node tmp:normalAnnotationExpr.getChildNodes()) {
                                if (tmp instanceof MemberValuePair) {
                                    MemberValuePair memberValuePair = (MemberValuePair) tmp;
    
                                    if ("value".equalsIgnoreCase(memberValuePair.getName().toString())) {
                                        System.out.println("URL:" + memberValuePair.getValue());
                                    }
                                }
                            }
                        }
    
                    }
    
                }
            }
            for(MethodDeclaration m:this.getAllMethod(cu)){
                System.out.println(m.getName());
                List<AnnotationExpr> listAnnotationExpr = m.getAnnotations();
                for(AnnotationExpr annotationExpr:listAnnotationExpr){
                    if("RequestMapping".equalsIgnoreCase(annotationExpr.getName().toString())){
                        List<Node> childNode = annotationExpr.getChildNodes();
                        for(Node n:childNode){
                            if(n instanceof MemberValuePair) {
                                MemberValuePair memberValuePair = (MemberValuePair) n;
    
                                if ("value".equalsIgnoreCase(memberValuePair.getName().toString())) {
                                    System.out.println("接口地址:" + memberValuePair.getValue());
                                }
                                if ("method".equalsIgnoreCase(memberValuePair.getName().toString())) {
                                    System.out.println("请求方法:" + memberValuePair.getValue());
                                }
                            }
                        }
                    }
    
    
                }
                List<Parameter> parameterList = m.getParameters();
                for(Parameter parameter:parameterList){
                    List<Node> requestParamList = parameter.getChildNodes();
                    for(Node requestParam:requestParamList){
                        if(requestParam instanceof NormalAnnotationExpr){
                            List<Node> valueList = requestParam.getChildNodes();
                            for(Node value:valueList){
                                if(value instanceof  MemberValuePair){
                                    MemberValuePair tmp = (MemberValuePair) value;
                                    if("value".equalsIgnoreCase(tmp.getName().toString())){
                                        System.out.println("参数名:"+tmp.getValue());
                                    }
                                    if("required".equalsIgnoreCase(tmp.getName().toString())){
                                        System.out.println("是否必传:"+tmp.getValue());
                                    }
                                }
                            }
                        }
                    }
                    if(!parameter.getType().toString().equalsIgnoreCase("HttpServletRequest")&& !parameter.getType().toString().equalsIgnoreCase("HttpServletResponse")){
                        System.out.println("参数类型:"+parameter.getType());
                    }
                }
            }
        }
    }
    

  • 相关阅读:
    磁盘分区,fdisk,gdisk,开机自动挂载,swap分区,修复文件系统,备份文件
    进程脱离窗口运行,僵尸、孤儿进程
    top命令、kill命令
    进程状态
    rpm包、挂载、yum命令
    DRF源码分析
    forms组件源码
    Django CBV源码分析
    魔法方法
    鸭子类型
  • 原文地址:https://www.cnblogs.com/csj2018/p/9487253.html
Copyright © 2020-2023  润新知