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.validator.internal;
016:
017: import static org.easymock.EasyMock.expect;
018: import static org.easymock.classextension.EasyMock.createStrictMock;
019: import static org.easymock.classextension.EasyMock.replay;
020: import static org.easymock.classextension.EasyMock.verify;
021: import static org.testng.Assert.*;
022:
023: import java.lang.reflect.Method;
024: import java.util.ArrayList;
025: import java.util.Collections;
026: import java.util.List;
027: import java.util.Map;
028:
029: import org.strecks.bind.internal.BindConvertInfo;
030: import org.strecks.converter.Converter;
031: import org.strecks.converter.SafeBeanUtilsConverter;
032: import org.strecks.converter.handler.ConversionHandler;
033: import org.strecks.converter.handler.DefaultConversionHandler;
034: import org.strecks.exceptions.ApplicationConfigurationException;
035: import org.strecks.form.controller.BadlyTypedValidatorForm;
036: import org.strecks.validator.IntegerValidator;
037: import org.strecks.validator.MaxLengthValidator;
038: import org.strecks.validator.Validator;
039: import org.strecks.validator.annotation.ValidateInteger;
040: import org.strecks.validator.internal.impl.BeanWithGetterValidator;
041: import org.strecks.validator.internal.impl.BeanWithNoGetter;
042: import org.strecks.validator.internal.impl.BeanWithNoValidators;
043: import org.strecks.validator.message.DefaultMessageParameterProvider;
044: import org.testng.Assert;
045: import org.testng.annotations.Test;
046:
047: /**
048: * @author Phil Zoio
049: */
050: public class TestValidationAnnotationReaderImpl {
051:
052: @Test
053: public void testNewMethodValidatorsNoConversion() throws Exception {
054: Converter converter = createStrictMock(Converter.class);
055:
056: List<ValidatorWrapper> list = new ArrayList<ValidatorWrapper>();
057:
058: ValidatorWrapper v1 = getNonConvertableWrapper(String.class);
059: ValidatorWrapper v2 = getNonConvertableWrapper(Object.class);
060:
061: list.add(v1);
062: list.add(v2);
063:
064: replay(converter);
065:
066: ValidationAnnotationReader reader = new ValidationAnnotationReader();
067: MethodValidators methodValidators = reader.newMethodValidators(
068: this .getClass(), new OrderedProperty("prop", 1), list,
069: converter);
070: assert !methodValidators.getRequiresConversion();
071: assert converter == methodValidators.getConverter();
072:
073: verify(converter);
074: }
075:
076: @Test
077: public void testNewMethodValidatorsWithConvesion() throws Exception {
078:
079: Converter converter = createStrictMock(Converter.class);
080:
081: List<ValidatorWrapper> list = new ArrayList<ValidatorWrapper>();
082:
083: ValidatorWrapper v1 = getConvertableWrapper(Integer.class);
084: ValidatorWrapper v2 = getConvertableWrapper(Number.class);
085: ValidatorWrapper v3 = getConvertableWrapper(Object.class);
086:
087: list.add(v1);
088: list.add(v2);
089: list.add(v3);
090:
091: // record expectation
092: converter.setTargetClass(Integer.class);
093:
094: replay(converter);
095:
096: ValidationAnnotationReader reader = new ValidationAnnotationReader();
097: MethodValidators methodValidators = reader.newMethodValidators(
098: this .getClass(), new OrderedProperty("prop", 1), list,
099: converter);
100: assert methodValidators.getRequiresConversion();
101: assert converter == methodValidators.getConverter();
102:
103: verify(converter);
104: }
105:
106: @Test
107: public void testAddValidator() throws Exception {
108: ValidatorWrapper wrapper = createStrictMock(ValidatorWrapper.class);
109: ValidationAnnotationReader reader = new ValidationAnnotationReader();
110:
111: List<ValidatorWrapper> validators = reader.addValidator(null,
112: wrapper);
113: assert validators != null;
114: assertEquals(validators.size(), 1);
115: assert validators.iterator().next() == wrapper;
116: }
117:
118: @Test
119: public void testAddValidatorEmpty() throws Exception {
120: ValidatorWrapper wrapper = createStrictMock(ValidatorWrapper.class);
121: ValidationAnnotationReader reader = new ValidationAnnotationReader();
122: List<ValidatorWrapper> validatorsIn = new ArrayList<ValidatorWrapper>();
123: List<ValidatorWrapper> validatorsOut = reader.addValidator(
124: validatorsIn, wrapper);
125:
126: assert validatorsIn == validatorsOut;
127:
128: assert validatorsOut != null;
129: assertEquals(validatorsOut.size(), 1);
130: assert validatorsOut.iterator().next() == wrapper;
131: }
132:
133: @Test
134: public void testGetConversionHandler() throws Exception {
135: BindConvertInfo bci = createStrictMock(BindConvertInfo.class);
136: ConversionHandler conversionHandler = createStrictMock(ConversionHandler.class);
137:
138: ValidationAnnotationReader reader = new ValidationAnnotationReader();
139:
140: expect(bci.getConversionHandler()).andReturn(conversionHandler);
141: replay(bci);
142: replay(conversionHandler);
143:
144: assert reader.getConversionHandler(bci) == conversionHandler;
145:
146: verify(bci);
147: verify(conversionHandler);
148: }
149:
150: @Test
151: public void testGetConversionHandlerNull() throws Exception {
152: ValidationAnnotationReader reader = new ValidationAnnotationReader();
153: assert reader.getConversionHandler(null) instanceof DefaultConversionHandler;
154: }
155:
156: @Test
157: public void testGetConverterNull() throws Exception {
158: ValidationAnnotationReader reader = new ValidationAnnotationReader();
159: Converter converter = reader.getConverter(this .getClass(),
160: "prop", null);
161: assert converter instanceof SafeBeanUtilsConverter;
162: }
163:
164: @SuppressWarnings("unchecked")
165: @Test
166: public void testGetConverterNone() throws Exception {
167: BindConvertInfo info = createStrictMock(BindConvertInfo.class);
168: Map converterMap = createStrictMock(Map.class);
169: Converter converter = createStrictMock(Converter.class);
170:
171: ValidationAnnotationReader reader = new ValidationAnnotationReader();
172:
173: expect(info.getConverterMap()).andReturn(converterMap);
174: expect(converterMap.get("prop")).andReturn(converter);
175:
176: replay(info);
177: replay(converterMap);
178:
179: assert converter == reader.getConverter(this .getClass(),
180: "prop", info);
181:
182: verify(info);
183: verify(converterMap);
184: }
185:
186: @Test
187: public void testGetGetter() throws Exception {
188: ValidationAnnotationReader reader = new ValidationAnnotationReader();
189: Method setter = BeanWithGetterValidator.class.getMethod(
190: "setProperty", new Class[] { String.class });
191:
192: assert null != reader.getGetter(setter);
193: }
194:
195: @Test
196: public void testGetNoGetter() throws Exception {
197: ValidationAnnotationReader reader = new ValidationAnnotationReader();
198: Method setter = BeanWithNoGetter.class.getMethod(
199: "setStringProperty", new Class[] { String.class });
200:
201: try {
202: reader.getGetter(setter);
203: Assert.fail("Should have caught "
204: + ApplicationConfigurationException.class);
205: } catch (ApplicationConfigurationException e) {
206:
207: Assert
208: .assertEquals(
209: e.getMessage(),
210: "Setter method public void org.strecks.validator.internal.impl.BeanWithNoGetter.setStringProperty(java.lang.String) "
211: + "has no corresponding getter method");
212: }
213: }
214:
215: @Test
216: public void testValidateWithGetterMethodAnnotation() {
217:
218: try {
219: BeanWithGetterValidator bean = new BeanWithGetterValidator();
220: ValidationAnnotationReader reader = new ValidationAnnotationReader();
221: reader.readValidationHandlers(bean, null);
222: Assert.fail("Should have caught "
223: + ApplicationConfigurationException.class);
224: } catch (ApplicationConfigurationException e) {
225: e.printStackTrace();
226: }
227: }
228:
229: @Test
230: public void testBadlyTypedValidatorMethod() throws Exception {
231:
232: try {
233: BadlyTypedValidatorForm bean = new BadlyTypedValidatorForm();
234:
235: Method method = bean.getClass().getMethod(
236: "getRequiredInteger");
237: MaxLengthValidator validator = new MaxLengthValidator();
238:
239: ValidationAnnotationReader reader = new ValidationAnnotationReader();
240: reader
241: .checkValidatorType("propertyName", method,
242: validator.getClass(), Validator.class,
243: ValidateInteger.class);
244:
245: Assert.fail("Should have caught "
246: + ApplicationConfigurationException.class);
247:
248: } catch (ApplicationConfigurationException e) {
249: Assert
250: .assertEquals(
251: e.getMessage(),
252: "Class org.strecks.form.controller.BadlyTypedValidatorForm has property propertyName whose return type java.lang.Integer does not match the type java.lang.String of the validator used by annotation @ValidateInteger");
253:
254: }
255: }
256:
257: @Test
258: public void testBeanWithNoAnnotations() throws Exception {
259: ValidationAnnotationReader reader = new ValidationAnnotationReader();
260: ValidationInfo info = reader.readValidationHandlers(
261: new BeanWithNoValidators(), null);
262:
263: Map<OrderedProperty, MethodValidators> map = info
264: .getValidators();
265: assert map != null;
266: assert map.size() == 0;
267: }
268:
269: private ValidatorWrapper getConvertableWrapper(Class<?> type) {
270: DefaultMessageParameterProvider provider = new DefaultMessageParameterProvider();
271: ValidatorWrapper wrapper = new ValidatorWrapper("key1", 20,
272: Collections.emptyList(), new IntegerValidator(), this
273: .getClass().getMethods()[0], provider, true,
274: type);
275: return wrapper;
276: }
277:
278: private ValidatorWrapper getNonConvertableWrapper(Class<?> type) {
279: DefaultMessageParameterProvider provider = new DefaultMessageParameterProvider();
280: ValidatorWrapper wrapper = new ValidatorWrapper("key1", 20,
281: Collections.emptyList(), new IntegerValidator(), this
282: .getClass().getMethods()[0], provider);
283: return wrapper;
284: }
285:
286: }
|