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