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.controller;
016:
017: import static org.easymock.EasyMock.expect;
018: import static org.easymock.classextension.EasyMock.createMock;
019: import static org.easymock.classextension.EasyMock.replay;
020: import static org.easymock.classextension.EasyMock.verify;
021:
022: import java.io.IOException;
023:
024: import javax.servlet.ServletContext;
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.struts.action.Action;
030: import org.apache.struts.action.ActionForm;
031: import org.apache.struts.action.ActionMapping;
032: import org.apache.struts.action.ActionServlet;
033: import org.apache.struts.config.ModuleConfig;
034: import org.strecks.context.impl.TestContextFactoryImpl;
035: import org.strecks.context.impl.TestContextImpl;
036: import org.strecks.controller.impl.ReadOnlyControllerAction;
037: import org.strecks.controller.impl.SimpleBindableForm;
038: import org.strecks.controller.impl.TestAction;
039: import org.strecks.form.controller.DelegatingForm;
040: import org.strecks.form.controller.WrappingForm;
041: import org.strecks.form.handler.FormWrapper;
042: import org.strecks.form.handler.FormPopulateSource;
043: import org.strecks.form.handler.FormPopulateSourceImpl;
044: import org.strecks.form.handler.FormValidationHandler;
045: import org.strecks.form.handler.FormValidationHandlerImpl;
046: import org.strecks.form.handler.ValidateBindFormWrapper;
047: import org.strecks.form.impl.SimpleStrutsForm;
048: import org.strecks.preprocess.RequestPreprocessor;
049: import org.testng.annotations.Test;
050:
051: /**
052: * @author Phil Zoio
053: */
054: public class TestControllerRequestProcessor {
055:
056: // Note we can't test processValidate() and processActionForm() since they still use the superclass, for which the
057: // dependencies must be resolved
058:
059: @Test
060: public void testInit() throws ServletException {
061:
062: ModuleConfig config = createMock(ModuleConfig.class);
063: ServletContext context = createMock(ServletContext.class);
064: ActionServlet servlet = createMock(ActionServlet.class);
065:
066: expect(servlet.getServletContext()).andReturn(context);
067: expect(config.getPrefix()).andReturn("prefix");
068:
069: replay(config);
070: replay(context);
071: replay(servlet);
072:
073: ControllerRequestProcessor controllerRequestProcessor = new ControllerRequestProcessor();
074: controllerRequestProcessor.init(servlet, config);
075:
076: verify(config);
077: verify(context);
078: verify(servlet);
079:
080: ControllerProcessorDelegate delegate = controllerRequestProcessor
081: .getDelegate();
082: ActionCreator actionCreator = controllerRequestProcessor
083: .getActionCreator();
084:
085: assert null != delegate;
086: assert null != actionCreator;
087:
088: }
089:
090: @Test
091: public void testProcessCachedMessages() throws ServletException,
092: IOException {
093:
094: RequestPreprocessor handler = createMock(RequestPreprocessor.class);
095:
096: handler.preprocessRequest(null);
097:
098: replay(handler);
099:
100: ControllerRequestProcessor controllerRequestProcessor = new ControllerRequestProcessor();
101: controllerRequestProcessor.setRequestPreprocessor(handler);
102: controllerRequestProcessor.preProcessCachedMessages(null);
103:
104: verify(handler);
105:
106: }
107:
108: @Test
109: public void testBasicProcessActionCreate() throws ServletException,
110: IOException {
111:
112: ActionMapping mapping = createMock(ActionMapping.class);
113: expect(mapping.getType()).andReturn(TestAction.class.getName())
114: .times(2);
115:
116: replay(mapping);
117:
118: ControllerRequestProcessor controllerRequestProcessor = new ControllerRequestProcessor();
119: controllerRequestProcessor
120: .setActionCreator(new ActionCreatorImpl());
121: Action action1 = controllerRequestProcessor
122: .processActionCreate(null, null, mapping);
123: Action action2 = controllerRequestProcessor
124: .processActionCreate(null, null, mapping);
125:
126: assert action1 != null;
127: assert action1 instanceof ReadOnlyControllerAction;
128:
129: verify(mapping);
130:
131: assert action1 != null;
132: assert action1 == action2;
133: assert controllerRequestProcessor.hasAction(TestAction.class
134: .getName());
135:
136: }
137:
138: @Test
139: public void testProcessActionCreateInteractions() throws Exception {
140:
141: ActionMapping mapping = createMock(ActionMapping.class);
142: ActionCreatorImpl creator = createMock(ActionCreatorImpl.class);
143:
144: expect(mapping.getType()).andReturn(TestAction.class.getName());
145: expect(creator.createAction(TestAction.class)).andReturn(
146: new ReadOnlyControllerAction());
147: expect(mapping.getType()).andReturn(TestAction.class.getName());
148:
149: replay(mapping);
150: replay(creator);
151:
152: ControllerRequestProcessor controllerRequestProcessor = new ControllerRequestProcessor();
153: controllerRequestProcessor.setActionCreator(creator);
154:
155: Action action1 = controllerRequestProcessor
156: .processActionCreate(null, null, mapping);
157: Action action2 = controllerRequestProcessor
158: .processActionCreate(null, null, mapping);
159:
160: verify(mapping);
161: verify(creator);
162:
163: assert action1 != null;
164: assert controllerRequestProcessor.hasAction(TestAction.class
165: .getName());
166:
167: // check that same instance is returned
168: assert action1 == action2;
169:
170: }
171:
172: /**
173: * Tests standard action processing
174: */
175: @Test
176: public void testProcessRegularAction() throws Exception {
177:
178: Action action = createMock(Action.class);
179: ModuleConfig config = createMock(ModuleConfig.class);
180: ServletContext context = createMock(ServletContext.class);
181: ActionServlet servlet = createMock(ActionServlet.class);
182:
183: expect(servlet.getServletContext()).andReturn(context);
184: expect(config.getPrefix()).andReturn("");
185: expect(
186: action.execute(null, null, (HttpServletRequest) null,
187: (HttpServletResponse) null)).andReturn(null)
188: .times(1);
189:
190: replay(action);
191: replay(config);
192: replay(context);
193: replay(servlet);
194:
195: ControllerRequestProcessor controllerRequestProcessor = new ControllerRequestProcessor();
196: controllerRequestProcessor.init(servlet, config);
197: controllerRequestProcessor
198: .setActionContextFactory(new TestContextFactoryImpl());
199: controllerRequestProcessor.processActionPerform(null, null,
200: action, null, null);
201:
202: verify(action);
203: verify(config);
204: verify(context);
205: verify(servlet);
206:
207: }
208:
209: /**
210: * Tests controller action processing
211: */
212: @Test
213: public void testProcessControllerAction() throws Exception {
214:
215: ModuleConfig config = createMock(ModuleConfig.class);
216: ServletContext context = createMock(ServletContext.class);
217: ActionServlet servlet = createMock(ActionServlet.class);
218: ReadOnlyControllerAction action = createMock(ReadOnlyControllerAction.class);
219: ControllerProcessorDelegate delegate = createMock(ControllerProcessorDelegate.class);
220:
221: expect(servlet.getServletContext()).andReturn(context);
222: expect(config.getPrefix()).andReturn("prefix");
223: expect(
224: delegate.handleActionPerform(action,
225: new TestContextImpl())).andReturn(null)
226: .times(1);
227:
228: replay(action);
229: replay(config);
230: replay(context);
231: replay(servlet);
232:
233: ControllerRequestProcessor controllerRequestProcessor = new ControllerRequestProcessor();
234: controllerRequestProcessor.init(servlet, config);
235: controllerRequestProcessor.setDelegate(delegate);
236: controllerRequestProcessor
237: .setActionContextFactory(new TestContextFactoryImpl());
238: controllerRequestProcessor.processActionPerform(null, null,
239: action, null, null);
240:
241: verify(action);
242: verify(config);
243: verify(context);
244: verify(servlet);
245:
246: }
247:
248: /**
249: * Tests null action form
250: */
251: @Test
252: public void testPostProcessActionForm1() throws Exception {
253: FormWrapper delegate = createMock(FormWrapper.class);
254:
255: replay(delegate);
256:
257: ControllerRequestProcessor processor = new ControllerRequestProcessor();
258: processor.setFormHandler(delegate);
259:
260: processor.postProcessActionForm(null, null, null);
261:
262: verify(delegate);
263:
264: }
265:
266: /**
267: * Tests standard Struts action form
268: */
269: @Test
270: public void testPostProcessActionForm2() throws Exception {
271: FormWrapper delegate = createMock(FormWrapper.class);
272: ActionForm form = new ActionForm() {
273:
274: private static final long serialVersionUID = 1L;
275: };
276:
277: expect(delegate.wrapForm(form, null)).andReturn(form);
278: replay(delegate);
279:
280: ControllerRequestProcessor processor = new ControllerRequestProcessor();
281: processor.setFormHandler(delegate);
282:
283: processor.postProcessActionForm(null, null, form);
284:
285: verify(delegate);
286:
287: }
288:
289: /**
290: * Tests interaction of form
291: */
292: @Test
293: public void testValidBindingForm2() throws Exception {
294:
295: FormWrapper delegate = createMock(FormWrapper.class);
296: DelegatingForm form = createMock(DelegatingForm.class);
297:
298: expect(delegate.wrapForm(form, null)).andReturn(form);
299: delegate.handleBindingForm(form, null);
300: delegate.handleValidForm(form, null);
301:
302: replay(delegate);
303:
304: ControllerRequestProcessor processor = new ControllerRequestProcessor();
305: processor.setFormHandler(delegate);
306:
307: processor.postProcessActionForm(null, null, form);
308:
309: verify(delegate);
310:
311: }
312:
313: /**
314: * Tests Form which is both <code>Validatable</code> and <code>Bindable</code>
315: */
316: @Test
317: public void testValidBindingForm3() throws Exception {
318: HttpServletRequest request = createMock(HttpServletRequest.class);
319:
320: FormWrapper delegate = new ValidateBindFormWrapper();
321: SimpleBindableForm simpleBindableForm = new SimpleBindableForm();
322:
323: ControllerRequestProcessor processor = new ControllerRequestProcessor();
324: processor.setFormHandler(delegate);
325:
326: assert processor.postProcessActionForm(request, null,
327: simpleBindableForm) instanceof WrappingForm;
328:
329: }
330:
331: /**
332: * Tests Form which is both <code>Validatable</code> and <code>Bindable</code>
333: */
334: @Test
335: public void testValidBindingForm4() throws Exception {
336: HttpServletRequest request = createMock(HttpServletRequest.class);
337:
338: FormWrapper delegate = new ValidateBindFormWrapper();
339: ActionForm actionForm = new SimpleStrutsForm();
340:
341: ControllerRequestProcessor processor = new ControllerRequestProcessor();
342: processor.setFormHandler(delegate);
343:
344: assert (processor.postProcessActionForm(request, null,
345: actionForm) == actionForm);
346:
347: }
348:
349: /**
350: * Checks prepopulate for returning raw form
351: */
352: @Test
353: public void testPrePopulate2() throws Exception {
354:
355: FormPopulateSource delegate = new FormPopulateSourceImpl();
356: ActionForm actionForm = new SimpleStrutsForm();
357: DelegatingForm delegator = new DelegatingForm(actionForm);
358:
359: ControllerRequestProcessor processor = new ControllerRequestProcessor();
360: processor.setFormPopulationSource(delegate);
361:
362: assert actionForm == processor.prePopulate(delegator, null);
363: assert actionForm == processor.prePopulate(actionForm, null);
364:
365: }
366:
367: /**
368: * Checks preValidate for returning form passed in
369: */
370: @Test
371: public void testPreValidate() throws Exception {
372:
373: FormValidationHandler delegate = new FormValidationHandlerImpl();
374: ActionForm actionForm = new SimpleStrutsForm();
375: DelegatingForm delegator = new DelegatingForm(actionForm);
376:
377: ControllerRequestProcessor processor = new ControllerRequestProcessor();
378: processor.setFormValidationHandler(delegate);
379:
380: assert delegator == processor.preValidate(delegator, null);
381: assert actionForm == processor.preValidate(actionForm, null);
382:
383: }
384:
385: }
|