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

TestShoppingCartServlet.java

package com.oreilly.javaxp.cactus.servlet;

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

/**
 * @author Brian M. Coyner
 * $version $Id: TestShoppingCartServlet.java,v 1.2 2003/02/27 00:24:44 jepc Exp $
 */
public class TestShoppingCartServlet extends ServletTestCase {

    private ShoppingCartServlet servlet;

    public TestShoppingCartServlet(String name) {
        super(name);
        this.servlet = new ShoppingCartServlet();
    }

    public void beginAddItemToCart(WebRequest webRequest) {
        webRequest.addParameter("operation",
                                ShoppingCartServlet.INSERT_ITEM);
        webRequest.addParameter("itemID", "12345");
    }

    public void testAddItemToCart() throws Exception {
        this.servlet.doGet(this.request, this.response);
        Object obj = this.session.getAttribute(ShoppingCartServlet.CART);
        assertNotNull("Shopping Cart should exist.", obj);
        assertTrue("Object should be a ShoppingCart",
                   obj instanceof ShoppingCart);
        ShoppingCart cart = (ShoppingCart) obj;
        Item item = cart.getItem("12345");
        assertNotNull("Item should exist.", item);
    }

    public void beginRemoveItemFromCart(WebRequest webRequest) {
        webRequest.addParameter("operation", ShoppingCartServlet.REMOVE_ITEM);
        webRequest.addParameter("itemID", "12345");
    }

    public void testRemoveItemFromCart() throws Exception {
        // we need to setup the session with data the servlet expects
        // to exist.
        ShoppingCart cart = new ShoppingCart();
        cart.addItem(new Item("12345", "Description"));
        this.session.setAttribute(ShoppingCartServlet.CART, cart);

        // now execute the doGet to remove the item from the cart.
        this.servlet.doGet(this.request, this.response);
        Object obj = this.session.getAttribute(ShoppingCartServlet.CART);
        assertNotNull("Shopping Cart should exist.", obj);
        assertTrue("Object should be a ShoppingCart",
                   obj instanceof ShoppingCart);
        cart = (ShoppingCart) obj;
        Item item = cart.getItem("12345");
        assertNull("Item should not exist.", item);
    }
}