package org.strecks.action.basic;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createStrictMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.strecks.action.BasicFormAction;
import org.strecks.action.basic.impl.BasicActionForm;
import org.strecks.action.basic.impl.SimpleFormAction;
import org.strecks.context.impl.TestContextImpl;
import org.strecks.controller.ActionCreator;
import org.strecks.controller.ActionCreatorImpl;
import org.strecks.form.controller.DelegatingForm;
import org.strecks.view.ActionForwardViewAdapter;
import org.strecks.view.ViewAdapter;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestBasicFormController
{
@Test
public void testCreationAction() throws Exception
{
ActionCreator actionCreator = new ActionCreatorImpl();
Action action = actionCreator.createAction(SimpleFormAction.class);
BasicFormController controller = (BasicFormController) action;
assert controller.getBeanSource() != null;
}
@Test
public void testWithValidBindingForm()
{
BasicFormController action = new BasicFormController();
ActionForward actionForward = new ActionForward();
HttpServletRequest request = createStrictMock(HttpServletRequest.class);
BasicFormAction actionBean = createStrictMock(BasicFormAction.class);
ActionMapping mapping = createStrictMock(ActionMapping.class);
DelegatingForm form = createStrictMock(DelegatingForm.class);
expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(null);
actionBean.setInputError(false);
form.setBindOutwards(true);
expect(actionBean.execute()).andReturn("success");
expect(mapping.findForward("success")).andReturn(actionForward);
expect(form.getBindOutwards()).andReturn(true);
form.bindOutwards(actionBean);
replay(actionBean);
replay(mapping);
replay(form);
replay(request);
ViewAdapter viewAdapter = action.executeAction(actionBean, new TestContextImpl(request, null, null, form, mapping));
ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
assert (va.getActionForward() == actionForward);
verify(actionBean);
verify(mapping);
verify(form);
verify(request);
}
@Test
public void testWithValidBindingFormInputError()
{
BasicFormController action = new BasicFormController();
ActionForward actionForward = new ActionForward();
HttpServletRequest request = createStrictMock(HttpServletRequest.class);
BasicFormAction actionBean = createStrictMock(BasicFormAction.class);
ActionMapping mapping = createStrictMock(ActionMapping.class);
DelegatingForm form = createStrictMock(DelegatingForm.class);
ActionErrors errors = new ActionErrors();
errors.add("prop", new ActionMessage("key"));
expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(errors);
actionBean.setInputError(true);
expect(actionBean.execute()).andReturn("success");
expect(form.getBindOutwards()).andReturn(false);
expect(mapping.findForward("success")).andReturn(actionForward);
replay(actionBean);
replay(mapping);
replay(form);
replay(request);
ViewAdapter viewAdapter = action.executeAction(actionBean, new TestContextImpl(request, null, null, form, mapping));
ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
assert (va.getActionForward() == actionForward);
verify(actionBean);
verify(mapping);
verify(form);
verify(request);
}
@Test
public void testWithRegularForm()
{
BasicFormController action = new BasicFormController();
ActionForward actionForward = new ActionForward();
HttpServletRequest request = createStrictMock(HttpServletRequest.class);
BasicFormAction actionBean = createStrictMock(BasicFormAction.class);
ActionMapping mapping = createStrictMock(ActionMapping.class);
BasicActionForm form = createStrictMock(BasicActionForm.class);
expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(null);
actionBean.setInputError(false);
expect(actionBean.execute()).andReturn("success");
expect(mapping.findForward("success")).andReturn(actionForward);
replay(actionBean);
replay(mapping);
replay(form);
replay(request);
ViewAdapter viewAdapter = action.executeAction(actionBean, new TestContextImpl(request, null, null, form, mapping));
ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
assert (va.getActionForward() == actionForward);
verify(actionBean);
verify(mapping);
verify(form);
verify(request);
}
}