/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.strecks.action.basic;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.createStrictMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import java.util.HashMap;
import java.util.Locale;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.MessageResourcesConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.util.MessageResources;
import org.strecks.action.basic.impl.SimpleLookupDispatchAction;
import org.strecks.context.impl.TestContextImpl;
import org.strecks.controller.ActionCreator;
import org.strecks.controller.ActionCreatorImpl;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.strecks.form.controller.DelegatingForm;
import org.strecks.view.ActionForwardViewAdapter;
import org.strecks.view.ViewAdapter;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestBasicLookupDispatchController
{
@Test
public void testCreationAction() throws Exception
{
ActionCreator actionCreator = new ActionCreatorImpl();
Action action = actionCreator.createAction(SimpleLookupDispatchAction.class);
BasicLookupDispatchController controller = (BasicLookupDispatchController) action;
assert controller.internalGetKeyMethodMap() != null;
assert controller.getBeanSource() != null;
}
@Test
public void testInsert()
{
BasicLookupDispatchController action = new BasicLookupDispatchController();
ActionForward actionForward = new ActionForward();
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("insert.key", "insert");
action.setKeyMethodMap(hashMap);
SimpleLookupDispatchAction actionBean = createMock(SimpleLookupDispatchAction.class);
ActionMapping mapping = createMock(ActionMapping.class);
HttpServletRequest request = createMock(HttpServletRequest.class);
HttpSession session = createMock(HttpSession.class);
ModuleConfig moduleConfig = createMock(ModuleConfig.class);
ActionServlet servlet = createMock(ActionServlet.class);
ServletContext context = createMock(ServletContext.class);
MessageResources messageResources = createMock(MessageResources.class);
DelegatingForm form = createStrictMock(DelegatingForm.class);
action.setServlet(servlet);
actionBean.preBind();
form.bindInwards(actionBean);
expect(request.getAttribute(Globals.CANCEL_KEY)).andReturn(null);
expect(mapping.getParameter()).andReturn("method");
expect(request.getParameter("method")).andReturn("Insert Stuff Here");
expect(request.getSession(false)).andReturn(session);
expect(session.getAttribute(Globals.LOCALE_KEY)).andReturn(Locale.getDefault());
expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(moduleConfig);
expect(moduleConfig.findMessageResourcesConfigs()).andReturn(new MessageResourcesConfig[]
{
new MessageResourcesConfig()
});
expect(servlet.getServletContext()).andReturn(context);
expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(moduleConfig);
expect(moduleConfig.getPrefix()).andReturn("");
expect(context.getAttribute(Globals.MESSAGES_KEY)).andReturn(messageResources);
expect(messageResources.getMessage(Locale.getDefault(), "insert.key")).andReturn("Insert Stuff Here");
expect(actionBean.insert()).andReturn("inserted");
expect(mapping.findForward("inserted")).andReturn(actionForward);
replay(actionBean);
replay(mapping);
replay(request);
replay(session);
replay(moduleConfig);
replay(servlet);
replay(context);
replay(messageResources);
replay(form);
ViewAdapter viewAdapter = action.executeAction(actionBean, new TestContextImpl(request, null, null, form,
mapping));
ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
assert (va.getActionForward() == actionForward);
verify(actionBean);
verify(mapping);
verify(request);
verify(session);
verify(moduleConfig);
verify(servlet);
verify(context);
verify(messageResources);
verify(form);
}
@Test
public void testNoKeyInActionMap()
{
BasicLookupDispatchController action = new BasicLookupDispatchController();
HashMap<String, String> hashMap = new HashMap<String, String>();
// hashMap.put("insert.key", "insert");
action.setKeyMethodMap(hashMap);
SimpleLookupDispatchAction actionBean = createMock(SimpleLookupDispatchAction.class);
ActionMapping mapping = createMock(ActionMapping.class);
HttpServletRequest request = createMock(HttpServletRequest.class);
HttpSession session = createMock(HttpSession.class);
ModuleConfig moduleConfig = createMock(ModuleConfig.class);
ActionServlet servlet = createMock(ActionServlet.class);
ServletContext context = createMock(ServletContext.class);
MessageResources messageResources = createMock(MessageResources.class);
action.setServlet(servlet);
expect(request.getAttribute(Globals.CANCEL_KEY)).andReturn(null);
expect(mapping.getParameter()).andReturn("method");
expect(request.getParameter("method")).andReturn("Insert Stuff Here");
expect(request.getSession(false)).andReturn(session);
expect(session.getAttribute(Globals.LOCALE_KEY)).andReturn(Locale.getDefault());
expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(moduleConfig);
expect(moduleConfig.findMessageResourcesConfigs()).andReturn(new MessageResourcesConfig[]
{
new MessageResourcesConfig()
});
expect(servlet.getServletContext()).andReturn(context);
expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(moduleConfig);
expect(moduleConfig.getPrefix()).andReturn("");
expect(context.getAttribute(Globals.MESSAGES_KEY)).andReturn(messageResources);
expect(mapping.getPath()).andReturn("appPath");
replay(actionBean);
replay(mapping);
replay(request);
replay(session);
replay(moduleConfig);
replay(servlet);
replay(context);
replay(messageResources);
try
{
action.executeAction(actionBean, new TestContextImpl(request, null, null, null, mapping));
}
catch (ApplicationRuntimeException e)
{
Assert
.assertEquals(e.getMessage(),
"javax.servlet.ServletException: Action[appPath] missing resource in key method map");
}
verify(actionBean);
verify(mapping);
verify(request);
verify(session);
verify(moduleConfig);
verify(servlet);
verify(context);
verify(messageResources);
}
@Test
public void testCancel()
{
BasicLookupDispatchController action = new BasicLookupDispatchController();
ActionForward actionForward = new ActionForward();
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("insert.key", "insert");
action.setKeyMethodMap(hashMap);
SimpleLookupDispatchAction actionBean = createMock(SimpleLookupDispatchAction.class);
ActionMapping mapping = createMock(ActionMapping.class);
HttpServletRequest request = createMock(HttpServletRequest.class);
expect(request.getAttribute(Globals.CANCEL_KEY)).andReturn(Boolean.TRUE);
expect(actionBean.cancel()).andReturn("cancelled");
expect(mapping.findForward("cancelled")).andReturn(actionForward);
replay(actionBean);
replay(mapping);
replay(request);
ViewAdapter viewAdapter = action.executeAction(actionBean, new TestContextImpl(request, null, null, null,
mapping));
ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
assert (va.getActionForward() == actionForward);
verify(actionBean);
verify(mapping);
verify(request);
}
}