001: package org.strecks.action.basic;
002:
003: import static org.easymock.EasyMock.expect;
004: import static org.easymock.classextension.EasyMock.createStrictMock;
005: import static org.easymock.classextension.EasyMock.replay;
006: import static org.easymock.classextension.EasyMock.verify;
007:
008: import javax.servlet.http.HttpServletRequest;
009:
010: import org.apache.struts.Globals;
011: import org.apache.struts.action.Action;
012: import org.apache.struts.action.ActionErrors;
013: import org.apache.struts.action.ActionForward;
014: import org.apache.struts.action.ActionMapping;
015: import org.apache.struts.action.ActionMessage;
016: import org.strecks.action.BasicFormAction;
017: import org.strecks.action.basic.impl.BasicActionForm;
018: import org.strecks.action.basic.impl.SimpleFormAction;
019: import org.strecks.context.impl.TestContextImpl;
020: import org.strecks.controller.ActionCreator;
021: import org.strecks.controller.ActionCreatorImpl;
022: import org.strecks.form.controller.DelegatingForm;
023: import org.strecks.view.ActionForwardViewAdapter;
024: import org.strecks.view.ViewAdapter;
025: import org.testng.annotations.Test;
026:
027: /**
028: * @author Phil Zoio
029: */
030: public class TestBasicFormController {
031:
032: @Test
033: public void testCreationAction() throws Exception {
034: ActionCreator actionCreator = new ActionCreatorImpl();
035: Action action = actionCreator
036: .createAction(SimpleFormAction.class);
037: BasicFormController controller = (BasicFormController) action;
038: assert controller.getBeanSource() != null;
039: }
040:
041: @Test
042: public void testWithValidBindingForm() {
043:
044: BasicFormController action = new BasicFormController();
045: ActionForward actionForward = new ActionForward();
046:
047: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
048: BasicFormAction actionBean = createStrictMock(BasicFormAction.class);
049: ActionMapping mapping = createStrictMock(ActionMapping.class);
050: DelegatingForm form = createStrictMock(DelegatingForm.class);
051:
052: expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(null);
053: actionBean.setInputError(false);
054: form.setBindOutwards(true);
055: expect(actionBean.execute()).andReturn("success");
056: expect(mapping.findForward("success")).andReturn(actionForward);
057: expect(form.getBindOutwards()).andReturn(true);
058: form.bindOutwards(actionBean);
059:
060: replay(actionBean);
061: replay(mapping);
062: replay(form);
063: replay(request);
064:
065: ViewAdapter viewAdapter = action
066: .executeAction(actionBean, new TestContextImpl(request,
067: null, null, form, mapping));
068: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
069: assert (va.getActionForward() == actionForward);
070:
071: verify(actionBean);
072: verify(mapping);
073: verify(form);
074: verify(request);
075:
076: }
077:
078: @Test
079: public void testWithValidBindingFormInputError() {
080:
081: BasicFormController action = new BasicFormController();
082: ActionForward actionForward = new ActionForward();
083:
084: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
085: BasicFormAction actionBean = createStrictMock(BasicFormAction.class);
086: ActionMapping mapping = createStrictMock(ActionMapping.class);
087: DelegatingForm form = createStrictMock(DelegatingForm.class);
088:
089: ActionErrors errors = new ActionErrors();
090: errors.add("prop", new ActionMessage("key"));
091: expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(
092: errors);
093: actionBean.setInputError(true);
094: expect(actionBean.execute()).andReturn("success");
095: expect(form.getBindOutwards()).andReturn(false);
096: expect(mapping.findForward("success")).andReturn(actionForward);
097:
098: replay(actionBean);
099: replay(mapping);
100: replay(form);
101: replay(request);
102:
103: ViewAdapter viewAdapter = action
104: .executeAction(actionBean, new TestContextImpl(request,
105: null, null, form, mapping));
106: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
107: assert (va.getActionForward() == actionForward);
108:
109: verify(actionBean);
110: verify(mapping);
111: verify(form);
112: verify(request);
113:
114: }
115:
116: @Test
117: public void testWithRegularForm() {
118:
119: BasicFormController action = new BasicFormController();
120: ActionForward actionForward = new ActionForward();
121:
122: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
123: BasicFormAction actionBean = createStrictMock(BasicFormAction.class);
124: ActionMapping mapping = createStrictMock(ActionMapping.class);
125: BasicActionForm form = createStrictMock(BasicActionForm.class);
126:
127: expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(null);
128: actionBean.setInputError(false);
129: expect(actionBean.execute()).andReturn("success");
130: expect(mapping.findForward("success")).andReturn(actionForward);
131:
132: replay(actionBean);
133: replay(mapping);
134: replay(form);
135: replay(request);
136:
137: ViewAdapter viewAdapter = action
138: .executeAction(actionBean, new TestContextImpl(request,
139: null, null, form, mapping));
140: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
141: assert (va.getActionForward() == actionForward);
142:
143: verify(actionBean);
144: verify(mapping);
145: verify(form);
146: verify(request);
147:
148: }
149:
150: }
|