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.createMock;
018: import static org.easymock.classextension.EasyMock.replay;
019: import static org.easymock.classextension.EasyMock.verify;
020:
021: import java.util.Map;
022:
023: import javax.servlet.http.HttpServletRequest;
024:
025: import org.strecks.bind.internal.BindConvertInfo;
026: import org.strecks.form.controller.DelegatingForm;
027: import org.strecks.form.impl.SimpleStrutsForm;
028: import org.testng.annotations.Test;
029:
030: /**
031: * @author Phil Zoio
032: */
033: public class TestValidBindFormHandler {
034:
035: @Test
036: public void testGetFormClass() {
037:
038: SimpleStrutsForm form = new SimpleStrutsForm();
039: DelegatingForm delegating = new DelegatingForm(form);
040:
041: ValidateBindFormWrapper validateBindFormHandler = new ValidateBindFormWrapper();
042: Class formClass1 = validateBindFormHandler
043: .getFormClass(delegating);
044: Class formClass2 = validateBindFormHandler.getFormClass(form);
045: assert formClass1 == formClass2;
046:
047: assert (formClass1 == SimpleStrutsForm.class);
048: }
049:
050: @Test
051: public void testGetReadableObject() {
052:
053: SimpleStrutsForm form = new SimpleStrutsForm();
054: DelegatingForm delegating = new DelegatingForm(form);
055:
056: ValidateBindFormWrapper validateBindFormHandler = new ValidateBindFormWrapper();
057: Object readable1 = validateBindFormHandler
058: .getReadableObject(delegating);
059: Object readable2 = validateBindFormHandler
060: .getReadableObject(form);
061: assert readable1 == readable2;
062:
063: assert (readable1 instanceof SimpleStrutsForm);
064: }
065:
066: @Test
067: public void testHandleValidForm() {
068:
069: SimpleStrutsForm form = new SimpleStrutsForm();
070: DelegatingForm delegating = new DelegatingForm(form);
071:
072: HttpServletRequest request = createMock(HttpServletRequest.class);
073:
074: replay(request);
075:
076: ValidateBindFormWrapper delegate = new ValidateBindFormWrapper();
077: delegate.handleValidForm(delegating, request);
078:
079: assert null != delegating.getValidationInfo();
080: assert null == delegating.getBindConvertInfo();
081:
082: assert delegate.getBindConvertMap().size() == 0;
083: assert delegate.getValidationHandlerMap().size() == 1;
084:
085: verify(request);
086: }
087:
088: @Test
089: public void testHandleBindingForm() {
090:
091: SimpleStrutsForm form = new SimpleStrutsForm();
092: DelegatingForm delegating = new DelegatingForm(form);
093: HttpServletRequest request = createMock(HttpServletRequest.class);
094:
095: replay(request);
096:
097: ValidateBindFormWrapper delegate = new ValidateBindFormWrapper();
098: delegate.handleBindingForm(delegating, request);
099:
100: assert null != delegating.getBindConvertInfo();
101: assert null == delegating.getValidationInfo();
102:
103: Map<Class, BindConvertInfo> bindHandlerMap = delegate
104: .getBindConvertMap();
105: assert bindHandlerMap.size() == 1;
106:
107: assert delegate.getValidationHandlerMap().size() == 0;
108:
109: verify(request);
110: }
111:
112: @Test
113: public void testHandleFormMultiTimes() {
114:
115: SimpleStrutsForm form1 = new SimpleStrutsForm();
116: DelegatingForm delegator1 = new DelegatingForm(form1);
117: SimpleStrutsForm form2 = new SimpleStrutsForm();
118: DelegatingForm delegator2 = new DelegatingForm(form2);
119:
120: HttpServletRequest request = createMock(HttpServletRequest.class);
121:
122: replay(request);
123:
124: FormWrapper delegate = new ValidateBindFormWrapper();
125: delegate.handleValidForm(delegator1, request);
126: delegate.handleValidForm(delegator2, request);
127: delegate.handleBindingForm(delegator1, request);
128: delegate.handleBindingForm(delegator2, request);
129:
130: assert null != delegator1.getBindConvertInfo();
131: assert null != delegator2.getValidationInfo();
132:
133: // now check that the same instances are used for each invocation
134: assert delegator1.getBindConvertInfo() == delegator2
135: .getBindConvertInfo();
136: assert delegator1.getValidationInfo() == delegator2
137: .getValidationInfo();
138:
139: verify(request);
140: }
141:
142: }
|