Source Code Cross Referenced for TimeValidatorTest.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:
021:        import java.util.Date;
022:        import java.util.Calendar;
023:        import java.util.Locale;
024:        import java.util.TimeZone;
025:
026:        /**
027:         * Test Case for TimeValidator.
028:         * 
029:         * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
030:         */
031:        public class TimeValidatorTest extends TestCase {
032:
033:            protected static final TimeZone GMT = TimeZone.getTimeZone("GMT"); // 0 offset
034:            protected static final TimeZone EST = TimeZone.getTimeZone("EST"); // - 5 hours
035:
036:            protected TimeValidator validator;
037:
038:            protected String[] patternValid = new String[] { "23-59-59",
039:                    "00-00-00", "00-00-01", "0-0-0", "1-12-1", "10-49-18",
040:                    "16-23-46" };
041:            protected Date[] patternExpect = new Date[] {
042:                    createDate(null, 235959, 0), createDate(null, 0, 0),
043:                    createDate(null, 1, 0), createDate(null, 0, 0),
044:                    createDate(null, 11201, 0), createDate(null, 104918, 0),
045:                    createDate(null, 162346, 0) };
046:            protected String[] localeValid = new String[] { "23:59", "00:00",
047:                    "00:01", "0:0", "1:12", "10:49", "16:23" };
048:            protected Date[] localeExpect = new Date[] {
049:                    createDate(null, 235900, 0), createDate(null, 0, 0),
050:                    createDate(null, 100, 0), createDate(null, 0, 0),
051:                    createDate(null, 11200, 0), createDate(null, 104900, 0),
052:                    createDate(null, 162300, 0) };
053:            protected String[] patternInvalid = new String[] { "24-00-00" // midnight
054:                    , "24-00-01" // past midnight
055:                    , "25-02-03" // invalid hour 
056:                    , "10-61-31" // invalid minute
057:                    , "10-01-61" // invalid second
058:                    , "05:02-29" // invalid sep 
059:                    , "0X-01:01" // invalid sep
060:                    , "05-0X-01" // invalid char
061:                    , "10-01-0X" // invalid char
062:                    , "01:01:05" // invalid pattern
063:                    , "10-10" // invalid pattern
064:                    , "10--10" // invalid pattern
065:                    , "10-10-" }; // invalid pattern
066:            protected String[] localeInvalid = new String[] { "24:00" // midnight
067:                    , "24:00" // past midnight
068:                    , "25:02" // invalid hour 
069:                    , "10:61" // invalid minute
070:                    , "05-02" // invalid sep 
071:                    , "0X:01" // invalid sep
072:                    , "05:0X" // invalid char
073:                    , "01-01" // invalid pattern
074:                    , "10:" // invalid pattern
075:                    , "10::1" // invalid pattern
076:                    , "10:1:" }; // invalid pattern
077:
078:            /**
079:             * Main
080:             * @param args arguments
081:             */
082:            public static void main(String[] args) {
083:                junit.textui.TestRunner.run(TimeValidatorTest.class);
084:            }
085:
086:            /**
087:             * Constructor
088:             * @param name test name
089:             */
090:            public TimeValidatorTest(String name) {
091:                super (name);
092:            }
093:
094:            protected void setUp() throws Exception {
095:                super .setUp();
096:                validator = new TimeValidator();
097:            }
098:
099:            /**
100:             * Tear down
101:             * @throws Exception
102:             */
103:            protected void tearDown() throws Exception {
104:                super .tearDown();
105:                validator = null;
106:            }
107:
108:            /**
109:             * Test Valid Dates with "pattern" validation
110:             */
111:            public void testPatternValid() {
112:                for (int i = 0; i < patternValid.length; i++) {
113:                    String text = i + " value=[" + patternValid[i]
114:                            + "] failed ";
115:                    Calendar calendar = validator.validate(patternValid[i],
116:                            "HH-mm-ss");
117:                    assertNotNull("validateObj() " + text, calendar);
118:                    Date date = calendar.getTime();
119:                    assertTrue("isValid() " + text, validator.isValid(
120:                            patternValid[i], "HH-mm-ss"));
121:                    assertEquals("compare " + text, patternExpect[i], date);
122:                }
123:            }
124:
125:            /**
126:             * Test Invalid Dates with "pattern" validation
127:             */
128:            public void testPatternInvalid() {
129:                for (int i = 0; i < patternInvalid.length; i++) {
130:                    String text = i + " value=[" + patternInvalid[i]
131:                            + "] passed ";
132:                    Object date = validator.validate(patternInvalid[i],
133:                            "HH-mm-ss");
134:                    assertNull("validate() " + text + date, date);
135:                    assertFalse("isValid() " + text, validator.isValid(
136:                            patternInvalid[i], "HH-mm-ss"));
137:                }
138:            }
139:
140:            /**
141:             * Test Valid Dates with "locale" validation
142:             */
143:            public void testLocaleValid() {
144:                for (int i = 0; i < localeValid.length; i++) {
145:                    String text = i + " value=[" + localeValid[i] + "] failed ";
146:                    Calendar calendar = validator.validate(localeValid[i],
147:                            Locale.UK);
148:                    assertNotNull("validate() " + text, calendar);
149:                    Date date = calendar.getTime();
150:                    assertTrue("isValid() " + text, validator.isValid(
151:                            localeValid[i], Locale.UK));
152:                    assertEquals("compare " + text, localeExpect[i], date);
153:                }
154:            }
155:
156:            /**
157:             * Test Invalid Dates with "locale" validation
158:             */
159:            public void testLocaleInvalid() {
160:                for (int i = 0; i < localeInvalid.length; i++) {
161:                    String text = i + " value=[" + localeInvalid[i]
162:                            + "] passed ";
163:                    Object date = validator.validate(localeInvalid[i],
164:                            Locale.US);
165:                    assertNull("validate() " + text + date, date);
166:                    assertFalse("isValid() " + text, validator.isValid(
167:                            localeInvalid[i], Locale.UK));
168:                }
169:            }
170:
171:            /**
172:             * Test time zone methods.
173:             */
174:            public void testTimeZone() {
175:                // Set the default Locale & TimeZone
176:                Locale origDefault = Locale.getDefault();
177:                Locale.setDefault(Locale.UK);
178:                TimeZone defaultZone = TimeZone.getDefault();
179:                TimeZone.setDefault(GMT);
180:
181:                Calendar result = null;
182:
183:                // Default Locale, Default TimeZone
184:                result = validator.validate("18:01");
185:                assertNotNull("default result", result);
186:                assertEquals("default zone", GMT, result.getTimeZone());
187:                assertEquals("default hour", 18, result
188:                        .get(Calendar.HOUR_OF_DAY));
189:                assertEquals("default minute", 01, result.get(Calendar.MINUTE));
190:                result = null;
191:
192:                // Default Locale, diff TimeZone
193:                result = validator.validate("16:49", EST);
194:                assertNotNull("zone result", result);
195:                assertEquals("zone zone", EST, result.getTimeZone());
196:                assertEquals("zone hour", 16, result.get(Calendar.HOUR_OF_DAY));
197:                assertEquals("zone minute", 49, result.get(Calendar.MINUTE));
198:                result = null;
199:
200:                // Pattern, diff TimeZone
201:                result = validator.validate("14-34", "HH-mm", EST);
202:                assertNotNull("pattern result", result);
203:                assertEquals("pattern zone", EST, result.getTimeZone());
204:                assertEquals("pattern hour", 14, result
205:                        .get(Calendar.HOUR_OF_DAY));
206:                assertEquals("pattern minute", 34, result.get(Calendar.MINUTE));
207:                result = null;
208:
209:                // Locale, diff TimeZone
210:                result = validator.validate("7:18 PM", Locale.US, EST);
211:                assertNotNull("locale result", result);
212:                assertEquals("locale zone", EST, result.getTimeZone());
213:                assertEquals("locale hour", 19, result
214:                        .get(Calendar.HOUR_OF_DAY));
215:                assertEquals("locale minute", 18, result.get(Calendar.MINUTE));
216:                result = null;
217:
218:                // Locale & Pattern, diff TimeZone
219:                result = validator.validate("31/Dez/05 21-05",
220:                        "dd/MMM/yy HH-mm", Locale.GERMAN, EST);
221:                assertNotNull("pattern result", result);
222:                assertEquals("pattern zone", EST, result.getTimeZone());
223:                assertEquals("pattern day", 2005, result.get(Calendar.YEAR));
224:                assertEquals("pattern day", 11, result.get(Calendar.MONTH)); // months are 0-11
225:                assertEquals("pattern day", 31, result.get(Calendar.DATE));
226:                assertEquals("pattern hour", 21, result
227:                        .get(Calendar.HOUR_OF_DAY));
228:                assertEquals("pattern minute", 05, result.get(Calendar.MINUTE));
229:                result = null;
230:
231:                // Locale & Pattern, default TimeZone
232:                result = validator.validate("31/Dez/05 21-05",
233:                        "dd/MMM/yy HH-mm", Locale.GERMAN);
234:                assertNotNull("pattern result", result);
235:                assertEquals("pattern zone", GMT, result.getTimeZone());
236:                assertEquals("pattern day", 2005, result.get(Calendar.YEAR));
237:                assertEquals("pattern day", 11, result.get(Calendar.MONTH)); // months are 0-11
238:                assertEquals("pattern day", 31, result.get(Calendar.DATE));
239:                assertEquals("pattern hour", 21, result
240:                        .get(Calendar.HOUR_OF_DAY));
241:                assertEquals("pattern minute", 05, result.get(Calendar.MINUTE));
242:                result = null;
243:
244:                // Restore the original default
245:                Locale.setDefault(origDefault);
246:                TimeZone.setDefault(defaultZone);
247:            }
248:
249:            /**
250:             * Test Invalid Dates with "locale" validation
251:             */
252:            public void testFormat() {
253:                // Set the default Locale
254:                Locale origDefault = Locale.getDefault();
255:                Locale.setDefault(Locale.UK);
256:
257:                Object test = TimeValidator.getInstance().validate("16:49:23",
258:                        "HH:mm:ss");
259:                assertNotNull("Test Date ", test);
260:                assertEquals("Format pattern", "16-49-23", validator.format(
261:                        test, "HH-mm-ss"));
262:                assertEquals("Format locale", "4:49 PM", validator.format(test,
263:                        Locale.US));
264:                assertEquals("Format default", "16:49", validator.format(test));
265:
266:                // Restore the original default
267:                Locale.setDefault(origDefault);
268:            }
269:
270:            /**
271:             * Test compare date methods
272:             */
273:            public void testCompare() {
274:                int testTime = 154523;
275:                int min = 100;
276:                int hour = 10000;
277:
278:                Calendar milliGreater = createTime(GMT, testTime, 500); // > milli sec
279:                Calendar value = createTime(GMT, testTime, 400); // test value
280:                Calendar milliLess = createTime(GMT, testTime, 300); // < milli sec
281:
282:                Calendar secGreater = createTime(GMT, testTime + 1, 100); // +1 sec
283:                Calendar secLess = createTime(GMT, testTime - 1, 100); // -1 sec
284:
285:                Calendar minGreater = createTime(GMT, testTime + min, 100); // +1 min
286:                Calendar minLess = createTime(GMT, testTime - min, 100); // -1 min
287:
288:                Calendar hourGreater = createTime(GMT, testTime + hour, 100); // +1 hour
289:                Calendar hourLess = createTime(GMT, testTime - hour, 100); // -1 hour
290:
291:                assertEquals("mili LT", -1, validator.compareTime(value,
292:                        milliGreater)); // > milli
293:                assertEquals("mili EQ", 0, validator.compareTime(value, value)); // same time
294:                assertEquals("mili GT", 1, validator.compareTime(value,
295:                        milliLess)); // < milli
296:
297:                assertEquals("secs LT", -1, validator.compareSeconds(value,
298:                        secGreater)); // +1 sec
299:                assertEquals("secs =1", 0, validator.compareSeconds(value,
300:                        milliGreater)); // > milli
301:                assertEquals("secs =2", 0, validator.compareSeconds(value,
302:                        value)); // same time
303:                assertEquals("secs =3", 0, validator.compareSeconds(value,
304:                        milliLess)); // < milli
305:                assertEquals("secs GT", 1, validator.compareSeconds(value,
306:                        secLess)); // -1 sec
307:
308:                assertEquals("mins LT", -1, validator.compareMinutes(value,
309:                        minGreater)); // +1 min
310:                assertEquals("mins =1", 0, validator.compareMinutes(value,
311:                        secGreater)); // +1 sec
312:                assertEquals("mins =2", 0, validator.compareMinutes(value,
313:                        value)); // same time
314:                assertEquals("mins =3", 0, validator.compareMinutes(value,
315:                        secLess)); // -1 sec
316:                assertEquals("mins GT", 1, validator.compareMinutes(value,
317:                        minLess)); // -1 min
318:
319:                assertEquals("hour LT", -1, validator.compareHours(value,
320:                        hourGreater)); // +1 hour
321:                assertEquals("hour =1", 0, validator.compareHours(value,
322:                        minGreater)); // +1 min
323:                assertEquals("hour =2", 0, validator.compareHours(value, value)); // same time
324:                assertEquals("hour =3", 0, validator.compareHours(value,
325:                        minLess)); // -1 min
326:                assertEquals("hour GT", 1, validator.compareHours(value,
327:                        hourLess)); // -1 hour
328:
329:            }
330:
331:            /**
332:             * Create a calendar instance for a specified time zone, date and time.
333:             * 
334:             * @param zone The time zone
335:             * @param time the time in HH:mm:ss format
336:             * @param millisecond the milliseconds
337:             * @return the new Calendar instance.
338:             */
339:            protected static Calendar createTime(TimeZone zone, int time,
340:                    int millisecond) {
341:                Calendar calendar = zone == null ? Calendar.getInstance()
342:                        : Calendar.getInstance(zone);
343:                int hour = ((time / 10000) * 10000);
344:                int min = ((time / 100) * 100) - hour;
345:                int sec = time - (hour + min);
346:                calendar.set(Calendar.YEAR, 1970);
347:                calendar.set(Calendar.MONTH, 0);
348:                calendar.set(Calendar.DATE, 1);
349:                calendar.set(Calendar.HOUR_OF_DAY, (hour / 10000));
350:                calendar.set(Calendar.MINUTE, (min / 100));
351:                calendar.set(Calendar.SECOND, sec);
352:                calendar.set(Calendar.MILLISECOND, millisecond);
353:                return calendar;
354:            }
355:
356:            /**
357:             * Create a date instance for a specified time zone, date and time.
358:             * 
359:             * @param zone The time zone
360:             * @param time the time in HH:mm:ss format
361:             * @param millisecond the milliseconds
362:             * @return the new Date instance.
363:             */
364:            protected static Date createDate(TimeZone zone, int time,
365:                    int millisecond) {
366:                Calendar calendar = createTime(zone, time, millisecond);
367:                return calendar.getTime();
368:            }
369:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.