View Javadoc
1   /*
2    * Copyright (C) 2020-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
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   * Gregorian calendar tests.
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     * Test is leap year constant.
32     */
33    private static final String TEST_IS_LEAP_YEAR = "testIsLeapYear"; //$NON-NLS-1$
34  
35    /**
36     * DE germany.
37     */
38    private static final String DE = "DE"; //$NON-NLS-1$
39  
40    /**
41     * IT italy.
42     */
43    private static final String IT = "IT"; //$NON-NLS-1$
44  
45    /**
46     * RU russia.
47     */
48    private static final String RU = "RU"; //$NON-NLS-1$
49  
50    /**
51     * Calendar not as expected constant.
52     */
53    private static final String CALENDAR_NOT_AS_EXPECTED = "Calendar not as expected"; //$NON-NLS-1$
54  
55  
56    /**
57     * Default constructor.
58     */
59    /* default */ GregorianCalendarTests()
60     {
61      super();
62     }
63  
64  
65    /**
66     * Test correct GregorianCalendar.
67     *
68     * @param country Country name
69     */
70    @ParameterizedTest
71    @ValueSource(strings = {GregorianCalendarTests.IT})
72    /* default */ 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     * Test factory.
81     *
82     * @param country Country name
83     */
84    @ParameterizedTest
85    @ValueSource(strings = {GregorianCalendarTests.IT})
86    /* default */ 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     * Test string value.
95     */
96    @Test
97    /* default */ 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    * Test hash code.
106    */
107   @Test
108   /* default */ 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", //$NON-NLS-1$
114       () -> assertEquals(calendar1.hashCode(), calendar2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
115       () -> assertNotEquals(calendar1.hashCode(), calendar3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
116     );
117    }
118 
119 
120   /**
121    * Test equals.
122    */
123   @Test
124   @SuppressWarnings("java:S5785")
125   /* default */ 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", //$NON-NLS-1$
132       () -> assertTrue(calendar1.equals(calendar1), "calendar11 is not equal"), //$NON-NLS-1$
133       () -> assertTrue(calendar1.equals(calendar2), "calendar12 are not equal"), //$NON-NLS-1$
134       () -> assertTrue(calendar2.equals(calendar1), "calendar21 are not equal"), //$NON-NLS-1$
135       () -> assertTrue(calendar2.equals(calendar4), "calendar24 are not equal"), //$NON-NLS-1$
136       () -> assertTrue(calendar1.equals(calendar4), "calendar14 are not equal"), //$NON-NLS-1$
137       () -> assertFalse(calendar1.equals(calendar3), "calendar13 are equal"), //$NON-NLS-1$
138       () -> assertFalse(calendar3.equals(calendar1), "calendar31 are equal"), //$NON-NLS-1$
139       () -> assertFalse(calendar1.equals(null), "calendar10 is equal") //$NON-NLS-1$
140     );
141    }
142 
143 
144   /**
145    * Test toString.
146    */
147   @Test
148   /* default */ void testToString()
149    {
150     final GregorianCalendar calendar = GregorianCalendar.of(Country.of(GregorianCalendarTests.IT));
151     assertEquals("GregorianCalendar[country=IT]", calendar.toString(), "toString not equal"); //$NON-NLS-1$ //$NON-NLS-2$
152    }
153 
154 
155   /**
156    * Test compareTo.
157    */
158   @Test
159   @SuppressWarnings("java:S5785")
160   /* default */ 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")); //$NON-NLS-1$
166     final GregorianCalendar calendar5 = GregorianCalendar.of(Country.of(GregorianCalendarTests.IT));
167     assertAll("testCompareTo", //$NON-NLS-1$
168       () -> assertTrue(calendar1.compareTo(calendar2) == -calendar2.compareTo(calendar1), "reflexive1"), //$NON-NLS-1$
169       () -> assertTrue(calendar1.compareTo(calendar3) == -calendar3.compareTo(calendar1), "reflexive2"), //$NON-NLS-1$
170       () -> assertTrue((calendar4.compareTo(calendar3) > 0) && (calendar3.compareTo(calendar1) > 0) && (calendar4.compareTo(calendar1) > 0), "transitive1"), //$NON-NLS-1$
171       () -> assertTrue((calendar1.compareTo(calendar2) == 0) && (Math.abs(calendar1.compareTo(calendar5)) == Math.abs(calendar2.compareTo(calendar5))), "sgn1"), //$NON-NLS-1$
172       () -> assertTrue((calendar1.compareTo(calendar2) == 0) && calendar1.equals(calendar2), "equals") //$NON-NLS-1$
173     );
174    }
175 
176 
177   /**
178    * Test isLeapYear.
179    */
180   @Test
181   /* default */ 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"), //$NON-NLS-1$
188       () -> assertTrue(calendarIT.isLeapYear(Year.of(2000)), "2000 is a leap year"), //$NON-NLS-1$
189       () -> assertTrue(calendarIT.isLeapYear(Year.of(2020)), "2020 is a leap year"), //$NON-NLS-1$
190       () -> assertFalse(calendarIT.isLeapYear(Year.of(2019)), "2019 is not a leap year"), //$NON-NLS-1$
191       () -> assertFalse(calendarIT.isLeapYear(Year.of(1582)), "1582 is not a leap year"), //$NON-NLS-1$
192       () -> assertTrue(calendarIT.isLeapYear(Year.of(1580)), "1580 is a leap year"), //$NON-NLS-1$
193       () -> assertTrue(calendarDE.isLeapYear(Year.of(1580)), "1700 is not a leap year"), //$NON-NLS-1$
194       () -> assertTrue(calendarRU.isLeapYear(Year.of(1920)), "1920 is a leap year"), //$NON-NLS-1$
195       () -> assertTrue(calendarRU.isLeapYear(Year.of(1900)), "1900 is a leap year") //$NON-NLS-1$
196      );
197    }
198 
199 
200   /**
201    * Test daysInMonth.
202    */
203   @Test
204   /* default */ 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"), //$NON-NLS-1$
210       () -> assertEquals(29, calendarIT.daysInMonth(Year.of(2020), Month.of(2)), "February should have 29 days"), //$NON-NLS-1$
211       () -> assertEquals(31, calendarIT.daysInMonth(Year.of(2020), Month.of(3)), "March should have 31 days"), //$NON-NLS-1$
212       () -> assertEquals(30, calendarIT.daysInMonth(Year.of(2020), Month.of(4)), "April should have 30 days"), //$NON-NLS-1$
213       () -> assertEquals(31, calendarIT.daysInMonth(Year.of(2020), Month.of(5)), "May should have 31 days"), //$NON-NLS-1$
214       () -> assertEquals(30, calendarIT.daysInMonth(Year.of(2020), Month.of(6)), "June should have 30 days"), //$NON-NLS-1$
215       () -> assertEquals(31, calendarIT.daysInMonth(Year.of(2020), Month.of(7)), "July should have 31 days"), //$NON-NLS-1$
216       () -> assertEquals(31, calendarIT.daysInMonth(Year.of(2020), Month.of(8)), "August should have 31 days"), //$NON-NLS-1$
217       () -> assertEquals(30, calendarIT.daysInMonth(Year.of(2020), Month.of(9)), "September should have 30 days"), //$NON-NLS-1$
218       () -> assertEquals(31, calendarIT.daysInMonth(Year.of(2020), Month.of(10)), "October should have 31 days"), //$NON-NLS-1$
219       () -> assertEquals(30, calendarIT.daysInMonth(Year.of(2020), Month.of(11)), "November should have 30 days"), //$NON-NLS-1$
220       () -> assertEquals(31, calendarIT.daysInMonth(Year.of(2020), Month.of(12)), "December should have 31 days"), //$NON-NLS-1$
221       () -> assertEquals(28, calendarIT.daysInMonth(Year.of(2019), Month.of(2)), "February should have 28 days"), //$NON-NLS-1$
222       () -> assertEquals(21, calendarIT.daysInMonth(Year.of(1582), Month.of(10)), "October 1582 should have 21 days"), //$NON-NLS-1$
223       () -> assertEquals(15, calendarRU.daysInMonth(Year.of(1918), Month.of(2)), "February 1918 should have 15 days"), //$NON-NLS-1$
224       () -> assertEquals(30, calendarIT.daysInMonth(Year.of(1582), Month.of(9)), "September 1582 should have 30 days") //$NON-NLS-1$
225      );
226    }
227 
228  }