Store and retrieve a string

 

+

Search Tips   |   Advanced Search

 

The DataShare class enables a virtual user to store a string value in one test and then retrieve it in a different test. The "first" test would need to include this custom code and call it with a single argument - the string that is to be stored. The subsequent tests would include the same custom code but would call it with no arguments; it will return the stored string value. The class also prints messages in the test log detailing its operation.

package test;

import com.ibm.rational.test.lt.kernel.IDataArea;
import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;


public class DataShare implements
        com.ibm.rational.test.lt.kernel.custom.ICustomCode2 
{

    public DataShare() 
    {
    }

    public String exec(ITestExecutionServices tes, String[] args) 
    {
        IDataArea userDataArea = tes.findDataArea(IDataArea.VIRTUALUSER);
        final String KEY = "Key1";
        StringVal dataVal_1;

        if (args.length == 0) 
        {
            dataVal_1 = (StringVal)userDataArea.get(KEY);

            tes.getTestLogManager().reportMessage("Retrieved string " 
                                                 + dataVal_1.value 
                                                 + " using key \"Key1\"");
        } 
        else 
        {
            dataVal_1 = new StringVal();
            dataVal_1.value = args[0];
            userDataArea.put(KEY, dataVal_1);

            tes.getTestLogManager().reportMessage("Stored string " 
                                                 + args[0] 
                                                 + " using key \"Key1\"");
        }

        return dataVal_1.value;
    }
                
    private class StringVal 
    {
        public String value;
    }
}