Index: ssts-web/src/test/java/test/forgon/disinfectsystem/selenium3/Utils.java =================================================================== diff -u -r36008 -r37348 --- ssts-web/src/test/java/test/forgon/disinfectsystem/selenium3/Utils.java (.../Utils.java) (revision 36008) +++ ssts-web/src/test/java/test/forgon/disinfectsystem/selenium3/Utils.java (.../Utils.java) (revision 37348) @@ -190,7 +190,9 @@ content = content.substring(startIndex, endIndex); } jsonObj = mapper.readValue(content,JSONObject.class); - jsonObj.put(key, value); + //写入配置项 + logger.debug("写入配置项:"+key+",成功"); + jsonObj.put(key, value); //格式化输出的JSON文件,即config.js Gson gson = new GsonBuilder().setPrettyPrinting().create(); @@ -212,7 +214,62 @@ } + /** + * 修改某个配置项 + * @param configFilePath + * @param key + * @param value + * @return + */ + public static void updateConfigJs(String key , Object value){ + JSONObject jsonObj = null; + String jsonObj1=null; + ObjectMapper mapper = new ObjectMapper().configure(Feature.ALLOW_COMMENTS, true); + mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); + mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true); + Scanner scan = null; + try { + String rootPath = Utils.settings.get("configJSPath").toString() + "/disinfectsystem/config/"; + String configFilePath = ConfigUtils.getProjectName() + "/config.js"; + String content = readFullFile(rootPath + configFilePath); + int startIndex = content.indexOf("{"); + //json变量定义的头(即左花括号之前的部分) + String jsonHeadContent = content.substring(0,startIndex); + int endIndex = (content.lastIndexOf("}") + 1); + if (startIndex != -1 && endIndex != -1 && startIndex < endIndex) { + content = content.substring(startIndex, endIndex); + } + jsonObj = mapper.readValue(content,JSONObject.class); + //如果存在该配置项,则修改 + if(jsonObj.has(key)){ + logger.debug("存在配置项:"+key+",该配置项已修改成功"); + jsonObj.replace(key, value); + + }else{ + logger.debug("不存在配置项:"+key); + } + + + //格式化输出的JSON文件,即config.js + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + jsonObj1=gson.toJson(jsonObj); + + String writeContent = jsonHeadContent; + writeContent += jsonObj1; + FileWriter writer = new FileWriter(new File(rootPath + configFilePath)); + writer.write(writeContent); + writer.flush(); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + }finally{ + if(scan != null){ + scan.close(); + } + } + } + /** * 删除某个配置项 * @param configFilePath