/*
* 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.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 static org.testng.Assert.fail;
import java.util.AbstractList;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionServlet;
import org.strecks.constants.InfrastructureKeys;
import org.strecks.context.ActionContext;
import org.strecks.context.impl.TestContextImpl;
import org.strecks.controller.impl.SimplePage;
import org.strecks.controller.impl.TestAction;
import org.strecks.exceptions.ApplicationRuntimeException;
import org.strecks.injection.internal.InjectionAnnotationReader;
import org.strecks.injection.internal.InjectionWrapper;
import org.strecks.navigate.NavigationHolder;
import org.strecks.navigate.factory.DefaultNavigationHandlerFactory;
import org.strecks.navigate.handler.PageNavigationHandler;
import org.strecks.navigate.internal.impl.ActionWithNavigate;
import org.strecks.navigate.internal.impl.ActionWithNullReturningNavigate;
import org.strecks.page.PageForward;
import org.strecks.source.DefaultActionBeanSource;
import org.strecks.view.ActionForwardViewAdapter;
import org.strecks.view.ViewAdapter;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestNavigableControllerAction
{
@Test
public void testNewActionBean()
{
BaseControllerAction action = new SimpleControllerAction();
action.setBeanSource(new DefaultActionBeanSource(TestAction.class));
Object newActionBean = action.getBeanSource().createBean(null);
assert newActionBean instanceof TestAction;
}
@Test
public void testDuffNewActionBean()
{
try
{
BaseControllerAction action = new SimpleControllerAction();
action.setBeanSource(new DefaultActionBeanSource(AbstractList.class));
action.getBeanSource().createBean(null);
fail("Should have caught UnrecoverableRuntimeException");
}
catch (ApplicationRuntimeException e)
{
e.printStackTrace();
}
}
@Test
public void testExecute() throws Exception
{
HttpServletRequest request = createMock(HttpServletRequest.class);
ActionServlet servlet = createMock(ActionServlet.class);
ActionContext actionContext = new TestContextImpl(request);
replay(request);
replay(servlet);
SimpleControllerAction action = new SimpleControllerAction();
action.setServlet(servlet);
action.setBeanSource(new DefaultActionBeanSource(TestAction.class));
action.executeController(new TestAction(), actionContext);
action.executeController(new TestAction(), actionContext);
Assert.assertEquals(action.getCallCount(), 2);
// verify that a different action bean is being used
Assert.assertEquals(action.getActionBeanCount(), 1);
verify(request);
verify(servlet);
}
@Test
public void testDoInjection() throws Exception
{
InjectionAnnotationReader c = new InjectionAnnotationReader();
InjectionAction actionBean = new InjectionAction();
c.readAnnotations(actionBean.getClass());
Map<String, InjectionWrapper> injectionHandlers = c.getInjectionMap();
HttpServletRequest request = createMock(HttpServletRequest.class);
ServletContext context = createMock(ServletContext.class);
// this will be called when the injection context is inspected
expect(request.getParameter("param")).andReturn("3");
replay(request);
SimpleControllerAction action = new SimpleControllerAction();
action.setInjectionHandlers(injectionHandlers);
action.doInjection(actionBean, new TestContextImpl(request, null, context, null, null));
// prove that the value has been injected
assert actionBean.getParam().equals(3);
verify(request);
}
@Test(expectedExceptions=IllegalArgumentException.class)
public void testFindForwardNavigationHolderNull()
{
SimpleControllerAction action = new SimpleControllerAction();
ActionWithNavigate bean = new ActionWithNavigate();
action.findActionForward(bean, new TestContextImpl());
}
@Test
public void testFindForward() throws Exception
{
HttpServletRequest request = createStrictMock(HttpServletRequest.class);
request.setAttribute(InfrastructureKeys.PAGE_BEAN, new SimplePage());
replay(request);
SimpleControllerAction action = new SimpleControllerAction();
ActionWithNavigate bean = new ActionWithNavigate();
NavigationHolder navigationHolder = new NavigationHolder(new PageNavigationHandler(),
new DefaultNavigationHandlerFactory(), bean.getClass().getMethod("nextPage"));
action.setNavigationHolder(navigationHolder);
ViewAdapter viewAdapter = action.findActionForward(bean, new TestContextImpl(request));
ActionForwardViewAdapter va = (ActionForwardViewAdapter) viewAdapter;
assert (va.getActionForward() instanceof PageForward);
verify(request);
}
@Test
public void testFindForwardNullReturningAction() throws Exception
{
HttpServletRequest request = createStrictMock(HttpServletRequest.class);
replay(request);
SimpleControllerAction action = new SimpleControllerAction();
ActionWithNullReturningNavigate bean = new ActionWithNullReturningNavigate();
NavigationHolder navigationHolder = new NavigationHolder(new PageNavigationHandler(),
new DefaultNavigationHandlerFactory(), bean.getClass().getMethod("nextPage"));
action.setNavigationHolder(navigationHolder);
assert null == action.findActionForward(bean, new TestContextImpl(request));
verify(request);
}
}