001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.action.navigable;
016:
017: import static org.easymock.EasyMock.expect;
018: import static org.easymock.classextension.EasyMock.createStrictMock;
019: import static org.easymock.classextension.EasyMock.replay;
020: import static org.easymock.classextension.EasyMock.verify;
021:
022: import javax.servlet.http.HttpServletRequest;
023:
024: import org.apache.struts.Globals;
025: import org.apache.struts.action.Action;
026: import org.apache.struts.action.ActionErrors;
027: import org.apache.struts.action.ActionForward;
028: import org.apache.struts.action.ActionMapping;
029: import org.apache.struts.action.ActionMessage;
030: import org.strecks.action.basic.impl.BasicActionForm;
031: import org.strecks.action.navigable.impl.NavigableFormRenderBean;
032: import org.strecks.context.impl.TestContextImpl;
033: import org.strecks.controller.ActionCreator;
034: import org.strecks.controller.ActionCreatorImpl;
035: import org.strecks.form.controller.DelegatingForm;
036: import org.strecks.navigate.internal.BeanNavigationReader;
037: import org.strecks.view.ActionForwardViewAdapter;
038: import org.strecks.view.ViewAdapter;
039: import org.testng.annotations.BeforeMethod;
040: import org.testng.annotations.Test;
041:
042: /**
043: * @author Phil Zoio
044: */
045: public class TestNavigableFormController {
046:
047: private NavigableFormController action;
048:
049: @BeforeMethod
050: public void setUp() {
051: action = new NavigableFormController();
052: BeanNavigationReader navigationReader = new BeanNavigationReader();
053: navigationReader.readAnnotations(NavigableFormRenderBean.class);
054: action.setNavigationHolder(navigationReader
055: .getNavigationHolder());
056: }
057:
058: @Test
059: public void testCreationAction() throws Exception {
060: ActionCreator actionCreator = new ActionCreatorImpl();
061: Action action = actionCreator
062: .createAction(NavigableFormRenderBean.class);
063: NavigableFormController controller = (NavigableFormController) action;
064: assert controller.getNavigationHolder() != null;
065: assert controller.getBeanSource() != null;
066: }
067:
068: @Test
069: public void testWithValidBindingFormInputError() {
070:
071: ActionForward actionForward = new ActionForward();
072:
073: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
074: NavigableFormRenderBean actionBean = createStrictMock(NavigableFormRenderBean.class);
075: ActionMapping mapping = createStrictMock(ActionMapping.class);
076: DelegatingForm form = createStrictMock(DelegatingForm.class);
077:
078: ActionErrors errors = new ActionErrors();
079: errors.add("prop", new ActionMessage("key"));
080: expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(
081: errors);
082: actionBean.setInputError(true);
083: actionBean.execute();
084: expect(form.getBindOutwards()).andReturn(false);
085: expect(actionBean.getSuccessResult()).andReturn("success");
086: expect(mapping.findForward("success")).andReturn(actionForward);
087:
088: replay(actionBean);
089: replay(mapping);
090: replay(form);
091: replay(request);
092:
093: ViewAdapter viewAdapter = action
094: .executeAction(actionBean, new TestContextImpl(request,
095: null, null, form, mapping));
096: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
097: assert (va.getActionForward() == actionForward);
098:
099: verify(actionBean);
100: verify(mapping);
101: verify(form);
102: verify(request);
103:
104: }
105:
106: @Test
107: public void testWithValidBindingForm() {
108:
109: ActionForward actionForward = new ActionForward();
110:
111: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
112: NavigableFormRenderBean actionBean = createStrictMock(NavigableFormRenderBean.class);
113: ActionMapping mapping = createStrictMock(ActionMapping.class);
114: DelegatingForm form = createStrictMock(DelegatingForm.class);
115:
116: expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(null);
117: actionBean.setInputError(false);
118: form.setBindOutwards(true);
119: actionBean.execute();
120: expect(form.getBindOutwards()).andReturn(true);
121: form.bindOutwards(actionBean);
122: expect(actionBean.getSuccessResult()).andReturn("success");
123: expect(mapping.findForward("success")).andReturn(actionForward);
124:
125: replay(actionBean);
126: replay(mapping);
127: replay(form);
128: replay(request);
129:
130: ViewAdapter viewAdapter = action
131: .executeAction(actionBean, new TestContextImpl(request,
132: null, null, form, mapping));
133: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
134: assert (va.getActionForward() == actionForward);
135:
136: verify(actionBean);
137: verify(mapping);
138: verify(form);
139: verify(request);
140:
141: }
142:
143: @Test
144: public void testWithRegularForm() {
145:
146: ActionForward actionForward = new ActionForward();
147:
148: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
149: NavigableFormRenderBean actionBean = createStrictMock(NavigableFormRenderBean.class);
150: ActionMapping mapping = createStrictMock(ActionMapping.class);
151: BasicActionForm form = createStrictMock(BasicActionForm.class);
152:
153: expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(null);
154: actionBean.setInputError(false);
155: actionBean.execute();
156: expect(actionBean.getSuccessResult()).andReturn("success");
157: expect(mapping.findForward("success")).andReturn(actionForward);
158:
159: replay(actionBean);
160: replay(mapping);
161: replay(form);
162: replay(request);
163:
164: ViewAdapter viewAdapter = action
165: .executeAction(actionBean, new TestContextImpl(request,
166: null, null, form, mapping));
167: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
168: assert (va.getActionForward() == actionForward);
169:
170: verify(actionBean);
171: verify(mapping);
172: verify(form);
173: verify(request);
174:
175: }
176:
177: }
|