Configuring Datapools, Synchronization Points, and Shared Variables

prevnext

Shared Variable Configuration


A test script that uses a shared variable must include, somewhere in its body, a block of code such as:

public static class SharedVarConfig extends SharedVarInfo {
	 	 	 public SharedVarConfig() {
	 	 	 setSharedVarNames(java.lang.String[] sv);
        }
    }

The sv argument of setSharedVarNames() -- a method of SharedVarInfo -- is an array containing the names of one or more shared variables. Add to this array the name of each shared variable in the script that you specified with one of the shared variable methods.

The following is an example of a Java program that uses a shared variable named square_number. Relevant lines apear in bold.

/**
* SquareClientTM demonstrates an EJB client that can be executed
 * from Rational Suite TestStudio using TestManager.
 *
 */

// EJB itself
import com.rational.square.Square;
import com.rational.square.SquareHome;

// Misc
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.ListResourceBundle;
import java.rmi.RemoteException;

// JNDI-related
import javax.naming.Context;
import javax.naming.InitialContext;

// TestManager
import com.rational.test.tss.*;


// Java test scripts must extend the TestScript interface.

public class SquareClientTM_sharedvar
    extends com.rational.test.tss.TestScript {

    public void testMain(String[] args) {
        try {

            // Create EJB
            TSSMeasure.commandStart("home001", "getHome",
	 	 	 	 	 	 	 	 	 	 	 	 	 	 MST_XCLNTCONN);
            Square square = getHome().create();
            TSSMeasure.commandEnd((short)TSS_CMD_STAT_PASS);

            // Call Square method
            long answer = 0;
	 	 	 	 	 int retval;
	     	 	 	 	 TSSInteger shval = new TSSInteger(0);
            try {
                retval = TSSSync.sharedVarWait("square_number",
                                              1,
                                              1000000,
                                              0,
                                              30000,
                                              shval);
            } catch(TSSException e) {
                System.err.print(e);
                throw e;
            }
            System.out.println( "Getting square of " +
	 	 	 	 	 	 	 	 	 	 	 	 	 shval.getValue());
            TSSMeasure.think(2000);
	 	 	 	 	 TSSMeasure.think(2000);
            TSSMeasure.commandStart("square001", "getSquare",
	 	 	 	 	 	 	 	 	 	 	 	 	 	 MST_WAITRESP);
            answer = square.getSquare(shval.getValue());
            TSSMeasure.commandEnd((short)TSS_CMD_STAT_PASS);
            System.out.println(answer);

            // Destroy EJB
            square.remove();
        }
        catch (RemoteException e) {
            System.err.println( "remoteException" + e.getMessage() );
            e.printStackTrace();
        }
        catch (NullPointerException e) {
            if (getHome() == null)
                System.err.println( "noHome" + e.getMessage() );
            else
                e.printStackTrace();
        }
        catch (Exception e) {
            System.err.println( "generalException" + e.getMessage() );
            e.printStackTrace();
        }
    }


    // Constructor

    public SquareClientTM_sharedvar() {
        super();
    }


    // Helper method to get the EJB's home.

    private static SquareHome getHome() {


        // Specify the name of the server so we can find the Square EJB.

        String homeName = "com/rational/square/SquareHome";


        // Specify the name of the host machine with the name server.
        // This example is intended to run locally.  Also, specify
        // the class name of the JNDI initial naming factory.

        Properties env = new Properties();
        env.put(Context.PROVIDER_URL, "iiop:///");
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "com.ibm.ejb.cb.runtime.CBCtxFactory");


        try {
            // The following is the simplest way to get the
	 	 	 	 	 // InitialContext.

            InitialContext ctx = new InitialContext(env);
            java.lang.Object obj=ctx.lookup( homeName );

            if (obj == null) {
                System.out.println( "ctx.lookup returned null object" );
                return null;  // fail
            }

            return ((SquareHome)
	 	 	 	 	 	 	 	 javax.rmi.PortableRemoteObject.narrow(obj,
	 	 	 	 	 	 	 	 	 com.rational.square.SquareHome.class));

        } catch (javax.naming.NamingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static class SharedVarConfig extends SharedVarInfo {
        public SharedVarConfig() {
            String sv[] = {
                "square_number"};
            setSharedVarNames(sv);
        }
    }

    public static void main(String args[]) {
        SquareClientTM_datapool sctm = new SquareClientTM_datapool();
        sctm.testMain(args);
    }
}

prevnext


Rational Test Script Services for Java Rational Software Corporation
Copyright (c) 2003, Rational Software Corporation http://www.rational.com
support@rational.com
info@rational.com