]> git.mxchange.org Git - jcountry-core.git/blob - test/org/mxchange/jcountry/model/data/CountryDataObjectTest.java
9ecb7541fb39cb24d28e8d9be52d727ebd3a1da3
[jcountry-core.git] / test / org / mxchange / jcountry / model / data / CountryDataObjectTest.java
1 /*
2  * Copyright (C) 2022 Roland Häder<roland@mxchange.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcountry.model.data;
18
19 import java.util.Date;
20 import org.testng.Assert;
21 import org.testng.annotations.DataProvider;
22 import org.testng.annotations.Test;
23
24 /**
25  * Test cases for CountryData class as an object (no entity-related tests)
26  * <p>
27  * @author Roland Häder<roland@mxchange.org>
28  */
29 public class CountryDataObjectTest {
30
31         private static final String ABROAD_DIAL_PREFIX = "+"; //NOI18N
32
33         private static final String COUNTRY_CODE = "DE"; //NOI18N
34
35         private static final String COUNTRY_I18N_KEY = "COUNTRY_GERMANY"; //NOI18N
36
37         private static final Short COUNTRY_PHONE_CODE0 = 0;
38
39         private static final Short COUNTRY_PHONE_CODE1 = 49;
40
41         private static final String EXTERNAL_DIAL_PREFIX = "0"; //NOI18N
42
43         /**
44          * Default constructor
45          */
46         public CountryDataObjectTest () {
47         }
48
49         @DataProvider (name = "same-country-provider")
50         public Object[][] createSameCountryObjectses () {
51                 return new Object[][]{
52                         {new CountryData(), new CountryData()},
53                         {new CountryData(
54                                 ABROAD_DIAL_PREFIX,
55                                 COUNTRY_CODE,
56                                 EXTERNAL_DIAL_PREFIX,
57                                 COUNTRY_I18N_KEY,
58                                 Boolean.TRUE,
59                                 COUNTRY_PHONE_CODE1
60                                 ), new CountryData(
61                          ABROAD_DIAL_PREFIX,
62                          COUNTRY_CODE,
63                          EXTERNAL_DIAL_PREFIX,
64                          COUNTRY_I18N_KEY,
65                          Boolean.TRUE,
66                          COUNTRY_PHONE_CODE1
67                          )}
68                 };
69         }
70
71         @Test (description = "Tests method Country.compareTo() responding to a null reference", expectedExceptions = NullPointerException.class)
72         public void testCompareToNullReference () {
73                 // Just a dummy ...
74                 final Country country = new CountryData();
75
76                 // This should cause a NPE with a non-empty message
77                 country.compareTo(null);
78         }
79
80         @Test (description = "Tests method Country.compareTo() responding to same country data", dataProvider = "same-country-provider")
81         public void testCompareToSameCountryData (final Country country1, final Country country2) {
82                 // Should always be 0
83                 Assert.assertEquals(country1.compareTo(country2), 0);
84         }
85
86         @Test (description = "Tests method Country.compareTo() responding to same instance ")
87         public void testCompareToSameInstance () {
88                 final Country country1 = new CountryData();
89                 Assert.assertEquals(country1.compareTo(country1), 0);
90         }
91
92         @Test (description = "Tests constructor with abroadDialPrefix being empty", expectedExceptions = IllegalArgumentException.class)
93         public void testConstructorEmptyAbroadDialPrefix () {
94                 // Should throw a NPE
95                 final Country country = new CountryData(
96                                           "", //NOI18N
97                                           COUNTRY_CODE,
98                                           EXTERNAL_DIAL_PREFIX,
99                                           COUNTRY_I18N_KEY,
100                                           Boolean.TRUE,
101                                           COUNTRY_PHONE_CODE1
102                           );
103
104                 // NEVER REACHED!
105                 Assert.fail("The required parameter must never be empty and if so must cause an IAE being thrown: country=" + country); //NOI18N
106         }
107
108         @Test (description = "Tests constructor with countryCodde being empty", expectedExceptions = IllegalArgumentException.class)
109         public void testConstructorEmptyCountryCode () {
110                 // Should throw a NPE
111                 final Country country = new CountryData(
112                                           ABROAD_DIAL_PREFIX,
113                                           "", //NOI18N
114                                           EXTERNAL_DIAL_PREFIX,
115                                           COUNTRY_I18N_KEY,
116                                           Boolean.TRUE,
117                                           COUNTRY_PHONE_CODE1
118                           );
119
120                 // NEVER REACHED!
121                 Assert.fail("The required parameter must never be empty and if so must cause an IAE being thrown: country=" + country); //NOI18N
122         }
123
124         @Test (description = "Tests constructor with countryI18nKey being empty", expectedExceptions = IllegalArgumentException.class)
125         public void testConstructorEmptyCountryI18nKey () {
126                 // Should throw a NPE
127                 final Country country = new CountryData(
128                                           ABROAD_DIAL_PREFIX,
129                                           COUNTRY_CODE,
130                                           EXTERNAL_DIAL_PREFIX,
131                                           "", //NOI18N
132                                           Boolean.TRUE,
133                                           COUNTRY_PHONE_CODE1
134                           );
135
136                 // NEVER REACHED!
137                 Assert.fail("The required parameter must never be empty and if so must cause an IAE being thrown: country=" + country); //NOI18N
138         }
139
140         @Test (description = "Tests constructor with countryExternalDialPrefix being empty", expectedExceptions = IllegalArgumentException.class)
141         public void testConstructorEmptyExternalDialPrefix () {
142                 // Should throw a NPE
143                 final Country country = new CountryData(
144                                           ABROAD_DIAL_PREFIX,
145                                           COUNTRY_CODE,
146                                           "", //NOI18N
147                                           COUNTRY_I18N_KEY,
148                                           Boolean.TRUE,
149                                           COUNTRY_PHONE_CODE1
150                           );
151
152                 // NEVER REACHED!
153                 Assert.fail("The required parameter must never be invalid and if so must cause an IAE being thrown: country=" + country); //NOI18N
154         }
155
156         @Test (description = "Tests constructor with invalid countryPhoneCode", expectedExceptions = IllegalArgumentException.class)
157         public void testConstructorInvalidCountryPhoneCode () {
158                 // Should throw a NPE
159                 final Country country = new CountryData(
160                                           ABROAD_DIAL_PREFIX,
161                                           COUNTRY_CODE,
162                                           EXTERNAL_DIAL_PREFIX,
163                                           COUNTRY_I18N_KEY, //NOI18N
164                                           Boolean.TRUE,
165                                           COUNTRY_PHONE_CODE0
166                           );
167
168                 // NEVER REACHED!
169                 Assert.fail("The required parameter must never be invalid or else an E being thrown: country=" + country); //NOI18N
170         }
171
172         @Test (description = "Tests constructor with abroadDialPrefix being null", expectedExceptions = NullPointerException.class)
173         public void testConstructorNullAbroadDialPrefix () {
174                 // Should throw a NPE
175                 final Country country = new CountryData(
176                                           null,
177                                           COUNTRY_CODE,
178                                           EXTERNAL_DIAL_PREFIX,
179                                           COUNTRY_I18N_KEY,
180                                           Boolean.TRUE,
181                                           COUNTRY_PHONE_CODE1
182                           );
183
184                 // NEVER REACHED!
185                 Assert.fail("The required parameter must never be null and if so must cause a NPE being thrown: country=" + country); //NOI18N
186         }
187
188         @Test (description = "Tests constructor with countryCodde being null", expectedExceptions = NullPointerException.class)
189         public void testConstructorNullCountryCode () {
190                 // Should throw a NPE
191                 final Country country = new CountryData(
192                                           ABROAD_DIAL_PREFIX,
193                                           null,
194                                           EXTERNAL_DIAL_PREFIX,
195                                           COUNTRY_I18N_KEY,
196                                           Boolean.TRUE,
197                                           COUNTRY_PHONE_CODE1
198                           );
199
200                 // NEVER REACHED!
201                 Assert.fail("The required parameter must never be null and if so must cause a NPE being thrown: country=" + country); //NOI18N
202         }
203
204         @Test (description = "Tests constructor with countryI18nKey being null", expectedExceptions = NullPointerException.class)
205         public void testConstructorNullCountryI18nKey () {
206                 // Should throw a NPE
207                 final Country country = new CountryData(
208                                           ABROAD_DIAL_PREFIX,
209                                           COUNTRY_CODE,
210                                           EXTERNAL_DIAL_PREFIX,
211                                           null,
212                                           Boolean.TRUE,
213                                           COUNTRY_PHONE_CODE1
214                           );
215
216                 // NEVER REACHED!
217                 Assert.fail("The required parameter must never be null and if so must cause a NPE being thrown: country=" + country); //NOI18N
218         }
219
220         @Test (description = "Tests constructor with countryIsLocalPrefixRequired being null", expectedExceptions = NullPointerException.class)
221         public void testConstructorNullCountryIsLocalPrefixRequired () {
222                 // Should throw a NPE
223                 final Country country = new CountryData(
224                                           ABROAD_DIAL_PREFIX,
225                                           COUNTRY_CODE,
226                                           EXTERNAL_DIAL_PREFIX,
227                                           COUNTRY_I18N_KEY,
228                                           null,
229                                           COUNTRY_PHONE_CODE1
230                           );
231
232                 // NEVER REACHED!
233                 Assert.fail("The required parameter must never be null and if so must cause a NPE being thrown: country=" + country); //NOI18N
234         }
235
236         @Test (description = "Tests constructor with countryPhoneCode being null", expectedExceptions = NullPointerException.class)
237         public void testConstructorNullCountryPhoneCode () {
238                 // Should throw a NPE
239                 final Country country = new CountryData(
240                                           ABROAD_DIAL_PREFIX,
241                                           COUNTRY_CODE,
242                                           EXTERNAL_DIAL_PREFIX,
243                                           COUNTRY_I18N_KEY,
244                                           Boolean.TRUE,
245                                           null
246                           );
247
248                 // NEVER REACHED!
249                 Assert.fail("The required parameter must never be null and if so must cause a NPE being thrown: country=" + country); //NOI18N
250         }
251
252         @Test (description = "Tests constructor with countryExternalDialPrefix being null", expectedExceptions = NullPointerException.class)
253         public void testConstructorNullExternalDialPrefix () {
254                 // Should throw a NPE
255                 final Country country = new CountryData(
256                                           ABROAD_DIAL_PREFIX,
257                                           COUNTRY_CODE,
258                                           null,
259                                           COUNTRY_I18N_KEY,
260                                           Boolean.TRUE,
261                                           COUNTRY_PHONE_CODE1
262                           );
263
264                 // NEVER REACHED!
265                 Assert.fail("The required parameter must never be null and if so must cause a NPE being thrown: country=" + country); //NOI18N
266         }
267
268         @Test (description = "Tests Country.countryEntryCreated getter/setter")
269         public void testCountryEntryCreated () {
270                 // Empty instance
271                 final Country country = new CountryData();
272                 final Date date = new Date();
273
274                 // Set it
275                 country.setCountryEntryCreated(date);
276
277                 // Should be the same!
278                 Assert.assertEquals(country.getCountryEntryCreated(), date);
279         }
280
281         @Test (description = "Tests Country.countryEntryUpdated getter/setter")
282         public void testCountryEntryUpdated () {
283                 // Empty instance
284                 final Country country = new CountryData();
285                 final Date date = new Date();
286
287                 // Set it
288                 country.setCountryEntryUpdated(date);
289
290                 // Should be the same!
291                 Assert.assertEquals(country.getCountryEntryUpdated(), date);
292         }
293
294         @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider")
295         public void testEqualsDifferentAbroadDialPrefix (final Country country1, final Country country2) {
296                 // First change countryAbroadDialPrefix to something else
297                 country1.setCountryAbroadDialPrefix("++"); //NOI18N
298                 Assert.assertFalse(country1.equals(country2));
299                 country1.setCountryAbroadDialPrefix(ABROAD_DIAL_PREFIX);
300         }
301
302         @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider")
303         public void testEqualsDifferentCountryCode (final Country country1, final Country country2) {
304                 // Next is countryCode
305                 country1.setCountryCode("XX"); //NOI18N
306                 Assert.assertFalse(country1.equals(country2));
307                 country1.setCountryCode(COUNTRY_CODE);
308         }
309
310         @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider")
311         public void testEqualsDifferentCountryId (final Country country1, final Country country2) {
312                 // Next is countryId
313                 country1.setCountryId(1l); //NOI18N
314                 Assert.assertFalse(country1.equals(country2));
315                 country1.setCountryId(null);
316         }
317
318         @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider")
319         public void testEqualsDifferentExternalDialPrefix (final Country country1, final Country country2) {
320                 // Next is countryExternalDialPrefix
321                 country1.setCountryExternalDialPrefix("00"); //NOI18N
322                 Assert.assertFalse(country1.equals(country2));
323                 country1.setCountryCode(EXTERNAL_DIAL_PREFIX);
324         }
325
326         @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider")
327         public void testEqualsDifferentI18nKey (final Country country1, final Country country2) {
328                 // Next is countryI18nKey
329                 country1.setCountryI18nKey("COUNTRY_XX"); //NOI18N
330                 Assert.assertFalse(country1.equals(country2));
331                 country1.setCountryI18nKey(COUNTRY_I18N_KEY);
332         }
333
334         @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider")
335         public void testEqualsDifferentIsLocalDialPrefixRequired (final Country country1, final Country country2) {
336                 // Next is countryId
337                 country1.setCountryIsLocalPrefixRequired(Boolean.FALSE);
338                 Assert.assertFalse(country1.equals(country2));
339                 country1.setCountryIsLocalPrefixRequired(Boolean.TRUE);
340         }
341
342         @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider")
343         public void testEqualsDifferentPhoneCode (final Country country1, final Country country2) {
344                 // Next is countryId
345                 country1.setCountryPhoneCode(COUNTRY_PHONE_CODE0);
346                 Assert.assertFalse(country1.equals(country2));
347                 country1.setCountryPhoneCode(COUNTRY_PHONE_CODE1);
348         }
349
350         @Test (description = "Tests if Country.equals() returns false when a null reference is provided")
351         @SuppressWarnings ("ObjectEqualsNull")
352         public void testEqualsNullReference () {
353                 final Country country = new CountryData();
354                 Assert.assertFalse(country.equals(null));
355         }
356
357         @Test (description = "Tests if Country.equals() returns false when an other object is provided")
358         public void testEqualsOtherObject () {
359                 final Country country = new CountryData();
360                 Assert.assertFalse(country.equals(new Object()));
361         }
362
363         @Test (description = "Tests if Country.equals() returns true when same instance is provided")
364         public void testEqualsSameInstance () {
365                 final Country country = new CountryData();
366                 Assert.assertTrue(country.equals(country));
367         }
368
369 }