From: Roland Häder Date: Thu, 3 Nov 2022 05:53:07 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;ds=sidebyside;h=fa6541d0c0b8ed1f5b457b161d74da5d2311d942;p=jcountry-core.git Continued: - added TestNG 6.8.1 - added unit tests for CountryUtils utilities class - added unit tests for CountryData being treated as a normal object (no entity) --- diff --git a/lib/nblibraries.properties b/lib/nblibraries.properties index 7bb190c..0687514 100644 --- a/lib/nblibraries.properties +++ b/lib/nblibraries.properties @@ -13,3 +13,7 @@ libs.jpa20-persistence.displayName=Persistence (JPA 2.1) libs.jpa20-persistence.javadoc=\ https://docs.oracle.com/javaee/7/api/ libs.jpa20-persistence.prop-maven-dependencies=org.eclipse.persistence:javax.persistence:2.1.0:jar +libs.testng.classpath=\ + ${base}/testng/testng-6.8.1-dist.jar +libs.testng.displayName=TestNG 6.8.1 +libs.testng.prop-maven-dependencies=org.testng:testng:6.8.1:jar diff --git a/lib/testng/testng-6.8.1-dist.jar b/lib/testng/testng-6.8.1-dist.jar new file mode 100644 index 0000000..11f1304 Binary files /dev/null and b/lib/testng/testng-6.8.1-dist.jar differ diff --git a/nbproject/project.properties b/nbproject/project.properties index 062c35f..3bf0936 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -46,7 +46,8 @@ javac.source=1.7 javac.target=1.7 javac.test.classpath=\ ${javac.classpath}:\ - ${build.classes.dir} + ${build.classes.dir}:\ + ${libs.testng.classpath} javac.test.processorpath=\ ${javac.test.classpath} javadoc.additionalparam= diff --git a/test/org/mxchange/jcountrycore/CountryCoreTestSuite.xml b/test/org/mxchange/jcountrycore/CountryCoreTestSuite.xml new file mode 100644 index 0000000..d5a28b5 --- /dev/null +++ b/test/org/mxchange/jcountrycore/CountryCoreTestSuite.xml @@ -0,0 +1,24 @@ + + + + + + + + + + diff --git a/test/org/mxchange/jcountrycore/test/model/data/CountryDataObjectTest.java b/test/org/mxchange/jcountrycore/test/model/data/CountryDataObjectTest.java new file mode 100644 index 0000000..f8a8fc6 --- /dev/null +++ b/test/org/mxchange/jcountrycore/test/model/data/CountryDataObjectTest.java @@ -0,0 +1,371 @@ +/* + * Copyright (C) 2022 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcountrycore.test.model.data; + +import java.util.Date; +import org.mxchange.jcountry.model.data.Country; +import org.mxchange.jcountry.model.data.CountryData; +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * Test cases for CountryData class as an object (no entity-related tests) + *

+ * @author Roland Häder + */ +public class CountryDataObjectTest { + + private static final String ABROAD_DIAL_PREFIX = "+"; //NOI18N + + private static final String COUNTRY_CODE = "DE"; //NOI18N + + private static final String COUNTRY_I18N_KEY = "COUNTRY_GERMANY"; //NOI18N + + private static final Short COUNTRY_PHONE_CODE0 = 0; + + private static final Short COUNTRY_PHONE_CODE1 = 49; + + private static final String EXTERNAL_DIAL_PREFIX = "0"; //NOI18N + + /** + * Default constructor + */ + public CountryDataObjectTest () { + } + + @DataProvider (name = "same-country-provider") + public Object[][] createSameCountryObjectses () { + return new Object[][]{ + {new CountryData(), new CountryData()}, + {new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + ), new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + )} + }; + } + + @Test (description = "Tests method Country.compareTo() responding to a null reference", expectedExceptions = NullPointerException.class) + public void testCompareToNullReference () { + // Just a dummy ... + final Country country = new CountryData(); + + // This should cause a NPE with a non-empty message + country.compareTo(null); + } + + @Test (description = "Tests method Country.compareTo() responding to same country data", dataProvider = "same-country-provider") + public void testCompareToSameCountryData (final Country country1, final Country country2) { + // Should always be 0 + Assert.assertEquals(country1.compareTo(country2), 0); + } + + @Test (description = "Tests method Country.compareTo() responding to same instance ") + public void testCompareToSameInstance () { + final Country country1 = new CountryData(); + Assert.assertEquals(country1.compareTo(country1), 0); + } + + @Test (description = "Tests constructor with abroadDialPrefix being empty", expectedExceptions = IllegalArgumentException.class) + public void testConstructorEmptyAbroadDialPrefix () { + // Should throw a NPE + final Country country = new CountryData( + "", //NOI18N + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + ); + + // NEVER REACHED! + Assert.fail("The required parameter must never be empty and if so must cause an IAE being thrown: country=" + country); //NOI18N + } + + @Test (description = "Tests constructor with countryCodde being empty", expectedExceptions = IllegalArgumentException.class) + public void testConstructorEmptyCountryCode () { + // Should throw a NPE + final Country country = new CountryData( + ABROAD_DIAL_PREFIX, + "", //NOI18N + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + ); + + // NEVER REACHED! + Assert.fail("The required parameter must never be empty and if so must cause an IAE being thrown: country=" + country); //NOI18N + } + + @Test (description = "Tests constructor with countryI18nKey being empty", expectedExceptions = IllegalArgumentException.class) + public void testConstructorEmptyCountryI18nKey () { + // Should throw a NPE + final Country country = new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + "", //NOI18N + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + ); + + // NEVER REACHED! + Assert.fail("The required parameter must never be empty and if so must cause an IAE being thrown: country=" + country); //NOI18N + } + + @Test (description = "Tests constructor with countryExternalDialPrefix being empty", expectedExceptions = IllegalArgumentException.class) + public void testConstructorEmptyExternalDialPrefix () { + // Should throw a NPE + final Country country = new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + "", //NOI18N + COUNTRY_I18N_KEY, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + ); + + // NEVER REACHED! + Assert.fail("The required parameter must never be invalid and if so must cause an IAE being thrown: country=" + country); //NOI18N + } + + @Test (description = "Tests constructor with invalid countryPhoneCode", expectedExceptions = IllegalArgumentException.class) + public void testConstructorInvalidCountryPhoneCode () { + // Should throw a NPE + final Country country = new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, //NOI18N + Boolean.TRUE, + COUNTRY_PHONE_CODE0 + ); + + // NEVER REACHED! + Assert.fail("The required parameter must never be invalid or else an E being thrown: country=" + country); //NOI18N + } + + @Test (description = "Tests constructor with abroadDialPrefix being null", expectedExceptions = NullPointerException.class) + public void testConstructorNullAbroadDialPrefix () { + // Should throw a NPE + final Country country = new CountryData( + null, + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + ); + + // NEVER REACHED! + Assert.fail("The required parameter must never be null and if so must cause a NPE being thrown: country=" + country); //NOI18N + } + + @Test (description = "Tests constructor with countryCodde being null", expectedExceptions = NullPointerException.class) + public void testConstructorNullCountryCode () { + // Should throw a NPE + final Country country = new CountryData( + ABROAD_DIAL_PREFIX, + null, + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + ); + + // NEVER REACHED! + Assert.fail("The required parameter must never be null and if so must cause a NPE being thrown: country=" + country); //NOI18N + } + + @Test (description = "Tests constructor with countryI18nKey being null", expectedExceptions = NullPointerException.class) + public void testConstructorNullCountryI18nKey () { + // Should throw a NPE + final Country country = new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + null, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + ); + + // NEVER REACHED! + Assert.fail("The required parameter must never be null and if so must cause a NPE being thrown: country=" + country); //NOI18N + } + + @Test (description = "Tests constructor with countryIsLocalPrefixRequired being null", expectedExceptions = NullPointerException.class) + public void testConstructorNullCountryIsLocalPrefixRequired () { + // Should throw a NPE + final Country country = new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, + null, + COUNTRY_PHONE_CODE1 + ); + + // NEVER REACHED! + Assert.fail("The required parameter must never be null and if so must cause a NPE being thrown: country=" + country); //NOI18N + } + + @Test (description = "Tests constructor with countryPhoneCode being null", expectedExceptions = NullPointerException.class) + public void testConstructorNullCountryPhoneCode () { + // Should throw a NPE + final Country country = new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, + Boolean.TRUE, + null + ); + + // NEVER REACHED! + Assert.fail("The required parameter must never be null and if so must cause a NPE being thrown: country=" + country); //NOI18N + } + + @Test (description = "Tests constructor with countryExternalDialPrefix being null", expectedExceptions = NullPointerException.class) + public void testConstructorNullExternalDialPrefix () { + // Should throw a NPE + final Country country = new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + null, + COUNTRY_I18N_KEY, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + ); + + // NEVER REACHED! + Assert.fail("The required parameter must never be null and if so must cause a NPE being thrown: country=" + country); //NOI18N + } + + @Test (description = "Tests if Country.equals() returns false when a null reference is provided") + @SuppressWarnings ("ObjectEqualsNull") + public void testEqualsNullReference () { + final Country country = new CountryData(); + Assert.assertFalse(country.equals(null)); + } + + @Test (description = "Tests if Country.equals() returns false when an other object is provided") + public void testEqualsOtherObject () { + final Country country = new CountryData(); + Assert.assertFalse(country.equals(new Object())); + } + + @Test (description = "Tests if Country.equals() returns true when same instance is provided") + public void testEqualsSameInstance () { + final Country country = new CountryData(); + Assert.assertTrue(country.equals(country)); + } + + @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider") + public void testEqualsDifferentAbroadDialPrefix (final Country country1, final Country country2) { + // First change countryAbroadDialPrefix to something else + country1.setCountryAbroadDialPrefix("++"); //NOI18N + Assert.assertFalse(country1.equals(country2)); + country1.setCountryAbroadDialPrefix(ABROAD_DIAL_PREFIX); + } + + @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider") + public void testEqualsDifferentCountryCode (final Country country1, final Country country2) { + // Next is countryCode + country1.setCountryCode("XX"); //NOI18N + Assert.assertFalse(country1.equals(country2)); + country1.setCountryCode(COUNTRY_CODE); + } + + @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider") + public void testEqualsDifferentExternalDialPrefix (final Country country1, final Country country2) { + // Next is countryExternalDialPrefix + country1.setCountryExternalDialPrefix("00"); //NOI18N + Assert.assertFalse(country1.equals(country2)); + country1.setCountryCode(EXTERNAL_DIAL_PREFIX); + } + + @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider") + public void testEqualsDifferentI18nKey (final Country country1, final Country country2) { + // Next is countryI18nKey + country1.setCountryI18nKey("COUNTRY_XX"); //NOI18N + Assert.assertFalse(country1.equals(country2)); + country1.setCountryI18nKey(COUNTRY_I18N_KEY); + } + + @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider") + public void testEqualsDifferentCountryId (final Country country1, final Country country2) { + // Next is countryId + country1.setCountryId(1l); //NOI18N + Assert.assertFalse(country1.equals(country2)); + country1.setCountryId(null); + } + + @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider") + public void testEqualsDifferentIsLocalDialPrefixRequired (final Country country1, final Country country2) { + // Next is countryId + country1.setCountryIsLocalPrefixRequired(Boolean.FALSE); + Assert.assertFalse(country1.equals(country2)); + country1.setCountryIsLocalPrefixRequired(Boolean.TRUE); + } + + @Test (description = "Tests if Country.equals() returns false when a field has been changed to an other value", dataProvider = "same-country-provider") + public void testEqualsDifferentPhoneCode (final Country country1, final Country country2) { + // Next is countryId + country1.setCountryPhoneCode(COUNTRY_PHONE_CODE0); + Assert.assertFalse(country1.equals(country2)); + country1.setCountryPhoneCode(COUNTRY_PHONE_CODE1); + } + + @Test (description = "Tests Country.countryEntryCreated getter/setter") + public void testCountryEntryCreated () { + // Empty instance + final Country country = new CountryData(); + final Date date = new Date(); + + // Set it + country.setCountryEntryCreated(date); + + // Should be the same! + Assert.assertEquals(country.getCountryEntryCreated(), date); + } + + @Test (description = "Tests Country.countryEntryUpdated getter/setter") + public void testCountryEntryUpdated () { + // Empty instance + final Country country = new CountryData(); + final Date date = new Date(); + + // Set it + country.setCountryEntryUpdated(date); + + // Should be the same! + Assert.assertEquals(country.getCountryEntryUpdated(), date); + } + +} diff --git a/test/org/mxchange/jcountrycore/test/model/utils/CountryUtilsTest.java b/test/org/mxchange/jcountrycore/test/model/utils/CountryUtilsTest.java new file mode 100644 index 0000000..65bd817 --- /dev/null +++ b/test/org/mxchange/jcountrycore/test/model/utils/CountryUtilsTest.java @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2022 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcountrycore.test.model.utils; + +import org.mxchange.jcountry.model.data.Country; +import org.mxchange.jcountry.model.data.CountryData; +import org.mxchange.jcountry.model.utils.CountryUtils; +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/** + * Test cases for CountryUtils class + *

+ * @author Roland Häder + */ +public class CountryUtilsTest { + + private static final String ABROAD_DIAL_PREFIX = "+"; //NOI18N + + private static final String COUNTRY_CODE = "DE"; //NOI18N + + private static final String COUNTRY_I18N_KEY = "COUNTRY_GERMANY"; //NOI18N + + private static final Short COUNTRY_PHONE_CODE1 = 49; + + private static final Short COUNTRY_PHONE_CODE2 = 1; + + private static final String EXTERNAL_DIAL_PREFIX = "0"; //NOI18N + + /** + * Default constructor + */ + public CountryUtilsTest () { + } + + @DataProvider (name = "different-country-provider") + public Object[][] createDifferentCountryObjectses () { + return new Object[][]{ + {new CountryData( + ABROAD_DIAL_PREFIX, + "US", //NOI18N + EXTERNAL_DIAL_PREFIX, + "COUNTRY_USA", //NOI18N + Boolean.TRUE, + COUNTRY_PHONE_CODE2 + ), new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + )} + }; + } + + @DataProvider (name = "left-null-country-provider") + public Object[][] createLeftNullCountryObjectses () { + return new Object[][]{ + {null, new CountryData()}, + {null, new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + )} + }; + } + + @DataProvider (name = "right-null-country-provider") + public Object[][] createRightNullCountryObjectses () { + return new Object[][]{ + {new CountryData(), null}, + {new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + ), null} + }; + } + + @DataProvider (name = "same-country-provider") + public Object[][] createSameCountryObjectses () { + return new Object[][]{ + {new CountryData(), new CountryData()}, + {new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + ), new CountryData( + ABROAD_DIAL_PREFIX, + COUNTRY_CODE, + EXTERNAL_DIAL_PREFIX, + COUNTRY_I18N_KEY, + Boolean.TRUE, + COUNTRY_PHONE_CODE1 + )} + }; + } + + @Test (description = "Tests the CountryUtils.compare() method with different country data", dataProvider = "different-country-provider") + public void testCompareDifferent (final Country country1, final Country country2) { + // Expect 1 always, as right side is higher + Assert.assertEquals(CountryUtils.compare(country1, country2), 1); + } + + @Test (description = "Tests the CountryUtils.compare() method with left-side being null and other country data", dataProvider = "left-null-country-provider") + public void testCompareLeftNull (final Country country1, final Country country2) { + // Expect -1 always + Assert.assertEquals(CountryUtils.compare(country1, country2), -1); + } + + @Test (description = "Tests the CountryUtils.compare() method with right-side being null and other country data", dataProvider = "right-null-country-provider") + public void testCompareRightNull (final Country country1, final Country country2) { + // Expect 1 always + Assert.assertEquals(CountryUtils.compare(country1, country2), 1); + } + + @Test (description = "Tests the CountryUtils.compare() method with same country data", dataProvider = "same-country-provider") + public void testCompareSame (final Country country1, final Country country2) { + // Expect 0 always + Assert.assertEquals(CountryUtils.compare(country1, country2), 0); + } + + @Test (description = "Tests the CountryUtils.copyCountryData() method with different instances", dataProvider = "different-country-provider") + public void testCopyCountryDifferentInstances (final Country country1, final Country country2) { + CountryUtils.copyCountryData(country1, country2); + } + + @Test (description = "Tests the CountryUtils.copyCountryData() method with same instances", dataProvider = "same-country-provider", expectedExceptions = IllegalArgumentException.class) + public void testCopyCountrySameInstances (final Country country1, final Country country2) { + CountryUtils.copyCountryData(country1, country2); + } + + @Test (description = "Tests the CountryUtils.copyCountryData() method with source null reference", dataProvider = "left-null-country-provider", expectedExceptions = NullPointerException.class) + public void testCopyCountrySourceNull (final Country country1, final Country country2) { + CountryUtils.copyCountryData(country1, country2); + } + + @Test (description = "Tests the CountryUtils.copyCountryData() method with target null reference", dataProvider = "right-null-country-provider", expectedExceptions = NullPointerException.class) + public void testCopyCountryTargetNull (final Country country1, final Country country2) { + CountryUtils.copyCountryData(country1, country2); + } + +}