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