001: package org.strecks.form.controller;
002:
003: import static org.easymock.EasyMock.expect;
004: import static org.easymock.classextension.EasyMock.createStrictMock;
005: import static org.easymock.classextension.EasyMock.replay;
006: import static org.easymock.classextension.EasyMock.verify;
007:
008: import java.sql.Date;
009: import java.util.HashMap;
010: import java.util.Iterator;
011: import java.util.Map;
012: import java.util.Set;
013:
014: import org.strecks.converter.ConversionState;
015: import org.strecks.converter.Converter;
016: import org.strecks.converter.handler.DefaultConversionHandler;
017: import org.strecks.exceptions.ApplicationConfigurationException;
018: import org.strecks.exceptions.ConversionException;
019: import org.strecks.exceptions.ConversionRuntimeException;
020: import org.strecks.form.impl.SimpleStrutsForm;
021: import org.strecks.validator.internal.MethodValidators;
022: import org.strecks.validator.internal.OrderedProperty;
023: import org.strecks.validator.internal.ValidationInfo;
024: import org.strecks.validator.strategy.ValidationHandler;
025: import org.testng.Assert;
026: import org.testng.annotations.BeforeMethod;
027: import org.testng.annotations.Test;
028:
029: /**
030: * @author Phil Zoio
031: */
032: public class TestDelegatingFormValidation {
033:
034: private SimpleStrutsForm simpleForm;
035:
036: private ValidationHandler handler;
037:
038: private DelegatingForm form;
039:
040: @BeforeMethod
041: public void setUp() {
042:
043: simpleForm = new SimpleStrutsForm();
044: handler = createStrictMock(ValidationHandler.class);
045:
046: }
047:
048: @SuppressWarnings("unchecked")
049: @Test
050: public void testValidate() throws ConversionException {
051:
052: Map validators = createStrictMock(Map.class);
053: Set keys = createStrictMock(Set.class);
054: Iterator iterator = createStrictMock(Iterator.class);
055: OrderedProperty op = createStrictMock(OrderedProperty.class);
056: MethodValidators methodValidators = createStrictMock(MethodValidators.class);
057: Converter converter = createStrictMock(Converter.class);
058:
059: Date sqlDate = Date.valueOf("1999-12-12");
060: simpleForm.setDateValue("1999-12-12");
061: ValidationInfo vi = new ValidationInfo(validators, handler,
062: new DefaultConversionHandler());
063:
064: expect(validators.keySet()).andReturn(keys);
065: expect(keys.iterator()).andReturn(iterator);
066: expect(iterator.hasNext()).andReturn(true);
067: expect(iterator.next()).andReturn(op);
068: expect(validators.get(op)).andReturn(methodValidators);
069: expect(methodValidators.getRequiresConversion())
070: .andReturn(true);
071: expect(methodValidators.getConverter()).andReturn(converter);
072: expect(op.getPropertyName()).andReturn("dateValue");
073: expect(converter.toTargetType("1999-12-12")).andReturn(sqlDate);
074: expect(methodValidators.getConverterType()).andReturn(
075: sqlDate.getClass());
076: expect(iterator.hasNext()).andReturn(false);
077:
078: HashMap<String, Object> map = new HashMap<String, Object>();
079: map.put("dateValue", sqlDate);
080:
081: // expect validationStrategy to be called
082: expect(handler.validate(vi, simpleForm, null, null, map))
083: .andReturn(null);
084:
085: replay(handler);
086: replay(validators);
087: replay(keys);
088: replay(iterator);
089: replay(op);
090: replay(methodValidators);
091: replay(converter);
092:
093: form = new DelegatingForm(simpleForm);
094: form.setValidationInfo(vi);
095: form.validate(null, null);
096:
097: verify(handler);
098: verify(validators);
099: verify(keys);
100: verify(iterator);
101: verify(op);
102: verify(methodValidators);
103: verify(converter);
104:
105: }
106:
107: @SuppressWarnings("unchecked")
108: @Test
109: public void testBadType() throws ConversionException {
110:
111: Map validators = createStrictMock(Map.class);
112: Set keys = createStrictMock(Set.class);
113: Iterator iterator = createStrictMock(Iterator.class);
114: OrderedProperty op = createStrictMock(OrderedProperty.class);
115: MethodValidators methodValidators = createStrictMock(MethodValidators.class);
116: Converter converter = createStrictMock(Converter.class);
117:
118: Date sqlDate = Date.valueOf("1999-12-12");
119: simpleForm.setDateValue("1999-12-12");
120: ValidationInfo vi = new ValidationInfo(validators, handler,
121: new DefaultConversionHandler());
122:
123: expect(validators.keySet()).andReturn(keys);
124: expect(keys.iterator()).andReturn(iterator);
125: expect(iterator.hasNext()).andReturn(true);
126: expect(iterator.next()).andReturn(op);
127: expect(validators.get(op)).andReturn(methodValidators);
128: expect(methodValidators.getRequiresConversion())
129: .andReturn(true);
130: expect(methodValidators.getConverter()).andReturn(converter);
131: expect(op.getPropertyName()).andReturn("dateValue");
132: expect(converter.toTargetType("1999-12-12")).andReturn(sqlDate);
133: expect(methodValidators.getConverterType()).andReturn(
134: String.class);
135:
136: replay(handler);
137: replay(validators);
138: replay(keys);
139: replay(iterator);
140: replay(op);
141: replay(methodValidators);
142: replay(converter);
143:
144: form = new DelegatingForm(simpleForm);
145: form.setValidationInfo(vi);
146:
147: try {
148: form.validate(null, null);
149: } catch (ApplicationConfigurationException e) {
150: Assert
151: .assertEquals(
152: e.getMessage(),
153: "Supplied value of dateValue in class "
154: + "org.strecks.form.impl.SimpleStrutsForm converted to class java.sql.Date "
155: + "and not the class java.lang.String expected by one or more validators. "
156: + "Check that the property contains an appropriate converter in its getter method");
157: }
158:
159: verify(handler);
160: verify(validators);
161: verify(keys);
162: verify(iterator);
163: verify(op);
164: verify(methodValidators);
165: verify(converter);
166:
167: }
168:
169: @SuppressWarnings("unchecked")
170: @Test
171: public void testValidateNull() throws ConversionException {
172:
173: Map validators = createStrictMock(Map.class);
174: Set keys = createStrictMock(Set.class);
175: Iterator iterator = createStrictMock(Iterator.class);
176: OrderedProperty op = createStrictMock(OrderedProperty.class);
177: MethodValidators methodValidators = createStrictMock(MethodValidators.class);
178: Converter converter = createStrictMock(Converter.class);
179:
180: ValidationInfo vi = new ValidationInfo(validators, handler,
181: new DefaultConversionHandler());
182:
183: expect(validators.keySet()).andReturn(keys);
184: expect(keys.iterator()).andReturn(iterator);
185: expect(iterator.hasNext()).andReturn(true);
186: expect(iterator.next()).andReturn(op);
187: expect(validators.get(op)).andReturn(methodValidators);
188: expect(methodValidators.getRequiresConversion())
189: .andReturn(true);
190: expect(methodValidators.getConverter()).andReturn(converter);
191: expect(op.getPropertyName()).andReturn("dateValue");
192: expect(converter.toTargetType(null)).andReturn(null);
193: expect(iterator.hasNext()).andReturn(false);
194:
195: HashMap<String, Object> map = new HashMap<String, Object>();
196: map.put("dateValue", ConversionState.NULL);
197:
198: // expect validationStrategy to be called
199: expect(handler.validate(vi, simpleForm, null, null, map))
200: .andReturn(null);
201:
202: replay(handler);
203: replay(validators);
204: replay(keys);
205: replay(iterator);
206: replay(op);
207: replay(methodValidators);
208: replay(converter);
209:
210: form = new DelegatingForm(simpleForm);
211: form.setValidationInfo(vi);
212: form.validate(null, null);
213:
214: verify(handler);
215: verify(validators);
216: verify(keys);
217: verify(iterator);
218: verify(op);
219: verify(methodValidators);
220: verify(converter);
221:
222: }
223:
224: @SuppressWarnings("unchecked")
225: @Test
226: public void testConversionFailure() throws ConversionException {
227:
228: Map validators = createStrictMock(Map.class);
229: Set keys = createStrictMock(Set.class);
230: Iterator iterator = createStrictMock(Iterator.class);
231: OrderedProperty op = createStrictMock(OrderedProperty.class);
232: MethodValidators methodValidators = createStrictMock(MethodValidators.class);
233: Converter converter = createStrictMock(Converter.class);
234:
235: ValidationInfo vi = new ValidationInfo(validators, handler,
236: new DefaultConversionHandler());
237:
238: expect(validators.keySet()).andReturn(keys);
239: expect(keys.iterator()).andReturn(iterator);
240: expect(iterator.hasNext()).andReturn(true);
241: expect(iterator.next()).andReturn(op);
242: expect(validators.get(op)).andReturn(methodValidators);
243: expect(methodValidators.getRequiresConversion())
244: .andReturn(true);
245: expect(methodValidators.getConverter()).andReturn(converter);
246: expect(op.getPropertyName()).andReturn("dateValue");
247: expect(converter.toTargetType(null)).andThrow(
248: new ConversionRuntimeException(null));
249: expect(iterator.hasNext()).andReturn(false);
250:
251: HashMap<String, Object> map = new HashMap<String, Object>();
252: map.put("dateValue", ConversionState.FAILURE);
253:
254: // expect validationStrategy to be called
255: expect(handler.validate(vi, simpleForm, null, null, map))
256: .andReturn(null);
257:
258: replay(handler);
259: replay(validators);
260: replay(keys);
261: replay(iterator);
262: replay(op);
263: replay(methodValidators);
264: replay(converter);
265:
266: form = new DelegatingForm(simpleForm);
267: form.setValidationInfo(vi);
268: form.validate(null, null);
269:
270: verify(handler);
271: verify(validators);
272: verify(keys);
273: verify(iterator);
274: verify(op);
275: verify(methodValidators);
276: verify(converter);
277:
278: }
279:
280: }
|