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.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   * Gregorian date tests.
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     * IT italy.
35     */
36    private static final String IT = "IT"; //$NON-NLS-1$
37  
38    /**
39     * Test comapre to constant.
40     */
41    private static final String TEST_COMPARE_TO = "testCompareTo"; //$NON-NLS-1$
42  
43    /**
44     * Date not as expected constant.
45     */
46    private static final String DATE_NOT_AS_EXPECTED = "Date not as expected"; //$NON-NLS-1$
47  
48    /**
49     * Date separator constant.
50     */
51    private static final String SEPARATOR = "-"; //$NON-NLS-1$
52  
53    /**
54     * Illegal argument exception expected constant.
55     */
56    private static final String ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED = "Illegal argument exception expected"; //$NON-NLS-1$
57  
58    /**
59     * Date constant.
60     */
61    private static final String DATE_2020_02_29 = "2020-02-29"; //$NON-NLS-1$
62  
63    /**
64     * Date constant.
65     */
66    private static final String DATE_2020_07_14 = "2020-07-14"; //$NON-NLS-1$
67  
68  
69    /**
70     * Default constructor.
71     */
72    /* default */ GregorianDateTests()
73     {
74      super();
75     }
76  
77  
78    /**
79     * Test correct GregorianDate.
80     *
81     * @param date ISO8601 date string
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    /* default */ 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     * Test wrong date.
95     *
96     * @param date ISO8601 date string
97     */
98    @ParameterizedTest
99    @ValueSource(strings = {"2019-02-29", "2020-04-31"})
100   /* default */ 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       /* final GregorianDate cleanDate = */ GregorianDate.of(year, month, day);
109      }, ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
110     );
111    }
112 
113 
114   /**
115    * Test GregorianDate.
116    */
117   @Test
118   /* default */ 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    * Test GregorianDate.
127    */
128   @Test
129   /* default */ void testOf2()
130    {
131     assertThrows(IllegalArgumentException.class, () ->
132      {
133       /* final GregorianDate cleanDate = */ GregorianDate.of("2020");
134      }, ILLEGAL_ARGUMENT_EXCEPTION_EXPECTED
135     );
136    }
137 
138 
139   /**
140    * Test get date.
141    */
142   @Test
143   /* default */ 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    * Test hash code.
152    */
153   @Test
154   /* default */ 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", //$NON-NLS-1$
160       () -> assertEquals(date1.hashCode(), date2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
161       () -> assertNotEquals(date1.hashCode(), date3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
162     );
163    }
164 
165 
166   /**
167    * Test equals.
168    */
169   @Test
170   @SuppressWarnings("java:S5785")
171   /* default */ 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", //$NON-NLS-1$
180       () -> assertTrue(date1.equals(date1), "date11 is not equal"), //$NON-NLS-1$
181       () -> assertTrue(date1.equals(date2), "date12 are not equal"), //$NON-NLS-1$
182       () -> assertTrue(date2.equals(date1), "date21 are not equal"), //$NON-NLS-1$
183       () -> assertTrue(date2.equals(date4), "date24 are not equal"), //$NON-NLS-1$
184       () -> assertTrue(date1.equals(date4), "date14 are not equal"), //$NON-NLS-1$
185       () -> assertFalse(date1.equals(date3), "date13 are equal"), //$NON-NLS-1$
186       () -> assertFalse(date3.equals(date1), "date31 are equal"), //$NON-NLS-1$
187       () -> assertFalse(date1.equals(null), "date10 is equal"), //$NON-NLS-1$
188       () -> assertFalse(date1.equals(date5), "date15 are equal"), //$NON-NLS-1$
189       () -> assertFalse(date1.equals(date6), "date16 are equal") //$NON-NLS-1$
190     );
191    }
192 
193 
194   /**
195    * Test toString.
196    */
197   @Test
198   /* default */ 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"); //$NON-NLS-1$ //$NON-NLS-2$
202    }
203 
204 
205   /**
206    * Test compareTo.
207    */
208   @Test
209   @SuppressWarnings("java:S5785")
210   /* default */ 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"), //$NON-NLS-1$
219       () -> assertTrue(date1.compareTo(date3) == -date3.compareTo(date1), "reflexive2"), //$NON-NLS-1$
220       () -> assertTrue((date4.compareTo(date3) > 0) && (date3.compareTo(date1) > 0) && (date4.compareTo(date1) > 0), "transitive1"), //$NON-NLS-1$
221       () -> assertTrue((date1.compareTo(date2) == 0) && (Math.abs(date1.compareTo(date5)) == Math.abs(date2.compareTo(date5))), "sgn1"), //$NON-NLS-1$
222       () -> assertTrue((date1.compareTo(date2) == 0) && date1.equals(date2), "equals") //$NON-NLS-1$
223     );
224    }
225 
226 
227   /**
228    * Test easter calculation.
229    */
230   @Test
231   /* default */ 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"), //$NON-NLS-1$
244       () -> assertEquals(GregorianDate.of(Year.of(2018), Month.of(4), Day.of(1)), easter2018, "Easter 2018 on wrong date"), //$NON-NLS-1$
245       () -> assertEquals(GregorianDate.of(Year.of(2015), Month.of(4), Day.of(5)), easter2015, "Easter 2015 on wrong date"), //$NON-NLS-1$
246       () -> assertEquals(GregorianDate.of(Year.of(2014), Month.of(4), Day.of(20)), easter2014, "Easter 2014 on wrong date"), //$NON-NLS-1$
247       () -> assertEquals(GregorianDate.of(Year.of(2011), Month.of(4), Day.of(24)), easter2011, "Easter 2011 on wrong date"), //$NON-NLS-1$
248       () -> assertEquals(GregorianDate.of(Year.of(2008), Month.of(3), Day.of(23)), easter2008, "Easter 2008 on wrong date"), //$NON-NLS-1$
249       () -> assertEquals(GregorianDate.of(Year.of(2005), Month.of(3), Day.of(27)), easter2005, "Easter 2005 on wrong date"), //$NON-NLS-1$
250       () -> assertEquals(GregorianDate.of(Year.of(1997), Month.of(3), Day.of(30)), easter1997, "Easter 1997 on wrong date"), //$NON-NLS-1$
251       () -> assertEquals(GregorianDate.of(Year.of(1991), Month.of(3), Day.of(31)), easter1991, "Easter 1991 on wrong date") //$NON-NLS-1$
252     );
253    }
254 
255  }