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.ActionForward;
027: import org.apache.struts.action.ActionMapping;
028: import org.strecks.action.basic.impl.BasicActionForm;
029: import org.strecks.action.navigable.impl.NavigableFormSubmitBean;
030: import org.strecks.context.impl.TestContextImpl;
031: import org.strecks.controller.ActionCreator;
032: import org.strecks.controller.ActionCreatorImpl;
033: import org.strecks.form.controller.DelegatingForm;
034: import org.strecks.navigate.internal.BeanNavigationReader;
035: import org.strecks.view.ActionForwardViewAdapter;
036: import org.strecks.view.ViewAdapter;
037: import org.testng.annotations.BeforeMethod;
038: import org.testng.annotations.Test;
039:
040: /**
041: * @author Phil Zoio
042: */
043: public class TestNavigableSubmitController {
044:
045: private NavigableSubmitController action;
046:
047: @BeforeMethod
048: public void setUp() {
049: action = new NavigableSubmitController();
050: BeanNavigationReader navigationReader = new BeanNavigationReader();
051: navigationReader.readAnnotations(NavigableFormSubmitBean.class);
052: action.setNavigationHolder(navigationReader
053: .getNavigationHolder());
054: }
055:
056: @Test
057: public void testCreationAction() throws Exception {
058: ActionCreator actionCreator = new ActionCreatorImpl();
059: Action action = actionCreator
060: .createAction(NavigableFormSubmitBean.class);
061: NavigableSubmitController controller = (NavigableSubmitController) action;
062: assert controller.getBeanSource() != null;
063: assert controller.getNavigationHolder() != null;
064: }
065:
066: @Test
067: public void testValidBindingForm() {
068:
069: ActionForward actionForward = new ActionForward();
070:
071: NavigableFormSubmitBean actionBean = createStrictMock(NavigableFormSubmitBean.class);
072: ActionMapping mapping = createStrictMock(ActionMapping.class);
073: DelegatingForm form = createStrictMock(DelegatingForm.class);
074: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
075:
076: expect(request.getAttribute(Globals.CANCEL_KEY))
077: .andReturn(null);
078: actionBean.preBind();
079: form.bindInwards(actionBean);
080: actionBean.execute();
081: expect(actionBean.getSuccessResult()).andReturn("success");
082: expect(mapping.findForward("success")).andReturn(actionForward);
083:
084: replay(actionBean);
085: replay(mapping);
086: replay(form);
087: replay(request);
088:
089: ViewAdapter viewAdapter = action
090: .executeAction(actionBean, new TestContextImpl(request,
091: null, null, form, mapping));
092: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
093: assert (va.getActionForward() == actionForward);
094:
095: verify(actionBean);
096: verify(mapping);
097: verify(form);
098: verify(request);
099:
100: }
101:
102: @Test
103: public void testCancelValidBindingForm() {
104:
105: ActionForward actionForward = new ActionForward();
106:
107: NavigableFormSubmitBean actionBean = createStrictMock(NavigableFormSubmitBean.class);
108: ActionMapping mapping = createStrictMock(ActionMapping.class);
109: DelegatingForm form = createStrictMock(DelegatingForm.class);
110: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
111:
112: expect(request.getAttribute(Globals.CANCEL_KEY)).andReturn(
113: Boolean.TRUE);
114: actionBean.cancel();
115: expect(actionBean.getSuccessResult()).andReturn("cancel");
116: expect(mapping.findForward("cancel")).andReturn(actionForward);
117:
118: replay(actionBean);
119: replay(mapping);
120: replay(form);
121: replay(request);
122:
123: ViewAdapter viewAdapter = action
124: .executeAction(actionBean, new TestContextImpl(request,
125: null, null, form, mapping));
126: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
127: assert (va.getActionForward() == actionForward);
128:
129: verify(actionBean);
130: verify(mapping);
131: verify(form);
132: verify(request);
133:
134: }
135:
136: @Test
137: public void testRegularForm() {
138:
139: ActionForward actionForward = new ActionForward();
140:
141: NavigableFormSubmitBean actionBean = createStrictMock(NavigableFormSubmitBean.class);
142: ActionMapping mapping = createStrictMock(ActionMapping.class);
143: BasicActionForm form = createStrictMock(BasicActionForm.class);
144: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
145:
146: expect(request.getAttribute(Globals.CANCEL_KEY))
147: .andReturn(null);
148: actionBean.execute();
149: expect(actionBean.getSuccessResult()).andReturn("success");
150: expect(mapping.findForward("success")).andReturn(actionForward);
151:
152: replay(actionBean);
153: replay(mapping);
154: replay(form);
155: replay(request);
156:
157: ViewAdapter viewAdapter = action
158: .executeAction(actionBean, new TestContextImpl(request,
159: null, null, form, mapping));
160: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
161: assert (va.getActionForward() == actionForward);
162:
163: verify(actionBean);
164: verify(mapping);
165: verify(form);
166: verify(request);
167:
168: }
169:
170: @Test
171: public void testCancelRegularForm() {
172:
173: ActionForward actionForward = new ActionForward();
174:
175: NavigableFormSubmitBean actionBean = createStrictMock(NavigableFormSubmitBean.class);
176: ActionMapping mapping = createStrictMock(ActionMapping.class);
177: BasicActionForm form = createStrictMock(BasicActionForm.class);
178: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
179:
180: expect(request.getAttribute(Globals.CANCEL_KEY)).andReturn(
181: Boolean.TRUE);
182: actionBean.cancel();
183: expect(actionBean.getSuccessResult()).andReturn("cancel");
184: expect(mapping.findForward("cancel")).andReturn(actionForward);
185:
186: replay(actionBean);
187: replay(mapping);
188: replay(form);
189: replay(request);
190:
191: ViewAdapter viewAdapter = action
192: .executeAction(actionBean, new TestContextImpl(request,
193: null, null, form, mapping));
194: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
195: assert (va.getActionForward() == actionForward);
196:
197: verify(actionBean);
198: verify(mapping);
199: verify(form);
200: verify(request);
201:
202: }
203:
204: }
|