/*
* 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.replay;
import static org.easymock.classextension.EasyMock.verify;
import static org.testng.Assert.fail;
import java.lang.reflect.Method;
import java.util.AbstractList;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionServlet;
import org.strecks.action.basic.BasicController;
import org.strecks.context.ActionContext;
import org.strecks.context.impl.TestContextImpl;
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.lifecycle.impl.ActionWithLifecycleMethods;
import org.strecks.lifecycle.impl.LifecycleMethodReader;
import org.strecks.source.DefaultActionBeanSource;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestBaseControllerAction
{
@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
public void testInitMethod() throws Exception
{
LifecycleMethodReader c = new LifecycleMethodReader();
ActionWithLifecycleMethods actionBean = new ActionWithLifecycleMethods();
c.readAnnotations(actionBean.getClass());
final Method initMethod = c.getInitMethod();
SimpleControllerAction action = new SimpleControllerAction();
action.setInitMethod(initMethod);
action.doInitMethod(actionBean);
// prove that the value has been injected
Assert.assertEquals(actionBean.getInitCount(), 1);
}
@Test
public void testNoLifecycleMethod() throws Exception
{
ActionWithLifecycleMethods actionBean = new ActionWithLifecycleMethods();
SimpleControllerAction action = new SimpleControllerAction();
action.doInitMethod(actionBean);
Assert.assertEquals(actionBean.getInitCount(), 0);
Assert.assertEquals(actionBean.getCloseCount(), 0);
}
@Test
public void testCloseMethod() throws Exception
{
LifecycleMethodReader c = new LifecycleMethodReader();
ActionWithLifecycleMethods actionBean = new ActionWithLifecycleMethods();
c.readAnnotations(actionBean.getClass());
final Method closeMethod = c.getCloseMethod();
SimpleControllerAction action = new SimpleControllerAction();
action.setCloseMethod(closeMethod);
action.doCloseMethod(actionBean);
// prove that the value has been injected
Assert.assertEquals(actionBean.getCloseCount(), 1);
}
@Test
public void testHasNoErrors()
{
HttpServletRequest request = createMock(HttpServletRequest.class);
expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(null);
replay(request);
BasicController controller = new BasicController();
assert false == controller.hasErrors(new TestContextImpl(request));
verify(request);
}
@Test
public void testHasEmptyErrors()
{
HttpServletRequest request = createMock(HttpServletRequest.class);
expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(new ActionErrors());
replay(request);
BasicController controller = new BasicController();
assert false == controller.hasErrors(new TestContextImpl(request));
verify(request);
}
@Test
public void testHasErrors()
{
HttpServletRequest request = createMock(HttpServletRequest.class);
ActionErrors actionErrors = new ActionErrors();
actionErrors.add("property", new ActionMessage("key"));
expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(actionErrors);
replay(request);
BasicController controller = new BasicController();
assert true == controller.hasErrors(new TestContextImpl(request));
verify(request);
}
}