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.City;
19  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20  
21  
22  /**
23   * City tests.
24   */
25  @SuppressFBWarnings({"EC_NULL_ARG", "RV_NEGATING_RESULT_OF_COMPARETO", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", "SPP_USE_ZERO_WITH_COMPARATOR"})
26  final class CityTests
27   {
28    /**
29     * Bremen.
30     */
31    private static final String BREMEN = "Bremen"; //$NON-NLS-1$
32  
33    /**
34     * Hannover.
35     */
36    private static final String HANNOVER = "Hannover"; //$NON-NLS-1$
37  
38    /**
39     * Illegal argument exception expected.
40     */
41    private static final String ILLEGAL_ARGUMENT = "Illegal argument exception expected"; //$NON-NLS-1$
42  
43    /**
44     * City not as expected.
45     */
46    private static final String CITY_NOT_AS_EXPECTED = "City not as expected"; //$NON-NLS-1$
47  
48  
49    /**
50     * Default constructor.
51     */
52    /* default */ CityTests()
53     {
54      super();
55     }
56  
57  
58    /**
59     * Test correct City.
60     *
61     * @param city City
62     */
63    @ParameterizedTest
64    @ValueSource(strings = {CityTests.BREMEN, "A", "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg"})
65    /* default */ void testCityCorrect(final String city)
66     {
67      final City cleanCity = City.of(city);
68      assertEquals(city, cleanCity.stringValue(), CityTests.CITY_NOT_AS_EXPECTED);
69     }
70  
71  
72    /**
73     * Test City with wrong lengths.
74     *
75     * @param city City
76     */
77    @ParameterizedTest
78    @ValueSource(strings = {"", "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefgh"})
79    /* default */ void testCityLength(final String city)
80     {
81      assertThrows(IllegalArgumentException.class, () ->
82       {
83        /* final City cleanCity = */ City.of(city);
84       }, CityTests.ILLEGAL_ARGUMENT
85      );
86     }
87  
88  
89    /**
90     * Test wrong City.
91     *
92     * @param city City
93     */
94    @ParameterizedTest
95    @ValueSource(strings = {"Bremen0815", "abc_def"})
96    /* default */ void testCityWrong(final String city)
97     {
98      assertThrows(IllegalArgumentException.class, () ->
99       {
100       /* final City cleanCity = */ City.of(city);
101      }, CityTests.ILLEGAL_ARGUMENT
102     );
103    }
104 
105 
106   /**
107    * Test get city.
108    */
109   @Test
110   /* default */ void testStringValue()
111    {
112     final City city = City.of(CityTests.BREMEN);
113     assertEquals(CityTests.BREMEN, city.stringValue(), CityTests.CITY_NOT_AS_EXPECTED);
114    }
115 
116 
117   /**
118    * Test hash code.
119    */
120   @Test
121   /* default */ void testHashCode()
122    {
123     final City city1 = City.of(CityTests.BREMEN);
124     final City city2 = City.of(CityTests.BREMEN);
125     final City city3 = City.of(CityTests.HANNOVER);
126     assertAll("testHashCode", //$NON-NLS-1$
127       () -> assertEquals(city1.hashCode(), city2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
128       () -> assertNotEquals(city1.hashCode(), city3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
129     );
130    }
131 
132 
133   /**
134    * Test equals.
135    */
136   @Test
137   @SuppressWarnings("java:S5785")
138   /* default */ void testEquals()
139    {
140     final City city1 = City.of(CityTests.BREMEN);
141     final City city2 = City.of(CityTests.BREMEN);
142     final City city3 = City.of(CityTests.HANNOVER);
143     final City city4 = City.of(CityTests.BREMEN);
144     assertAll("testEquals", //$NON-NLS-1$
145       () -> assertTrue(city1.equals(city1), "city11 is not equal"), //$NON-NLS-1$
146       () -> assertTrue(city1.equals(city2), "city12 are not equal"), //$NON-NLS-1$
147       () -> assertTrue(city2.equals(city1), "city21 are not equal"), //$NON-NLS-1$
148       () -> assertTrue(city2.equals(city4), "city24 are not equal"), //$NON-NLS-1$
149       () -> assertTrue(city1.equals(city4), "city14 are not equal"), //$NON-NLS-1$
150       () -> assertFalse(city1.equals(city3), "city13 are equal"), //$NON-NLS-1$
151       () -> assertFalse(city3.equals(city1), "city31 are equal"), //$NON-NLS-1$
152       () -> assertFalse(city1.equals(null), "city10 is equal") //$NON-NLS-1$
153     );
154    }
155 
156 
157   /**
158    * Test toString.
159    */
160   @Test
161   /* default */ void testToString()
162    {
163     final City city = City.of(CityTests.BREMEN);
164     assertEquals("City[city=Bremen]", city.toString(), "toString not equal"); //$NON-NLS-1$ //$NON-NLS-2$
165    }
166 
167 
168   /**
169    * Test compareTo.
170    */
171   @Test
172   @SuppressWarnings("java:S5785")
173   /* default */ void testCompareTo()
174    {
175     final City city1 = City.of(CityTests.BREMEN);
176     final City city2 = City.of(CityTests.BREMEN);
177     final City city3 = City.of("Hamburg"); //$NON-NLS-1$
178     final City city4 = City.of(CityTests.HANNOVER);
179     final City city5 = City.of(CityTests.BREMEN);
180     assertAll("testCompareTo", //$NON-NLS-1$
181       () -> assertTrue(city1.compareTo(city2) == -city2.compareTo(city1), "reflexive1"), //$NON-NLS-1$
182       () -> assertTrue(city1.compareTo(city3) == -city3.compareTo(city1), "reflexive2"), //$NON-NLS-1$
183       () -> assertTrue((city4.compareTo(city3) > 0) && (city3.compareTo(city1) > 0) && (city4.compareTo(city1) > 0), "transitive1"), //$NON-NLS-1$
184       () -> assertTrue((city1.compareTo(city2) == 0) && (Math.abs(city1.compareTo(city5)) == Math.abs(city2.compareTo(city5))), "sgn1"), //$NON-NLS-1$
185       () -> assertTrue((city1.compareTo(city2) == 0) && city1.equals(city2), "equals") //$NON-NLS-1$
186     );
187    }
188 
189  }