diff --git a/src/main/java/com/github/natche/gravatarjavaclient/profile/serialization/GravatarProfileLanguage.java b/src/main/java/com/github/natche/gravatarjavaclient/profile/serialization/GravatarProfileLanguage.java index 5ef36fd..603ceea 100644 --- a/src/main/java/com/github/natche/gravatarjavaclient/profile/serialization/GravatarProfileLanguage.java +++ b/src/main/java/com/github/natche/gravatarjavaclient/profile/serialization/GravatarProfileLanguage.java @@ -8,7 +8,6 @@ */ @SuppressWarnings("ClassCanBeRecord") /* GSON needs this */ public final class GravatarProfileLanguage { - /** * The language code (e.g., "en" for English). */ @@ -41,6 +40,7 @@ public final class GravatarProfileLanguage { * @param isPrimary whether this is the primary language * @param order the order of the language in the profile * @throws NullPointerException if any string is null + * @throws IllegalArgumentException if name or code is empty */ public GravatarProfileLanguage(String code, String name, @@ -48,6 +48,8 @@ public GravatarProfileLanguage(String code, int order) { Preconditions.checkNotNull(code); Preconditions.checkNotNull(name); + Preconditions.checkArgument(!name.trim().isEmpty()); + Preconditions.checkArgument(!code.trim().isEmpty()); this.code = code; this.name = name; diff --git a/src/test/kotlin/com/github/natche/gravatarjavaclient/profile/serialization/GravatarProfileLanguageTest.kt b/src/test/kotlin/com/github/natche/gravatarjavaclient/profile/serialization/GravatarProfileLanguageTest.kt new file mode 100644 index 0000000..847e261 --- /dev/null +++ b/src/test/kotlin/com/github/natche/gravatarjavaclient/profile/serialization/GravatarProfileLanguageTest.kt @@ -0,0 +1,90 @@ +package com.github.natche.gravatarjavaclient.profile.serialization + +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test +import java.lang.IllegalArgumentException + +/** + * Tests for [GravatarProfileLanguage]s. + */ +class GravatarProfileLanguageTest { + /** + * Tests for construction. + */ + @Test + fun testConstruction() { + assertThrows(NullPointerException::class.java) + { GravatarProfileLanguage(null, null, false, 0) } + assertThrows(NullPointerException::class.java) + { GravatarProfileLanguage("", null, false, 0) } + assertThrows(IllegalArgumentException::class.java) + { GravatarProfileLanguage("", "", false, 0) } + assertThrows(IllegalArgumentException::class.java) + { GravatarProfileLanguage("en", "", false, 0) } + + assertDoesNotThrow { GravatarProfileLanguage("en", "English", false, 0) } + } + + /** + * Tests for the accessors. + */ + @Test + fun testAccessors() { + val profile = GravatarProfileLanguage("en", "English", true, 69) + assertEquals("en", profile.code) + assertEquals("English", profile.name) + assertTrue(profile.isPrimary) + assertEquals(69, profile.order) + } + + /** + * Tests for the to string method. + */ + @Test + fun testToString() { + val profile = GravatarProfileLanguage("en", "English", true, 69) + val otherProfile = GravatarProfileLanguage("de", "German", false, 2) + + assertEquals("GravatarProfileLanguage{code=\"en\", name=\"English\"," + + " isPrimary=true, order=69}", profile.toString()) + assertEquals("GravatarProfileLanguage{code=\"de\", name=\"German\"," + + " isPrimary=false, order=2}", otherProfile.toString()) + } + + /** + * Tests for the equals method. + */ + @Test + fun testEquals() { + val profile = GravatarProfileLanguage("en", "English", true, 69) + val equalProfile = GravatarProfileLanguage("en", "English", true, 69) + val differentCode = GravatarProfileLanguage("de", "English", true, 69) + val differentName = GravatarProfileLanguage("en", "German", true, 69) + val differentPrimary = GravatarProfileLanguage("en", "English", false, 69) + val differentOrder = GravatarProfileLanguage("en", "English", true, 2) + + assertEquals(profile, profile) + assertEquals(profile, equalProfile) + assertNotEquals(profile, differentCode) + assertNotEquals(profile, differentName) + assertNotEquals(profile, differentPrimary) + assertNotEquals(profile, differentOrder) + assertNotEquals(profile, Object()) + } + + /** + * Tests for the hash code method. + */ + @Test + fun testHashcode() { + val profile = GravatarProfileLanguage("en", "English", true, 69) + val equalProfile = GravatarProfileLanguage("en", "English", true, 69) + val differentCode = GravatarProfileLanguage("de", "English", true, 69) + + assertEquals(-1512064419, profile.hashCode()) + assertEquals(-1512064419, equalProfile.hashCode()) + assertEquals(-1513256059, differentCode.hashCode()) + assertEquals(profile.hashCode(), equalProfile.hashCode()) + assertNotEquals(profile.hashCode(), differentCode.hashCode()) + } +} \ No newline at end of file