BeanShell方法实现详解

1. 简述:
BeanShell是一种脚本语言,一种完全符合Java语法的java脚本语言,并且又拥有自己的一些语法和方法,BeanShell是一种松散类型的脚本语言(这点和JS类似)。实际工程中可以与用BeanShell脚本语言完成所需的简单或复杂对象的逻辑判断
下载地址:http://www.beanshell.org
2. 环境设置:
1)配置BeanShell
A. 把;bsh-xx.jar放到$JAVA_HOME/jre/lib/ext文件夹下
B.unix: export CLASSPATH=$CLASSPATH:bsh-xx.jar
C. Windows: set classpath %classpath%;bsh-xx.jar
本文中使用的是方法A,将bsh-2.0b4.jar放到C:\jdk1.5.0_15\jre\lib\ext目录下
2)检测配置

打开dos窗口,键入:java bsh.Console命令


出现BeanShell图片代表设置成功,BeanShell开始运行


3. 具体应用:

1)在Eclipse中建立工程TestBeanShell


2)添加所需Java类文件
建立Bsh1.java,此文件中记录用使用BeanShell编写的方法。
建立Person.java,此文件记录一个简单Java类对象。

建立ServiceContext.java,此文件记录一个复杂Java类对象。

3)BeanShell方法实现详解

//Person.java
package bhs.shell;
public class Person { 
	private String pname;
	public Person(){ 
		super();
	}
	public String getPname() { return pname; }
	public void setPname(String pname) { this.pname = pname; } 
}
//ServiceContext.java
package bhs.shell;
import java.util.HashMap; import java.util.Map;
public class ServiceContext {
	public static final String EVENT_ID = "EVENT_ID"; public static final String MESSAGE_ID = "MESSAGE_ID";
	private Map<String, Object> context = new HashMap<String, Object>();
	public void setAttribute(String key, Object value) { context.put(key, value); }
	public Object getAttribute(String key) { return context.get(key); }
	public void removeAttribute(String key) { context.remove(key); } }
//Bsh1.java
package bhs.shell;
import java.util.*;
import bsh.EvalError; import bsh.Interpreter; import bsh.UtilEvalError;
public class Bsh1 { 
	public static void main(String[] args) throws EvalError, UtilEvalError {
		Bsh1.testBshSimpleType();
		Bsh1.testBshComplexType();
		Bsh1.testBshComplexExpression(); 
	} 
	public static boolean testBshSimpleType() throws EvalError { 
		Interpreter i = new Interpreter(); // Construct an interpreter 
		Boolean flag = false; 
		i.set("input", 3); 
		i.set("b", 7); 
		// Eval a statement and get the result 
		flag = (Boolean) i.eval("b > input"); 
		System.out.print("Simple Type result: "+flag+"\n"); return flag; 
	} 
	public static boolean testBshComplexType()throws EvalError, UtilEvalError { 
		ServiceContext context = new ServiceContext(); 
		List<Person> list= new ArrayList<Person>(); 
		Person p1=new Person(); 
		p1.setPname("eva"); 
		Person p2=new Person();
		p2.setPname("jason"); 
		list.add(p1); 
		list.add(p2); 
		context.setAttribute("input", list); 
		Boolean flag = false; 
		flag=(Boolean)validateCondition("((Person)context.getAttribute(\"input\").get(1)).getPname().equals(\"jason\")", context);
		System.out.print("Complex Type result: "+flag+"\n"); return flag; 
	} 
	public static boolean testBshComplexExpression()throws EvalError, UtilEvalError{ 
		ServiceContext context = new ServiceContext(); 
		context.setAttribute("a", 3); 
		context.setAttribute("b", 4); 
		context.setAttribute("c", 6); 
		context.setAttribute("d", null);
		Boolean flag = false; 
		flag=(Boolean) validateCondition("((int)context.getAttribute(\"a\")<(int)context.getAttribute(\"b\"))&&(context.getAttribute(\"d\")==null)",context); 
		System.out.print("Complex Expression result: "+flag+"\n"); 
		return flag; 
	}
	public static boolean validateCondition(String condition, ServiceContext context) throws EvalError, UtilEvalError{ 
		Interpreter i = new Interpreter();// construct an Interpreter 
		Boolean flag = false;// record the result expression estimated 
		i.getNameSpace().importPackage("bhs.shell");// import whole package or import special class with "importClass(Person.class.getName())" 
		i.set("context", context); 
		flag = (Boolean) i.eval(condition); 
		return flag; 
	} 
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在JMeter中使用Fastjson进行JSON处理可以通过导入com.alibaba.fastjson.*包来实现。在BeanShell中,你可以使用以下代码来导入Fastjson包: import com.alibaba.fastjson.*; 然后,你可以使用JSON.parseObject()方法将Response转换为JSON对象。例如: String response = prev.getResponseDataAsString(); JSONObject responseObj = JSON.parseObject(response); 如果你需要将列表中的元素转换为JSON字符串,你可以使用JSON.toJSONString()方法。例如: String getindex = vars.get("get_logisticsOrderNo_ALL"); List result = new ArrayList(); for (String get_Str : getindex.split(",")){ String toStrJson = JSON.toJSONString(get_Str); result.add(toStrJson); } vars.put("new_list", result.toString()); 同样地,你也可以使用JSON.toJSONString()方法将列表转换为JSON字符串。例如: String getindex = vars.get("get_logisticsOrderNo_matchNr"); List list = new ArrayList(); int toIntIndex = Integer.parseInt(getindex); for(int i = 0; i < toIntIndex; i++){ String valuess = vars.get("get_logisticsOrderNo_"+(i + 1)); list.add(valuess); } String jsonString = JSON.toJSONString(list); //vars.put("arr",list); 这样,你就可以在JMeter中使用Fastjson进行JSON处理了。 #### 引用[.reference_title] - *1* *3* [【Jmeter BeanShell】的使用场景详解](https://blog.csdn.net/weixin_67553250/article/details/129558555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [jmeter beanShell fastJson 解析](https://blog.csdn.net/sh309127/article/details/103455933)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值