001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.form.handler;
016:
017: import static org.easymock.classextension.EasyMock.createStrictMock;
018: import static org.easymock.classextension.EasyMock.replay;
019: import static org.easymock.classextension.EasyMock.verify;
020:
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpSession;
023:
024: import org.apache.struts.Globals;
025: import org.apache.struts.action.ActionErrors;
026: import org.apache.struts.action.ActionMapping;
027: import org.easymock.classextension.EasyMock;
028: import org.strecks.form.impl.SimpleStrutsForm;
029: import org.testng.annotations.Test;
030:
031: /**
032: * @author Phil Zoio
033: */
034: public class TestFormValidationHandlerImpl {
035:
036: @Test
037: public void testPreValidate() {
038:
039: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
040: ActionMapping mapping = createStrictMock(ActionMapping.class);
041: HttpSession session = createStrictMock(HttpSession.class);
042:
043: replay(mapping);
044: replay(request);
045: replay(session);
046:
047: FormValidationHandlerImpl handler = new FormValidationHandlerImpl();
048: SimpleStrutsForm form = new SimpleStrutsForm();
049: assert handler.preValidate(request, form) == form;
050:
051: verify(mapping);
052: verify(request);
053: verify(session);
054: }
055:
056: @Test
057: public void testPreValidateNoValidate() {
058:
059: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
060: ActionMapping mapping = createStrictMock(ActionMapping.class);
061: replay(mapping);
062: replay(request);
063:
064: FormValidationHandlerImpl delegate = new FormValidationHandlerImpl();
065: assert null == delegate.preValidate(request, null);
066:
067: verify(mapping);
068: verify(request);
069: }
070:
071: @Test
072: public void testPostValidateFailed() {
073:
074: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
075: HttpSession session = createStrictMock(HttpSession.class);
076:
077: final ActionErrors actionErrors = new ActionErrors();
078: EasyMock.expect(request.getAttribute(Globals.ERROR_KEY))
079: .andReturn(actionErrors);
080: EasyMock.expect(request.getSession()).andReturn(session);
081: session.setAttribute(Globals.ERROR_KEY, actionErrors);
082:
083: replay(request);
084: replay(session);
085:
086: FormValidationHandlerImpl delegate = new FormValidationHandlerImpl();
087: delegate.postValidate(request, null, false);
088:
089: verify(request);
090: verify(session);
091: }
092:
093: @Test
094: public void testPostValidateSucceeded() {
095:
096: HttpServletRequest request = createStrictMock(HttpServletRequest.class);
097: ActionMapping mapping = createStrictMock(ActionMapping.class);
098:
099: replay(mapping);
100: replay(request);
101:
102: FormValidationHandlerImpl delegate = new FormValidationHandlerImpl();
103: delegate.postValidate(request, mapping, true);
104:
105: verify(mapping);
106: verify(request);
107:
108: }
109: }
|