이 예제에서는 IBM의 Trade 애플리케이션을 사용하는 주식 매입 트랜잭션의 레코딩을 사용합니다. 여기에 나타난 개념을 다른 애플리케이션의 테스트에서 사용할 수 있습니다.
테스트는 로그인 ID의 데이터 풀 대체를 사용하여 주식 매입 트랜잭션의 레코딩에서 시작됩니다.
페이지는 다음 그림에 표시된 것처럼 5회의 반복 루프에 랩핑됩니다.
테스트의 여러 페이지 중에는 세 가지 항목의 사용자 정의 코드("C"가 포함된 초록색 원으로 표시됨)가 있습니다. 다음 예제에서 사용자 정의 코드의 세 가지 항목을 탐색합니다.
사용자 정의 코드의 첫 번째 부분 InitializeBuyTest가 여기에 언급되어 있습니다.
package customcode;
import java.util.Random;
import com.ibm.rational.test.lt.kernel.IDataArea;
import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
import com.ibm.rational.test.lt.kernel.services.IVirtualUserInfo;
/**
* @author unknown
*/
public class InitializeBuyTest implements
com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {
/**
* Instances of this will be created using the no-arg constructor.
*/
public InitializeBuyTest() {
}
/**
* For description of ICustomCode2 and ITestExecutionServices interfaces,
* see the Javadoc information. */
public String exec(ITestExecutionServices tes, String[] args) {
// Get the test's data area and set a flag indicating that nothing
// has failed yet. This flag will be used later to break out
// of the schedule loop as soon as a failure is encountered.
IDataArea dataArea = tes.findDataArea(IDataArea.TEST);
dataArea.put("failedYet", "false");
// Get the virtual users's data area
IDataArea vda = tes.findDataArea(IDataArea.VIRTUALUSER);
// Randomly select a stock to purchase from the set of s:0 to s:499.
IVirtualUserInfo vuInfo = (IVirtualUserInfo) vda.get(IVirtualUserInfo.KEY);
Random rand = vuInfo.getRandom();
String stock = "s:" + Integer.toString(rand.nextInt(499));
// Persist the name of the stock in the virtual user's data area.
vda.put("myStock", stock);
return stock;
}
이 사용자 정의 코드는 메소드 exec()에 있습니다.
먼저 나중에 오류가 발견되었을 때 테스트 루프를 중지하는 데 사용될 플래그 값(이 경우에는 문자열)을 저장할 테스트의 데이터 영역을 확보합니다. 이런 방식으로 저장된 데이터를 테스트 전체에서 유지할수 있습니다.
그런 다음 랜덤으로 생성되는 주식 문자열이 작성됩니다. 값은 stock 변수로 저장되고 메소드의 리턴 값으로 다시 전달됩니다. 이 리턴 값은 다음 그림에 표시된 것처럼 나중에 요청에서 대체로 사용됩니다.
강조표시된 항목에서는 InitializeBuyTest 사용자 정의 코드 항목이 리턴한 값인 대체(s%3A716)를 사용합니다. 테스트의 방향을 결정하기 위해 사용자 정의 코드를 사용 중입니다.
InitializeBuyTest에 있는 코드의 그 다음 행에서는 가상 사용자 데이터 영역을 사용하여 나중에 참조할 수 있도록 주식의 이름을 저장합니다. 다시, 이런 방식으로 저장된 데이터를 테스트 전체에서 유지할수 있습니다.
사용자 정의 코드의 두 번째 부분을 CheckStock이라고 합니다. 그 내용은 다음과 같습니다(exec() 메소드만 나열함).
public String exec(ITestExecutionServices tes, String[] args) {
// Get the actual and requested stock purchased.
String actualStock = args[0].replaceAll("<B>", "");
actualStock = actualStock.substring(0, actualStock.indexOf("<"));
String requestedStock = args[1];
// Set the log level to ALL.
IDataArea dataArea = tes.findDataArea(IDataArea.TEST);
ITestInfo testInfo = (ITestInfo)dataArea.get(ITestInfo.KEY);
testInfo.setTestLogLevel(ITestLogManager.ALL);
// If the log level is set to ALL, report the actual and requested stock
// purchased.
ITestLogManager testLogManager = tes.getTestLogManager();
if (testLogManager.wouldReport(ITestLogManager.ALL)) {
testLogManager.reportMessage("Actual stock purchased: "
+ actualStock + ". Requested stock: " + requestedStock
+ ".");
}
// If the actual and requested stock don't match, submit a FAIL verdict.
if (testLogManager.wouldReport(ITestLogManager.ALL)) {
if (!actualStock.equalsIgnoreCase(requestedStock)) {
testLogManager.reportVerdict(
"Actual and requested purchase stock do not match.",
VerdictEvent.VERDICT_FAIL);
// Use the test's data area to record the fact that an error has
// occurred.
dataArea.put("failedYet", "true");
}
}
return null;
}
이 코드는 코드에 전달된 두 개의 인수를 추출하여 시작합니다. 다음 그림에 표시된 것처럼 원래 레코딩의 응답 파트가 강조표시되고 참조로 사용됩니다.
필요한 텍스트(이 경우 실제로 매입한 주식의 이름)를 획득하려면 문자열을 약간 조작해야 합니다. 그런 다음 다음 그림에 표시된 것처럼 새로 작성된 이 참조를 CheckStock에 인수로 전달합니다.
InitializeBuyTest의 리턴 값이 인수로도 전달됩니다.
CheckStock 사용자 정의 코드 항목에서는 이들 값을 사용하여 InitializeBuyTest에서 생성한 랜덤으로 선택된 주식이 테스트를 실행하는 동안 실제로 매입되었는지 확인합니다.
그런 다음 CheckStock에서 테스트 로그 레벨을 설정하고 실제 주식 매입과 요청된 주식 매입을 보고하며 이 둘이 일치하지 않을 경우 실패 판정을 내립니다. 또한 CheckStock이 테스트의 데이터 영역에 failedYet 태그와 연관된 true 값을 저장합니다.
사용자 정의 코드의 세 번째 부분(exec() 메소드만 있음)이 여기에 언급되어 있습니다.
public String exec(ITestExecutionServices tes, String[] args) {
// Get the test log manager.
ITestLogManager testLogManager = tes.getTestLogManager();
// Get the test's data area and get a flag indicating to
// see if anything has failed yet. If so, stop the loop.
IDataArea dataArea = tes.findDataArea(IDataArea.TEST);
String failedYet = (String) dataArea.get("failedYet");
// Break out of the loop if an error has been encountered.
if (failedYet.equalsIgnoreCase("true")) {
tes.getLoopControl().breakLoop();
if (testLogManager.wouldReport(ITestLogManager.ALL)) {
testLogManager.reportMessage("Loop stopped.");
}
}
return null;
}
이 코드에서는 테스트의 데이터 영역을 사용하여 failedYet 태그와 연관된 사용자 정의 값을 판별합니다. failedYet이 true인 경우 StopLoopCheck가 테스트 루프를 중단합니다.