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.assertTrue;
12
13 import org.junit.jupiter.api.Test;
14 import org.junit.jupiter.params.ParameterizedTest;
15 import org.junit.jupiter.params.provider.ValueSource;
16
17 import de.powerstat.validation.values.Country;
18 import de.powerstat.validation.values.GregorianCalendar;
19 import de.powerstat.validation.values.Month;
20 import de.powerstat.validation.values.Year;
21 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22
23
24
25
26
27 @SuppressFBWarnings({"EC_NULL_ARG", "RV_NEGATING_RESULT_OF_COMPARETO", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", "SPP_USE_ZERO_WITH_COMPARATOR"})
28 final class GregorianCalendarTests
29 {
30
31
32
33 private static final String TEST_IS_LEAP_YEAR = "testIsLeapYear";
34
35
36
37
38 private static final String DE = "DE";
39
40
41
42
43 private static final String IT = "IT";
44
45
46
47
48 private static final String RU = "RU";
49
50
51
52
53 private static final String CALENDAR_NOT_AS_EXPECTED = "Calendar not as expected";
54
55
56
57
58
59 GregorianCalendarTests()
60 {
61 super();
62 }
63
64
65
66
67
68
69
70 @ParameterizedTest
71 @ValueSource(strings = {GregorianCalendarTests.IT})
72 void testCalendarCorrect(final String country)
73 {
74 final GregorianCalendar cleanCalendar = GregorianCalendar.of(Country.of(country));
75 assertEquals(country, cleanCalendar.getCountry().stringValue(), CALENDAR_NOT_AS_EXPECTED);
76 }
77
78
79
80
81
82
83
84 @ParameterizedTest
85 @ValueSource(strings = {GregorianCalendarTests.IT})
86 void testOf(final String country)
87 {
88 final GregorianCalendar cleanCalendar = GregorianCalendar.of(country);
89 assertEquals(country, cleanCalendar.getCountry().stringValue(), CALENDAR_NOT_AS_EXPECTED);
90 }
91
92
93
94
95
96 @Test
97 void testStringValue()
98 {
99 final GregorianCalendar cal = GregorianCalendar.of(Country.of(DE));
100 assertEquals(DE, cal.stringValue(), "Calendar country not as expected");
101 }
102
103
104
105
106
107 @Test
108 void testHashCode()
109 {
110 final GregorianCalendar calendar1 = GregorianCalendar.of(Country.of(GregorianCalendarTests.IT));
111 final GregorianCalendar calendar2 = GregorianCalendar.of(Country.of(GregorianCalendarTests.IT));
112 final GregorianCalendar calendar3 = GregorianCalendar.of(Country.of(GregorianCalendarTests.DE));
113 assertAll("testHashCode",
114 () -> assertEquals(calendar1.hashCode(), calendar2.hashCode(), "hashCodes are not equal"),
115 () -> assertNotEquals(calendar1.hashCode(), calendar3.hashCode(), "hashCodes are equal")
116 );
117 }
118
119
120
121
122
123 @Test
124 @SuppressWarnings("java:S5785")
125 void testEquals()
126 {
127 final GregorianCalendar calendar1 = GregorianCalendar.of(Country.of(GregorianCalendarTests.IT));
128 final GregorianCalendar calendar2 = GregorianCalendar.of(Country.of(GregorianCalendarTests.IT));
129 final GregorianCalendar calendar3 = GregorianCalendar.of(Country.of(GregorianCalendarTests.DE));
130 final GregorianCalendar calendar4 = GregorianCalendar.of(Country.of(GregorianCalendarTests.IT));
131 assertAll("testEquals",
132 () -> assertTrue(calendar1.equals(calendar1), "calendar11 is not equal"),
133 () -> assertTrue(calendar1.equals(calendar2), "calendar12 are not equal"),
134 () -> assertTrue(calendar2.equals(calendar1), "calendar21 are not equal"),
135 () -> assertTrue(calendar2.equals(calendar4), "calendar24 are not equal"),
136 () -> assertTrue(calendar1.equals(calendar4), "calendar14 are not equal"),
137 () -> assertFalse(calendar1.equals(calendar3), "calendar13 are equal"),
138 () -> assertFalse(calendar3.equals(calendar1), "calendar31 are equal"),
139 () -> assertFalse(calendar1.equals(null), "calendar10 is equal")
140 );
141 }
142
143
144
145
146
147 @Test
148 void testToString()
149 {
150 final GregorianCalendar calendar = GregorianCalendar.of(Country.of(GregorianCalendarTests.IT));
151 assertEquals("GregorianCalendar[country=IT]", calendar.toString(), "toString not equal");
152 }
153
154
155
156
157
158 @Test
159 @SuppressWarnings("java:S5785")
160 void testCompareTo()
161 {
162 final GregorianCalendar calendar1 = GregorianCalendar.of(Country.of(GregorianCalendarTests.IT));
163 final GregorianCalendar calendar2 = GregorianCalendar.of(Country.of(GregorianCalendarTests.IT));
164 final GregorianCalendar calendar3 = GregorianCalendar.of(Country.of(GregorianCalendarTests.DE));
165 final GregorianCalendar calendar4 = GregorianCalendar.of(Country.of("US"));
166 final GregorianCalendar calendar5 = GregorianCalendar.of(Country.of(GregorianCalendarTests.IT));
167 assertAll("testCompareTo",
168 () -> assertTrue(calendar1.compareTo(calendar2) == -calendar2.compareTo(calendar1), "reflexive1"),
169 () -> assertTrue(calendar1.compareTo(calendar3) == -calendar3.compareTo(calendar1), "reflexive2"),
170 () -> assertTrue((calendar4.compareTo(calendar3) > 0) && (calendar3.compareTo(calendar1) > 0) && (calendar4.compareTo(calendar1) > 0), "transitive1"),
171 () -> assertTrue((calendar1.compareTo(calendar2) == 0) && (Math.abs(calendar1.compareTo(calendar5)) == Math.abs(calendar2.compareTo(calendar5))), "sgn1"),
172 () -> assertTrue((calendar1.compareTo(calendar2) == 0) && calendar1.equals(calendar2), "equals")
173 );
174 }
175
176
177
178
179
180 @Test
181 void testIsLeapYear()
182 {
183 final GregorianCalendar calendarIT = GregorianCalendar.of(Country.of(GregorianCalendarTests.IT));
184 final GregorianCalendar calendarDE = GregorianCalendar.of(Country.of(GregorianCalendarTests.DE));
185 final GregorianCalendar calendarRU = GregorianCalendar.of(Country.of(GregorianCalendarTests.RU));
186 assertAll(GregorianCalendarTests.TEST_IS_LEAP_YEAR,
187 () -> assertFalse(calendarIT.isLeapYear(Year.of(1900)), "1900 is not a leap year"),
188 () -> assertTrue(calendarIT.isLeapYear(Year.of(2000)), "2000 is a leap year"),
189 () -> assertTrue(calendarIT.isLeapYear(Year.of(2020)), "2020 is a leap year"),
190 () -> assertFalse(calendarIT.isLeapYear(Year.of(2019)), "2019 is not a leap year"),
191 () -> assertFalse(calendarIT.isLeapYear(Year.of(1582)), "1582 is not a leap year"),
192 () -> assertTrue(calendarIT.isLeapYear(Year.of(1580)), "1580 is a leap year"),
193 () -> assertTrue(calendarDE.isLeapYear(Year.of(1580)), "1700 is not a leap year"),
194 () -> assertTrue(calendarRU.isLeapYear(Year.of(1920)), "1920 is a leap year"),
195 () -> assertTrue(calendarRU.isLeapYear(Year.of(1900)), "1900 is a leap year")
196 );
197 }
198
199
200
201
202
203 @Test
204 void testDaysInMonth()
205 {
206 final GregorianCalendar calendarIT = GregorianCalendar.of(Country.of(GregorianCalendarTests.IT));
207 final GregorianCalendar calendarRU = GregorianCalendar.of(Country.of(GregorianCalendarTests.RU));
208 assertAll(GregorianCalendarTests.TEST_IS_LEAP_YEAR,
209 () -> assertEquals(31, calendarIT.daysInMonth(Year.of(2020), Month.of(1)), "January should have 31 days"),
210 () -> assertEquals(29, calendarIT.daysInMonth(Year.of(2020), Month.of(2)), "February should have 29 days"),
211 () -> assertEquals(31, calendarIT.daysInMonth(Year.of(2020), Month.of(3)), "March should have 31 days"),
212 () -> assertEquals(30, calendarIT.daysInMonth(Year.of(2020), Month.of(4)), "April should have 30 days"),
213 () -> assertEquals(31, calendarIT.daysInMonth(Year.of(2020), Month.of(5)), "May should have 31 days"),
214 () -> assertEquals(30, calendarIT.daysInMonth(Year.of(2020), Month.of(6)), "June should have 30 days"),
215 () -> assertEquals(31, calendarIT.daysInMonth(Year.of(2020), Month.of(7)), "July should have 31 days"),
216 () -> assertEquals(31, calendarIT.daysInMonth(Year.of(2020), Month.of(8)), "August should have 31 days"),
217 () -> assertEquals(30, calendarIT.daysInMonth(Year.of(2020), Month.of(9)), "September should have 30 days"),
218 () -> assertEquals(31, calendarIT.daysInMonth(Year.of(2020), Month.of(10)), "October should have 31 days"),
219 () -> assertEquals(30, calendarIT.daysInMonth(Year.of(2020), Month.of(11)), "November should have 30 days"),
220 () -> assertEquals(31, calendarIT.daysInMonth(Year.of(2020), Month.of(12)), "December should have 31 days"),
221 () -> assertEquals(28, calendarIT.daysInMonth(Year.of(2019), Month.of(2)), "February should have 28 days"),
222 () -> assertEquals(21, calendarIT.daysInMonth(Year.of(1582), Month.of(10)), "October 1582 should have 21 days"),
223 () -> assertEquals(15, calendarRU.daysInMonth(Year.of(1918), Month.of(2)), "February 1918 should have 15 days"),
224 () -> assertEquals(30, calendarIT.daysInMonth(Year.of(1582), Month.of(9)), "September 1582 should have 30 days")
225 );
226 }
227
228 }