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: import static org.testng.Assert.fail;
022:
023: import java.lang.reflect.Method;
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.Globals;
031: import org.apache.struts.action.ActionErrors;
032: import org.apache.struts.action.ActionMessage;
033: import org.apache.struts.action.ActionServlet;
034: import org.strecks.action.basic.BasicController;
035: import org.strecks.context.ActionContext;
036: import org.strecks.context.impl.TestContextImpl;
037: import org.strecks.controller.impl.TestAction;
038: import org.strecks.exceptions.ApplicationRuntimeException;
039: import org.strecks.injection.internal.InjectionAnnotationReader;
040: import org.strecks.injection.internal.InjectionWrapper;
041: import org.strecks.lifecycle.impl.ActionWithLifecycleMethods;
042: import org.strecks.lifecycle.impl.LifecycleMethodReader;
043: import org.strecks.source.DefaultActionBeanSource;
044: import org.testng.Assert;
045: import org.testng.annotations.Test;
046:
047: /**
048: * @author Phil Zoio
049: */
050: public class TestBaseControllerAction {
051:
052: @Test
053: public void testNewActionBean() {
054:
055: BaseControllerAction action = new SimpleControllerAction();
056: action.setBeanSource(new DefaultActionBeanSource(
057: TestAction.class));
058: Object newActionBean = action.getBeanSource().createBean(null);
059: assert newActionBean instanceof TestAction;
060:
061: }
062:
063: @Test
064: public void testDuffNewActionBean() {
065: try {
066: BaseControllerAction action = new SimpleControllerAction();
067: action.setBeanSource(new DefaultActionBeanSource(
068: AbstractList.class));
069:
070: action.getBeanSource().createBean(null);
071: fail("Should have caught UnrecoverableRuntimeException");
072: } catch (ApplicationRuntimeException e) {
073: e.printStackTrace();
074: }
075: }
076:
077: @Test
078: public void testExecute() throws Exception {
079:
080: HttpServletRequest request = createMock(HttpServletRequest.class);
081: ActionServlet servlet = createMock(ActionServlet.class);
082: ActionContext actionContext = new TestContextImpl(request);
083:
084: replay(request);
085: replay(servlet);
086:
087: SimpleControllerAction action = new SimpleControllerAction();
088: action.setServlet(servlet);
089:
090: action.setBeanSource(new DefaultActionBeanSource(
091: TestAction.class));
092: action.executeController(new TestAction(), actionContext);
093: action.executeController(new TestAction(), actionContext);
094:
095: Assert.assertEquals(action.getCallCount(), 2);
096:
097: // verify that a different action bean is being used
098: Assert.assertEquals(action.getActionBeanCount(), 1);
099:
100: verify(request);
101: verify(servlet);
102:
103: }
104:
105: @Test
106: public void testDoInjection() throws Exception {
107:
108: InjectionAnnotationReader c = new InjectionAnnotationReader();
109: InjectionAction actionBean = new InjectionAction();
110: c.readAnnotations(actionBean.getClass());
111: Map<String, InjectionWrapper> injectionHandlers = c
112: .getInjectionMap();
113:
114: HttpServletRequest request = createMock(HttpServletRequest.class);
115: ServletContext context = createMock(ServletContext.class);
116:
117: // this will be called when the injection context is inspected
118: expect(request.getParameter("param")).andReturn("3");
119: replay(request);
120:
121: SimpleControllerAction action = new SimpleControllerAction();
122: action.setInjectionHandlers(injectionHandlers);
123:
124: action.doInjection(actionBean, new TestContextImpl(request,
125: null, context, null, null));
126:
127: // prove that the value has been injected
128: assert actionBean.getParam().equals(3);
129:
130: verify(request);
131:
132: }
133:
134: @Test
135: public void testInitMethod() throws Exception {
136: LifecycleMethodReader c = new LifecycleMethodReader();
137: ActionWithLifecycleMethods actionBean = new ActionWithLifecycleMethods();
138: c.readAnnotations(actionBean.getClass());
139: final Method initMethod = c.getInitMethod();
140:
141: SimpleControllerAction action = new SimpleControllerAction();
142: action.setInitMethod(initMethod);
143:
144: action.doInitMethod(actionBean);
145:
146: // prove that the value has been injected
147: Assert.assertEquals(actionBean.getInitCount(), 1);
148: }
149:
150: @Test
151: public void testNoLifecycleMethod() throws Exception {
152: ActionWithLifecycleMethods actionBean = new ActionWithLifecycleMethods();
153: SimpleControllerAction action = new SimpleControllerAction();
154: action.doInitMethod(actionBean);
155: Assert.assertEquals(actionBean.getInitCount(), 0);
156: Assert.assertEquals(actionBean.getCloseCount(), 0);
157: }
158:
159: @Test
160: public void testCloseMethod() throws Exception {
161: LifecycleMethodReader c = new LifecycleMethodReader();
162: ActionWithLifecycleMethods actionBean = new ActionWithLifecycleMethods();
163: c.readAnnotations(actionBean.getClass());
164: final Method closeMethod = c.getCloseMethod();
165:
166: SimpleControllerAction action = new SimpleControllerAction();
167: action.setCloseMethod(closeMethod);
168:
169: action.doCloseMethod(actionBean);
170:
171: // prove that the value has been injected
172: Assert.assertEquals(actionBean.getCloseCount(), 1);
173: }
174:
175: @Test
176: public void testHasNoErrors() {
177:
178: HttpServletRequest request = createMock(HttpServletRequest.class);
179:
180: expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(null);
181:
182: replay(request);
183:
184: BasicController controller = new BasicController();
185: assert false == controller.hasErrors(new TestContextImpl(
186: request));
187:
188: verify(request);
189:
190: }
191:
192: @Test
193: public void testHasEmptyErrors() {
194:
195: HttpServletRequest request = createMock(HttpServletRequest.class);
196:
197: expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(
198: new ActionErrors());
199:
200: replay(request);
201:
202: BasicController controller = new BasicController();
203: assert false == controller.hasErrors(new TestContextImpl(
204: request));
205:
206: verify(request);
207:
208: }
209:
210: @Test
211: public void testHasErrors() {
212:
213: HttpServletRequest request = createMock(HttpServletRequest.class);
214:
215: ActionErrors actionErrors = new ActionErrors();
216: actionErrors.add("property", new ActionMessage("key"));
217: expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(
218: actionErrors);
219:
220: replay(request);
221:
222: BasicController controller = new BasicController();
223: assert true == controller
224: .hasErrors(new TestContextImpl(request));
225:
226: verify(request);
227:
228: }
229:
230: }
|