Package org.strecks.action.navigable

Source Code of org.strecks.action.navigable.TestNavigableLookupDispatchController

/*
* 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.navigable;

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.NavigableLookupDispatchAction;
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.navigate.internal.BeanNavigationReader;
import org.strecks.view.ActionForwardViewAdapter;
import org.strecks.view.ViewAdapter;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
* @author Phil Zoio
*/
public class TestNavigableLookupDispatchController
{

  private NavigableLookupDispatchController action;

  @BeforeMethod
  public void setUp()
  {

    action = new NavigableLookupDispatchController();
    BeanNavigationReader navigationReader = new BeanNavigationReader();
    navigationReader.readAnnotations(NavigableLookupDispatchAction.class);
    action.setNavigationHolder(navigationReader.getNavigationHolder());

  }

  @Test
  public void testCreationAction() throws Exception
  {
    ActionCreator actionCreator = new ActionCreatorImpl();
    Action action = actionCreator.createAction(NavigableLookupDispatchAction.class);
    NavigableLookupDispatchController controller = (NavigableLookupDispatchController) action;
    assert controller.getBeanSource() != null;
    assert controller.getNavigationHolder() != null;
    assert controller.internalGetKeyMethodMap() != null;
  }

  @Test
  public void testInsert()
  {

    ActionForward actionForward = new ActionForward();
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("insert.key", "insert");

    action.setKeyMethodMap(hashMap);

    NavigableLookupDispatchAction actionBean = createMock(NavigableLookupDispatchAction.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");
    actionBean.insert();
    expect(actionBean.getNavigationResult()).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()
  {

    HashMap<String, String> hashMap = new HashMap<String, String>();
    // hashMap.put("insert.key", "insert");

    action.setKeyMethodMap(hashMap);

    NavigableLookupDispatchAction actionBean = createMock(NavigableLookupDispatchAction.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()
  {

    ActionForward actionForward = new ActionForward();
    HashMap<String, String> hashMap = new HashMap<String, String>();
    hashMap.put("insert.key", "insert");

    action.setKeyMethodMap(hashMap);

    NavigableLookupDispatchAction actionBean = createMock(NavigableLookupDispatchAction.class);

    ActionMapping mapping = createMock(ActionMapping.class);
    HttpServletRequest request = createMock(HttpServletRequest.class);

    expect(request.getAttribute(Globals.CANCEL_KEY)).andReturn(Boolean.TRUE);
    actionBean.cancel();
    expect(actionBean.getNavigationResult()).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);

  }

}
TOP

Related Classes of org.strecks.action.navigable.TestNavigableLookupDispatchController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.