1818
1919import static com .google .common .testing .SerializableTester .reserializeAndAssert ;
2020import static com .google .common .truth .Truth .assertThat ;
21+ import static org .junit .Assert .fail ;
2122
2223import com .google .common .testing .EqualsTester ;
2324import java .text .ParseException ;
@@ -34,10 +35,80 @@ public class DateTest {
3435
3536 @ Test
3637 public void parseDate () {
37- Date date = Date .parseDate ("2016-09-18" );
38- assertThat (date .getYear ()).isEqualTo (2016 );
39- assertThat (date .getMonth ()).isEqualTo (9 );
40- assertThat (date .getDayOfMonth ()).isEqualTo (18 );
38+ verifyDate ("2016-09-18" , 2016 , 9 , 18 );
39+ verifyDate ("2000-01-01" , 2000 , 1 , 1 );
40+ verifyDate ("9999-12-31" , 9999 , 12 , 31 );
41+ verifyDate ("0001-01-01" , 1 , 1 , 1 );
42+ verifyDate ("2000-02-29" , 2000 , 2 , 29 ); // This is a valid leap year.
43+ verifyDate ("1900-02-29" , 1900 , 2 , 29 ); // This is NOT a valid leap year.
44+ verifyDate ("2001-02-29" , 2001 , 2 , 29 ); // Also not a valid leap year.
45+ verifyDate ("2000-04-31" , 2000 , 4 , 31 ); // Not a valid date.
46+ }
47+
48+ private void verifyDate (String input , int year , int month , int day ) {
49+ Date date = Date .parseDate (input );
50+ assertThat (date .getYear ()).isEqualTo (year );
51+ assertThat (date .getMonth ()).isEqualTo (month );
52+ assertThat (date .getDayOfMonth ()).isEqualTo (day );
53+ }
54+
55+ @ Test
56+ public void parseInvalidDates () {
57+ parseInvalidDate ("2016/09/18" );
58+ parseInvalidDate ("2016 09 18" );
59+ parseInvalidDate ("2016-9-18" );
60+ parseInvalidDate ("2016-09-18T10:00" );
61+ parseInvalidDate ("" );
62+ parseInvalidDate ("test" );
63+ parseInvalidDate ("aaaa-bb-cc" );
64+ parseInvalidDate ("aaaa-01-01" );
65+ parseInvalidDate ("2019-bb-01" );
66+ parseInvalidDate ("2019-01-cc" );
67+ parseInvalidMonth ("2000-13-01" );
68+ parseInvalidMonth ("2000-00-01" );
69+ parseInvalidDay ("2000-12-32" );
70+ parseInvalidDay ("2000-12-00" );
71+ parseInvalidDate ("10000-01-01" );
72+ parseInvalidYear ("0000-01-01" );
73+ parseInvalidYear ("-001-01-01" );
74+ parseInvalidMonth ("0001--1-01" );
75+ parseInvalidDay ("0001-01--1" );
76+ }
77+
78+ private void parseInvalidDate (String input ) {
79+ try {
80+ Date .parseDate (input );
81+ fail ("Expected exception" );
82+ } catch (IllegalArgumentException e ) {
83+ assertThat (e .getMessage ()).contains ("Invalid date" );
84+ }
85+ }
86+
87+ private void parseInvalidYear (String input ) {
88+ try {
89+ Date .parseDate (input );
90+ fail ("Expected exception" );
91+ } catch (IllegalArgumentException e ) {
92+ assertThat (e .getMessage ()).contains ("Invalid year" );
93+ }
94+ }
95+
96+ private void parseInvalidMonth (String input ) {
97+ try {
98+ Date .parseDate (input );
99+ fail ("Expected exception" );
100+ } catch (IllegalArgumentException e ) {
101+ assertThat (e .getMessage ()).contains ("Invalid month" );
102+ }
103+ }
104+
105+ private void parseInvalidDay (String input ) {
106+ try {
107+ Date .parseDate (input );
108+ fail ("Expected exception" );
109+ } catch (IllegalArgumentException e ) {
110+ assertThat (e .getMessage ()).contains ("Invalid day" );
111+ }
41112 }
42113
43114 @ Test
0 commit comments