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.navigable;
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:
023: import java.util.HashMap;
024: import java.util.Locale;
025:
026: import javax.servlet.ServletContext;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpSession;
029:
030: import org.apache.struts.Globals;
031: import org.apache.struts.action.Action;
032: import org.apache.struts.action.ActionForward;
033: import org.apache.struts.action.ActionMapping;
034: import org.apache.struts.action.ActionServlet;
035: import org.apache.struts.config.MessageResourcesConfig;
036: import org.apache.struts.config.ModuleConfig;
037: import org.apache.struts.util.MessageResources;
038: import org.strecks.action.basic.impl.NavigableLookupDispatchAction;
039: import org.strecks.context.impl.TestContextImpl;
040: import org.strecks.controller.ActionCreator;
041: import org.strecks.controller.ActionCreatorImpl;
042: import org.strecks.exceptions.ApplicationRuntimeException;
043: import org.strecks.form.controller.DelegatingForm;
044: import org.strecks.navigate.internal.BeanNavigationReader;
045: import org.strecks.view.ActionForwardViewAdapter;
046: import org.strecks.view.ViewAdapter;
047: import org.testng.Assert;
048: import org.testng.annotations.BeforeMethod;
049: import org.testng.annotations.Test;
050:
051: /**
052: * @author Phil Zoio
053: */
054: public class TestNavigableLookupDispatchController {
055:
056: private NavigableLookupDispatchController action;
057:
058: @BeforeMethod
059: public void setUp() {
060:
061: action = new NavigableLookupDispatchController();
062: BeanNavigationReader navigationReader = new BeanNavigationReader();
063: navigationReader
064: .readAnnotations(NavigableLookupDispatchAction.class);
065: action.setNavigationHolder(navigationReader
066: .getNavigationHolder());
067:
068: }
069:
070: @Test
071: public void testCreationAction() throws Exception {
072: ActionCreator actionCreator = new ActionCreatorImpl();
073: Action action = actionCreator
074: .createAction(NavigableLookupDispatchAction.class);
075: NavigableLookupDispatchController controller = (NavigableLookupDispatchController) action;
076: assert controller.getBeanSource() != null;
077: assert controller.getNavigationHolder() != null;
078: assert controller.internalGetKeyMethodMap() != null;
079: }
080:
081: @Test
082: public void testInsert() {
083:
084: ActionForward actionForward = new ActionForward();
085: HashMap<String, String> hashMap = new HashMap<String, String>();
086: hashMap.put("insert.key", "insert");
087:
088: action.setKeyMethodMap(hashMap);
089:
090: NavigableLookupDispatchAction actionBean = createMock(NavigableLookupDispatchAction.class);
091:
092: ActionMapping mapping = createMock(ActionMapping.class);
093: HttpServletRequest request = createMock(HttpServletRequest.class);
094: HttpSession session = createMock(HttpSession.class);
095: ModuleConfig moduleConfig = createMock(ModuleConfig.class);
096: ActionServlet servlet = createMock(ActionServlet.class);
097: ServletContext context = createMock(ServletContext.class);
098: MessageResources messageResources = createMock(MessageResources.class);
099: DelegatingForm form = createStrictMock(DelegatingForm.class);
100:
101: action.setServlet(servlet);
102:
103: actionBean.preBind();
104: form.bindInwards(actionBean);
105: expect(request.getAttribute(Globals.CANCEL_KEY))
106: .andReturn(null);
107: expect(mapping.getParameter()).andReturn("method");
108: expect(request.getParameter("method")).andReturn(
109: "Insert Stuff Here");
110: expect(request.getSession(false)).andReturn(session);
111: expect(session.getAttribute(Globals.LOCALE_KEY)).andReturn(
112: Locale.getDefault());
113: expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(
114: moduleConfig);
115: expect(moduleConfig.findMessageResourcesConfigs())
116: .andReturn(
117: new MessageResourcesConfig[] { new MessageResourcesConfig() });
118: expect(servlet.getServletContext()).andReturn(context);
119: expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(
120: moduleConfig);
121: expect(moduleConfig.getPrefix()).andReturn("");
122: expect(context.getAttribute(Globals.MESSAGES_KEY)).andReturn(
123: messageResources);
124: expect(
125: messageResources.getMessage(Locale.getDefault(),
126: "insert.key")).andReturn("Insert Stuff Here");
127: actionBean.insert();
128: expect(actionBean.getNavigationResult()).andReturn("inserted");
129: expect(mapping.findForward("inserted"))
130: .andReturn(actionForward);
131:
132: replay(actionBean);
133: replay(mapping);
134: replay(request);
135: replay(session);
136: replay(moduleConfig);
137: replay(servlet);
138: replay(context);
139: replay(messageResources);
140: replay(form);
141:
142: ViewAdapter viewAdapter = action
143: .executeAction(actionBean, new TestContextImpl(request,
144: null, null, form, mapping));
145: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
146: assert (va.getActionForward() == actionForward);
147:
148: verify(actionBean);
149: verify(mapping);
150: verify(request);
151: verify(session);
152: verify(moduleConfig);
153: verify(servlet);
154: verify(context);
155: verify(messageResources);
156: verify(form);
157:
158: }
159:
160: @Test
161: public void testNoKeyInActionMap() {
162:
163: HashMap<String, String> hashMap = new HashMap<String, String>();
164: // hashMap.put("insert.key", "insert");
165:
166: action.setKeyMethodMap(hashMap);
167:
168: NavigableLookupDispatchAction actionBean = createMock(NavigableLookupDispatchAction.class);
169:
170: ActionMapping mapping = createMock(ActionMapping.class);
171: HttpServletRequest request = createMock(HttpServletRequest.class);
172: HttpSession session = createMock(HttpSession.class);
173: ModuleConfig moduleConfig = createMock(ModuleConfig.class);
174: ActionServlet servlet = createMock(ActionServlet.class);
175: ServletContext context = createMock(ServletContext.class);
176: MessageResources messageResources = createMock(MessageResources.class);
177:
178: action.setServlet(servlet);
179:
180: expect(request.getAttribute(Globals.CANCEL_KEY))
181: .andReturn(null);
182: expect(mapping.getParameter()).andReturn("method");
183: expect(request.getParameter("method")).andReturn(
184: "Insert Stuff Here");
185: expect(request.getSession(false)).andReturn(session);
186: expect(session.getAttribute(Globals.LOCALE_KEY)).andReturn(
187: Locale.getDefault());
188: expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(
189: moduleConfig);
190: expect(moduleConfig.findMessageResourcesConfigs())
191: .andReturn(
192: new MessageResourcesConfig[] { new MessageResourcesConfig() });
193: expect(servlet.getServletContext()).andReturn(context);
194: expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(
195: moduleConfig);
196: expect(moduleConfig.getPrefix()).andReturn("");
197: expect(context.getAttribute(Globals.MESSAGES_KEY)).andReturn(
198: messageResources);
199:
200: expect(mapping.getPath()).andReturn("appPath");
201:
202: replay(actionBean);
203: replay(mapping);
204: replay(request);
205: replay(session);
206: replay(moduleConfig);
207: replay(servlet);
208: replay(context);
209: replay(messageResources);
210:
211: try {
212: action.executeAction(actionBean, new TestContextImpl(
213: request, null, null, null, mapping));
214: } catch (ApplicationRuntimeException e) {
215: Assert
216: .assertEquals(
217: e.getMessage(),
218: "javax.servlet.ServletException: Action[appPath] missing resource in key method map");
219: }
220:
221: verify(actionBean);
222: verify(mapping);
223: verify(request);
224: verify(session);
225: verify(moduleConfig);
226: verify(servlet);
227: verify(context);
228: verify(messageResources);
229:
230: }
231:
232: @Test
233: public void testCancel() {
234:
235: ActionForward actionForward = new ActionForward();
236: HashMap<String, String> hashMap = new HashMap<String, String>();
237: hashMap.put("insert.key", "insert");
238:
239: action.setKeyMethodMap(hashMap);
240:
241: NavigableLookupDispatchAction actionBean = createMock(NavigableLookupDispatchAction.class);
242:
243: ActionMapping mapping = createMock(ActionMapping.class);
244: HttpServletRequest request = createMock(HttpServletRequest.class);
245:
246: expect(request.getAttribute(Globals.CANCEL_KEY)).andReturn(
247: Boolean.TRUE);
248: actionBean.cancel();
249: expect(actionBean.getNavigationResult()).andReturn("cancelled");
250: expect(mapping.findForward("cancelled")).andReturn(
251: actionForward);
252:
253: replay(actionBean);
254: replay(mapping);
255: replay(request);
256:
257: ViewAdapter viewAdapter = action
258: .executeAction(actionBean, new TestContextImpl(request,
259: null, null, null, mapping));
260: ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
261: assert (va.getActionForward() == actionForward);
262:
263: verify(actionBean);
264: verify(mapping);
265: verify(request);
266:
267: }
268:
269: }
|