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