Source Code Cross Referenced for BaseCalendarValidatorTest.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 junit.framework.TestCase;
020:        import java.util.Date;
021:        import java.util.Calendar;
022:        import java.util.Locale;
023:        import java.util.TimeZone;
024:
025:        /**
026:         * Base Calendar Test Case.
027:         * 
028:         * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
029:         */
030:        public class BaseCalendarValidatorTest extends TestCase {
031:
032:            protected AbstractCalendarValidator validator;
033:
034:            protected static final TimeZone GMT = TimeZone.getTimeZone("GMT"); // 0 offset
035:            protected static final TimeZone EST = TimeZone.getTimeZone("EST"); // - 5 hours
036:            protected static final TimeZone EET = TimeZone.getTimeZone("EET"); // + 2 hours
037:            protected static final TimeZone UTC = TimeZone.getTimeZone("UTC"); // + 2 hours
038:
039:            protected String[] patternValid = new String[] { "2005-01-01",
040:                    "2005-12-31", "2004-02-29" // valid leap
041:                    , "2005-04-30", "05-12-31", "2005-1-1", "05-1-1" };
042:            protected String[] localeValid = new String[] { "01/01/2005",
043:                    "12/31/2005", "02/29/2004" // valid leap
044:                    , "04/30/2005", "12/31/05", "1/1/2005", "1/1/05" };
045:            protected Date[] patternExpect = new Date[] {
046:                    createDate(null, 20050101, 0),
047:                    createDate(null, 20051231, 0),
048:                    createDate(null, 20040229, 0),
049:                    createDate(null, 20050430, 0),
050:                    createDate(null, 20051231, 0),
051:                    createDate(null, 20050101, 0),
052:                    createDate(null, 20050101, 0) };
053:            protected String[] patternInvalid = new String[] { "2005-00-01" // zero month
054:                    , "2005-01-00" // zero day 
055:                    , "2005-13-03" // month invalid
056:                    , "2005-04-31" // invalid day 
057:                    , "2005-03-32" // invalid day 
058:                    , "2005-02-29" // invalid leap
059:                    , "200X-01-01" // invalid char
060:                    , "2005-0X-01" // invalid char
061:                    , "2005-01-0X" // invalid char
062:                    , "01/01/2005" // invalid pattern
063:                    , "2005-01" // invalid pattern
064:                    , "2005--01" // invalid pattern
065:                    , "2005-01-" }; // invalid pattern
066:            protected String[] localeInvalid = new String[] { "01/00/2005" // zero month
067:                    , "00/01/2005" // zero day 
068:                    , "13/01/2005" // month invalid
069:                    , "04/31/2005" // invalid day 
070:                    , "03/32/2005" // invalid day 
071:                    , "02/29/2005" // invalid leap
072:                    , "01/01/200X" // invalid char
073:                    , "01/0X/2005" // invalid char
074:                    , "0X/01/2005" // invalid char
075:                    , "01-01-2005" // invalid pattern
076:                    , "01/2005" // invalid pattern
077:                    // --------      ,"/01/2005"    ---- passes on some JDK
078:                    , "01//2005" }; // invalid pattern
079:
080:            /**
081:             * Constructor
082:             * @param name test name
083:             */
084:            public BaseCalendarValidatorTest(String name) {
085:                super (name);
086:            }
087:
088:            /**
089:             * Set Up.
090:             * @throws Exception
091:             */
092:            protected void setUp() throws Exception {
093:                super .setUp();
094:            }
095:
096:            /**
097:             * Tear down
098:             * @throws Exception
099:             */
100:            protected void tearDown() throws Exception {
101:                super .tearDown();
102:                validator = null;
103:            }
104:
105:            /**
106:             * Test Valid Dates with "pattern" validation
107:             */
108:            public void testPatternValid() {
109:                for (int i = 0; i < patternValid.length; i++) {
110:                    String text = i + " value=[" + patternValid[i]
111:                            + "] failed ";
112:                    Object date = validator.parse(patternValid[i], "yy-MM-dd",
113:                            null, null);
114:                    assertNotNull("validateObj() " + text + date, date);
115:                    assertTrue("isValid() " + text, validator.isValid(
116:                            patternValid[i], "yy-MM-dd"));
117:                    if (date instanceof  Calendar) {
118:                        date = ((Calendar) date).getTime();
119:                    }
120:                    assertEquals("compare " + text, patternExpect[i], date);
121:                }
122:            }
123:
124:            /**
125:             * Test Invalid Dates with "pattern" validation
126:             */
127:            public void testPatternInvalid() {
128:                for (int i = 0; i < patternInvalid.length; i++) {
129:                    String text = i + " value=[" + patternInvalid[i]
130:                            + "] passed ";
131:                    Object date = validator.parse(patternInvalid[i],
132:                            "yy-MM-dd", null, null);
133:                    assertNull("validateObj() " + text + date, date);
134:                    assertFalse("isValid() " + text, validator.isValid(
135:                            patternInvalid[i], "yy-MM-dd"));
136:                }
137:            }
138:
139:            /**
140:             * Test Valid Dates with "locale" validation
141:             */
142:            public void testLocaleValid() {
143:                for (int i = 0; i < localeValid.length; i++) {
144:                    String text = i + " value=[" + localeValid[i] + "] failed ";
145:                    Object date = validator.parse(localeValid[i], null,
146:                            Locale.US, null);
147:                    assertNotNull("validateObj() " + text + date, date);
148:                    assertTrue("isValid() " + text, validator.isValid(
149:                            localeValid[i], Locale.US));
150:                    if (date instanceof  Calendar) {
151:                        date = ((Calendar) date).getTime();
152:                    }
153:                    assertEquals("compare " + text, patternExpect[i], date);
154:                }
155:            }
156:
157:            /**
158:             * Test Invalid Dates with "locale" validation
159:             */
160:            public void testLocaleInvalid() {
161:                for (int i = 0; i < localeInvalid.length; i++) {
162:                    String text = i + " value=[" + localeInvalid[i]
163:                            + "] passed ";
164:                    Object date = validator.parse(localeInvalid[i], null,
165:                            Locale.US, null);
166:                    assertNull("validateObj() " + text + date, date);
167:                    assertFalse("isValid() " + text, validator.isValid(
168:                            localeInvalid[i], Locale.US));
169:                }
170:            }
171:
172:            /**
173:             * Test Invalid Dates with "locale" validation
174:             */
175:            public void testFormat() {
176:
177:                // Create a Date or Calendar
178:                Object test = validator.parse("2005-11-28", "yyyy-MM-dd", null,
179:                        null);
180:                assertNotNull("Test Date ", test);
181:                assertEquals("Format pattern", "28.11.05", validator.format(
182:                        test, "dd.MM.yy"));
183:                assertEquals("Format locale", "11/28/05", validator.format(
184:                        test, Locale.US));
185:            }
186:
187:            /**
188:             * Create a calendar instance for a specified time zone, date and time.
189:             * 
190:             * @param zone The time zone
191:             * @param date The date in yyyyMMdd format
192:             * @param time the time in HH:mm:ss format
193:             * @return the new Calendar instance.
194:             */
195:            protected static Calendar createCalendar(TimeZone zone, int date,
196:                    int time) {
197:                Calendar calendar = zone == null ? Calendar.getInstance()
198:                        : Calendar.getInstance(zone);
199:                int year = ((date / 10000) * 10000);
200:                int mth = ((date / 100) * 100) - year;
201:                int day = date - (year + mth);
202:                int hour = ((time / 10000) * 10000);
203:                int min = ((time / 100) * 100) - hour;
204:                int sec = time - (hour + min);
205:                calendar.set(Calendar.YEAR, (year / 10000));
206:                calendar.set(Calendar.MONTH, ((mth / 100) - 1));
207:                calendar.set(Calendar.DATE, day);
208:                calendar.set(Calendar.HOUR_OF_DAY, (hour / 10000));
209:                calendar.set(Calendar.MINUTE, (min / 100));
210:                calendar.set(Calendar.SECOND, sec);
211:                calendar.set(Calendar.MILLISECOND, 0);
212:                return calendar;
213:            }
214:
215:            /**
216:             * Create a date instance for a specified time zone, date and time.
217:             * 
218:             * @param zone The time zone
219:             * @param date The date in yyyyMMdd format
220:             * @param time the time in HH:mm:ss format
221:             * @return the new Date instance.
222:             */
223:            protected static Date createDate(TimeZone zone, int date, int time) {
224:                Calendar calendar = createCalendar(zone, date, time);
225:                return calendar.getTime();
226:            }
227:
228:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.