在做自动化的过程中,一定会遇到很多验证的点,但是有些验证功能是通用的,所以我封装了一个通用的验证类,来解决重复验证的问题,之前也写过一个,现在这个增加了一下数组的验证,还有一些隐藏bug的修复。话不多说,分享代码,供大家参考:
package com.fun.frame;import com.fun.base.bean.RequestInfo;import com.fun.base.interfaces.IBase;import com.fun.frame.httpclient.FanLibrary;import com.fun.utils.Regex;import net.sf.json.JSONException;import net.sf.json.JSONObject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.ArrayList;import java.util.Arrays;import java.util.List;/** * 通用验证方法封装 */public class Verify extends SourceCode { private static Logger logger = LoggerFactory.getLogger(Verify.class); /** * 断言的json对象 */ private JSONObject verifyJson; /** * 断言的code码 */ private int code; /** * 断言的json对象分行解析 */ private List<String> lines = new ArrayList<>(); public Verify(JSONObject jsonObject) { this.verifyJson = jsonObject; this.lines = parseJsonLines(jsonObject); } /** * 获取 code *<p>这里的requestinfo主要的目的是为了拦截一些不必要的checkcode验证的,主要有black_host名单提供,在使用时,注意requestinfo的非空校验</p> * * @return */ public int getCode() { return FanLibrary.getiBase().checkCode(verifyJson, null); } /** * 校验code码是否正确,==0 * * @return */ public boolean isRight() { return 0 == this.getCode(); } /** * 获取节点值 * * @param key 节点 * @return 返回节点值 */ public String getValue(String key) { int size = lines.size(); for (int i = 0; i < size; i++) { String line = lines.get(i); if (line.startsWith(key + ":")) return line.replaceFirst(key + ":", EMPTY); } return EMPTY; } /** * 校验是否包含文本 * * @param text 需要校验的文本 * @return 返回 Boolean 值 */ public boolean isContains(String... text) { boolean result = true; String content = verifyJson.toString(); int length = text.length; for (int i = 0; i < length; i++) { if (!result) break; result = content.contains(text[i]) & result; } return result; } /** * 校验节点值为数字 * * @param value 节点名 * @return 返回 Boolean 值 */ public boolean isNum(String... value) { boolean result = true; int length = value.length; for (int i = 0; i < length; i++) { String key = value[i] + ":"; if (!verifyJson.toString().contains(value[i]) || !result) return false; for (int k = 0; k < lines.size(); k++) { String line = lines.get(k); if (line.startsWith(key)) { String lineValue = line.replaceFirst(key, EMPTY); result = isNumber(lineValue) & result; } } } return result; } /** * 校验节点值不为空 * * @param keys 节点名 * @return 返回 Boolean 值,为空返回false,不为空返回true */ public boolean notNull(String... keys) { boolean result = true; for (int i = 0; i < keys.length; i++) { String key = keys[i] + ":"; if (!verifyJson.toString().contains(keys[i]) || !result) return false; for (int k = 0; k < lines.size(); k++) { String line = lines.get(k); if (line.startsWith(key)) { String lineValue = line.replaceFirst(key, EMPTY); result = lineValue != null & !lineValue.isEmpty() & result; } } } return result; } /** * 验证是否为列表,根据字段后面的符号是否是[ * * @param key 返回体的字段值 * @return */ public boolean isArray(String key) { String json = verifyJson.toString(); int index = json.indexOf(key); char a = json.charAt(index + key.length() + 2); return a == '['; } /** * 验证是否是json,根据后面跟的符号是否是{ * * @param key 返回体的字段值 * @return */ public boolean isJson(String key) { String json = verifyJson.toString(); int index = json.indexOf(key); char a = json.charAt(index + key.length() + 2); if (a == '{') return true; return false; } /** * 是否是Boolean值 * * @return */ public boolean isBoolean(String... value) { boolean result = true; int length = value.length; for (int i = 0; i < length; i++) { String key = value[i] + ":"; if (!verifyJson.toString().contains(value[i]) || !result) return false; for (int k = 0; k < lines.size(); k++) { String line = lines.get(k); if (line.startsWith(key)) { String lineValue = line.replaceFirst(key, EMPTY); result = Regex.isRegex(lineValue, "^(false)|(true)$") & result; } } } return result; } /** * 验证正则匹配结果 * * @param regex * @return */ public boolean isRegex(String regex) { String text = verifyJson.toString(); return Regex.isRegex(text, regex); } /** * 解析json信息 * * @param response json格式的响应实体 * @return json每个字段和值,key:value形式 */ public static List<String> parseJsonLines(JSONObject response) { String jsonStr = response.toString();// 先将json对象转化为string对象 jsonStr = jsonStr.replaceAll(",", LINE); jsonStr = jsonStr.replaceAll("\"", EMPTY); jsonStr = jsonStr.replaceAll("\\\\/", OR); jsonStr = jsonStr.replaceAll("\\{", LINE); jsonStr = jsonStr.replaceAll("\\[", LINE); jsonStr = jsonStr.replaceAll("}", LINE); jsonStr = jsonStr.replaceAll("]", LINE); package com.fun.frame;
import com.fun.base.bean.RequestInfo;
import com.fun.base.interfaces.IBase;
import com.fun.frame.httpclient.FanLibrary;
import com.fun.utils.Regex;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 通用验证方法封装
*/
public class Verify extends SourceCode {
private static Logger logger = LoggerFactory.getLogger(Verify.class);
/**
* 断言的json对象
*/
private JSONObject verifyJson;
/**
* 断言的code码
*/
private int code;
/**
* 断言的json对象分行解析
*/
private List<String> lines = new ArrayList<>();
public Verify(JSONObject jsonObject) {
this.verifyJson = jsonObject;
this.lines = parseJsonLines(jsonObject);
}
/**
* 获取 code
*<p>这里的requestinfo主要的目的是为了拦截一些不必要的checkcode验证的,主要有black_host名单提供,在使用时,注意requestinfo的非空校验</p>
*
* @return
*/
public int getCode() {
return FanLibrary.getiBase().checkCode(verifyJson, null);
}
/**
* 校验code码是否正确,==0
*
* @return
*/
public boolean isRight() {
return 0 == this.getCode();
}
/**
* 获取节点值
*
* @param key 节点
* @return 返回节点值
*/
public String getValue(String key) {
int size = lines.size();
for (int i = 0; i < size; i++) {
String line = lines.get(i);
if (line.startsWith(key + ":"))
return line.replaceFirst(key + ":", EMPTY);
}
return EMPTY;
}
/**
* 校验是否包含文本
*
* @param text 需要校验的文本
* @return 返回 Boolean 值
*/
public boolean isContains(String... text) {
boolean result = true;
String content = verifyJson.toString();
int length = text.length;
for (int i = 0; i < length; i++) {
if (!result) break;
result = content.contains(text[i]) & result;
}
return result;
}
/**
* 校验节点值为数字
*
* @param value 节点名
* @return 返回 Boolean 值
*/
public boolean isNum(String... value) {
boolean result = true;
int length = value.length;
for (int i = 0; i < length; i++) {
String key = value[i] + ":";
if (!verifyJson.toString().contains(value[i]) || !result)
return false;
for (int k = 0; k < lines.size(); k++) {
String line = lines.get(k);
if (line.startsWith(key)) {
String lineValue = line.replaceFirst(key, EMPTY);
result = isNumber(lineValue) & result;
}
}
}
return result;
}
/**
* 校验节点值不为空
*
* @param keys 节点名
* @return 返回 Boolean 值,为空返回false,不为空返回true
*/
public boolean notNull(String... keys) {
boolean result = true;
for (int i = 0; i < keys.length; i++) {
String key = keys[i] + ":";
if (!verifyJson.toString().contains(keys[i]) || !result)
return false;
for (int k = 0; k < lines.size(); k++) {
String line = lines.get(k);
if (line.startsWith(key)) {
String lineValue = line.replaceFirst(key, EMPTY);
result = lineValue != null & !lineValue.isEmpty() & result;
}
}
}
return result;
}
/**
* 验证是否为列表,根据字段后面的符号是否是[
*
* @param key 返回体的字段值
* @return
*/
public boolean isArray(String key) {
String json = verifyJson.toString();
int index = json.indexOf(key);
char a = json.charAt(index + key.length() + 2);
return a == '[';
}
/**
* 验证是否是json,根据后面跟的符号是否是{
*
* @param key 返回体的字段值
* @return
*/
public boolean isJson(String key) {
String json = verifyJson.toString();
int index = json.indexOf(key);
char a = json.charAt(index + key.length() + 2);
if (a == '{')
return true;
return false;
}
/**
* 是否是Boolean值
*
* @return
*/
public boolean isBoolean(String... value) {
boolean result = true;
int length = value.length;
for (int i = 0; i < length; i++) {
String key = value[i] + ":";
if (!verifyJson.toString().contains(value[i]) || !result)
return false;
for (int k = 0; k < lines.size(); k++) {
String line = lines.get(k);
if (line.startsWith(key)) {
String lineValue = line.replaceFirst(key, EMPTY);
result = Regex.isRegex(lineValue, "^(false)|(true)$") & result;
}
}
}
return result;
}
/**
* 验证正则匹配结果
*
* @param regex
* @return
*/
public boolean isRegex(String regex) {
String text = verifyJson.toString();
return Regex.isRegex(text, regex);
}
/**
* 解析json信息
*
* @param response json格式的响应实体
* @return json每个字段和值,key:value形式
*/
public static List<String> parseJsonLines(JSONObject response) {
String jsonStr = response.toString();// 先将json对象转化为string对象
jsonStr = jsonStr.replaceAll(",", LINE);
jsonStr = jsonStr.replaceAll("\"", EMPTY);
jsonStr = jsonStr.replaceAll("\\\\/", OR);
jsonStr = jsonStr.replaceAll("\\{", LINE);
jsonStr = jsonStr.replaceAll("\\[", LINE);
jsonStr = jsonStr.replaceAll("}", LINE);
jsonStr = jsonStr.replaceAll("]", LINE);
List<String> jsonLines = Arrays.asList(jsonStr.split(LINE));
return jsonLines;
}
}
List<String> jsonLines = Arrays.asList(jsonStr.split(LINE)); return jsonLines; }}复制代码
















