01: package com.mockrunner.example.struts;
02:
03: import javax.servlet.http.HttpServletRequest;
04: import javax.servlet.http.HttpServletResponse;
05:
06: import org.apache.struts.action.Action;
07: import org.apache.struts.action.ActionForm;
08: import org.apache.struts.action.ActionForward;
09: import org.apache.struts.action.ActionMapping;
10: import org.apache.struts.action.ActionMessage;
11: import org.apache.struts.action.ActionMessages;
12:
13: /**
14: * This action fulfils the order for some type of product.
15: * If there's not enough in the stock, an error is returned.
16: * This action will be tested with {@link OrderActionTest} in order
17: * to demonstrate the usage of {@link com.mockrunner.struts.ActionTestModule}.
18: */
19: public class OrderAction extends Action {
20: public ActionForward execute(ActionMapping mapping,
21: ActionForm form, HttpServletRequest request,
22: HttpServletResponse response) throws Exception {
23: OrderForm orderForm = (OrderForm) form;
24: String id = orderForm.getId();
25: int amount = orderForm.getAmount();
26: OrderManager orderManager = OrderManager.instance(request
27: .getSession().getServletContext());
28: if (orderManager.getStock(id) < amount) {
29: ActionMessages errors = new ActionMessages();
30: ActionMessage error = new ActionMessage(
31: "not.enough.in.stock", id);
32: errors.add(ActionMessages.GLOBAL_MESSAGE, error);
33: saveErrors(request, errors);
34: return mapping.findForward("failure");
35: }
36: orderManager.order(id, amount);
37: return mapping.findForward("success");
38: }
39: }
|