|
|
|
Artima SuiteRunner |
Why |
Getting Started |
Tutorial |
Get Help |
Discuss |
Print |
Email |
First Page |
Previous |
Next
|
Suite Execution by Overriding execute
|
Advertisement
|
processNewAccount Method
The processNewAccount method simply:
testStarting on the Reporter.
Account instances, and assigns its reference to the account instance variable.
Account constructor returns normally, invokes testSucceeded on the Reporter.
testFailed on the Reporter.
Here's the processNewAccount method:
// Process a "newAccount" command from the script
private void processNewAccount(Reporter reporter, int lineNum) {
Report report = new Report(this, "ScriptDrivenAccountSuite.processNewAccount",
addLineNumber("About to create a new Account object.", lineNum));
reporter.testStarting(report);
try {
account = new Account();
report = new Report(this, "ScriptDrivenAccountSuite.processNewAccount",
addLineNumber("Created a new Account object.", lineNum));
reporter.testSucceeded(report);
}
catch (Exception e) {
String msg = addLineNumber("Account constructor threw an exception.",
lineNum);
report = new Report(this, "ScriptDrivenAccountSuite.processNewAccount",
msg, e);
reporter.testFailed(report);
}
}
processGetBalance Method
The processGetBalance method:
long expected balance from the passed tokens List, and stores it
in the expectedBalance local variable.
testStarting on the Reporter.
getBalance on the current Account instance, and assigns the return
value to the balance local variable.
balance is equal to expectedBalance, invokes testSucceeded on the Reporter.
expectedBalance and expectedBalance values are unequal,
or getBalance method throws an exception), invokes testFailed on the Reporter.
Here is the processGetBalance method:
// The getBalance command has two tokens, "getBalance" and a long integer
// expected return value, as in:
//
// getBalance 20
//
private void processGetBalance(Reporter reporter, List tokens, int lineNum) {
if (tokens.size() != 1) {
// Runner will report this RuntimeException as with suiteAborted
// method invocation on the Reporter
String msg = addLineNumber("getBalance commands must have 1 "
+ "argument, a long expected balance.", lineNum);
throw new RuntimeException(msg);
}
long expectedBalance = -1;
// The getBalance command has two tokens, a long value and an error message
Object o = tokens.get(0);
if (o instanceof Long) {
expectedBalance = ((Long) o).longValue();
}
else {
// Runner will report this RuntimeException as with suiteAborted
// method invocation on the Reporter
String msg = addLineNumber("The first argument of a getBalance "
+ "command must be a Long expected balance.", lineNum);
throw new RuntimeException(msg);
}
String msg = addLineNumber("About to process a getBalance command. "
+ "Expecting getBalance to return " + Long.toString(expectedBalance)
+ ".", lineNum);
Report report = new Report(this, "ScriptDrivenAccountSuite.processGetBalance",
msg);
reporter.testStarting(report);
long balance = account.getBalance();
if (balance == expectedBalance) {
msg = addLineNumber("The getBalance method returned "
+ Long.toString(expectedBalance) + ", as expected.", lineNum);
report = new Report(this, "ScriptDrivenAccountSuite.processGetBalance",
msg);
reporter.testSucceeded(report);
}
else {
msg = addLineNumber("The getBalance method returned "
+ Long.toString(balance) + ", when "
+ Long.toString(expectedBalance) + " was expected.", lineNum);
report = new Report(this, "ScriptDrivenAccountSuite.processGetBalance",
msg);
reporter.testFailed(report);
}
}
processDeposit Method
The processDeposit method:
long amount to deposit from the passed tokens List, and stores it
in the amountToDeposit local variable.
List contains only one element, execute the simpler form of the deposit
command that doesn't expect an exception:
testStarting on the Reporter.
deposit on the Reporter, passing in the amountToDeposit.
deposit method returns normally, invokes testSucceeded on the Reporter.
deposit method throws an exception), invokes testFailed on the Reporter.
deposit command that expects a specified exception:
String expected exception fully qualified name from the passed tokens List, and stores it
in the expectedName local variable.
testStarting on the Reporter.
deposit on the Reporter, passing in the amountToDeposit.
deposit method throws an exception whose fully qualified name has exactly the same value
as expectedName, invokes testSucceeded on the Reporter.
deposit method returned normally or threw an exception other than the expected one),
invokes testFailed on the Reporter.
Here is the processDeposit method:
// The deposit command has two forms. The simpler form has two
// tokens, "deposit" and a long integer amount to deposit, as in:
//
// deposit 20
//
// The two token form of the deposit command causes this ScriptDrivenAccountSuite
// to deposit the long integer to the current Account object.
//
// The more complex form of the deposit command has three tokens, "deposit",
// a long integer amount to deposit, and the fully qualified name of an expected
// exception. For example:
//
// deposit -1 java.lang.IllegalArgumentException
//
private void processDeposit(Reporter reporter, List tokens, int lineNum) {
if (tokens.size() != 1 && tokens.size() != 2) {
// Runner will report this RuntimeException as with suiteAborted
// method invocation on the Reporter
String msg = addLineNumber("A deposit command "
+ "must have either 1 or 2 arguments.", lineNum);
throw new RuntimeException(msg);
}
// Both forms require a long amount to deposit as the first token
long amountToDeposit = -1;
Object o = tokens.get(0);
if (o instanceof Long) {
amountToDeposit = ((Long) o).longValue();
}
else {
// Runner will report this RuntimeException as with suiteAborted
// method invocation on the Reporter
String msg = addLineNumber("First argument to a deposit command "
+ "must be a long amount to deposit.", lineNum);
throw new RuntimeException();
}
if (tokens.size() == 1) {
String msg = addLineNumber("About to Deposit "
+ Long.toString(amountToDeposit) + ".", lineNum);
Report report = new Report(this, "ScriptDrivenAccountSuite.processDeposit",
msg);
reporter.testStarting(report);
try {
account.deposit(amountToDeposit);
msg = addLineNumber("Deposited " + Long.toString(amountToDeposit)
+ ".", lineNum);
report = new Report(this, "ScriptDrivenAccountSuite.processDeposit",
msg);
reporter.testSucceeded(report);
}
catch (Exception e) {
msg = addLineNumber("The deposit method threw an exception "
+ "when attempting to deposit " + Long.toString(amountToDeposit)
+ ".", lineNum);
report = new Report(this, "ScriptDrivenAccountSuite.processDeposit",
msg, e);
reporter.testFailed(report);
}
}
else {
// Parse the fully qualified exception name
String expectedName = null;
o = tokens.get(1);
if (o instanceof String) {
expectedName = (String) o;
}
else {
// Runner will report this RuntimeException as with suiteAborted
// method invocation on the Reporter
String msg = addLineNumber("Second argument to a deposit command "
+ "must be a String fully qualified exception name.", lineNum);
throw new RuntimeException(msg);
}
String msg = addLineNumber("About to Deposit "
+ Long.toString(amountToDeposit) + ".", lineNum);
Report report = new Report(this, "ScriptDrivenAccountSuite.processDeposit",
msg);
reporter.testStarting(report);
try {
account.deposit(amountToDeposit);
msg = addLineNumber("The deposit method returned normally, when "
+ expectedName + " was expected.", lineNum);
report = new Report(this, "ScriptDrivenAccountSuite.processDeposit",
msg);
reporter.testFailed(report);
}
catch (Exception e) {
String thrownName = e.getClass().getName();
if (thrownName.equals(expectedName)) {
msg = addLineNumber("The deposit method threw "
+ expectedName + ", as expected.", lineNum);
report = new Report(this, "ScriptDrivenAccountSuite.processDeposit",
msg);
reporter.testSucceeded(report);
}
else {
msg = addLineNumber("The deposit method threw "
+ thrownName + ", when "
+ expectedName + " was expected.", lineNum);
report = new Report(this, "ScriptDrivenAccountSuite.processDeposit",
msg);
reporter.testFailed(report);
}
}
}
}
processWithdraw Method
The processWithdraw method:
long amount to withdraw from the passed tokens List, and stores it
in the amountToWithdraw local variable.
List contains only one element, execute the simpler form of the withdraw
command that doesn't expect an exception:
testStarting on the Reporter.
withdraw on the Reporter, passing in the amountToWithdraw.
withdraw method returns a value equal to the amountToWithdraw,
invokes testSucceeded on the Reporter.
withdraw method returns a value different from the amountToWithdraw
or throws an exception), invokes testFailed on the Reporter.
withdraw command that expects a specified exception:
String expected exception fully qualified name from the passed tokens List, and stores it
in the expectedName local variable.
testStarting on the Reporter.
withdraw on the Reporter, passing in the amountToWithdraw.
withdraw method throws an exception whose fully qualified name has exactly the same value
as expectedName, invokes testSucceeded on the Reporter.
withdraw method returned normally or threw an exception other than the expected one),
invokes testFailed on the Reporter.
Here is the processWithdraw method:
// The withdraw command has two forms. The simpler form has two
// tokens, "withdraw" and a long integer amount to withdraw, as in:
//
// withdraw 20
//
// The two token form of the withdraw command causes this ScriptDrivenAccountSuite
// to withdraw the long integer from the current Account object.
//
// The more complex form of the withdraw command has three tokens, "withdraw",
// a long integer amount to withdraw, and the fully qualified name of an expected
// exception. For example:
//
// withdraw 10 com.artima.examples.account.ex6.InsufficientFundsException
//
private void processWithdraw(Reporter reporter, List tokens, int lineNum) {
if (tokens.size() != 1 && tokens.size() != 2) {
// Runner will report this RuntimeException as with suiteAborted
// method invocation on the Reporter
String msg = addLineNumber("A withdraw command "
+ "must have either 1 or 2 arguments.", lineNum);
throw new RuntimeException(msg);
}
// Both forms require a long amount to withdraw as the first token
long amountToWithdraw = -1;
Object o = tokens.get(0);
if (o instanceof Long) {
amountToWithdraw = ((Long) o).longValue();
}
else {
// Runner will report this RuntimeException as with suiteAborted
// method invocation on the Reporter
String msg = addLineNumber("First argument to a withdraw command "
+ "must be a long amount to withdraw.", lineNum);
throw new RuntimeException();
}
if (tokens.size() == 1) {
String msg = addLineNumber("About to withdraw "
+ Long.toString(amountToWithdraw) + ".", lineNum);
Report report = new Report(this, "ScriptDrivenAccountSuite.processWithdraw",
msg);
reporter.testStarting(report);
try {
long withdrawn = account.withdraw(amountToWithdraw);
if (withdrawn == amountToWithdraw) {
msg = addLineNumber("Withdrew " + Long.toString(amountToWithdraw)
+ ".", lineNum);
report = new Report(this,
"ScriptDrivenAccountSuite.processWithdraw", msg);
reporter.testSucceeded(report);
}
else {
msg = addLineNumber("The withdraw method returned "
+ Long.toString(withdrawn) + ", when "
+ Long.toString(amountToWithdraw)
+ " was expected.", lineNum);
report = new Report(this,
"ScriptDrivenAccountSuite.processWithdraw", msg);
reporter.testFailed(report);
}
}
catch (Exception e) {
msg = addLineNumber("The withdraw method threw an exception "
+ "when attempting to withdraw " + Long.toString(amountToWithdraw)
+ ".", lineNum);
report = new Report(this, "ScriptDrivenAccountSuite.processWithdraw",
msg, e);
reporter.testFailed(report);
}
}
else {
// Parse the fully qualified exception name
String expectedName = null;
o = tokens.get(1);
if (o instanceof String) {
expectedName = (String) o;
}
else {
// Runner will report this RuntimeException as with suiteAborted
// method invocation on the Reporter
String msg = addLineNumber("Second argument to a withdraw command "
+ "must be a fully qualified exception name.", lineNum);
throw new RuntimeException(msg);
}
try {
String msg = addLineNumber("About to withdraw "
+ Long.toString(amountToWithdraw) + ".", lineNum);
Report report = new Report(this,
"ScriptDrivenAccountSuite.processWithdraw", msg);
reporter.testStarting(report);
account.withdraw(amountToWithdraw);
msg = addLineNumber("The withdraw method returned normally, when "
+ expectedName + " was expected.", lineNum);
report = new Report(this,
"ScriptDrivenAccountSuite.processWithdraw", msg);
reporter.testFailed(report);
}
catch (Exception e) {
String thrownName = e.getClass().getName();
if (thrownName.equals(expectedName)) {
String msg = addLineNumber("The withdraw method threw "
+ expectedName + ", as expected.", lineNum);
Report report = new Report(this,
"ScriptDrivenAccountSuite.processWithdraw", msg);
reporter.testSucceeded(report);
}
else {
String msg = addLineNumber("The withdraw method threw "
+ thrownName + ", when "
+ expectedName + " was expected.", lineNum);
Report report = new Report(this,
"ScriptDrivenAccountSuite.processWithdraw", msg);
reporter.testFailed(report);
}
}
}
}
|
Sponsored Links
|