FileDocCategorySizeDatePackage
TestCalculatorServlet.javaAPI DocExample3351Sat Mar 15 19:39:50 GMT 2003com.oreilly.javaxp.cactus.servlet

TestCalculatorServlet.java

package com.oreilly.javaxp.cactus.servlet;

import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;

import javax.servlet.ServletException;

/**
 * @author Brian M. Coyner
 * $version $Id: TestCalculatorServlet.java,v 1.3 2003/02/27 00:24:44 jepc Exp $
 */
public class TestCalculatorServlet extends ServletTestCase {
    public TestCalculatorServlet(String name) {
        super(name);
    }

    public void beginVerifyValidAdditionRequestParameters(WebRequest req) {
        req.addParameter(CalculatorServlet.OPERAND_ONE, "4");
        req.addParameter(CalculatorServlet.OPERAND_TWO, "60");
        req.addParameter(CalculatorServlet.OPERATION, "+");
    }

    public void testVerifyValidAdditionRequestParameters() throws Exception {
        CalculatorServlet servlet = new CalculatorServlet();
        servlet.verifyRequestParameters(this.request);
    }

    public void beginVerifyValidSubtractionRequestParameters(WebRequest req) {
        req.addParameter(CalculatorServlet.OPERAND_ONE, "45");
        req.addParameter(CalculatorServlet.OPERAND_TWO, "22");
        req.addParameter(CalculatorServlet.OPERATION, "-");
    }

    public void testVerifyValidSubtractionRequestParameters() throws Exception {
        CalculatorServlet servlet = new CalculatorServlet();
        servlet.verifyRequestParameters(this.request);
    }

    public void beginVerifyInvalidOperandOneParameter(WebRequest req) {
        req.addParameter(CalculatorServlet.OPERAND_ONE, "INVALID");
        req.addParameter(CalculatorServlet.OPERAND_TWO, "22");
        req.addParameter(CalculatorServlet.OPERATION, "+");
    }

    public void testVerifyInvalidOperandOneParameter() {
        CalculatorServlet servlet = new CalculatorServlet();
        try {
            servlet.verifyRequestParameters(this.request);
            fail("Calculator Exception should have been caught because " +
                 "operand one was invalid.");
        } catch (ServletException e) {
        }
    }

    public void beginVerifyInvalidOperandTwoParameter(WebRequest req) {
        req.addParameter(CalculatorServlet.OPERAND_ONE, "22");
        req.addParameter(CalculatorServlet.OPERAND_TWO, "INVALID");
        req.addParameter(CalculatorServlet.OPERATION, "+");
    }

    public void testVerifyInvalidOperandTwoParameter() {
        CalculatorServlet servlet = new CalculatorServlet();
        try {
            servlet.verifyRequestParameters(this.request);
            fail("Calculator Exception should have been caught because " +
                 "operand two was invalid.");
        } catch (ServletException e) {
        }
    }

    public void beginVerifyInvalidOperatorParameter(WebRequest req) {
        req.addParameter(CalculatorServlet.OPERAND_ONE, "13");
        req.addParameter(CalculatorServlet.OPERAND_TWO, "22");
        req.addParameter(CalculatorServlet.OPERATION, "%");
    }

    public void testVerifyInvalidOperatorParameter() {
        CalculatorServlet servlet = new CalculatorServlet();
        try {
            servlet.verifyRequestParameters(this.request);
            fail("Calculator Exception should have been caught because " +
                 "operator was invalid.");
        } catch (ServletException e) {
        }
    }
}