/*
* 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 java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.ModuleConfig;
import org.strecks.context.impl.TestContextFactoryImpl;
import org.strecks.context.impl.TestContextImpl;
import org.strecks.controller.impl.ReadOnlyControllerAction;
import org.strecks.controller.impl.SimpleBindableForm;
import org.strecks.controller.impl.TestAction;
import org.strecks.form.controller.DelegatingForm;
import org.strecks.form.controller.WrappingForm;
import org.strecks.form.handler.FormWrapper;
import org.strecks.form.handler.FormPopulateSource;
import org.strecks.form.handler.FormPopulateSourceImpl;
import org.strecks.form.handler.FormValidationHandler;
import org.strecks.form.handler.FormValidationHandlerImpl;
import org.strecks.form.handler.ValidateBindFormWrapper;
import org.strecks.form.impl.SimpleStrutsForm;
import org.strecks.preprocess.RequestPreprocessor;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestControllerRequestProcessor
{
// Note we can't test processValidate() and processActionForm() since they still use the superclass, for which the
// dependencies must be resolved
@Test
public void testInit() throws ServletException
{
ModuleConfig config = createMock(ModuleConfig.class);
ServletContext context = createMock(ServletContext.class);
ActionServlet servlet = createMock(ActionServlet.class);
expect(servlet.getServletContext()).andReturn(context);
expect(config.getPrefix()).andReturn("prefix");
replay(config);
replay(context);
replay(servlet);
ControllerRequestProcessor controllerRequestProcessor = new ControllerRequestProcessor();
controllerRequestProcessor.init(servlet, config);
verify(config);
verify(context);
verify(servlet);
ControllerProcessorDelegate delegate = controllerRequestProcessor.getDelegate();
ActionCreator actionCreator = controllerRequestProcessor.getActionCreator();
assert null != delegate;
assert null != actionCreator;
}
@Test
public void testProcessCachedMessages() throws ServletException, IOException
{
RequestPreprocessor handler = createMock(RequestPreprocessor.class);
handler.preprocessRequest(null);
replay(handler);
ControllerRequestProcessor controllerRequestProcessor = new ControllerRequestProcessor();
controllerRequestProcessor.setRequestPreprocessor(handler);
controllerRequestProcessor.preProcessCachedMessages(null);
verify(handler);
}
@Test
public void testBasicProcessActionCreate() throws ServletException, IOException
{
ActionMapping mapping = createMock(ActionMapping.class);
expect(mapping.getType()).andReturn(TestAction.class.getName()).times(2);
replay(mapping);
ControllerRequestProcessor controllerRequestProcessor = new ControllerRequestProcessor();
controllerRequestProcessor.setActionCreator(new ActionCreatorImpl());
Action action1 = controllerRequestProcessor.processActionCreate(null, null, mapping);
Action action2 = controllerRequestProcessor.processActionCreate(null, null, mapping);
assert action1 != null;
assert action1 instanceof ReadOnlyControllerAction;
verify(mapping);
assert action1 != null;
assert action1 == action2;
assert controllerRequestProcessor.hasAction(TestAction.class.getName());
}
@Test
public void testProcessActionCreateInteractions() throws Exception
{
ActionMapping mapping = createMock(ActionMapping.class);
ActionCreatorImpl creator = createMock(ActionCreatorImpl.class);
expect(mapping.getType()).andReturn(TestAction.class.getName());
expect(creator.createAction(TestAction.class)).andReturn(new ReadOnlyControllerAction());
expect(mapping.getType()).andReturn(TestAction.class.getName());
replay(mapping);
replay(creator);
ControllerRequestProcessor controllerRequestProcessor = new ControllerRequestProcessor();
controllerRequestProcessor.setActionCreator(creator);
Action action1 = controllerRequestProcessor.processActionCreate(null, null, mapping);
Action action2 = controllerRequestProcessor.processActionCreate(null, null, mapping);
verify(mapping);
verify(creator);
assert action1 != null;
assert controllerRequestProcessor.hasAction(TestAction.class.getName());
// check that same instance is returned
assert action1 == action2;
}
/**
* Tests standard action processing
*/
@Test
public void testProcessRegularAction() throws Exception
{
Action action = createMock(Action.class);
ModuleConfig config = createMock(ModuleConfig.class);
ServletContext context = createMock(ServletContext.class);
ActionServlet servlet = createMock(ActionServlet.class);
expect(servlet.getServletContext()).andReturn(context);
expect(config.getPrefix()).andReturn("");
expect(action.execute(null, null, (HttpServletRequest) null, (HttpServletResponse) null)).andReturn(null)
.times(1);
replay(action);
replay(config);
replay(context);
replay(servlet);
ControllerRequestProcessor controllerRequestProcessor = new ControllerRequestProcessor();
controllerRequestProcessor.init(servlet, config);
controllerRequestProcessor.setActionContextFactory(new TestContextFactoryImpl());
controllerRequestProcessor.processActionPerform(null, null, action, null, null);
verify(action);
verify(config);
verify(context);
verify(servlet);
}
/**
* Tests controller action processing
*/
@Test
public void testProcessControllerAction() throws Exception
{
ModuleConfig config = createMock(ModuleConfig.class);
ServletContext context = createMock(ServletContext.class);
ActionServlet servlet = createMock(ActionServlet.class);
ReadOnlyControllerAction action = createMock(ReadOnlyControllerAction.class);
ControllerProcessorDelegate delegate = createMock(ControllerProcessorDelegate.class);
expect(servlet.getServletContext()).andReturn(context);
expect(config.getPrefix()).andReturn("prefix");
expect(delegate.handleActionPerform(action, new TestContextImpl())).andReturn(null).times(1);
replay(action);
replay(config);
replay(context);
replay(servlet);
ControllerRequestProcessor controllerRequestProcessor = new ControllerRequestProcessor();
controllerRequestProcessor.init(servlet, config);
controllerRequestProcessor.setDelegate(delegate);
controllerRequestProcessor.setActionContextFactory(new TestContextFactoryImpl());
controllerRequestProcessor.processActionPerform(null, null, action, null, null);
verify(action);
verify(config);
verify(context);
verify(servlet);
}
/**
* Tests null action form
*/
@Test
public void testPostProcessActionForm1() throws Exception
{
FormWrapper delegate = createMock(FormWrapper.class);
replay(delegate);
ControllerRequestProcessor processor = new ControllerRequestProcessor();
processor.setFormHandler(delegate);
processor.postProcessActionForm(null, null, null);
verify(delegate);
}
/**
* Tests standard Struts action form
*/
@Test
public void testPostProcessActionForm2() throws Exception
{
FormWrapper delegate = createMock(FormWrapper.class);
ActionForm form = new ActionForm()
{
private static final long serialVersionUID = 1L;
};
expect(delegate.wrapForm(form, null)).andReturn(form);
replay(delegate);
ControllerRequestProcessor processor = new ControllerRequestProcessor();
processor.setFormHandler(delegate);
processor.postProcessActionForm(null, null, form);
verify(delegate);
}
/**
* Tests interaction of form
*/
@Test
public void testValidBindingForm2() throws Exception
{
FormWrapper delegate = createMock(FormWrapper.class);
DelegatingForm form = createMock(DelegatingForm.class);
expect(delegate.wrapForm(form, null)).andReturn(form);
delegate.handleBindingForm(form, null);
delegate.handleValidForm(form, null);
replay(delegate);
ControllerRequestProcessor processor = new ControllerRequestProcessor();
processor.setFormHandler(delegate);
processor.postProcessActionForm(null, null, form);
verify(delegate);
}
/**
* Tests Form which is both <code>Validatable</code> and <code>Bindable</code>
*/
@Test
public void testValidBindingForm3() throws Exception
{
HttpServletRequest request = createMock(HttpServletRequest.class);
FormWrapper delegate = new ValidateBindFormWrapper();
SimpleBindableForm simpleBindableForm = new SimpleBindableForm();
ControllerRequestProcessor processor = new ControllerRequestProcessor();
processor.setFormHandler(delegate);
assert processor.postProcessActionForm(request, null, simpleBindableForm) instanceof WrappingForm;
}
/**
* Tests Form which is both <code>Validatable</code> and <code>Bindable</code>
*/
@Test
public void testValidBindingForm4() throws Exception
{
HttpServletRequest request = createMock(HttpServletRequest.class);
FormWrapper delegate = new ValidateBindFormWrapper();
ActionForm actionForm = new SimpleStrutsForm();
ControllerRequestProcessor processor = new ControllerRequestProcessor();
processor.setFormHandler(delegate);
assert (processor.postProcessActionForm(request, null, actionForm) == actionForm);
}
/**
* Checks prepopulate for returning raw form
*/
@Test
public void testPrePopulate2() throws Exception
{
FormPopulateSource delegate = new FormPopulateSourceImpl();
ActionForm actionForm = new SimpleStrutsForm();
DelegatingForm delegator = new DelegatingForm(actionForm);
ControllerRequestProcessor processor = new ControllerRequestProcessor();
processor.setFormPopulationSource(delegate);
assert actionForm == processor.prePopulate(delegator, null);
assert actionForm == processor.prePopulate(actionForm, null);
}
/**
* Checks preValidate for returning form passed in
*/
@Test
public void testPreValidate() throws Exception
{
FormValidationHandler delegate = new FormValidationHandlerImpl();
ActionForm actionForm = new SimpleStrutsForm();
DelegatingForm delegator = new DelegatingForm(actionForm);
ControllerRequestProcessor processor = new ControllerRequestProcessor();
processor.setFormValidationHandler(delegate);
assert delegator == processor.preValidate(delegator, null);
assert actionForm == processor.preValidate(actionForm, null);
}
}