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.EasyMock.expectLastCall;
019: import static org.easymock.classextension.EasyMock.createStrictMock;
020: import static org.easymock.classextension.EasyMock.replay;
021: import static org.easymock.classextension.EasyMock.verify;
022: import static org.testng.Assert.*;
023:
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.List;
027:
028: import javax.servlet.http.HttpServletRequest;
029:
030: import org.apache.struts.action.ActionForward;
031: import org.strecks.constants.InfrastructureKeys;
032: import org.strecks.context.ActionContext;
033: import org.strecks.context.impl.TestContextImpl;
034: import org.strecks.controller.impl.TestAction;
035: import org.strecks.controller.impl.TypedAfterInterceptor;
036: import org.strecks.controller.impl.TypedBeforeInterceptor;
037: import org.strecks.exceptions.ApplicationRuntimeException;
038: import org.strecks.interceptor.AfterInterceptor;
039: import org.strecks.interceptor.BeforeInterceptor;
040: import org.strecks.source.ActionBeanSource;
041: import org.strecks.view.ActionForwardViewAdapter;
042: import org.strecks.view.RenderingViewAdapter;
043: import org.testng.annotations.Test;
044:
045: /**
046: * @author Phil Zoio
047: */
048: public class TestControllerProcessorDelegateImpl {
049:
050: @Test
051: public void testHandleActionPerform() throws Exception {
052:
053: ControllerAction action = createStrictMock(ControllerAction.class);
054: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
055: TestAction actionBean = createStrictMock(TestAction.class);
056: ActionBeanSource beanSource = createStrictMock(ActionBeanSource.class);
057:
058: ActionContext actionContext = new TestContextImpl(request);
059:
060: // check the expected order of method invocation
061: expect(action.getBeanSource()).andReturn(beanSource);
062: expect(beanSource.createBean(actionContext)).andReturn(
063: actionBean);
064: action.preExecute(actionBean, actionContext);
065: expect(action.getBeforeInterceptors()).andReturn(null);
066: expect(action.executeController(actionBean, actionContext))
067: .andReturn(null);
068: expect(action.getAfterInterceptors()).andReturn(null);
069: request
070: .setAttribute(InfrastructureKeys.ACTION_BEAN,
071: actionBean);
072: // request.setAttribute(InfrastructureKeys.ACTION_FORWARD, null);
073: action.postExecute(actionBean, actionContext, null);
074:
075: replay(action);
076: replay(request);
077: replay(actionBean);
078: replay(beanSource);
079:
080: ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
081: delegate.handleActionPerform(action, actionContext);
082:
083: verify(action);
084: verify(request);
085: verify(actionBean);
086: verify(beanSource);
087:
088: }
089:
090: @Test
091: public void testHandleViewRenderingAdapter() throws Exception {
092:
093: RenderingViewAdapter viewAdapter = createStrictMock(RenderingViewAdapter.class);
094: ControllerAction action = createStrictMock(ControllerAction.class);
095: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
096: TestAction actionBean = createStrictMock(TestAction.class);
097: ActionBeanSource beanSource = createStrictMock(ActionBeanSource.class);
098:
099: ActionContext actionContext = new TestContextImpl(request);
100:
101: // check the expected order of method invocation
102: expect(action.getBeanSource()).andReturn(beanSource);
103: expect(beanSource.createBean(actionContext)).andReturn(
104: actionBean);
105: action.preExecute(actionBean, actionContext);
106:
107: expect(action.getBeforeInterceptors()).andReturn(null);
108: expect(action.executeController(actionBean, actionContext))
109: .andReturn(viewAdapter);
110: expect(action.getAfterInterceptors()).andReturn(null);
111:
112: request
113: .setAttribute(InfrastructureKeys.ACTION_BEAN,
114: actionBean);
115: action.postExecute(actionBean, actionContext, null);
116: viewAdapter.render(actionContext);
117:
118: replay(action);
119: replay(request);
120: replay(actionBean);
121: replay(beanSource);
122:
123: ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
124: delegate.handleActionPerform(action, actionContext);
125:
126: verify(action);
127: verify(request);
128: verify(actionBean);
129: verify(beanSource);
130:
131: }
132:
133: @SuppressWarnings("unchecked")
134: @Test
135: public void testHandleWithInterceptors() throws Exception {
136:
137: ActionForward actionForward = new ActionForward();
138: BeforeInterceptor before1 = createStrictMock(BeforeInterceptor.class);
139: BeforeInterceptor before2 = createStrictMock(BeforeInterceptor.class);
140: Collection<BeforeInterceptor> befores = new ArrayList<BeforeInterceptor>();
141: befores.add(before1);
142: befores.add(before2);
143:
144: BeforeInterceptor actionBefore1 = createStrictMock(BeforeInterceptor.class);
145: BeforeInterceptor actionBefore2 = createStrictMock(BeforeInterceptor.class);
146: List<BeforeInterceptor> actionBefores = new ArrayList<BeforeInterceptor>();
147: actionBefores.add(actionBefore1);
148: actionBefores.add(actionBefore2);
149:
150: AfterInterceptor after1 = createStrictMock(AfterInterceptor.class);
151: AfterInterceptor after2 = createStrictMock(AfterInterceptor.class);
152: Collection<AfterInterceptor> afters = new ArrayList<AfterInterceptor>();
153: afters.add(after1);
154: afters.add(after2);
155:
156: AfterInterceptor actionAfter1 = createStrictMock(AfterInterceptor.class);
157: AfterInterceptor actionAfter2 = createStrictMock(AfterInterceptor.class);
158: List<AfterInterceptor> actionAfters = new ArrayList<AfterInterceptor>();
159: actionAfters.add(actionAfter1);
160: actionAfters.add(actionAfter2);
161:
162: ControllerAction action = createStrictMock(ControllerAction.class);
163: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
164: TestAction actionBean = createStrictMock(TestAction.class);
165: ActionBeanSource beanSource = createStrictMock(ActionBeanSource.class);
166:
167: ActionContext actionContext = new TestContextImpl(request);
168:
169: // check the expected order of method invocation
170: expect(action.getBeanSource()).andReturn(beanSource);
171: expect(beanSource.createBean(actionContext)).andReturn(
172: actionBean);
173: action.preExecute(actionBean, actionContext);
174:
175: before1.beforeExecute(actionBean, actionContext);
176: before2.beforeExecute(actionBean, actionContext);
177:
178: expect(action.getBeforeInterceptors()).andReturn(actionBefores);
179: actionBefore1.beforeExecute(actionBean, actionContext);
180: actionBefore2.beforeExecute(actionBean, actionContext);
181:
182: expect(action.executeController(actionBean, actionContext))
183: .andReturn(new ActionForwardViewAdapter(actionForward));
184:
185: expect(action.getAfterInterceptors()).andReturn(actionAfters);
186: actionAfter1.afterExecute(actionBean, actionContext, null);
187: actionAfter2.afterExecute(actionBean, actionContext, null);
188:
189: after1.afterExecute(actionBean, actionContext, null);
190: after2.afterExecute(actionBean, actionContext, null);
191: action.postExecute(actionBean, actionContext, null);
192:
193: request
194: .setAttribute(InfrastructureKeys.ACTION_BEAN,
195: actionBean);
196: request.setAttribute(InfrastructureKeys.ACTION_FORWARD,
197: actionForward);
198:
199: replay(action);
200: replay(request);
201: replay(actionBean);
202: replay(beanSource);
203: replay(before1);
204: replay(before2);
205: replay(actionBefore1);
206: replay(actionBefore2);
207: replay(after1);
208: replay(after2);
209:
210: ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
211: delegate.setBeforeInterceptors(befores);
212: delegate.setAfterInterceptors(afters);
213: delegate.handleActionPerform(action, actionContext);
214:
215: verify(action);
216: verify(request);
217: verify(actionBean);
218: verify(beanSource);
219: verify(before1);
220: verify(before2);
221: verify(actionBefore1);
222: verify(actionBefore2);
223: verify(after1);
224: verify(after2);
225:
226: }
227:
228: @SuppressWarnings("unchecked")
229: @Test
230: public void testThrowOnBefore() throws Exception {
231:
232: BeforeInterceptor before1 = createStrictMock(BeforeInterceptor.class);
233: BeforeInterceptor before2 = createStrictMock(BeforeInterceptor.class);
234: Collection<BeforeInterceptor> befores = new ArrayList<BeforeInterceptor>();
235: befores.add(before1);
236: befores.add(before2);
237:
238: AfterInterceptor after1 = createStrictMock(AfterInterceptor.class);
239: AfterInterceptor after2 = createStrictMock(AfterInterceptor.class);
240: Collection<AfterInterceptor> afters = new ArrayList<AfterInterceptor>();
241: afters.add(after1);
242: afters.add(after2);
243:
244: AfterInterceptor actionAfter1 = createStrictMock(AfterInterceptor.class);
245: AfterInterceptor actionAfter2 = createStrictMock(AfterInterceptor.class);
246: List<AfterInterceptor> actionAfters = new ArrayList<AfterInterceptor>();
247: actionAfters.add(actionAfter1);
248: actionAfters.add(actionAfter2);
249:
250: ControllerAction action = createStrictMock(ControllerAction.class);
251: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
252: TestAction actionBean = createStrictMock(TestAction.class);
253: ActionBeanSource beanSource = createStrictMock(ActionBeanSource.class);
254:
255: ActionContext actionContext = new TestContextImpl(request);
256:
257: // check the expected order of method invocation
258: expect(action.getBeanSource()).andReturn(beanSource);
259: expect(beanSource.createBean(actionContext)).andReturn(
260: actionBean);
261: action.preExecute(actionBean, actionContext);
262:
263: IllegalStateException exception = new IllegalStateException();
264:
265: // now expected mock calls
266: before1.beforeExecute(actionBean, actionContext);
267: expectLastCall().andThrow(exception);
268:
269: expect(action.getAfterInterceptors()).andReturn(actionAfters);
270: actionAfter1.afterExecute(actionBean, actionContext, exception);
271: actionAfter2.afterExecute(actionBean, actionContext, exception);
272:
273: after1.afterExecute(actionBean, actionContext, exception);
274: after2.afterExecute(actionBean, actionContext, exception);
275: action.postExecute(actionBean, actionContext, exception);
276:
277: replay(action);
278: replay(request);
279: replay(actionBean);
280: replay(beanSource);
281: replay(before1);
282: replay(before2);
283: replay(after1);
284: replay(after2);
285:
286: ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
287: delegate.setBeforeInterceptors(befores);
288: delegate.setAfterInterceptors(afters);
289: try {
290: delegate.handleActionPerform(action, actionContext);
291: } catch (IllegalStateException e) {
292: }
293:
294: verify(action);
295: verify(request);
296: verify(actionBean);
297: verify(beanSource);
298: verify(before1);
299: verify(before2);
300: verify(after1);
301: verify(after2);
302:
303: }
304:
305: @SuppressWarnings("unchecked")
306: @Test
307: public void testThrowOnAfter() throws Exception {
308:
309: AfterInterceptor after1 = createStrictMock(AfterInterceptor.class);
310: AfterInterceptor after2 = createStrictMock(AfterInterceptor.class);
311: Collection<AfterInterceptor> afters = new ArrayList<AfterInterceptor>();
312: afters.add(after1);
313: afters.add(after2);
314:
315: ControllerAction action = createStrictMock(ControllerAction.class);
316: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
317: TestAction actionBean = createStrictMock(TestAction.class);
318: ActionBeanSource beanSource = createStrictMock(ActionBeanSource.class);
319:
320: ActionContext actionContext = new TestContextImpl(request);
321:
322: // check the expected order of method invocation
323: expect(action.getBeanSource()).andReturn(beanSource);
324: expect(beanSource.createBean(actionContext)).andReturn(
325: actionBean);
326: action.preExecute(actionBean, actionContext);
327:
328: expect(action.getBeforeInterceptors()).andReturn(null);
329: expect(action.executeController(actionBean, actionContext))
330: .andReturn(null);
331: expect(action.getAfterInterceptors()).andReturn(null);
332:
333: IllegalStateException exception = new IllegalStateException();
334: after1.afterExecute(actionBean, actionContext, null);
335: expectLastCall().andThrow(exception);
336:
337: // check that we continue - i.e we don't fall over in a heap because the after interceptor
338: // failed
339: after2.afterExecute(actionBean, actionContext, null);
340: request
341: .setAttribute(InfrastructureKeys.ACTION_BEAN,
342: actionBean);
343: action.postExecute(actionBean, actionContext, null);
344:
345: replay(action);
346: replay(request);
347: replay(actionBean);
348: replay(beanSource);
349: replay(after1);
350: replay(after2);
351:
352: ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
353: delegate.setAfterInterceptors(afters);
354: delegate.handleActionPerform(action, actionContext);
355:
356: verify(action);
357: verify(request);
358: verify(actionBean);
359: verify(beanSource);
360: verify(after1);
361: verify(after2);
362:
363: }
364:
365: @SuppressWarnings("unchecked")
366: @Test
367: public void testThrowClassCastOnAfter() throws Exception {
368:
369: AfterInterceptor after1 = createStrictMock(AfterInterceptor.class);
370: AfterInterceptor after2 = createStrictMock(AfterInterceptor.class);
371: Collection<AfterInterceptor> afters = new ArrayList<AfterInterceptor>();
372: afters.add(after1);
373: afters.add(after2);
374:
375: ControllerAction action = createStrictMock(ControllerAction.class);
376: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
377: TestAction actionBean = createStrictMock(TestAction.class);
378: ActionBeanSource beanSource = createStrictMock(ActionBeanSource.class);
379:
380: ActionContext actionContext = new TestContextImpl(request);
381:
382: // check the expected order of method invocation
383: expect(action.getBeanSource()).andReturn(beanSource);
384: expect(beanSource.createBean(actionContext)).andReturn(
385: actionBean);
386: action.preExecute(actionBean, actionContext);
387:
388: expect(action.getBeforeInterceptors()).andReturn(null);
389: expect(action.executeController(actionBean, actionContext))
390: .andReturn(null);
391: expect(action.getAfterInterceptors()).andReturn(null);
392:
393: ClassCastException exception = new ClassCastException();
394: after1.afterExecute(actionBean, actionContext, null);
395: expectLastCall().andThrow(exception);
396:
397: replay(action);
398: replay(request);
399: replay(actionBean);
400: replay(beanSource);
401: replay(after1);
402: replay(after2);
403:
404: ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
405: delegate.setAfterInterceptors(afters);
406:
407: try {
408: delegate.handleActionPerform(action, actionContext);
409: } catch (ApplicationRuntimeException e) {
410: assertTrue(e.getCause() instanceof ClassCastException);
411: }
412:
413: verify(action);
414: verify(request);
415: verify(actionBean);
416: verify(beanSource);
417: verify(after1);
418: verify(after2);
419:
420: }
421:
422: @Test
423: public void testInvalidBeforeInterceptor() throws Exception {
424: try {
425: ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
426: delegate.runBeforeInterceptor("should be Integer",
427: new TypedBeforeInterceptor(), null);
428: fail();
429: } catch (ApplicationRuntimeException e) {
430: assertEquals(
431: e.getMessage(),
432: "Action bean class java.lang.String is not compatible with "
433: + "BeforeInterceptor implementation org.strecks.controller.impl.TypedBeforeInterceptor, "
434: + "which is parameterized with the type class java.lang.Integer");
435: }
436: }
437:
438: @Test
439: public void testInvalidAfterInterceptor() throws Exception {
440: try {
441: ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
442: delegate.runAfterInterceptor("should be Integer",
443: new TypedAfterInterceptor(), null, null);
444: fail();
445: } catch (ApplicationRuntimeException e) {
446: assertEquals(
447: e.getMessage(),
448: "Action bean class java.lang.String is not compatible with "
449: + "AfterInterceptor implementation org.strecks.controller.impl.TypedAfterInterceptor, "
450: + "which is parameterized with the type class java.lang.Integer");
451: }
452: }
453:
454: }
|