Source Code Cross Referenced for DateValidatorTest.java in  » Library » Apache-commons-validator-1.3.1-src » org » apache » commons » validator » routines » 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 » Library » Apache commons validator 1.3.1 src » org.apache.commons.validator.routines 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.validator.routines;
018:
019:        import java.util.Date;
020:        import java.util.Locale;
021:        import java.util.TimeZone;
022:
023:        /**
024:         * Test Case for DateValidator.
025:         * 
026:         * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
027:         */
028:        public class DateValidatorTest extends BaseCalendarValidatorTest {
029:
030:            private DateValidator dateValidator;
031:
032:            /**
033:             * Main
034:             * @param args arguments
035:             */
036:            public static void main(String[] args) {
037:                junit.textui.TestRunner.run(DateValidatorTest.class);
038:            }
039:
040:            /**
041:             * Constructor
042:             * @param name test name
043:             */
044:            public DateValidatorTest(String name) {
045:                super (name);
046:            }
047:
048:            /**
049:             * Set Up.
050:             * @throws Exception
051:             */
052:            protected void setUp() throws Exception {
053:                super .setUp();
054:                dateValidator = new DateValidator();
055:                validator = dateValidator;
056:            }
057:
058:            /**
059:             * Test DateValidator validate Methods
060:             */
061:            public void testDateValidatorMethods() {
062:                Locale.setDefault(Locale.US);
063:                Locale locale = Locale.GERMAN;
064:                String pattern = "yyyy-MM-dd";
065:                String patternVal = "2005-12-31";
066:                String germanVal = "31 Dez 2005";
067:                String germanPattern = "dd MMM yyyy";
068:                String localeVal = "31.12.2005";
069:                String defaultVal = "12/31/05";
070:                String XXXX = "XXXX";
071:                Date expected = createCalendar(null, 20051231, 0).getTime();
072:
073:                assertEquals("validate(A) default", expected, DateValidator
074:                        .getInstance().validate(defaultVal));
075:                assertEquals("validate(A) locale ", expected, DateValidator
076:                        .getInstance().validate(localeVal, locale));
077:                assertEquals("validate(A) pattern", expected, DateValidator
078:                        .getInstance().validate(patternVal, pattern));
079:                assertEquals("validate(A) both", expected, DateValidator
080:                        .getInstance().validate(germanVal, germanPattern,
081:                                Locale.GERMAN));
082:
083:                assertTrue("isValid(A) default", DateValidator.getInstance()
084:                        .isValid(defaultVal));
085:                assertTrue("isValid(A) locale ", DateValidator.getInstance()
086:                        .isValid(localeVal, locale));
087:                assertTrue("isValid(A) pattern", DateValidator.getInstance()
088:                        .isValid(patternVal, pattern));
089:                assertTrue("isValid(A) both", DateValidator.getInstance()
090:                        .isValid(germanVal, germanPattern, Locale.GERMAN));
091:
092:                assertNull("validate(B) default", DateValidator.getInstance()
093:                        .validate(XXXX));
094:                assertNull("validate(B) locale ", DateValidator.getInstance()
095:                        .validate(XXXX, locale));
096:                assertNull("validate(B) pattern", DateValidator.getInstance()
097:                        .validate(XXXX, pattern));
098:                assertNull("validate(B) both", DateValidator.getInstance()
099:                        .validate("31 Dec 2005", germanPattern, Locale.GERMAN));
100:
101:                assertFalse("isValid(B) default", DateValidator.getInstance()
102:                        .isValid(XXXX));
103:                assertFalse("isValid(B) locale ", DateValidator.getInstance()
104:                        .isValid(XXXX, locale));
105:                assertFalse("isValid(B) pattern", DateValidator.getInstance()
106:                        .isValid(XXXX, pattern));
107:                assertFalse("isValid(B) both", DateValidator.getInstance()
108:                        .isValid("31 Dec 2005", germanPattern, Locale.GERMAN));
109:
110:                // Test Time Zone
111:                TimeZone zone = (TimeZone.getDefault().getRawOffset() == EET
112:                        .getRawOffset() ? EST : EET);
113:                Date expectedZone = createCalendar(zone, 20051231, 0).getTime();
114:                assertFalse("default/zone same " + zone,
115:                        expected.getTime() == expectedZone.getTime());
116:
117:                assertEquals("validate(C) default", expectedZone, DateValidator
118:                        .getInstance().validate(defaultVal, zone));
119:                assertEquals("validate(C) locale ", expectedZone, DateValidator
120:                        .getInstance().validate(localeVal, locale, zone));
121:                assertEquals("validate(C) pattern", expectedZone, DateValidator
122:                        .getInstance().validate(patternVal, pattern, zone));
123:                assertEquals("validate(C) both", expectedZone, DateValidator
124:                        .getInstance().validate(germanVal, germanPattern,
125:                                Locale.GERMAN, zone));
126:            }
127:
128:            /**
129:             * Test compare date methods
130:             */
131:            public void testCompare() {
132:                int sameTime = 124522;
133:                int testDate = 20050823;
134:                Date diffHour = createDate(GMT, testDate, 115922); // same date, different time
135:
136:                Date value = createDate(GMT, testDate, sameTime); // test value
137:                Date date20050824 = createDate(GMT, 20050824, sameTime); // +1 day
138:                Date date20050822 = createDate(GMT, 20050822, sameTime); // -1 day
139:
140:                Date date20050830 = createDate(GMT, 20050830, sameTime); // +1 week
141:                Date date20050816 = createDate(GMT, 20050816, sameTime); // -1 week
142:
143:                Date date20050901 = createDate(GMT, 20050901, sameTime); // +1 month
144:                Date date20050801 = createDate(GMT, 20050801, sameTime); // same month
145:                Date date20050731 = createDate(GMT, 20050731, sameTime); // -1 month
146:
147:                Date date20051101 = createDate(GMT, 20051101, sameTime); // +1 quarter (Feb Start)
148:                Date date20051001 = createDate(GMT, 20051001, sameTime); // +1 quarter
149:                Date date20050701 = createDate(GMT, 20050701, sameTime); // same quarter
150:                Date date20050630 = createDate(GMT, 20050630, sameTime); // -1 quarter
151:                Date date20050110 = createDate(GMT, 20050110, sameTime); // Previous Year qtr (Fen start)
152:
153:                Date date20060101 = createDate(GMT, 20060101, sameTime); // +1 year
154:                Date date20050101 = createDate(GMT, 20050101, sameTime); // same year
155:                Date date20041231 = createDate(GMT, 20041231, sameTime); // -1 year
156:
157:                assertEquals("date LT", -1, dateValidator.compareDates(value,
158:                        date20050824, null)); // +1 day
159:                assertEquals("date EQ", 0, dateValidator.compareDates(value,
160:                        diffHour, null)); // same day, diff hour
161:                assertEquals("date GT", 1, dateValidator.compareDates(value,
162:                        date20050822, null)); // -1 day
163:
164:                assertEquals("week LT", -1, dateValidator.compareWeeks(value,
165:                        date20050830, null)); // +1 week 
166:                assertEquals("week =1", 0, dateValidator.compareWeeks(value,
167:                        date20050824, null)); // +1 day
168:                assertEquals("week =2", 0, dateValidator.compareWeeks(value,
169:                        date20050822, null)); // same week
170:                assertEquals("week =3", 0, dateValidator.compareWeeks(value,
171:                        date20050822, null)); // -1 day
172:                assertEquals("week GT", 1, dateValidator.compareWeeks(value,
173:                        date20050816, null)); // -1 week
174:
175:                assertEquals("mnth LT", -1, dateValidator.compareMonths(value,
176:                        date20050901, null)); // +1 month 
177:                assertEquals("mnth =1", 0, dateValidator.compareMonths(value,
178:                        date20050830, null)); // +1 week 
179:                assertEquals("mnth =2", 0, dateValidator.compareMonths(value,
180:                        date20050801, null)); // same month
181:                assertEquals("mnth =3", 0, dateValidator.compareMonths(value,
182:                        date20050816, null)); // -1 week
183:                assertEquals("mnth GT", 1, dateValidator.compareMonths(value,
184:                        date20050731, null)); // -1 month
185:
186:                assertEquals("qtrA <1", -1, dateValidator.compareQuarters(
187:                        value, date20051101, null)); // +1 quarter (Feb) 
188:                assertEquals("qtrA <2", -1, dateValidator.compareQuarters(
189:                        value, date20051001, null)); // +1 quarter 
190:                assertEquals("qtrA =1", 0, dateValidator.compareQuarters(value,
191:                        date20050901, null)); // +1 month 
192:                assertEquals("qtrA =2", 0, dateValidator.compareQuarters(value,
193:                        date20050701, null)); // same quarter
194:                assertEquals("qtrA =3", 0, dateValidator.compareQuarters(value,
195:                        date20050731, null)); // -1 month
196:                assertEquals("qtrA GT", 1, dateValidator.compareQuarters(value,
197:                        date20050630, null)); // -1 quarter
198:
199:                // Change quarter 1 to start in Feb
200:                assertEquals("qtrB LT", -1, dateValidator.compareQuarters(
201:                        value, date20051101, null, 2)); // +1 quarter (Feb) 
202:                assertEquals("qtrB =1", 0, dateValidator.compareQuarters(value,
203:                        date20051001, null, 2)); // same quarter 
204:                assertEquals("qtrB =2", 0, dateValidator.compareQuarters(value,
205:                        date20050901, null, 2)); // +1 month 
206:                assertEquals("qtrB =3", 1, dateValidator.compareQuarters(value,
207:                        date20050701, null, 2)); // same quarter
208:                assertEquals("qtrB =4", 1, dateValidator.compareQuarters(value,
209:                        date20050731, null, 2)); // -1 month
210:                assertEquals("qtrB GT", 1, dateValidator.compareQuarters(value,
211:                        date20050630, null, 2)); // -1 quarter
212:                assertEquals("qtrB prev", 1, dateValidator.compareQuarters(
213:                        value, date20050110, null, 2)); // Jan Prev year qtr
214:
215:                assertEquals("year LT", -1, dateValidator.compareYears(value,
216:                        date20060101, null)); // +1 year 
217:                assertEquals("year EQ", 0, dateValidator.compareYears(value,
218:                        date20050101, null)); // same year
219:                assertEquals("year GT", 1, dateValidator.compareYears(value,
220:                        date20041231, null)); // -1 year
221:
222:                // Compare using alternative TimeZone
223:                Date sameDayTwoAm = createDate(GMT, testDate, 20000);
224:                assertEquals("date LT", -1, dateValidator.compareDates(value,
225:                        date20050824, EST)); // +1 day
226:                assertEquals("date EQ", 0, dateValidator.compareDates(value,
227:                        diffHour, EST)); // same day, diff hour
228:                assertEquals("date EQ", 1, dateValidator.compareDates(value,
229:                        sameDayTwoAm, EST)); // same day, diff hour
230:                assertEquals("date GT", 1, dateValidator.compareDates(value,
231:                        date20050822, EST)); // -1 day
232:            }
233:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.