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 8 of 11  >>

Advertisement

The processLine Method

ScriptDrivenAccountSuite's execute method invokes processLine for each line it reads from the script file. The processLine method first removes any commments and trims any white space from either end of the line by passing the line to trimLine. If the trimmed line is empty, processLine simply returns. Otherwise, processLine attempts to interpret the trimmed line as test command.

processLine invokes getTokens, which breaks the line into its white space-separated tokens. getTokens returns a List of the tokens, which will contain Longs for long values specified in the command, Strings for commands and exception names appearing in the line.

Finally, processLine looks at the first token in the List returned by getTokens, which should be one of the four valid test commands: newAccount, getBalance, deposit, or withdraw. processLine passes control to one of four methods, each of which is responsible for processing one kind of command:

When formatting Strings to send to the Reporter, the methods of ScriptDrivenAccountSuite (including processLine) pass the raw String and current line number to addLineNumber. The addLineNumber method incorporates the current line number into the message sent to the Reporter. Similar to a compiler supplying both raw information and a line number when reporting compilation errors, the script file line number helps users of ScriptDrivenAccountSuite track down reported problems in the original script file.

Here's the processLine method:

    private void processLine(Reporter reporter, String line, int lineNum) {

        line = trimLine(line);

        // Ignore blank lines
        if (line.length() == 0) {
            return;
        }

        List tokensList = getTokens(line, lineNum);

        // First token must be a string with the value of "newAccount",
        // "getBalance", "deposit", or "withdraw".
        Object token = tokensList.get(0);
        if (token instanceof String) {

            String command = (String) token;

            if (command.equals("newAccount")) {
                processNewAccount(reporter, lineNum);
            }
            else if (command.equals("getBalance")) {
                processGetBalance(reporter, tokensList.subList(1,
                    tokensList.size()), lineNum);
            }
            else if (command.equals("deposit")) {
                processDeposit(reporter, tokensList.subList(1,
                    tokensList.size()), lineNum);
            }
            else if (command.equals("withdraw")) {
                processWithdraw(reporter,
                    tokensList.subList(1, tokensList.size()), lineNum);
            }
            else {

                // Runner will report this RuntimeException as with suiteAborted
                // method invocation on the Reporter
                String msg = addLineNumber("Initial token in line must be one of "
                    + "\"newAccount\", \"getBalance\", \"deposit\", or \"withdraw\". "
                    + " Problem token: " + token.toString(), lineNum);
                throw new RuntimeException(msg);
            }
        }
        else {

            // Runner will report this RuntimeException as with suiteAborted
            // method invocation on the Reporter
            String msg = addLineNumber("Initial token in line must be one of "
                + "\"newAccount\", \"getBalance\", \"deposit\", or \"withdraw\". "
                + " Problem token: " + token.toString(), lineNum);
            throw new RuntimeException(msg);
        }
    }

Here is the addLineNumber method:

    private String addLineNumber(String raw, int lineNum) {
        return "Script file line " + Integer.toString(lineNum)
            + ": " + raw;
    }

Here is the getTokens method:

    // Returns a List of Strings and/or Longs. For example, for the line:
    //
    // deposit 20 java.lang.ArithmeticException
    //
    // This method will return a List containing Strings and Longs with the values:
    //
    // "deposit"
    // 20L
    // "java.lang.ArithmethidException"
    //
    private List getTokens(String line, int lineNum) {

        List tokensList = new ArrayList();

        int pos = 0;

        // Eat any white space at the beginning
        while (pos < line.length()) {

            while (pos < line.length()
                && Character.isWhitespace(line.charAt(pos))) {

                ++pos;
            }

            // Recognize '-' as the start of a negative long value
            if (Character.isDigit(line.charAt(pos)) || line.charAt(pos) == '-') {

                int nextWhiteSpacePos = pos + 1;
                while (nextWhiteSpacePos < line.length()
                    && !Character.isWhitespace(line.charAt(nextWhiteSpacePos))) {

                    ++nextWhiteSpacePos;
                }

                String longString = line.substring(pos, nextWhiteSpacePos);

                try {
                    tokensList.add(new Long(longString));
                }
                catch (NumberFormatException e) {
                    // Runner will report this RuntimeException as
                    // with suiteAborted method invocation on the Reporter
                    throw new RuntimeException(
                        addLineNumber("Invalid long integer: "
                        + longString + ".", lineNum));
                }

                pos = nextWhiteSpacePos;
            }
            else {

                int nextWhiteSpacePos = pos + 1;
                while (nextWhiteSpacePos < line.length()
                    && !Character.isWhitespace(line.charAt(nextWhiteSpacePos))) {
                    ++nextWhiteSpacePos;
                }

                String wordString = line.substring(pos, nextWhiteSpacePos);

                tokensList.add(wordString);

                pos = nextWhiteSpacePos;
            }
        }

        return tokensList;
    }

<<  Page 8 of 11  >>


Sponsored Links



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