/*
* 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.form.handler;
import static org.easymock.classextension.EasyMock.createStrictMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.easymock.classextension.EasyMock;
import org.strecks.form.impl.SimpleStrutsForm;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestFormValidationHandlerImpl
{
@Test
public void testPreValidate()
{
HttpServletRequest request = createStrictMock(HttpServletRequest.class);
ActionMapping mapping = createStrictMock(ActionMapping.class);
HttpSession session = createStrictMock(HttpSession.class);
replay(mapping);
replay(request);
replay(session);
FormValidationHandlerImpl handler = new FormValidationHandlerImpl();
SimpleStrutsForm form = new SimpleStrutsForm();
assert handler.preValidate(request, form) == form;
verify(mapping);
verify(request);
verify(session);
}
@Test
public void testPreValidateNoValidate()
{
HttpServletRequest request = createStrictMock(HttpServletRequest.class);
ActionMapping mapping = createStrictMock(ActionMapping.class);
replay(mapping);
replay(request);
FormValidationHandlerImpl delegate = new FormValidationHandlerImpl();
assert null == delegate.preValidate(request, null);
verify(mapping);
verify(request);
}
@Test
public void testPostValidateFailed()
{
HttpServletRequest request = createStrictMock(HttpServletRequest.class);
HttpSession session = createStrictMock(HttpSession.class);
final ActionErrors actionErrors = new ActionErrors();
EasyMock.expect(request.getAttribute(Globals.ERROR_KEY)).andReturn(actionErrors);
EasyMock.expect(request.getSession()).andReturn(session);
session.setAttribute(Globals.ERROR_KEY, actionErrors);
replay(request);
replay(session);
FormValidationHandlerImpl delegate = new FormValidationHandlerImpl();
delegate.postValidate(request, null, false);
verify(request);
verify(session);
}
@Test
public void testPostValidateSucceeded()
{
HttpServletRequest request = createStrictMock(HttpServletRequest.class);
ActionMapping mapping = createStrictMock(ActionMapping.class);
replay(mapping);
replay(request);
FormValidationHandlerImpl delegate = new FormValidationHandlerImpl();
delegate.postValidate(request, mapping, true);
verify(mapping);
verify(request);
}
}