Package org.strecks.controller

Source Code of org.strecks.controller.TestControllerProcessorDelegateImpl

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

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
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.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForward;
import org.strecks.constants.InfrastructureKeys;
import org.strecks.context.ActionContext;
import org.strecks.context.impl.TestContextImpl;
import org.strecks.controller.impl.TestAction;
import org.strecks.controller.impl.TypedAfterInterceptor;
import org.strecks.controller.impl.TypedBeforeInterceptor;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.strecks.interceptor.AfterInterceptor;
import org.strecks.interceptor.BeforeInterceptor;
import org.strecks.source.ActionBeanSource;
import org.strecks.view.ActionForwardViewAdapter;
import org.strecks.view.RenderingViewAdapter;
import org.testng.annotations.Test;

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

  @Test
  public void testHandleActionPerform() throws Exception
  {

    ControllerAction action = createStrictMock(ControllerAction.class);
    HttpServletRequest request = createStrictMock(HttpServletRequest.class);
    TestAction actionBean = createStrictMock(TestAction.class);
    ActionBeanSource beanSource = createStrictMock(ActionBeanSource.class);

    ActionContext actionContext = new TestContextImpl(request);

    // check the expected order of method invocation
    expect(action.getBeanSource()).andReturn(beanSource);
    expect(beanSource.createBean(actionContext)).andReturn(actionBean);
    action.preExecute(actionBean, actionContext);
    expect(action.getBeforeInterceptors()).andReturn(null);
    expect(action.executeController(actionBean, actionContext)).andReturn(null);
    expect(action.getAfterInterceptors()).andReturn(null);
    request.setAttribute(InfrastructureKeys.ACTION_BEAN, actionBean);
    // request.setAttribute(InfrastructureKeys.ACTION_FORWARD, null);
    action.postExecute(actionBean, actionContext, null);

    replay(action);
    replay(request);
    replay(actionBean);
    replay(beanSource);

    ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
    delegate.handleActionPerform(action, actionContext);

    verify(action);
    verify(request);
    verify(actionBean);
    verify(beanSource);

  }

  @Test
  public void testHandleViewRenderingAdapter() throws Exception
  {

    RenderingViewAdapter viewAdapter = createStrictMock(RenderingViewAdapter.class);
    ControllerAction action = createStrictMock(ControllerAction.class);
    HttpServletRequest request = createStrictMock(HttpServletRequest.class);
    TestAction actionBean = createStrictMock(TestAction.class);
    ActionBeanSource beanSource = createStrictMock(ActionBeanSource.class);

    ActionContext actionContext = new TestContextImpl(request);

    // check the expected order of method invocation
    expect(action.getBeanSource()).andReturn(beanSource);
    expect(beanSource.createBean(actionContext)).andReturn(actionBean);
    action.preExecute(actionBean, actionContext);

    expect(action.getBeforeInterceptors()).andReturn(null);
    expect(action.executeController(actionBean, actionContext)).andReturn(viewAdapter);
    expect(action.getAfterInterceptors()).andReturn(null);

    request.setAttribute(InfrastructureKeys.ACTION_BEAN, actionBean);
    action.postExecute(actionBean, actionContext, null);
    viewAdapter.render(actionContext);

    replay(action);
    replay(request);
    replay(actionBean);
    replay(beanSource);

    ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
    delegate.handleActionPerform(action, actionContext);

    verify(action);
    verify(request);
    verify(actionBean);
    verify(beanSource);

  }

  @SuppressWarnings("unchecked")
  @Test
  public void testHandleWithInterceptors() throws Exception
  {

    ActionForward actionForward = new ActionForward();
    BeforeInterceptor before1 = createStrictMock(BeforeInterceptor.class);
    BeforeInterceptor before2 = createStrictMock(BeforeInterceptor.class);
    Collection<BeforeInterceptor> befores = new ArrayList<BeforeInterceptor>();
    befores.add(before1);
    befores.add(before2);

    BeforeInterceptor actionBefore1 = createStrictMock(BeforeInterceptor.class);
    BeforeInterceptor actionBefore2 = createStrictMock(BeforeInterceptor.class);
    List<BeforeInterceptor> actionBefores = new ArrayList<BeforeInterceptor>();
    actionBefores.add(actionBefore1);
    actionBefores.add(actionBefore2);

    AfterInterceptor after1 = createStrictMock(AfterInterceptor.class);
    AfterInterceptor after2 = createStrictMock(AfterInterceptor.class);
    Collection<AfterInterceptor> afters = new ArrayList<AfterInterceptor>();
    afters.add(after1);
    afters.add(after2);

    AfterInterceptor actionAfter1 = createStrictMock(AfterInterceptor.class);
    AfterInterceptor actionAfter2 = createStrictMock(AfterInterceptor.class);
    List<AfterInterceptor> actionAfters = new ArrayList<AfterInterceptor>();
    actionAfters.add(actionAfter1);
    actionAfters.add(actionAfter2);

    ControllerAction action = createStrictMock(ControllerAction.class);
    HttpServletRequest request = createStrictMock(HttpServletRequest.class);
    TestAction actionBean = createStrictMock(TestAction.class);
    ActionBeanSource beanSource = createStrictMock(ActionBeanSource.class);

    ActionContext actionContext = new TestContextImpl(request);

    // check the expected order of method invocation
    expect(action.getBeanSource()).andReturn(beanSource);
    expect(beanSource.createBean(actionContext)).andReturn(actionBean);
    action.preExecute(actionBean, actionContext);

    before1.beforeExecute(actionBean, actionContext);
    before2.beforeExecute(actionBean, actionContext);

    expect(action.getBeforeInterceptors()).andReturn(actionBefores);
    actionBefore1.beforeExecute(actionBean, actionContext);
    actionBefore2.beforeExecute(actionBean, actionContext);

    expect(action.executeController(actionBean, actionContext)).andReturn(
        new ActionForwardViewAdapter(actionForward));

    expect(action.getAfterInterceptors()).andReturn(actionAfters);
    actionAfter1.afterExecute(actionBean, actionContext, null);
    actionAfter2.afterExecute(actionBean, actionContext, null);

    after1.afterExecute(actionBean, actionContext, null);
    after2.afterExecute(actionBean, actionContext, null);
    action.postExecute(actionBean, actionContext, null);

    request.setAttribute(InfrastructureKeys.ACTION_BEAN, actionBean);
    request.setAttribute(InfrastructureKeys.ACTION_FORWARD, actionForward);

    replay(action);
    replay(request);
    replay(actionBean);
    replay(beanSource);
    replay(before1);
    replay(before2);
    replay(actionBefore1);
    replay(actionBefore2);
    replay(after1);
    replay(after2);

    ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
    delegate.setBeforeInterceptors(befores);
    delegate.setAfterInterceptors(afters);
    delegate.handleActionPerform(action, actionContext);

    verify(action);
    verify(request);
    verify(actionBean);
    verify(beanSource);
    verify(before1);
    verify(before2);
    verify(actionBefore1);
    verify(actionBefore2);
    verify(after1);
    verify(after2);

  }

  @SuppressWarnings("unchecked")
  @Test
  public void testThrowOnBefore() throws Exception
  {

    BeforeInterceptor before1 = createStrictMock(BeforeInterceptor.class);
    BeforeInterceptor before2 = createStrictMock(BeforeInterceptor.class);
    Collection<BeforeInterceptor> befores = new ArrayList<BeforeInterceptor>();
    befores.add(before1);
    befores.add(before2);

    AfterInterceptor after1 = createStrictMock(AfterInterceptor.class);
    AfterInterceptor after2 = createStrictMock(AfterInterceptor.class);
    Collection<AfterInterceptor> afters = new ArrayList<AfterInterceptor>();
    afters.add(after1);
    afters.add(after2);

    AfterInterceptor actionAfter1 = createStrictMock(AfterInterceptor.class);
    AfterInterceptor actionAfter2 = createStrictMock(AfterInterceptor.class);
    List<AfterInterceptor> actionAfters = new ArrayList<AfterInterceptor>();
    actionAfters.add(actionAfter1);
    actionAfters.add(actionAfter2);

    ControllerAction action = createStrictMock(ControllerAction.class);
    HttpServletRequest request = createStrictMock(HttpServletRequest.class);
    TestAction actionBean = createStrictMock(TestAction.class);
    ActionBeanSource beanSource = createStrictMock(ActionBeanSource.class);

    ActionContext actionContext = new TestContextImpl(request);

    // check the expected order of method invocation
    expect(action.getBeanSource()).andReturn(beanSource);
    expect(beanSource.createBean(actionContext)).andReturn(actionBean);
    action.preExecute(actionBean, actionContext);

    IllegalStateException exception = new IllegalStateException();

    // now expected mock calls
    before1.beforeExecute(actionBean, actionContext);
    expectLastCall().andThrow(exception);

    expect(action.getAfterInterceptors()).andReturn(actionAfters);
    actionAfter1.afterExecute(actionBean, actionContext, exception);
    actionAfter2.afterExecute(actionBean, actionContext, exception);

    after1.afterExecute(actionBean, actionContext, exception);
    after2.afterExecute(actionBean, actionContext, exception);
    action.postExecute(actionBean, actionContext, exception);

    replay(action);
    replay(request);
    replay(actionBean);
    replay(beanSource);
    replay(before1);
    replay(before2);
    replay(after1);
    replay(after2);

    ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
    delegate.setBeforeInterceptors(befores);
    delegate.setAfterInterceptors(afters);
    try
    {
      delegate.handleActionPerform(action, actionContext);
    }
    catch (IllegalStateException e)
    {
    }

    verify(action);
    verify(request);
    verify(actionBean);
    verify(beanSource);
    verify(before1);
    verify(before2);
    verify(after1);
    verify(after2);

  }

  @SuppressWarnings("unchecked")
  @Test
  public void testThrowOnAfter() throws Exception
  {

    AfterInterceptor after1 = createStrictMock(AfterInterceptor.class);
    AfterInterceptor after2 = createStrictMock(AfterInterceptor.class);
    Collection<AfterInterceptor> afters = new ArrayList<AfterInterceptor>();
    afters.add(after1);
    afters.add(after2);

    ControllerAction action = createStrictMock(ControllerAction.class);
    HttpServletRequest request = createStrictMock(HttpServletRequest.class);
    TestAction actionBean = createStrictMock(TestAction.class);
    ActionBeanSource beanSource = createStrictMock(ActionBeanSource.class);

    ActionContext actionContext = new TestContextImpl(request);

    // check the expected order of method invocation
    expect(action.getBeanSource()).andReturn(beanSource);
    expect(beanSource.createBean(actionContext)).andReturn(actionBean);
    action.preExecute(actionBean, actionContext);

    expect(action.getBeforeInterceptors()).andReturn(null);
    expect(action.executeController(actionBean, actionContext)).andReturn(null);
    expect(action.getAfterInterceptors()).andReturn(null);

    IllegalStateException exception = new IllegalStateException();
    after1.afterExecute(actionBean, actionContext, null);
    expectLastCall().andThrow(exception);

    // check that we continue - i.e we don't fall over in a heap because the after interceptor
    // failed
    after2.afterExecute(actionBean, actionContext, null);
    request.setAttribute(InfrastructureKeys.ACTION_BEAN, actionBean);
    action.postExecute(actionBean, actionContext, null);

    replay(action);
    replay(request);
    replay(actionBean);
    replay(beanSource);
    replay(after1);
    replay(after2);

    ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
    delegate.setAfterInterceptors(afters);
    delegate.handleActionPerform(action, actionContext);

    verify(action);
    verify(request);
    verify(actionBean);
    verify(beanSource);
    verify(after1);
    verify(after2);

  }
 
  @SuppressWarnings("unchecked")
  @Test
  public void testThrowClassCastOnAfter() throws Exception
  {

    AfterInterceptor after1 = createStrictMock(AfterInterceptor.class);
    AfterInterceptor after2 = createStrictMock(AfterInterceptor.class);
    Collection<AfterInterceptor> afters = new ArrayList<AfterInterceptor>();
    afters.add(after1);
    afters.add(after2);

    ControllerAction action = createStrictMock(ControllerAction.class);
    HttpServletRequest request = createStrictMock(HttpServletRequest.class);
    TestAction actionBean = createStrictMock(TestAction.class);
    ActionBeanSource beanSource = createStrictMock(ActionBeanSource.class);

    ActionContext actionContext = new TestContextImpl(request);

    // check the expected order of method invocation
    expect(action.getBeanSource()).andReturn(beanSource);
    expect(beanSource.createBean(actionContext)).andReturn(actionBean);
    action.preExecute(actionBean, actionContext);

    expect(action.getBeforeInterceptors()).andReturn(null);
    expect(action.executeController(actionBean, actionContext)).andReturn(null);
    expect(action.getAfterInterceptors()).andReturn(null);

    ClassCastException exception = new ClassCastException();
    after1.afterExecute(actionBean, actionContext, null);
    expectLastCall().andThrow(exception);

    replay(action);
    replay(request);
    replay(actionBean);
    replay(beanSource);
    replay(after1);
    replay(after2);

    ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
    delegate.setAfterInterceptors(afters);
   
    try
    {
    delegate.handleActionPerform(action, actionContext);
    }
    catch (ApplicationRuntimeException e)
    {
      assertTrue(e.getCause() instanceof ClassCastException);
    }

    verify(action);
    verify(request);
    verify(actionBean);
    verify(beanSource);
    verify(after1);
    verify(after2);

  }

  @Test
  public void testInvalidBeforeInterceptor() throws Exception
  {
    try
    {
      ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
      delegate.runBeforeInterceptor("should be Integer", new TypedBeforeInterceptor(), null);
      fail();
    }
    catch (ApplicationRuntimeException e)
    {
      assertEquals(e.getMessage(), "Action bean class java.lang.String is not compatible with " +
          "BeforeInterceptor implementation org.strecks.controller.impl.TypedBeforeInterceptor, " +
          "which is parameterized with the type class java.lang.Integer");
    }
  }

  @Test
  public void testInvalidAfterInterceptor() throws Exception
  {
    try
    {
      ControllerProcessorDelegateImpl delegate = new ControllerProcessorDelegateImpl();
      delegate.runAfterInterceptor("should be Integer", new TypedAfterInterceptor(), null, null);
      fail();
    }
    catch (ApplicationRuntimeException e)
    {
      assertEquals(e.getMessage(), "Action bean class java.lang.String is not compatible with " +
          "AfterInterceptor implementation org.strecks.controller.impl.TypedAfterInterceptor, " +
          "which is parameterized with the type class java.lang.Integer");
    }
  }

}
TOP

Related Classes of org.strecks.controller.TestControllerProcessorDelegateImpl

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.