Initialization Transaction

The first transaction executed in a test enables virtual machine initialization. The following function calls must be performed within this transaction:

JavaCreateJavaVM() initializes and starts the Java Virtual Machine.

JavaSetOption() starts the Java Virtual Machine. Several parameters are required, including home directory, Java version, and classpath. These may be configured in the Java section of the active profile or scripted by the JavaSetOption command.

JavaLoadObject is used to create a Java object. It is necessary to instantiate the class where the Java test code resides. For example, if the Java test code was implemented in a class called Test.java, the following method call would instantiate the class and store a handle on it in a global variable. The handle is used whenever a reference to the test class is required.

hTestObj := JavaLoadObject("Test");

If the Java test class calls SilkPerformer functions, then a reference to the SilkPerformer context object must be passed to the testclass. The SilkPerformer context object must be instantiated first, then the handle on it can be passed to the constructor of the test class by the JavaSetObject command.

hPerf := JavaLoadObject("silk/performer/SilkPerformer");
JavaSetObject(JAVA_STATIC_METHOD, hPerf);

The corresponding code in the test class must be a constructor that takes the SilkPerformer context object as a parameter. It makes sense to store the context object as a member variable so that it can be used by all method calls in the Java test class.

private SilkPerformer SilkPerformer = null;
public Test(SilkPerformer perf)
{
  SilkPerformer = perf;
}

To avoid memory leaks, references to Java objects in BDL should be freed by the JavaFreeObject command. The handle on the SilkPerformer context object is only used in the TInit transaction, so it can be freed at the end of the transaction. Note that only the reference to the object is freed, the Java object itself remains alive as long as there are Java references to it. The handle on the instantiated test object is needed throughout the test. It is freed in the TEnd transaction.

JavaFreeObject(hPerf);

Implementation of the initial transaction should resemble the following:

var
  hTestObj   : number;

dcltrans
  transaction TInit
  var
    hPerf : number;
  begin    
    JavaCreateJavaVM();

    hPerf := JavaLoadObject("silk/performer/SilkPerformer");
    JavaSetObject(JAVA_STATIC_METHOD, hPerf);
    hTestObj := JavaLoadObject("Test");
    JavaFreeObject(hPerf);
  end TInit;