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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20
21
22
23
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 CountryTests
27 {
28
29
30
31 private static final String FR = "FR";
32
33
34
35
36 private static final String DE = "DE";
37
38
39
40
41 private static final String ILLEGAL_ARGUMENT = "Illegal argument exception expected";
42
43
44
45
46 private static final String COUNTRY_CODE_NOT_AS_EXPECTED = "Country code not as expected";
47
48
49
50
51
52 CountryTests()
53 {
54 super();
55 }
56
57
58
59
60
61
62
63 @ParameterizedTest
64 @ValueSource(strings = {CountryTests.DE})
65 void testCountryOk0(final String alpha2)
66 {
67 final Country cleanCountry = Country.of(alpha2);
68 assertEquals(alpha2, cleanCountry.stringValue(), CountryTests.COUNTRY_CODE_NOT_AS_EXPECTED);
69 }
70
71
72
73
74
75
76
77 @ParameterizedTest
78 @ValueSource(strings = {"D", "DEA"})
79 void testCountryLength(final String alpha2)
80 {
81 assertThrows(IllegalArgumentException.class, () ->
82 {
83 Country.of(alpha2);
84 }, CountryTests.ILLEGAL_ARGUMENT
85 );
86 }
87
88
89
90
91
92
93
94 @ParameterizedTest
95 @ValueSource(strings = {"D~", "ZZ"})
96 void testCountryIllegalParameters(final String alpha2)
97 {
98 assertThrows(IllegalArgumentException.class, () ->
99 {
100 Country.of(alpha2);
101 }, CountryTests.ILLEGAL_ARGUMENT
102 );
103 }
104
105
106
107
108
109 @Test
110 void testStringValue()
111 {
112 final Country country = Country.of(CountryTests.DE);
113 assertEquals(CountryTests.DE, country.stringValue(), CountryTests.COUNTRY_CODE_NOT_AS_EXPECTED);
114 }
115
116
117
118
119
120 @Test
121 void testHashCode()
122 {
123 final Country country1 = Country.of(CountryTests.DE);
124 final Country country2 = Country.of(CountryTests.DE);
125 final Country country3 = Country.of(CountryTests.FR);
126 assertAll("testHashCode",
127 () -> assertEquals(country1.hashCode(), country2.hashCode(), "hashCodes are not equal"),
128 () -> assertNotEquals(country1.hashCode(), country3.hashCode(), "hashCodes are equal")
129 );
130 }
131
132
133
134
135
136 @Test
137 @SuppressWarnings("java:S5785")
138 void testEquals()
139 {
140 final Country country1 = Country.of(CountryTests.DE);
141 final Country country2 = Country.of(CountryTests.DE);
142 final Country country3 = Country.of(CountryTests.FR);
143 final Country country4 = Country.of(CountryTests.DE);
144 assertAll("testEquals",
145 () -> assertTrue(country1.equals(country1), "country11 is not equal"),
146 () -> assertTrue(country1.equals(country2), "country12 are not equal"),
147 () -> assertTrue(country2.equals(country1), "country21 are not equal"),
148 () -> assertTrue(country2.equals(country4), "country24 are not equal"),
149 () -> assertTrue(country1.equals(country4), "country14 are not equal"),
150 () -> assertFalse(country1.equals(country3), "country13 are equal"),
151 () -> assertFalse(country3.equals(country1), "country31 are equal"),
152 () -> assertFalse(country1.equals(null), "country10 is equal")
153 );
154 }
155
156
157
158
159
160 @Test
161 void testToString()
162 {
163 final Country country = Country.of(CountryTests.DE);
164 assertEquals("Country[alpha2=DE]", country.toString(), "toString not equal");
165 }
166
167
168
169
170
171 @Test
172 @SuppressWarnings("java:S5785")
173 void testCompareTo()
174 {
175 final Country country1 = Country.of(CountryTests.DE);
176 final Country country2 = Country.of(CountryTests.DE);
177 final Country country3 = Country.of(CountryTests.FR);
178 final Country country4 = Country.of("GB");
179 final Country country5 = Country.of(CountryTests.DE);
180 assertAll("testCompareTo",
181 () -> assertTrue(country1.compareTo(country2) == -country2.compareTo(country1), "reflexive1"),
182 () -> assertTrue(country1.compareTo(country3) == -country3.compareTo(country1), "reflexive2"),
183 () -> assertTrue((country4.compareTo(country3) > 0) && (country3.compareTo(country1) > 0) && (country4.compareTo(country1) > 0), "transitive1"),
184 () -> assertTrue((country1.compareTo(country2) == 0) && (Math.abs(country1.compareTo(country5)) == Math.abs(country2.compareTo(country5))), "sgn1"),
185 () -> assertTrue((country1.compareTo(country2) == 0) && country1.equals(country2), "equals")
186 );
187 }
188
189 }