Package org.strecks.navigate.handler

Source Code of org.strecks.navigate.handler.TestActionRedirectNavigationHandler

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

import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createStrictMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import static org.testng.Assert.assertEquals;

import java.util.HashMap;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForward;
import org.strecks.constants.InfrastructureKeys;
import org.strecks.context.impl.TestContextImpl;
import org.strecks.view.ActionForwardViewAdapter;
import org.strecks.view.ViewAdapter;
import org.strecks.web.RedirectHelper;
import org.testng.annotations.Test;

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

  @Test
  public void testGetRedirectURL()
  {

    ActionRedirectNavigationHandler handler = new ActionRedirectNavigationHandler();
    HashMap<String, String> parameters = getRequestParameters();

    String redirectURL = handler.getRedirectURL("action", parameters);

    assertEquals(redirectURL, "action.do?param1=value1&param2=value2");

  }

  @Test
  public void testGetRedirectURLNullParams()
  {

    ActionRedirectNavigationHandler handler = new ActionRedirectNavigationHandler();
    String redirectURL = handler.getRedirectURL("action", null);
    assertEquals(redirectURL, "action.do");

  }

  @Test
  public void testGetActionForward()
  {

    HttpServletRequest request = createStrictMock(HttpServletRequest.class);
    HttpSession session = createStrictMock(HttpSession.class);
    RedirectHelper redirectHelper = createStrictMock(RedirectHelper.class);

    expect(request.getAttribute(InfrastructureKeys.REDIRECT_HELPER)).andReturn(redirectHelper);
    expect(redirectHelper.getRequestParameters()).andReturn(getRequestParameters());
    expect(request.getSession()).andReturn(session);
    expect(redirectHelper.getSessionParameters()).andReturn(getSessionParameters());
    session.setAttribute(InfrastructureKeys.REDIRECT_PARAMETERS, getSessionParameters());

    replay(request);
    replay(session);
    replay(redirectHelper);

    ActionRedirectNavigationHandler handler = new ActionRedirectNavigationHandler();
    ViewAdapter viewAdapter = handler.getActionForward(new TestContextImpl(request), "action");

    ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
    ActionForward actionForward = va.getActionForward();

    verify(request);
    verify(session);
    verify(redirectHelper);

    assertEquals(actionForward.getPath(), "action.do?param1=value1&param2=value2");
    assert actionForward.getRedirect();

  }

  private HashMap<String, String> getRequestParameters()
  {
    HashMap<String, String> parameters = new LinkedHashMap<String, String>();

    parameters.put("param1", "value1");
    parameters.put("param2", "value2");
    return parameters;
  }

  private HashMap<String, Object> getSessionParameters()
  {
    HashMap<String, Object> parameters = new LinkedHashMap<String, Object>();

    parameters.put("param1", 1);
    parameters.put("param2", 12L);
    return parameters;
  }

}
TOP

Related Classes of org.strecks.navigate.handler.TestActionRedirectNavigationHandler

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.