The Artima Developer Community
Sponsored Link

Drive Your Unit Tests with Custom Scripts
How to Customize Suite Execution by Overriding execute
by Bill Venners
March 10, 2003

<<  Page 9 of 11  >>

Advertisement

The processNewAccount Method

The processNewAccount method simply:

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);
        }
    }

The processGetBalance Method

The processGetBalance method:

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);
        }
    }

The processDeposit Method

The processDeposit method:

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);
                }
            }
        }
    }

The processWithdraw Method

The processWithdraw method:

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);
                }
            }
        }
    }

<<  Page 9 of 11  >>


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us