import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import bsh.Interpreter;
import bsh.NameSpace;
import bsh.Primitive;
import bsh.UtilEvalError;
import bsh.Variable;
public class BeanShellTest {
@Test
public void testDynamicVar() throws Exception {
Interpreter inter = new Interpreter();
inter.setNameSpace(new NameSpaceWarp(inter.getNameSpace(),
"mynamespace"));
Object re = inter.eval("A999=A001+A002;");
System.out.println(re);
Assert.assertEquals(300, re);
}
@Test
public void testPrimitive() throws Exception {
Interpreter inter = new Interpreter();
inter.set("SZGC0000", new Integer(109));
inter.set("SZGA1000", new Integer(6));
inter.set("SZGG1000", new Integer(0));
inter.set("SZGK1000", new Integer(0));
inter.set("SZGC0300", new Integer(103));
Object vvv = inter
.eval("SZGC0000==SZGA1000+SZGG1000+SZGK1000+SZGC0300;");
System.out.println("1====" + vvv);
Assert.assertFalse((Boolean) vvv);
inter.set("SZGC0000", 109);
inter.set("SZGA1000", 6);
inter.set("SZGG1000", 0);
inter.set("SZGK1000", 0);
inter.set("SZGC0300", 103);
vvv = inter.eval("SZGC0000==SZGA1000+SZGG1000+SZGK1000+SZGC0300;");
System.out.println("2======" + vvv);
Assert.assertTrue((Boolean) vvv);
}
static class NameSpaceWarp extends NameSpace {
private static final long serialVersionUID = 1L;
Map<String, Object> kpis;
public NameSpaceWarp(NameSpace ns, String name) {
super(ns, name);
kpis = Collections.synchronizedMap(new HashMap<String, Object>());
kpis.put("A001", 100);
kpis.put("A002", 200);
kpis.put("A003", 300);
}
@Override
protected Variable getVariableImpl(String name, boolean recurse)
throws UtilEvalError {
Variable var = super.getVariableImpl(name, recurse);
if (var == null && kpis.containsKey(name)) {
Object o = kpis.remove(name);
if (o == null) {
o = Primitive.NULL;
}
setVariable(name, o, false);
return super.getVariableImpl(name, recurse);
}
return var;
}
}
}
beanShell中NameSpace使用
最新推荐文章于 2021-07-31 20:32:07 发布