KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > datetime > TestValids


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