zoukankan      html  css  js  c++  java
  • TODO:AppiumTestDistribution--CapabilityManager 类

    该类代码详见git:https://github.com/AppiumTestDistribution/AppiumTestDistribution/tree/master/src/main/java/com/appium/capabilities

    我只知道他是用来做caps/capacities.json的json解析,但是具体怎么解析的不是很清楚,对jsonObject的类使用不清楚,看得很吃力,也没个注释。

    部分代码如下:

    /**
     * 初始化caps/capabilities.json的设备配置文件,getAllATDOverrideEnvVars方法可以从系统变量中获取atd开头的key -value键值对
     */
    public class CapabilityManager {
    
        private static CapabilityManager instance;
        private JSONObject capabilities;
    
        /**
         * 初始化配置文件:从caps/capabilities.json中读取设备信息
         */
        private CapabilityManager() {
            //设备配置路径
            String capabilitiesFilePath = getCapabilityLocation();
    //        解析json
            JsonParser jsonParser = new JsonParser(capabilitiesFilePath);
            StringBuilder varParsing = new StringBuilder(200);
            varParsing.append("atd").append("_");
            capabilities = loadAndOverrideFromEnvVars(jsonParser.getObjectFromJSON(),
                    new JSONObject(),
                    getAllATDOverrideEnvVars(),
                    varParsing);
        }
    
        /**
         * 单例模式
         * @return 返回设备配置文件信息
         */
        public static CapabilityManager getInstance() {
            if (instance == null) {
                instance = new CapabilityManager();
            }
            return instance;
        }
    
        /**
         * 返回当前系统的keyValue映射关系,找到atd开头的参数
         * @return atd开头的key的键值对
         */
        private Map<String, Object> getAllATDOverrideEnvVars() {
            Map<String, Object> atdOverrideEnv = new HashMap<>();
            System.getenv().forEach((key, value) -> {
                if (key.startsWith("atd")) {
                    atdOverrideEnv.put(key, value);
                }
            });
            return atdOverrideEnv;
        }

    ……………… }

      

    这里我不知道从哪里可以找到atd得配置,,,,在代码中找了很多地方,包括配置文件,都没有找到atd,这个来源不是很懂,要去读一下System.getenv()的API文档。

    另两个方法看的很疑惑,这里只po部分代码,这两个方法来回的调用,看得我一脸懵逼。。。。今天看了个设计模式叫代理模式,不知道这个方法的设计和代理模式有没有什么关联。还要找时间研究一下。

    /**
         * 加载并重写系统变量,遍历所有的json的最小单位,
         * @param originalObject 旧json
         * @param objectToUpdate 新json
         * @param allATDOverrideEnvVars 含有atd的键值对
         * @param currentPath 当前的String串
         * @return jsonObject
         */
        private JSONObject loadAndOverrideFromEnvVars(JSONObject originalObject,
                                                      JSONObject objectToUpdate,
                                                      Map<String, Object> allATDOverrideEnvVars,
                                                      StringBuilder currentPath) {
            Set<String> keys = originalObject.keySet();
            keys.forEach(keyStr -> {
                Object keyvalue = originalObject.get(keyStr);
                if (keyvalue instanceof JSONObject) {
                    processJSONObject(objectToUpdate,
                            allATDOverrideEnvVars,
                            currentPath,
                            //当前的json的key值
                            keyStr,
                            //当前key的value
                            (JSONObject) keyvalue);
                } else if (keyvalue instanceof JSONArray) {
                    processJSONArray(objectToUpdate,
                            allATDOverrideEnvVars,
                            currentPath,
                            keyStr,
                            (JSONArray) keyvalue);
                } else {
                    processJSONObject(objectToUpdate,
                            currentPath,
                            keyStr,
                            keyvalue);
                }
            });
            return objectToUpdate;
        }
    

      

    processJSONArray/JsonObject 的截取代码如下:

    private void processJSONObject(JSONObject objectToUpdate,
                                        StringBuilder currentPath,
                                        String keyStr,
                                        Object keyvalue) {
            currentPath.append(keyStr);
            String getFromEnv = System.getenv(currentPath.toString());
            Object updatedValue = (null == getFromEnv) ? keyvalue : getFromEnv;
            objectToUpdate.put(keyStr, updatedValue);
            currentPath.delete(currentPath.lastIndexOf("_") + 1, currentPath.length());
        }
    
        private void processJSONArray(JSONObject objectToUpdate,
                                      Map<String, Object> allATDOverrideEnvVars,
                                      StringBuilder currentPath,
                                      String keyStr,
                                      JSONArray keyvalue) {
            JSONArray jsonArray = new JSONArray();
            objectToUpdate.put(keyStr, jsonArray);
            currentPath.append(keyStr).append("_");
            JSONArray arrayValues = keyvalue;
            for (int arrIndex = 0; arrIndex < arrayValues.length(); arrIndex++) {
                processJSONArrayItem(allATDOverrideEnvVars,
                        currentPath,
                        jsonArray,
                        arrayValues,
                        arrIndex);
            }
            currentPath.delete(currentPath.lastIndexOf(keyStr), currentPath.length());
        }
    
        private void processJSONArrayItem(Map<String, Object> allATDOverrideEnvVars,
                                          StringBuilder currentPath,
                                          JSONArray jsonArray,
                                          JSONArray arrayValues,
                                          int arrIndex) {
            JSONObject arrayItem = (JSONObject) arrayValues.get(arrIndex);
            JSONObject jsonObject = new JSONObject();
            jsonArray.put(jsonObject);
            currentPath.append(arrIndex).append("_");
            loadAndOverrideFromEnvVars((JSONObject) arrayItem,
                    jsonObject,
                    allATDOverrideEnvVars,
                    currentPath);
            currentPath.delete(currentPath.lastIndexOf(String.valueOf(arrIndex)), currentPath.length());
        }
    
        /**
         *
         * @param objectToUpdate 要修改的jsonObject
         * @param allATDOverrideEnvVars 含有atd的键值对
         * @param currentPath 当前的string串
         * @param keyStr key值
         * @param keyvalue value值
         */
        private void processJSONObject(JSONObject objectToUpdate,
                                       Map<String, Object> allATDOverrideEnvVars,
                                       StringBuilder currentPath,
                                       String keyStr,
                                       JSONObject keyvalue) {
            JSONObject jsonObject = new JSONObject();
            //增加一层json的嵌套,key保留,value为新增的空的value
            objectToUpdate.put(keyStr, jsonObject);
            //string 增加_
            currentPath.append(keyStr).append("_");
            loadAndOverrideFromEnvVars(keyvalue, jsonObject, allATDOverrideEnvVars, currentPath);
            currentPath.delete(currentPath.lastIndexOf(keyStr), currentPath.length());
        }
    

      

  • 相关阅读:
    [转载] kill命令
    [转载] Linux的Top命令解析
    json互相转换
    C# 13位时间戳(unix时间戳)
    第八篇:cx_Oracle出现的问题
    linux:eth网卡对应的物理网口判断
    ls显示前几行或后几行数据
    第七篇:suds.TypeNotFound: Type not found: '(string, http://schemas.xmlsoap.org/soap/encoding/, )'
    第六篇:python中numpy.zeros(np.zeros)的使用方法
    第五篇:selenium调用IE问题(Protected Mode settings are not the same for all zones)
  • 原文地址:https://www.cnblogs.com/zhizhiyin/p/11168757.html
Copyright © 2011-2022 走看看