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.createStrictMock;
020: import static org.easymock.classextension.EasyMock.replay;
021: import static org.easymock.classextension.EasyMock.verify;
022: import static org.testng.Assert.fail;
023:
024: import java.util.AbstractList;
025: import java.util.Map;
026:
027: import javax.servlet.ServletContext;
028: import javax.servlet.http.HttpServletRequest;
029:
030: import org.apache.struts.action.ActionServlet;
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.SimplePage;
035: import org.strecks.controller.impl.TestAction;
036: import org.strecks.exceptions.ApplicationRuntimeException;
037: import org.strecks.injection.internal.InjectionAnnotationReader;
038: import org.strecks.injection.internal.InjectionWrapper;
039: import org.strecks.navigate.NavigationHolder;
040: import org.strecks.navigate.factory.DefaultNavigationHandlerFactory;
041: import org.strecks.navigate.handler.PageNavigationHandler;
042: import org.strecks.navigate.internal.impl.ActionWithNavigate;
043: import org.strecks.navigate.internal.impl.ActionWithNullReturningNavigate;
044: import org.strecks.page.PageForward;
045: import org.strecks.source.DefaultActionBeanSource;
046: import org.strecks.view.ActionForwardViewAdapter;
047: import org.strecks.view.ViewAdapter;
048: import org.testng.Assert;
049: import org.testng.annotations.Test;
050:
051: /**
052: * @author Phil Zoio
053: */
054: public class TestNavigableControllerAction {
055:
056: @Test
057: public void testNewActionBean() {
058:
059: BaseControllerAction action = new SimpleControllerAction();
060: action.setBeanSource(new DefaultActionBeanSource(
061: TestAction.class));
062: Object newActionBean = action.getBeanSource().createBean(null);
063: assert newActionBean instanceof TestAction;
064:
065: }
066:
067: @Test
068: public void testDuffNewActionBean() {
069: try {
070: BaseControllerAction action = new SimpleControllerAction();
071: action.setBeanSource(new DefaultActionBeanSource(
072: AbstractList.class));
073:
074: action.getBeanSource().createBean(null);
075: fail("Should have caught UnrecoverableRuntimeException");
076: } catch (ApplicationRuntimeException e) {
077: e.printStackTrace();
078: }
079: }
080:
081: @Test
082: public void testExecute() throws Exception {
083:
084: HttpServletRequest request = createMock(HttpServletRequest.class);
085: ActionServlet servlet = createMock(ActionServlet.class);
086: ActionContext actionContext = new TestContextImpl(request);
087:
088: replay(request);
089: replay(servlet);
090:
091: SimpleControllerAction action = new SimpleControllerAction();
092: action.setServlet(servlet);
093:
094: action.setBeanSource(new DefaultActionBeanSource(
095: TestAction.class));
096: action.executeController(new TestAction(), actionContext);
097: action.executeController(new TestAction(), actionContext);
098:
099: Assert.assertEquals(action.getCallCount(), 2);
100:
101: // verify that a different action bean is being used
102: Assert.assertEquals(action.getActionBeanCount(), 1);
103:
104: verify(request);
105: verify(servlet);
106:
107: }
108:
109: @Test
110: public void testDoInjection() throws Exception {
111:
112: InjectionAnnotationReader c = new InjectionAnnotationReader();
113: InjectionAction actionBean = new InjectionAction();
114: c.readAnnotations(actionBean.getClass());
115: Map<String, InjectionWrapper> injectionHandlers = c
116: .getInjectionMap();
117:
118: HttpServletRequest request = createMock(HttpServletRequest.class);
119: ServletContext context = createMock(ServletContext.class);
120:
121: // this will be called when the injection context is inspected
122: expect(request.getParameter("param")).andReturn("3");
123: replay(request);
124:
125: SimpleControllerAction action = new SimpleControllerAction();
126: action.setInjectionHandlers(injectionHandlers);
127:
128: action.doInjection(actionBean, new TestContextImpl(request,
129: null, context, null, null));
130:
131: // prove that the value has been injected
132: assert actionBean.getParam().equals(3);
133:
134: verify(request);
135:
136: }
137:
138: @Test(expectedExceptions=IllegalArgumentException.class)
139: public void testFindForwardNavigationHolderNull() {
140: SimpleControllerAction action = new SimpleControllerAction();
141: ActionWithNavigate bean = new ActionWithNavigate();
142:
143: action.findActionForward(bean, new TestContextImpl());
144: }
145:
146: @Test
147: public void testFindForward() throws Exception {
148:
149: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
150: request.setAttribute(InfrastructureKeys.PAGE_BEAN,
151: new SimplePage());
152:
153: replay(request);
154:
155: SimpleControllerAction action = new SimpleControllerAction();
156: ActionWithNavigate bean = new ActionWithNavigate();
157:
158: NavigationHolder navigationHolder = new NavigationHolder(
159: new PageNavigationHandler(),
160: new DefaultNavigationHandlerFactory(), bean.getClass()
161: .getMethod("nextPage"));
162:
163: action.setNavigationHolder(navigationHolder);
164:
165: ViewAdapter viewAdapter = action.findActionForward(bean,
166: new TestContextImpl(request));
167:
168: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
169: assert (va.getActionForward() instanceof PageForward);
170:
171: verify(request);
172: }
173:
174: @Test
175: public void testFindForwardNullReturningAction() throws Exception {
176:
177: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
178:
179: replay(request);
180:
181: SimpleControllerAction action = new SimpleControllerAction();
182: ActionWithNullReturningNavigate bean = new ActionWithNullReturningNavigate();
183:
184: NavigationHolder navigationHolder = new NavigationHolder(
185: new PageNavigationHandler(),
186: new DefaultNavigationHandlerFactory(), bean.getClass()
187: .getMethod("nextPage"));
188:
189: action.setNavigationHolder(navigationHolder);
190:
191: assert null == action.findActionForward(bean,
192: new TestContextImpl(request));
193:
194: verify(request);
195: }
196:
197: }
|