Skip to content
DevTest Solutions - 10.1
Documentation powered by DocOps

Using BeanShell in DevTest

Last update April 22, 2015

BeanShell (http://www.beanshell.org/) is a free, open source, lightweight Java scripting language. BeanShell is a Java application that uses the Reflection API to execute Java statements and expressions dynamically. By using BeanShell, you avoid the need to compile class files.

BeanShell lets you type standard Java syntax (statements and expressions) on a command line and see the results immediately. A Swing GUI is also available. BeanShell can also be called from a Java class, which is how it is used in the product.

BeanShell is used in several places:

Using BeanShell Scripting Language

The major difference between BeanShell Java and compiled Java is in the type system. Java is strongly typed, but BeanShell can loosen the typing in its scripting environment. You can, however, impose strict typing in BeanShell if you want.

BeanShell relaxes typing in a natural way that lets you write BeanShell scripts that look like standard Java method code. However, you can also write scripts that look more like a traditional scripting language, such as Perl or JavaScript, while maintaining the Java syntax framework.

If a variable has been typed, then BeanShell honors and checks the type. If a variable is not typed, BeanShell only signals an error if you try to misuse the actual type of the variable.

The following Java fragments are all valid in BeanShell:

foo = "Foo";

four = (2+2) * 2 / 2.0;

print(foo + " = " + four);

.

.

hash = new Hashtable();

date = new Date();

hash.put("today", date);

.

.

BeanShell lets you declare and then use methods. Arguments and return types can also be loosely typed:

  • Typed
    int addTwoNumbers(int a, int b){
    return a + b;
    }
  • Loosely Typed
    add (a,b){
    return a + b;
    }

In the second example, the following works correctly:

sumI = add (5,7);

sumS = add("DevTest " , "Rocks");
sumM = add ("version ", 2);

BeanShell also provides a library of commands that facilitate its use.

A few examples of these commands are:

  • source(): Read a BeanShell (bsh) script.
  • run(): Run a bsh script.
  • exec(): Run a native application.
  • cd(), copy(), and so on: UNIX-like shell commands.
  • print(): Print argument as a string.
  • eval(): Evaluate the string argument as code.

For more information, see the BeanShell User Guide at http://www.beanshell.org/.

You can also get BeanShell, the source code, and the complete Javadoc at the same place.

Using BeanShell as Stand-alone

BeanShell is available as a stand-alone interpreter so you can try it outside of DevTest. You can download BeanShell from www.beanshell.org. This download is a single small JAR file named bsh-xx.jar (xx is the version number; currently 2.0). Add the JAR file to your classpath.

You can use BeanShell in the following configurations:

  • From a command line: java bsh.Interpreter [script name] [args]
  • From BeanShell GUI: java bsh.Console
  • From a Java class:

    Import bsh.Interpreter;

    .

    .

    Interpreter I = new Interpreter();

    i.set ("x",5);

    i.set("today", new Date());

    Date d = (Date)i.get("date");

    i.eval("myX = x * 10");

    System.out.println(i.get("myX"));

Using BeanShell in DevTest

The BeanShell interpreter is used in the Java Script Execution step and the Assert by Script Execution assertion. Both of these elements also expose DevTest Java objects and the current DevTest state (properties). This provides a powerful environment for adding custom functionality. The exposed Java objects can be used to both interrogate and to modify the current state of the test. For example, you can read, modify, and create DevTest properties in your scripts.

As a starting point, become familiar with the TestExec class in DevTest. Information about TestExec and many other classes can be found in Using the SDK.

DevTest also uses BeanShell inside property notation when an equal sign is present. For example:

{{= new Date()}} 

This property expression is interpreted using BeanShell.

Using Date Utilities

The com.itko.util.DateUtils class includes a number of date utility functions as static methods. These functions all return the formatted date as a string. You can use these functions in parameter expressions or the Java Script step.

com.itko.util.DateUtils.formatDate(Date date, String format)

com.itko.util.DateUtils.formatCurrentDate(String format)

com.itko.util.DateUtils.formatCurrentDate(int offsetInSec, String format)

com.itko.util.DateUtils.rfc3339(Date date)

com.itko.util.DateUtils.rfc3339()

com.itko.util.DateUtils.rfc3339(int offsetInSec)

com.itko.util.DateUtils.samlDate(Date date)

com.itko.util.DateUtils.samlDate()

com.itko.util.DateUtils.samlDate(int offsetInSec)

For example, if you have a web service call that takes a formatted date string and the server is 2 minutes slow, you can use:

=com.itko.util.DateUtils.formatCurrentDate(-120,"yyyy-MM-dd'T'HH:mm:ss.SSSZ")

This generates the string "2007-11-22T13:30:37.545-0500", the current time minus 120 seconds formatted according to these guidelines.

RFC 3339 is slightly different from the date that the default Java date formatter generates. If you need a strict RFC 3339 date, you can use the rcf3339 functions:

=com.itko.util.DateUtils.rfc3339()

This generates the string "2007-11-22T13:30:37.545-05:00".

SAML dates are formatted using the format "yyyy-MM-dd'T'HH:mm:ss'Z'". The samlDate functions are simply helpers so you do not need to remember that format string when using the formatDate APIs.

For more information, see:

http://download.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

http://tools.ietf.org/html/rfc3339#section-5.6

Was this helpful?

Please log in to post comments.