Source Code Cross Referenced for TestValidationAnnotationReaderImpl.java in  » Web-Framework » Strecks » org » strecks » validator » internal » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » Strecks » org.strecks.validator.internal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.