1、BeanShell上下文(Context/Namespace)
Reader in = new InputStreamReader( System.in );
PrintStream out = System.out;
PrintStream err = System.err;
boolean interactive = true;;
bsh.Interpreter i = new Interpreter( in, out, err, interactive );
Collection theObjectReadyForShellUser = new ArrayList();
theObjectReadyForShellUser.add("Str1");
i.set("myObject", theObjectReadyForShellUser);
i.run();
}
用戶的UI:
bsh % System.out.println( myObject.get(0) );
Str1
bsh %
Shell的上下文在測試中特別有用。想一下,如果將上面的“theObjectReadyForShellUser”換成一個預先為測試用戶生成的RMI本地接口存根,由測試用戶調用相應的存根方法。這可應用于動態測試,也可以應用于系統的遠程管理。
2、靜態Java代碼與動態Java代碼的組合使用
public static void main(String[] args) throws Throwable {Reader in = new InputStreamReader( System.in );
PrintStream out = System.out;
PrintStream err = System.err;
boolean interactive = true;;
bsh.Interpreter i = new Interpreter( in, out, err, interactive );
//show a dialog for user to input command.
String command = JOptionPane.showInputDialog( "Input Command(s)" );
i.eval( command );//Run the command
}
延伸閱讀
文章來源于領測軟件測試網 http://www.kjueaiud.com/