Skip to content

Commit

Permalink
add test for profile language
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanCheshire committed Sep 11, 2024
1 parent 83718c7 commit 3d07d1f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/
@SuppressWarnings("ClassCanBeRecord") /* GSON needs this */
public final class GravatarProfileLanguage {

/**
* The language code (e.g., "en" for English).
*/
Expand Down Expand Up @@ -41,13 +40,16 @@ 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,
boolean isPrimary,
int order) {
Preconditions.checkNotNull(code);
Preconditions.checkNotNull(name);
Preconditions.checkArgument(!name.trim().isEmpty());
Preconditions.checkArgument(!code.trim().isEmpty());

this.code = code;
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
@@ -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())
}
}

0 comments on commit 3d07d1f

Please sign in to comment.