KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > datetime > ValidsTest


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.datetime;
4
5 import junit.framework.TestCase;
6
7
8 public class ValidsTest extends TestCase {
9
10     public void testValidDateTime() {
11         assertTrue(TimeUtil.isValidDate(2002, 1, 31));
12         assertFalse(TimeUtil.isValidDate(2002, 1, 32));
13         assertFalse(TimeUtil.isValidDate(2002, 2, 29));
14         assertFalse(TimeUtil.isValidDate(2002, 2, 0));
15         assertFalse(TimeUtil.isValidDate(2002, 0, 1));
16         assertFalse(TimeUtil.isValidDate(2002, 13, 29));
17         assertTrue(TimeUtil.isValidDate(2002, 12, 29));
18         assertTrue(TimeUtil.isValidDate(2000, 2, 29));
19         assertFalse(TimeUtil.isValidDate(1900, 2, 29));
20         
21         assertTrue(TimeUtil.isValidTime(0, 0, 0));
22         assertFalse(TimeUtil.isValidTime(0, 0, 60));
23         assertFalse(TimeUtil.isValidTime(0, 60, 0));
24         assertTrue(TimeUtil.isValidTime(0, 59, 0));
25         assertFalse(TimeUtil.isValidTime(24, 0, 0));
26         assertTrue(TimeUtil.isValidTime(23, 0, 0));
27         assertTrue(TimeUtil.isValidTime(23, 59, 0));
28         assertTrue(TimeUtil.isValidTime(23, 59, 59));
29         assertTrue(TimeUtil.isValidTime(23, 59, 59.999999999));
30
31         assertTrue(TimeUtil.isValidDateTime(2000, 2, 29, 23, 59, 59.999999999));
32         assertFalse(TimeUtil.isValidDateTime(2001, 2, 29, 23, 59, 59.999999999));
33         assertFalse(TimeUtil.isValidDateTime(2000, -1, 79, 23, 59, 59.999999999));
34         assertFalse(TimeUtil.isValidDateTime(2000, 1, 79, 23, 59, 59.999999999));
35     }
36
37
38     public void testIsValid() {
39         JDateTime jdt = new JDateTime();
40         assertTrue(jdt.isValid("2002-01-31"));
41         assertTrue(jdt.isValid("2002-1-31"));
42         assertFalse(jdt.isValid("2002-1-32"));
43         assertFalse(jdt.isValid("2002-2-29"));
44         assertFalse(jdt.isValid("2002-02-29"));
45         assertFalse(jdt.isValid("2002-02-0"));
46         assertFalse(jdt.isValid("2002-2-0"));
47         assertFalse(jdt.isValid("2002-0-01"));
48         assertFalse(jdt.isValid("2002-00-01"));
49         assertFalse(jdt.isValid("2002-13-29"));
50         assertTrue(jdt.isValid("2002-12-29"));
51         assertTrue(jdt.isValid("2000-2-29"));
52         assertTrue(jdt.isValid("2000-02-29"));
53         assertFalse(jdt.isValid("1900-2-29"));
54
55         assertTrue(jdt.isValid("2002-1-1"));
56         assertTrue(jdt.isValid("2002-01-01"));
57         assertTrue(jdt.isValid("2002-1-01"));
58         assertTrue(jdt.isValid("2002-01-1"));
59
60         assertTrue(jdt.isValid("0-1-1"));
61
62         assertTrue(jdt.isValid("0-1-1 12"));
63         assertTrue(jdt.isValid("0-1-1 12:23"));
64         assertTrue(jdt.isValid("0-1-1 12:23:34"));
65         assertTrue(jdt.isValid("0-1-1 12:23:00"));
66         assertTrue(jdt.isValid("0-1-1 12:23:01"));
67         assertTrue(jdt.isValid("0-1-1 12:23:0"));
68         assertTrue(jdt.isValid("0-1-1 12:23:1"));
69         assertTrue(jdt.isValid("0-1-1 12:23:34.567"));
70         assertTrue(jdt.isValid("0-1-1 02:03:04.007"));
71         assertTrue(jdt.isValid("0-1-1 2:3:4.007"));
72         assertFalse(jdt.isValid("0-1-1 2:3:60.000"));
73         assertTrue(jdt.isValid("0-1-1 2:3:59.999"));
74
75         assertFalse(jdt.isValid("a-a-a a:a:a"));
76         assertFalse(jdt.isValid("z-1-1 2:3:4.007"));
77         assertFalse(jdt.isValid("2-A-1 2:3:4.007"));
78         assertFalse(jdt.isValid("2-3-1 2:3: .4.007"));
79         assertTrue(jdt.isValid("2-3-1 2:3: 4.007"));
80     }
81
82     public void testIsValid2() {
83         JDateTime jdt = new JDateTime();
84         String JavaDoc date = jdt.toString("YYYY-MM-DD");
85
86         for (int sec = 0; sec < 60; sec++) {
87             for (int ms = 0; ms < 1000; ms++) {
88                 String JavaDoc mss;
89                 if (ms < 10) {
90                     mss = "00" + ms;
91                 } else if (ms < 100) {
92                     mss = "0" + ms;
93                 } else {
94                     mss = "" + ms;
95                 }
96                 String JavaDoc s1 = date + " 00:00:" + sec + "." + mss;
97                 assertTrue(jdt.isValid(s1));
98             }
99         }
100
101
102     }
103
104 }
105
106
Popular Tags