Determine where a test is running

 

+

Search Tips   |   Advanced Search

 

The ComputerSpecific class determinesthe hostname on which the test is running, prints the hostname and IP address as a message in the test log, and returns different strings based on the hostname.

package test;

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

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * The ComputerSpecific class determined the hostname on which the test is 
 * running, prints the hostname and IP address as a message in the test log,
 * and returns different strings based on the hostname.
 */

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

    /**
     * Create using the no-arg constructor.
     */
    public ComputerSpecific() 
    {
    }

    public String exec(ITestExecutionServices tes, String[] args) 
    {
        String hostName = "Unknown";
        String hostAddress = "Unknown";
                
        try 
        {
            hostName = InetAddress.getLocalHost().getHostName();
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } 
        catch (UnknownHostException e) 
        {
            tes.getTestLogManager().reportMessage("Not able to obtain host information");
            return null;
        }

        tes.getTestLogManager().reportMessage("The hostname is " 
                                            + hostName 
                                            + "; IP address is " 
                                            + hostAddress);
        if (hostName.equals("host-1234"))
            return "Special";
        else
            return "Normal";
    }
}