1
2
3
4 package de.powerstat.validation.values.test;
5
6
7 import static org.junit.jupiter.api.Assertions.assertAll;
8 import static org.junit.jupiter.api.Assertions.assertEquals;
9 import static org.junit.jupiter.api.Assertions.assertFalse;
10 import static org.junit.jupiter.api.Assertions.assertNotEquals;
11 import static org.junit.jupiter.api.Assertions.assertThrows;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import org.junit.jupiter.api.Test;
15 import org.junit.jupiter.params.ParameterizedTest;
16 import org.junit.jupiter.params.provider.ValueSource;
17
18 import de.powerstat.validation.values.Country;
19 import de.powerstat.validation.values.Day;
20 import de.powerstat.validation.values.GregorianCalendar;
21 import de.powerstat.validation.values.GregorianDate;
22 import de.powerstat.validation.values.Month;
23 import de.powerstat.validation.values.Year;
24 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25
26
27
28
29
30 @SuppressFBWarnings({"EC_NULL_ARG", "RV_NEGATING_RESULT_OF_COMPARETO", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", "SPP_USE_ZERO_WITH_COMPARATOR", "CLI_CONSTANT_LIST_INDEX", "PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS"})
31 final class GregorianDateTests
32 {
33
34
35
36 private static final String IT = "IT";
37
38
39
40
41 private static final String TEST_COMPARE_TO = "testCompareTo";
42
43
44
45
46 private static final String DATE_NOT_AS_EXPECTED = "Date not as expected";
47
48
49
50
51 private static final String SEPARATOR = "-";
52
53
54
55
56 private static final String ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED = "Illegal argument exception expected";
57
58
59
60
61 private static final String DATE_2020_02_29 = "2020-02-29";
62
63
64
65
66 private static final String DATE_2020_07_14 = "2020-07-14";
67
68
69
70
71
72 GregorianDateTests()
73 {
74 super();
75 }
76
77
78
79
80
81
82
83 @ParameterizedTest
84 @ValueSource(strings = {DATE_2020_07_14, "2020-06-30", DATE_2020_02_29, "2020-02-28", "2020-01-29", "2019-02-28", "2019-01-29"})
85 void testDateCorrect(final String date)
86 {
87 final String[] dateParts = date.split(GregorianDateTests.SEPARATOR);
88 final GregorianDate cleanDate = GregorianDate.of(Year.of(Long.parseLong(dateParts[0])), Month.of(Integer.parseInt(dateParts[1])), Day.of(Integer.parseInt(dateParts[2])));
89 assertEquals(date, cleanDate.stringValue(), GregorianDateTests.DATE_NOT_AS_EXPECTED);
90 }
91
92
93
94
95
96
97
98 @ParameterizedTest
99 @ValueSource(strings = {"2019-02-29", "2020-04-31"})
100 void testDateWrong(final String date)
101 {
102 final String[] dateParts = date.split(GregorianDateTests.SEPARATOR);
103 final Year year = Year.of(Long.parseLong(dateParts[0]));
104 final Month month = Month.of(Integer.parseInt(dateParts[1]));
105 final Day day = Day.of(Integer.parseInt(dateParts[2]));
106 assertThrows(IllegalArgumentException.class, () ->
107 {
108 GregorianDate.of(year, month, day);
109 }, ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
110 );
111 }
112
113
114
115
116
117 @Test
118 void testOf1()
119 {
120 final GregorianDate cleanDate = GregorianDate.of(DATE_2020_02_29);
121 assertEquals(DATE_2020_02_29, cleanDate.stringValue(), GregorianDateTests.DATE_NOT_AS_EXPECTED);
122 }
123
124
125
126
127
128 @Test
129 void testOf2()
130 {
131 assertThrows(IllegalArgumentException.class, () ->
132 {
133 GregorianDate.of("2020");
134 }, ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
135 );
136 }
137
138
139
140
141
142 @Test
143 void testStringValue()
144 {
145 final GregorianDate date = GregorianDate.of(Year.of(2020), Month.of(7), Day.of(14));
146 assertEquals(DATE_2020_07_14, date.stringValue(), GregorianDateTests.DATE_NOT_AS_EXPECTED);
147 }
148
149
150
151
152
153 @Test
154 void testHashCode()
155 {
156 final GregorianDate date1 = GregorianDate.of(Year.of(2020), Month.of(7), Day.of(14));
157 final GregorianDate date2 = GregorianDate.of(Year.of(2020), Month.of(7), Day.of(14));
158 final GregorianDate date3 = GregorianDate.of(Year.of(2021), Month.of(8), Day.of(15));
159 assertAll("testHashCode",
160 () -> assertEquals(date1.hashCode(), date2.hashCode(), "hashCodes are not equal"),
161 () -> assertNotEquals(date1.hashCode(), date3.hashCode(), "hashCodes are equal")
162 );
163 }
164
165
166
167
168
169 @Test
170 @SuppressWarnings("java:S5785")
171 void testEquals()
172 {
173 final GregorianDate date1 = GregorianDate.of(Year.of(2020), Month.of(7), Day.of(14));
174 final GregorianDate date2 = GregorianDate.of(Year.of(2020), Month.of(7), Day.of(14));
175 final GregorianDate date3 = GregorianDate.of(Year.of(2021), Month.of(7), Day.of(14));
176 final GregorianDate date4 = GregorianDate.of(Year.of(2020), Month.of(7), Day.of(14));
177 final GregorianDate date5 = GregorianDate.of(Year.of(2020), Month.of(8), Day.of(14));
178 final GregorianDate date6 = GregorianDate.of(Year.of(2020), Month.of(7), Day.of(15));
179 assertAll("testEquals",
180 () -> assertTrue(date1.equals(date1), "date11 is not equal"),
181 () -> assertTrue(date1.equals(date2), "date12 are not equal"),
182 () -> assertTrue(date2.equals(date1), "date21 are not equal"),
183 () -> assertTrue(date2.equals(date4), "date24 are not equal"),
184 () -> assertTrue(date1.equals(date4), "date14 are not equal"),
185 () -> assertFalse(date1.equals(date3), "date13 are equal"),
186 () -> assertFalse(date3.equals(date1), "date31 are equal"),
187 () -> assertFalse(date1.equals(null), "date10 is equal"),
188 () -> assertFalse(date1.equals(date5), "date15 are equal"),
189 () -> assertFalse(date1.equals(date6), "date16 are equal")
190 );
191 }
192
193
194
195
196
197 @Test
198 void testToString()
199 {
200 final GregorianDate date = GregorianDate.of(Year.of(2020), Month.of(7), Day.of(14));
201 assertEquals("GregorianDate[country=IT, date=2020-07-14]", date.toString(), "toString not equal");
202 }
203
204
205
206
207
208 @Test
209 @SuppressWarnings("java:S5785")
210 void testCompareTo()
211 {
212 final GregorianDate date1 = GregorianDate.of(Year.of(2020), Month.of(7), Day.of(14));
213 final GregorianDate date2 = GregorianDate.of(Year.of(2020), Month.of(7), Day.of(14));
214 final GregorianDate date3 = GregorianDate.of(Year.of(2021), Month.of(7), Day.of(14));
215 final GregorianDate date4 = GregorianDate.of(Year.of(2021), Month.of(8), Day.of(14));
216 final GregorianDate date5 = GregorianDate.of(Year.of(2020), Month.of(7), Day.of(14));
217 assertAll(GregorianDateTests.TEST_COMPARE_TO,
218 () -> assertTrue(date1.compareTo(date2) == -date2.compareTo(date1), "reflexive1"),
219 () -> assertTrue(date1.compareTo(date3) == -date3.compareTo(date1), "reflexive2"),
220 () -> assertTrue((date4.compareTo(date3) > 0) && (date3.compareTo(date1) > 0) && (date4.compareTo(date1) > 0), "transitive1"),
221 () -> assertTrue((date1.compareTo(date2) == 0) && (Math.abs(date1.compareTo(date5)) == Math.abs(date2.compareTo(date5))), "sgn1"),
222 () -> assertTrue((date1.compareTo(date2) == 0) && date1.equals(date2), "equals")
223 );
224 }
225
226
227
228
229
230 @Test
231 void testEaster()
232 {
233 final GregorianDate easter2020 = GregorianDate.easter(GregorianCalendar.of(Country.of(GregorianDateTests.IT)), Year.of(2020));
234 final GregorianDate easter2018 = GregorianDate.easter(GregorianCalendar.of(Country.of(GregorianDateTests.IT)), Year.of(2018));
235 final GregorianDate easter2015 = GregorianDate.easter(GregorianCalendar.of(Country.of(GregorianDateTests.IT)), Year.of(2015));
236 final GregorianDate easter2014 = GregorianDate.easter(GregorianCalendar.of(Country.of(GregorianDateTests.IT)), Year.of(2014));
237 final GregorianDate easter2011 = GregorianDate.easter(GregorianCalendar.of(Country.of(GregorianDateTests.IT)), Year.of(2011));
238 final GregorianDate easter2008 = GregorianDate.easter(GregorianCalendar.of(Country.of(GregorianDateTests.IT)), Year.of(2008));
239 final GregorianDate easter2005 = GregorianDate.easter(GregorianCalendar.of(Country.of(GregorianDateTests.IT)), Year.of(2005));
240 final GregorianDate easter1997 = GregorianDate.easter(GregorianCalendar.of(Country.of(GregorianDateTests.IT)), Year.of(1997));
241 final GregorianDate easter1991 = GregorianDate.easter(GregorianCalendar.of(Country.of(GregorianDateTests.IT)), Year.of(1991));
242 assertAll(GregorianDateTests.TEST_COMPARE_TO,
243 () -> assertEquals(GregorianDate.of(Year.of(2020), Month.of(4), Day.of(12)), easter2020, "Easter 2020 on wrong date"),
244 () -> assertEquals(GregorianDate.of(Year.of(2018), Month.of(4), Day.of(1)), easter2018, "Easter 2018 on wrong date"),
245 () -> assertEquals(GregorianDate.of(Year.of(2015), Month.of(4), Day.of(5)), easter2015, "Easter 2015 on wrong date"),
246 () -> assertEquals(GregorianDate.of(Year.of(2014), Month.of(4), Day.of(20)), easter2014, "Easter 2014 on wrong date"),
247 () -> assertEquals(GregorianDate.of(Year.of(2011), Month.of(4), Day.of(24)), easter2011, "Easter 2011 on wrong date"),
248 () -> assertEquals(GregorianDate.of(Year.of(2008), Month.of(3), Day.of(23)), easter2008, "Easter 2008 on wrong date"),
249 () -> assertEquals(GregorianDate.of(Year.of(2005), Month.of(3), Day.of(27)), easter2005, "Easter 2005 on wrong date"),
250 () -> assertEquals(GregorianDate.of(Year.of(1997), Month.of(3), Day.of(30)), easter1997, "Easter 1997 on wrong date"),
251 () -> assertEquals(GregorianDate.of(Year.of(1991), Month.of(3), Day.of(31)), easter1991, "Easter 1991 on wrong date")
252 );
253 }
254
255 }