From: Roland Häder Date: Fri, 4 Nov 2022 17:53:33 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=6e6a5eec3260bbdf9599aef55e454070c69dfd51;p=jcontacts-core.git Continued: - added unit tests for ContactUtils class - "isOwnContact" needs to be now explicitly set, copied and compared - also added missing copying (thanks to the unit tests!) --- 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/src/org/mxchange/jcontacts/model/contact/UserContact.java b/src/org/mxchange/jcontacts/model/contact/UserContact.java index 8f89373..89f8924 100644 --- a/src/org/mxchange/jcontacts/model/contact/UserContact.java +++ b/src/org/mxchange/jcontacts/model/contact/UserContact.java @@ -41,6 +41,7 @@ import javax.persistence.Transient; import org.apache.commons.lang3.StringUtils; import org.mxchange.jcontacts.model.contact.title.PersonalTitle; import org.mxchange.jcoreutils.comparable.ComparableUtils; +import org.mxchange.jcoreutils.dates.DateUtils; import org.mxchange.jcoreutils.number.SafeNumberUtils; import org.mxchange.jcountry.model.data.Country; import org.mxchange.jcountry.model.data.CountryData; @@ -222,8 +223,6 @@ public class UserContact implements Contact { * Default constructor */ public UserContact () { - // Default is not user's own contact - this.contactOwnContact = Boolean.FALSE; } /** @@ -233,8 +232,9 @@ public class UserContact implements Contact { * @param contactFirstName First name * @param contactFamilyName Family name * @param contactCountry Country instance + * @param isOwnContact Whether this is own contact */ - public UserContact (final PersonalTitle contactPersonalTitle, final String contactFirstName, final String contactFamilyName, final Country contactCountry) { + public UserContact (final PersonalTitle contactPersonalTitle, final String contactFirstName, final String contactFamilyName, final Country contactCountry, final Boolean isOwnContact) { // Invoke default constructor this(); @@ -263,6 +263,9 @@ public class UserContact implements Contact { } else if (contactCountry.getCountryId() < 1) { // Throw IAE throw new IllegalArgumentException(MessageFormat.format("contactCountry.countryId={0} is not valid", contactCountry.getCountryId())); //NOI18N + } else if (null == isOwnContact) { + // Throw NPE + throw new NullPointerException("isOwnContact is null"); // NOI18N } // Set all @@ -270,6 +273,70 @@ public class UserContact implements Contact { this.contactFirstName = contactFirstName; this.contactFamilyName = contactFamilyName; this.contactCountry = contactCountry; + this.contactOwnContact = isOwnContact; + } + + /** + * Constructor with all fields, except created/updated and primary key + *

+ * @param personalTitle Personal title (Mr./Mrs.) + * @param firstName First name + * @param familyName Family name + * @param country Country (e.g. current) + * @param street Street name + * @param houseNumber House number + * @param houseNumberExtension Extension to house number (e.g. a in 12a) + * @param zipCode ZIP code + * @param city City + * @param emailAddress email address + * @param academicTitle Academic title + * @param birthday Birthday + * @param comment Comment + * @param isOwnContact Whether this is own contact + *

+ * @todo Find a way to stop 0000-00-00 as birthdays to be entered + */ + public UserContact (final PersonalTitle personalTitle, final String firstName, final String familyName, final Country country, final String street, final Short houseNumber, final String houseNumberExtension, final Integer zipCode, final String city, final String emailAddress, final String academicTitle, final Date birthday, final String comment, final Boolean isOwnContact) { + // Call other constructor first + this(personalTitle, firstName, familyName, country, isOwnContact); + + // Validate parameter + if (street != null && street.isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("street is empty"); // NOI18N + } else if (houseNumber != null && houseNumber < 0) { + // No zero house numbers exist + throw new IllegalArgumentException(MessageFormat.format("houseNumber={0} is not valid", houseNumber)); // NOI18N + } else if (houseNumberExtension != null && houseNumberExtension.isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("houseNumberExtension is empty"); // NOI18N + } else if (zipCode != null && zipCode < 0) { + // No zero house numbers exist + throw new IllegalArgumentException(MessageFormat.format("zipCode={0} is not valid", zipCode)); // NOI18N + } else if (city != null && city.isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("city is empty"); // NOI18N + } else if (emailAddress != null && emailAddress.isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("emailAddress is empty"); // NOI18N + } else if (academicTitle != null && academicTitle.isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("academicTitle is empty"); // NOI18N + } else if (comment != null && comment.isEmpty()) { + // Throw IAE + throw new IllegalArgumentException("comment is empty"); // NOI18N + } + + // Set all fields + this.contactStreet = street; + this.contactHouseNumber = houseNumber; + this.contactHouseNumberExtension = houseNumberExtension; + this.contactZipCode = zipCode; + this.contactCity = city; + this.contactEmailAddress = emailAddress; + this.contactTitle = academicTitle; + this.contactBirthday = birthday; + this.contactComment = comment; } @Override @@ -301,12 +368,19 @@ public class UserContact implements Contact { this.getContactPersonalTitle().compareTo(contact.getContactPersonalTitle()), // ... academical title StringUtils.compareIgnoreCase(this.getContactTitle(), contact.getContactTitle()), - // .. family name is next ... + // .. family name ... StringUtils.compareIgnoreCase(this.getContactFamilyName(), contact.getContactFamilyName()), - // .. first name is second ... + // .. next is first... StringUtils.compareIgnoreCase(this.getContactFirstName(), contact.getContactFirstName()), // ... next is email address - StringUtils.compareIgnoreCase(this.getContactEmailAddress(), contact.getContactEmailAddress()),}; + StringUtils.compareIgnoreCase(this.getContactEmailAddress(), contact.getContactEmailAddress()), + // ... next birthday - year + DateUtils.compareYearMonthDay(this.getContactBirthday(), contact.getContactBirthday()), + // ... next "is own" flag + Boolean.compare(this.isOwnContact(), contact.isOwnContact()), + // ... next comment + StringUtils.compare(this.getContactComment(), contact.getContactComment()) + }; // Check all values final int comparison = ComparableUtils.checkAll(comparators); diff --git a/src/org/mxchange/jcontacts/model/utils/ContactUtils.java b/src/org/mxchange/jcontacts/model/utils/ContactUtils.java index 3464630..0a70b10 100644 --- a/src/org/mxchange/jcontacts/model/utils/ContactUtils.java +++ b/src/org/mxchange/jcontacts/model/utils/ContactUtils.java @@ -106,6 +106,8 @@ public class ContactUtils implements Serializable { // - other data targetContact.setContactBirthday(sourceContact.getContactBirthday()); targetContact.setContactComment(sourceContact.getContactComment()); + targetContact.setContactEmailAddress(sourceContact.getContactEmailAddress()); + targetContact.setContactOwnContact(sourceContact.isOwnContact()); } /** @@ -124,22 +126,25 @@ public class ContactUtils implements Serializable { // First contact is null throw new NullPointerException("contact is null"); //NOI18N } else if (null == other) { - // Secondcontact is null + // Second contact is null throw new NullPointerException("other is null"); //NOI18N } // Check all data fields, except id number return ((Objects.equals(contact.getContactBirthday(), other.getContactBirthday())) && (Objects.equals(contact.getContactCity(), other.getContactCity())) && + (Objects.equals(contact.getContactComment(), other.getContactComment())) && (Objects.equals(contact.getContactCountry(), other.getContactCountry())) && (Objects.equals(contact.getContactEmailAddress(), other.getContactEmailAddress())) && (Objects.equals(contact.getContactFamilyName(), other.getContactFamilyName())) && (Objects.equals(contact.getContactFirstName(), other.getContactFirstName())) && (Objects.equals(contact.getContactPersonalTitle(), other.getContactPersonalTitle())) && (Objects.equals(contact.getContactHouseNumber(), other.getContactHouseNumber())) && + (Objects.equals(contact.getContactHouseNumberExtension(), other.getContactHouseNumberExtension())) && (Objects.equals(contact.getContactStreet(), other.getContactStreet())) && (Objects.equals(contact.getContactTitle(), other.getContactTitle())) && - (Objects.equals(contact.getContactZipCode(), other.getContactZipCode()))); + (Objects.equals(contact.getContactZipCode(), other.getContactZipCode())) && + (Objects.equals(contact.isOwnContact(), other.isOwnContact()))); } /** diff --git a/test/org/mxchange/jcontacts/ContactsCoreTestSuite.xml b/test/org/mxchange/jcontacts/ContactsCoreTestSuite.xml new file mode 100644 index 0000000..10ebae3 --- /dev/null +++ b/test/org/mxchange/jcontacts/ContactsCoreTestSuite.xml @@ -0,0 +1,24 @@ + + + + + + + + + + diff --git a/test/org/mxchange/jcontacts/model/utils/ContactUtilsTest.java b/test/org/mxchange/jcontacts/model/utils/ContactUtilsTest.java new file mode 100644 index 0000000..96e73be --- /dev/null +++ b/test/org/mxchange/jcontacts/model/utils/ContactUtilsTest.java @@ -0,0 +1,679 @@ +/* + * 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.jcontacts.model.utils; + +import java.util.Calendar; +import java.util.Date; +import java.util.Objects; +import org.mxchange.jcontacts.model.contact.Contact; +import org.mxchange.jcontacts.model.contact.UserContact; +import org.mxchange.jcontacts.model.contact.title.PersonalTitle; +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 ContactUtils class + *

+ * @author Roland Häder + */ +public class ContactUtilsTest { + + private static final Country CONTACT_COUNTRY1 = new CountryData( + "+", //NOI18N + "DE", //NOI18N + "0", //NOI18N + "COUNTRY_GERMANY", //NOI18N + Boolean.FALSE, + Short.valueOf("49") //NOI18N + ); + + private static final Country CONTACT_COUNTRY2 = new CountryData( + "+", //NOI18N + "US", //NOI18N + "0", //NOI18N + "COUNTRY_USA", //NOI18N + Boolean.FALSE, + Short.valueOf("1") //NOI18N + ); + + private static final String CONTACT_FIRST_NAME1 = "Bob"; //NOI18N + + private static final String CONTACT_FIRST_NAME2 = "Alice"; //NOI18N + + private static final Short CONTACT_HOUSE_NUMBER1 = 123; + + private static final Short CONTACT_HOUSE_NUMBER2 = 456; + + private static final String CONTACT_LAST_NAME1 = "Johnson"; //NOI18N + + private static final String CONTACT_LAST_NAME2 = "Andrews"; + + private static final String CONTACT_STREET_NAME1 = "Park Street"; //NOI18N + + private static final String CONTACT_STREET_NAME2 = "Sunshine Strip"; //NOI18N + + static { + // Fake ids + CONTACT_COUNTRY1.setCountryId(1l); + CONTACT_COUNTRY2.setCountryId(2l); + } + + /** + * Default constructor + */ + public ContactUtilsTest () { + } + + @DataProvider (name = "different-contact-provider") + public Object[][] createDifferentContacts () { + // Different date objects + final Date date = new Date(); + final Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + calendar.add(Calendar.MONTH, 1); + + return new Object[][]{ + // Different personal title + {new UserContact( + PersonalTitle.MRS, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + Boolean.FALSE + ) + // Different first name + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME2, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + Boolean.FALSE + ) + // Different family name + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + Boolean.FALSE + ), new UserContact( + PersonalTitle.MRS, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME2, + CONTACT_COUNTRY1, + Boolean.FALSE + ) + // Different country + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY2, + Boolean.FALSE + ) + // Different "is own" flag + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + Boolean.TRUE + ) + // All optional fields (except fax, land-line and mobile number) versus required fields only filled out + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + Boolean.FALSE + ) + // Different street + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME2, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ) + // Different house number + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER2, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ) + // Different house number extension (both set) + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "b", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ) + // Different house number extension (left null) + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + null, //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "b", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ) + // Different house number extension (right null) + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + null, //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ) + // Different ZIP code + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 67890, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ) + // Different city + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "New York", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ) + // Different email address + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@company.com", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ) + // Different accdemical title + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "PHD", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ) + // Different birthday + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + calendar.getTime(), + "A tester user", //NOI18N + Boolean.FALSE + ) + // Different comment + }, {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr.", //NOI18N + date, + "A tester user", //NOI18N + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + CONTACT_STREET_NAME1, //NOI18N + CONTACT_HOUSE_NUMBER1, + "a", //NOI18N + 12345, + "Chicago", //NOI18N + "bob@example.org", //NOI18N + "Dr,", //NOI18N + date, + "A testing user", //NOI18N + Boolean.FALSE + ) + }}; + } + + @DataProvider (name = "left-null-contact-provider") + public Object[][] createLeftNullContacts () { + return new Object[][]{ + {null, new UserContact()}, + {null, new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + Boolean.FALSE + ) + }}; + } + + @DataProvider (name = "right-null-contact-provider") + public Object[][] createRightNullContacts () { + return new Object[][]{ + {new UserContact(), null}, + {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + Boolean.FALSE + ), null + }}; + } + + @DataProvider (name = "same-contact-provider") + public Object[][] createSameContacts () { + return new Object[][]{ + {new UserContact(), new UserContact()}, + {new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + Boolean.FALSE + ), new UserContact( + PersonalTitle.MR, + CONTACT_FIRST_NAME1, + CONTACT_LAST_NAME1, + CONTACT_COUNTRY1, + Boolean.FALSE + ) + }}; + } + + @Test (description = "Tests if comparing different contacts returns expected value", dataProvider = "different-contact-provider") + public void testCompareDifferentContact (final Contact contact1, final Contact contact2) { + // Should be never zero + Assert.assertTrue(ContactUtils.compare(contact1, contact2) != 0); + } + + @Test (description = "Tests if comparing left-null contact returns expected value", dataProvider = "left-null-contact-provider") + public void testCompareLeftNullContact (final Contact contact1, final Contact contact2) { + // Should be -1 + Assert.assertEquals(ContactUtils.compare(contact1, contact2), -1); + } + + @Test (description = "Tests if comparing right-null contact returns expected value", dataProvider = "right-null-contact-provider") + public void testCompareRightNullContact (final Contact contact1, final Contact contact2) { + // Should be -1 + Assert.assertEquals(ContactUtils.compare(contact1, contact2), 1); + } + + @Test (description = "Tests if comparing same contacts returns expected value", dataProvider = "same-contact-provider") + public void testCompareSameContact (final Contact contact1, final Contact contact2) { + // Should be 0 + Assert.assertEquals(ContactUtils.compare(contact1, contact2), 0); + } + + @Test (description = "Tests for an exception being expected when targetContact is null", dataProvider = "different-contact-provider") + public void testCopyContactDataDifferentContact (final Contact contact1, final Contact contact2) { + // Should cause an NPE + ContactUtils.copyContactData(contact1, contact2); + + // Both instances must be equal now + Assert.assertTrue(Objects.equals(contact2, contact1)); + } + + @Test (description = "Tests for an exception being expected when sourceContact is null", dataProvider = "left-null-contact-provider", expectedExceptions = NullPointerException.class) + public void testCopyContactDataLeftNull (final Contact contact1, final Contact contact2) { + // Should cause an NPE + ContactUtils.copyContactData(contact1, contact2); + + // NEVER REACHED! + Assert.fail("If sourceContact is null, a NPE should always be thrown"); //NOI18N + } + + @Test (description = "Tests for an exception being expected when targetContact is null", dataProvider = "right-null-contact-provider", expectedExceptions = NullPointerException.class) + public void testCopyContactDataRightNull (final Contact contact1, final Contact contact2) { + // Should cause an NPE + ContactUtils.copyContactData(contact1, contact2); + + // NEVER REACHED! + Assert.fail("If targetContact is null, a NPE should always be thrown"); //NOI18N + } + + @Test (description = "Tests for an exception being expected when targetContact is null", dataProvider = "same-contact-provider", expectedExceptions = IllegalArgumentException.class) + public void testCopyContactDataSameContact (final Contact contact1, final Contact contact2) { + // Should cause an NPE + ContactUtils.copyContactData(contact1, contact2); + + // NEVER REACHED! + Assert.fail("If sourceContact and targetContact are the same IAE should always be thrown"); //NOI18N + } + + @Test (description = "Tests for false when both contacts are different", dataProvider = "different-contact-provider") + public void testIsSameContactDataDifferentContact (final Contact contact1, final Contact contact2) { + // Should return false + Assert.assertFalse(ContactUtils.isSameContact(contact1, contact2)); + } + + @Test (description = "Tests for an exception being expected when first Contact is null", dataProvider = "left-null-contact-provider", expectedExceptions = NullPointerException.class) + public void testIsSameContactDataLeftNull (final Contact contact1, final Contact contact2) { + // Should cause an NPE + final boolean isSame = ContactUtils.isSameContact(contact1, contact2); + + // NEVER REACHED! + Assert.fail("If first Contact is null, a NPE should always be thrown: isSame=" + isSame); //NOI18N + } + + @Test (description = "Tests for an exception being expected when second Contact is null", dataProvider = "right-null-contact-provider", expectedExceptions = NullPointerException.class) + public void testIsSameContactDataRightNull (final Contact contact1, final Contact contact2) { + // Should cause an NPE + final boolean isSame = ContactUtils.isSameContact(contact1, contact2); + + // NEVER REACHED! + Assert.fail("If secondContact is null, a NPE should always be thrown: isSame=" + isSame); //NOI18N + } + + @Test (description = "Tests for true when both contacts are the same", dataProvider = "same-contact-provider") + public void testIsSameContactDataSameContact (final Contact contact1, final Contact contact2) { + // Should return true + Assert.assertTrue(ContactUtils.isSameContact(contact1, contact2)); + } + +}