This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
/
DateTest.java
172 lines (153 loc) · 5.48 KB
/
DateTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud;
import static com.google.common.testing.SerializableTester.reserializeAndAssert;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import com.google.common.testing.EqualsTester;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link Date}. */
@RunWith(JUnit4.class)
public class DateTest {
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
@Test
public void parseDate() {
verifyDate("2016-09-18", 2016, 9, 18);
verifyDate("2000-01-01", 2000, 1, 1);
verifyDate("9999-12-31", 9999, 12, 31);
verifyDate("0001-01-01", 1, 1, 1);
verifyDate("2000-02-29", 2000, 2, 29); // This is a valid leap year.
verifyDate("1900-02-29", 1900, 2, 29); // This is NOT a valid leap year.
verifyDate("2001-02-29", 2001, 2, 29); // Also not a valid leap year.
verifyDate("2000-04-31", 2000, 4, 31); // Not a valid date.
}
private void verifyDate(String input, int year, int month, int day) {
Date date = Date.parseDate(input);
assertThat(date.getYear()).isEqualTo(year);
assertThat(date.getMonth()).isEqualTo(month);
assertThat(date.getDayOfMonth()).isEqualTo(day);
}
@Test
public void parseInvalidDates() {
parseInvalidDate("2016/09/18");
parseInvalidDate("2016 09 18");
parseInvalidDate("2016-9-18");
parseInvalidDate("2016-09-18T10:00");
parseInvalidDate("");
parseInvalidDate("test");
parseInvalidDate("aaaa-bb-cc");
parseInvalidDate("aaaa-01-01");
parseInvalidDate("2019-bb-01");
parseInvalidDate("2019-01-cc");
parseInvalidMonth("2000-13-01");
parseInvalidMonth("2000-00-01");
parseInvalidDay("2000-12-32");
parseInvalidDay("2000-12-00");
parseInvalidDate("10000-01-01");
parseInvalidYear("0000-01-01");
parseInvalidYear("-001-01-01");
parseInvalidMonth("0001--1-01");
parseInvalidDay("0001-01--1");
}
private void parseInvalidDate(String input) {
try {
Date.parseDate(input);
fail("Expected exception");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("Invalid date");
}
}
private void parseInvalidYear(String input) {
try {
Date.parseDate(input);
fail("Expected exception");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("Invalid year");
}
}
private void parseInvalidMonth(String input) {
try {
Date.parseDate(input);
fail("Expected exception");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("Invalid month");
}
}
private void parseInvalidDay(String input) {
try {
Date.parseDate(input);
fail("Expected exception");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).contains("Invalid day");
}
}
@Test
public void testToString() {
Date date = Date.fromYearMonthDay(10, 9, 5);
assertThat(date.toString()).isEqualTo("0010-09-05");
date = Date.fromYearMonthDay(2016, 12, 31);
assertThat(date.toString()).isEqualTo("2016-12-31");
date = Date.fromYearMonthDay(1, 1, 1);
assertThat(date.toString()).isEqualTo("0001-01-01");
}
@Test
public void equalAndHashCode() {
Date d1 = Date.fromYearMonthDay(2016, 9, 18);
Date d2 = Date.fromYearMonthDay(2016, 9, 18);
Date d3 = Date.fromYearMonthDay(2016, 9, 19);
new EqualsTester().addEqualityGroup(d1, d2).addEqualityGroup(d3).testEquals();
}
@Test
public void validOrdering() {
Date d1 = Date.fromYearMonthDay(2016, 9, 18);
Date d2 = Date.fromYearMonthDay(2016, 9, 19);
Date d3 = Date.fromYearMonthDay(2016, 9, 20);
Date d4 = Date.fromYearMonthDay(2016, 10, 1);
Date d5 = Date.fromYearMonthDay(2017, 1, 1);
assertDescending(d5, d4, d3, d2, d1);
}
@Test
public void serialization() {
reserializeAndAssert(Date.fromYearMonthDay(2017, 4, 20));
}
@Test
public void testToJavaUtilDate() throws ParseException {
Date gcDate = Date.parseDate("2016-09-18");
java.util.Date juDate1 = SIMPLE_DATE_FORMAT.parse("2016-09-18");
java.util.Date juDate2 = Date.toJavaUtilDate(gcDate);
assertThat(juDate1).isEqualTo(juDate2);
}
@Test
public void testFromJavaUtilDate() throws ParseException {
java.util.Date juDate = SIMPLE_DATE_FORMAT.parse("2016-09-18");
Date gcDate = Date.fromJavaUtilDate(juDate);
assertThat(gcDate.getYear()).isEqualTo(2016);
assertThat(gcDate.getMonth()).isEqualTo(9);
assertThat(gcDate.getDayOfMonth()).isEqualTo(18);
}
private void assertDescending(Date... dates) {
for (int i = 0; i < dates.length - 1; i++) {
assertThat(dates[i]).isEquivalentAccordingToCompareTo(dates[i]);
for (int j = i + 1; j < dates.length; j++) {
assertThat(dates[i]).isGreaterThan(dates[j]);
}
}
}
}