⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stafsxe.java

📁 Software Testing Automation Framework (STAF)的开发代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package com.ibm.staf.service.sxe;/*****************************************************************************//* Software Testing Automation Framework (STAF)                              *//* (C) Copyright IBM Corp. 2002, 2004, 2005                                  *//*                                                                           *//* This software is licensed under the Common Public License (CPL) V1.0.     *//*****************************************************************************/import com.ibm.staf.*;import com.ibm.staf.service.*;import java.io.File;import java.io.FileReader;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Date;import java.util.Calendar;import java.util.StringTokenizer;import java.util.Map;/** * This class provides the full function of the STAFSXE * Service. */public class STAFSXE implements STAFServiceInterfaceLevel30{    private STAFHandle sHandle;    private String fServiceName;    private STAFCommandParser sxeParser;    private STAFCommandParser fListParser;    private String logName = "SXELOG";    private String fLocalMachineName = "";    private STAFMapClassDefinition fExecutionResultsMapClass;    private STAFMapClassDefinition fErrorResultsMapClass;    private STAFMapClassDefinition fLogStartStopMapClass;    private STAFMapClassDefinition fLogPassFailMapClass;    private STAFMapClassDefinition fLogCommandMapClass;    private STAFMapClassDefinition fSettingsMapClass;    /* static strings for parser */    private final static String EXECUTE = "EXECUTE";    private final static String VERSION = "VERSION";    private final static String HELP = "HELP";    private final static String FILE = "FILE";    private final static String LOOP = "LOOP";    private final static String FOREVER = "FOREVER";    private final static String MINRUNTIME = "MINRUNTIME";    private final static String LOGNAME = "LOGNAME";    /* other static values */    private final static String SXEVERSION = "3.0.2";    private final static int START = 0;    private final static int STOPPASS = 1;    private final static int STOPFAIL = 2;    private final static String LOGLEVELVAR = "{STAF/Service/SXE/LogLevel}";    private final static String ELAPSEDTARGETVAR =        "{STAF/Service/SXE/ElapsedTarget}";    private final static String ELAPSEDTOLVAR =        "{STAF/Service/SXE/ElapsedTolerance}";    private final static String LOGNONE = "NONE";    private final static String LOGFILEONLY = "FILE";    private final static String LOGCOMMAND = "COMMAND";    /* SXE STAF return codes */    public final static int EXECUTIONERROR = 4001;    public final static String EXECUTIONERRORInfo = "Execution Error";    public final static String EXECUTIONERRORDesc =        "An error occurred during execution";    public final static int ELAPSEDTIMEFAIL = 4004;    public final static String ELAPSEDTIMEFAILInfo =        "Elapsed Target Time Exceeded";    public final static String ELAPSEDTIMEFAILDesc =        "The testcase elapsed time exceeded the elapsed target " +        "time by more than the elapsed tolerance value.";    public final static int ELAPSEDTIMEERROR = 4005;    public final static String ELAPSEDTIMEERRORInfo = "Elapsed Target Error";    public final static String ELAPSEDTIMEERRORDesc =        "An error occurred resolving the elapsed target variables.";/** * STAFSXE constructor */public STAFSXE(){    super();}/** * Method required by implemented STAF interface. This is the * entry point for a STAF request. */public STAFResult acceptRequest(STAFServiceInterfaceLevel30.RequestInfo reqInfo){    String request = reqInfo.request.toUpperCase();    STAFResult result;    if (request.startsWith(EXECUTE))        result = handleExecute(reqInfo);    else if (request.startsWith("LIST"))        result = handleList(reqInfo);    else if (request.startsWith(VERSION))        result = handleVersion(reqInfo);    else if (request.startsWith(HELP))        result = handleHelp(reqInfo);    else        result = new STAFResult(STAFResult.InvalidRequestString);    return result;}/** * Checks if the elapsed time is within the specified parameters. * * Returns false if the elapsed time exceeded the target+tolerance, * or true otherwise. */private boolean checkElapsedTime(long startTime, long stopTime,                                 long elapsedTarget, int elapsedTolerance){    /* Calculate the maxTime in millis:       elapsedTarget + elapsedTarget * elapsedTolerance% */    long maxTime = (long) (elapsedTarget +        elapsedTarget*(((double) elapsedTolerance)/100));    if (stopTime - startTime > maxTime)        return false;    else        return true;}/** * Executes all commands in the specified file. One iteration * through this method executes the file once. * If a command returns a non-zero return code, execution * will stop at that point. * Returns the STAFResult from executing the file. This will * be the STAFResult from a failed command if one failed. */private STAFResult execute(STAFHandle fHandle, String filename,                           String logName, int loopNum){    long startTime;    long stopTime = 0;    /* Use 1 length arrays and a StringBuffer so that other called methods        may directly modify these primitives/objects */    long[] elapsedTarget = new long[1];    int[] elapsedTolerance = new int[1];    StringBuffer elapsedTargetSBuf = new StringBuffer();    STAFMarshallingContext mc = new STAFMarshallingContext();    /* open file */    BufferedReader in = null;    STAFResult result;    try    {   //big try block so we can close file in finally        try        {            in = new BufferedReader(new FileReader(new File(filename)));        }        catch(FileNotFoundException fnfe)        {            // Create a map containing the error results            Map errorMap = fErrorResultsMapClass.createInstance();            errorMap.put("loopNum", String.valueOf(loopNum));            errorMap.put("rc", String.valueOf(STAFResult.FileOpenError));            errorMap.put("result", fnfe.toString());            mc.setRootObject(errorMap);            mc.setMapClassDefinition(fErrorResultsMapClass);            return new STAFResult(EXECUTIONERROR, mc.marshall());        }        /* iterate through file and execute STAF commands */        int commandCount = 0;        int lineCount = 0;        /* Get startTime and log test start */        startTime = new Date().getTime(); //get StartTime        logFile(logName, START, fHandle, loopNum, startTime, stopTime,            elapsedTargetSBuf.toString(), elapsedTolerance[0]);        while(true)        {            String nextLine = null;            /* Read a line from file */            try            {                nextLine = in.readLine();            }            catch(IOException ioe)            {                stopTime = new Date().getTime(); //get stopTime, IOError                // Create a map containing the error results                Map errorMap = fErrorResultsMapClass.createInstance();                errorMap.put("loopNum", String.valueOf(loopNum));                errorMap.put("lineNum", String.valueOf(lineCount + 1));                errorMap.put("commandNum", String.valueOf(commandCount + 1));                errorMap.put("command", nextLine);                errorMap.put("rc", String.valueOf(STAFResult.FileReadError));                errorMap.put("result", ioe.toString());                mc.setRootObject(errorMap);                mc.setMapClassDefinition(fErrorResultsMapClass);                result = new STAFResult(EXECUTIONERROR, mc.marshall());                logFile(logName, STOPFAIL, fHandle, loopNum, startTime,                    stopTime, elapsedTargetSBuf.toString(),                    elapsedTolerance[0]);                break;            }            if (nextLine == null)            {   //at end of file, no failures                stopTime = new Date().getTime(); //get stopTime, success                boolean checkElapsed;                try                {                    checkElapsed = isCheckElapsed(fHandle, elapsedTargetSBuf,                        elapsedTarget, elapsedTolerance);                }                catch(Exception e)                {                    // Create a map containing the error results                    Map errorMap = fErrorResultsMapClass.createInstance();                    errorMap.put("loopNum", String.valueOf(loopNum));                    errorMap.put("rc", String.valueOf(ELAPSEDTIMEERROR));                    errorMap.put("result",                                 "Error in ElapsedTime/ElapsedTolerance " +                                 "variables.  Exception: " + e.toString());                    mc.setRootObject(errorMap);                    mc.setMapClassDefinition(fErrorResultsMapClass);                    result = new STAFResult(ELAPSEDTIMEERROR, mc.marshall());                    logCommand(logName, fHandle, STOPFAIL, null, result);                    logFile(logName, STOPFAIL, fHandle, loopNum, startTime,                        stopTime, elapsedTargetSBuf.toString(),                        elapsedTolerance[0]);                    break;                }                if (checkElapsed &&                    !checkElapsedTime(startTime, stopTime, elapsedTarget[0],                    elapsedTolerance[0]))                {                    //elapsed time check failed                    // Create a map containing the error results                    Map errorMap = fErrorResultsMapClass.createInstance();                    errorMap.put("loopNum", String.valueOf(loopNum));                    errorMap.put("rc", String.valueOf(ELAPSEDTIMEFAIL));                    errorMap.put("result", "Elapsed Time: " +                                 getElapsedTime(startTime, stopTime));                    mc.setRootObject(errorMap);                    mc.setMapClassDefinition(fErrorResultsMapClass);                    result = new STAFResult(ELAPSEDTIMEFAIL, mc.marshall());                    logFile(logName, STOPFAIL, fHandle, loopNum, startTime,                        stopTime, elapsedTargetSBuf.toString(),                        elapsedTolerance[0]);                }                else                {                    // Create a map containing the successful execution results                    Map resultMap = fExecutionResultsMapClass.createInstance();                    resultMap.put("loops", String.valueOf(loopNum));                    resultMap.put("commands", String.valueOf(commandCount));                    mc.setRootObject(resultMap);                    mc.setMapClassDefinition(fExecutionResultsMapClass);                    result = new STAFResult(STAFResult.Ok, mc.marshall());                    logFile(logName, STOPPASS, fHandle, loopNum, startTime,                        stopTime, elapsedTargetSBuf.toString(),                        elapsedTolerance[0]);                }                break;            }            /* If line is blank go to next line */            nextLine = nextLine.trim(); //trim whitespace            if (nextLine.equals(new String()))            {                lineCount++;                continue;            }            /* Goto next line if 1st non-whitespace character is # */            if (nextLine.startsWith("#"))            {                lineCount++;                continue;            }            logCommand(logName, fHandle, START, nextLine, null);            result = sendSTAFCommand(fHandle, nextLine);            if (result.rc != STAFResult.Ok)            {                stopTime = new Date().getTime(); //get stopTime, fail                                // Create a map containing the error results                Map errorMap = fErrorResultsMapClass.createInstance();                errorMap.put("loopNum", String.valueOf(loopNum));                errorMap.put("lineNum", String.valueOf(lineCount + 1));                errorMap.put("commandNum", String.valueOf(commandCount + 1));                errorMap.put("command", nextLine);                errorMap.put("rc", String.valueOf(result.rc));                errorMap.put("result", result.result);                mc.setRootObject(errorMap);                mc.setMapClassDefinition(fErrorResultsMapClass);                result = new STAFResult(EXECUTIONERROR, mc.marshall());                logCommand(logName, fHandle, STOPFAIL, nextLine, result);                logFile(logName, STOPFAIL, fHandle, loopNum, startTime,                    stopTime, elapsedTargetSBuf.toString(),                    elapsedTolerance[0]);                break;            }            logCommand(logName, fHandle, STOPPASS, nextLine, result);            commandCount++;            lineCount++;        }    }    finally    {   //make sure we always try to close file        try        {            in.close();        }        catch(Exception e)        {            // do nothing??        }    }    return result;}/** * Returns a String representation of the difference in * two times. The inputs are two times expressed in milliseconds * and stored in a long (see java.util.Date). * The returned format is: hh:mm:ss.mmm */private String getElapsedTime(long start, long stop){    long time = stop - start;    long hour = time / 3600000;    time = time%3600000;    long min = time / 60000;    time = time%60000;    long sec = time / 1000;    long milli = time%1000;    String milliString;    /* Make sure that millis are represented properly */    if(milli < 10)    {        milliString = "00"+String.valueOf(milli);    }    else if (milli < 100)    {        milliString = "0"+String.valueOf(milli);    }    else    {        milliString = String.valueOf(milli);    }    String formattedTime =        String.valueOf(hour)+":"+String.valueOf(min)+":"+        String.valueOf(sec)+"."+milliString;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -