使用Java正则实现Loadrunner的关联函数web_save_param
工作需要,用Java实现loadrunner的web_saveparam函数,如下:
package com.xuwangcheng.demo;
/**
* 关联参数
* @author xuwangcheng14@163.com
* @version 1.0.0
* @description
* @date 2020/7/5 21:16
*/
public class RelationParam {
/**
* 左边界
*/
private String lb;
/**
* 右边界
*/
private String rb;
/**
* 取值顺序
*/
private Integer order = 1;
/**
* 偏移量
*/
private Integer offset = 0;
/**
* 取值长度
*/
private Integer length;
public String getLb() {
return lb;
}
public void setLb(String lb) {
this.lb = lb;
}
public String getRb() {
return rb;
}
public void setRb(String rb) {
this.rb = rb;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
public Integer getOffset() {
return offset;
}
public void setOffset(Integer offset) {
this.offset = offset;
}
public Integer getLength() {
return length;
}
public void setLength(Integer length) {
this.length = length;
}
}
package com.xuwangcheng.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author xuwangcheng14@163.com
* @version 1.0.0
* @description
* @date 2020/7/5 21:18
*/
public class DemoTest {
public static void main(String[] args) {
String str = "{\"root\":{\"name\":\"xuwangcheng\",\"age\":30}}";
RelationParam param = new RelationParam();
param.setLb("\"age\":");
param.setRb("}}");
System.out.println(getValueByRelationKeyWord(param, str));
}
/**
* 关联取值
* @param param 参数
* @param string 取值字符串
* @return
*/
private static String getValueByRelationKeyWord(RelationParam param, String string) {
if (isEmpty(string) || param == null) {
return string;
}
String result = "";
if (isEmpty(param.getLb()) && isEmpty(param.getRb())) {
result = string;
} else {
// 根据左右边界取值
String regex = param.getLb() + "(.*?)" + param.getRb();
Pattern pattern = Pattern.compile(regex);
List<String> regStrs = new ArrayList<String>();
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
regStrs.add(matcher.group(1));
}
if (regStrs.size() < param.getOrder()) {
return null;
}
//根据order取值
result = regStrs.get(param.getOrder() - 1);
//根据offset取值
if (param.getOffset() != null && param.getOffset() < result.length()) {
result = result.substring(param.getOffset());
}
// 根据length取值
if (param.getLength() != null) {
result = result.substring(0, param.getLength());
}
}
return result;
}
private static boolean isEmpty (String s){
if (s == null || s.length() == 0) {
return true;
}
return false;
}
}
正文到此结束
- 本文标签: Loadrunner 性能测试 java
- 版权声明: 本站原创文章,于2020年07月08日由白卡pala发布,转载请注明出处