Package org.strecks.navigate.spring

Source Code of org.strecks.navigate.spring.TestSpringNavigationHandler

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

import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.testng.Assert.assertEquals;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.Globals;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.strecks.context.ActionContext;
import org.strecks.exceptions.ApplicationConfigurationException;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.strecks.testng.AbstractMockTest;
import org.strecks.view.ViewAdapter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestSpringNavigationHandler extends AbstractMockTest
{

  private View view;

  private Map model;

  private ActionContext context;

  private HttpServletRequest request;

  private ServletContext servletContext;

  private WebApplicationContext wac;

  private ViewResolver viewResolver;

  private Locale locale;

  @BeforeMethod
  public void setUp()
  {

    model = new HashMap();
    view = newMock(View.class);
    context = newMock(ActionContext.class);
    request = newMock(HttpServletRequest.class);
    servletContext = newMock(ServletContext.class);
    wac = newMock(WebApplicationContext.class);
    viewResolver = newMock(ViewResolver.class);
    locale = Locale.getDefault();

    resetMocks();

  }

  @Test
  public void testGetViewAdapterWithResolver() throws Exception
  {

    SpringViewNavigationHandler handler = new SpringViewNavigationHandler("resolver_name");

    expect(context.getRequest()).andReturn(request);
    request.setAttribute(eq(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE), isA(StrecksLocaleResolver.class));
    expect(context.getContext()).andReturn(servletContext);
    expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
        .andReturn(wac);
    expect(wac.getBean("resolver_name")).andReturn(viewResolver);
    expect(request.getAttribute(Globals.LOCALE_KEY)).andReturn(locale);
    expect(viewResolver.resolveViewName("view_name", locale)).andReturn(view);

    replayMocks();
    ViewAdapter adapter = handler.getActionForward(context, new ModelAndView("view_name", model));

    assert adapter instanceof SpringRenderingViewAdapter;
    SpringRenderingViewAdapter va = (SpringRenderingViewAdapter) adapter;
    assert va.getModel().equals(model);
    assert va.getView() == view;

    verifyMocks();

  }

  @Test
  public void testNotViewResolver() throws Exception
  {

    SpringViewNavigationHandler handler = new SpringViewNavigationHandler("resolver_name");

    expect(context.getRequest()).andReturn(request);
    request.setAttribute(eq(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE), isA(StrecksLocaleResolver.class));
    expect(context.getContext()).andReturn(servletContext);
    expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
        .andReturn(wac);
    expect(wac.getBean("resolver_name")).andReturn("this should be a view resolver");

    replayMocks();

    try
    {
      handler.getActionForward(context, new ModelAndView("view_name", model));
    }
    catch (ApplicationConfigurationException e)
    {
      assertEquals(e.getMessage(), "Bean referenced using the 'resolver' attribute of @SpringView "
          + "annotation is not instance of " + "org.springframework.web.servlet.ViewResolver");
    }

    verifyMocks();

  }

  @Test
  public void testResolverException() throws Exception
  {

    SpringViewNavigationHandler handler = new SpringViewNavigationHandler("resolver_name");
   
    expect(context.getRequest()).andReturn(request);
    request.setAttribute(eq(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE), isA(StrecksLocaleResolver.class));
    expect(context.getContext()).andReturn(servletContext);
    expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
        .andReturn(wac);
    expect(wac.getBean("resolver_name")).andReturn(viewResolver);
    expect(request.getAttribute(Globals.LOCALE_KEY)).andReturn(locale);
    expect(viewResolver.resolveViewName("view_name", locale)).andThrow(new IllegalStateException());

    replayMocks();

    try
    {
      handler.getActionForward(context, new ModelAndView("view_name", model));
    }
    catch (ApplicationRuntimeException e)
    {
      assert e.getMessage().startsWith("Exception thrown during resolution of view name view_name using ViewResolver");
    }

    verifyMocks();

  }

  @Test
  public void testGetViewAdapterWithNamedView() throws Exception
  {

    SpringViewNavigationHandler handler = new SpringViewNavigationHandler("");

    expect(context.getRequest()).andReturn(request);
    request.setAttribute(eq(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE), isA(StrecksLocaleResolver.class));
    expect(context.getContext()).andReturn(servletContext);
    expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
        .andReturn(wac);
    expect(wac.getBean("view_name")).andReturn(view);

    replayMocks();
    ViewAdapter adapter = handler.getActionForward(context, new ModelAndView("view_name", model));

    assert adapter instanceof SpringRenderingViewAdapter;
    SpringRenderingViewAdapter va = (SpringRenderingViewAdapter) adapter;
    assert va.getModel().equals(model);
    assert va.getView() == view;

    verifyMocks();

  }

  @Test
  public void testGetViewAdapterWithView() throws Exception
  {

    SpringViewNavigationHandler handler = new SpringViewNavigationHandler("");

    expect(context.getRequest()).andReturn(request);
    request.setAttribute(eq(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE), isA(StrecksLocaleResolver.class));

    replayMocks();
    ViewAdapter adapter = handler.getActionForward(context, new ModelAndView(view, model));

    assert adapter instanceof SpringRenderingViewAdapter;
    SpringRenderingViewAdapter va = (SpringRenderingViewAdapter) adapter;
    assert va.getModel().equals(model);
    assert va.getView() == view;

    verifyMocks();

  }
 
  @Test
  public void testGetViewBeanNotView() throws Exception
  {

    SpringViewNavigationHandler handler = new SpringViewNavigationHandler("");

    expect(context.getRequest()).andReturn(request);
    request.setAttribute(eq(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE), isA(StrecksLocaleResolver.class));
    expect(context.getContext()).andReturn(servletContext);
    expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
        .andReturn(wac);
    expect(wac.getBean("view_name")).andReturn("This should be a view");

    replayMocks();

    try
    {
      handler.getActionForward(context, new ModelAndView("view_name", model));
    }
    catch (ApplicationConfigurationException e)
    {
      assertEquals(e.getMessage(), "Bean referenced using the 'viewName' property " +
          "of the returned ModelAndView of the method containing @SpringView " +
          "is not an instance of org.springframework.web.servlet.View");
    }
   
    verifyMocks();

 
 

  @Test
  public void testHandler()
  {

    SpringViewNavigationHandler handler = new SpringViewNavigationHandler("view_name");
    assert handler.isUseResolver();
    assert handler.getLocaleResolver() instanceof StrecksLocaleResolver;
    assertEquals(handler.getName(), "view_name");

    handler = new SpringViewNavigationHandler("");
    assert !handler.isUseResolver();
    assert handler.getLocaleResolver() instanceof StrecksLocaleResolver;
    assert null == handler.getName();

    handler = new SpringViewNavigationHandler(null);
    assert !handler.isUseResolver();
    assert handler.getLocaleResolver() instanceof StrecksLocaleResolver;
    assert null == handler.getName();

  }

}
TOP

Related Classes of org.strecks.navigate.spring.TestSpringNavigationHandler

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.