From c98ecb933291c5525f444fd4790a80f50973f07d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 19 Mar 2019 18:16:58 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20Prevent=20empty=20names=20in=20pr?= =?utf8?q?ofile?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/sone/data/Profile.java | 6 +- .../net/pterodactylus/sone/data/ProfileTest.kt | 69 ++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/data/Profile.java b/src/main/java/net/pterodactylus/sone/data/Profile.java index 76e0acf..8ea09f7 100644 --- a/src/main/java/net/pterodactylus/sone/data/Profile.java +++ b/src/main/java/net/pterodactylus/sone/data/Profile.java @@ -127,7 +127,7 @@ public class Profile implements Fingerprintable { */ @Nonnull public Profile setFirstName(@Nullable String firstName) { - this.firstName = firstName; + this.firstName = "".equals(firstName) ? null : firstName; return this; } @@ -150,7 +150,7 @@ public class Profile implements Fingerprintable { */ @Nonnull public Profile setMiddleName(@Nullable String middleName) { - this.middleName = middleName; + this.middleName = "".equals(middleName) ? null : middleName; return this; } @@ -173,7 +173,7 @@ public class Profile implements Fingerprintable { */ @Nonnull public Profile setLastName(@Nullable String lastName) { - this.lastName = lastName; + this.lastName = "".equals(lastName) ? null : lastName; return this; } diff --git a/src/test/kotlin/net/pterodactylus/sone/data/ProfileTest.kt b/src/test/kotlin/net/pterodactylus/sone/data/ProfileTest.kt index 5c82574..54ba0e0 100644 --- a/src/test/kotlin/net/pterodactylus/sone/data/ProfileTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/data/ProfileTest.kt @@ -14,6 +14,75 @@ class ProfileTest { private val profile = Profile(sone) @Test + fun `first name is initialized with null`() { + assertThat(profile.firstName, nullValue()) + } + + @Test + fun `setting first name to value will set it to value`() { + profile.firstName = "first name" + assertThat(profile.firstName, equalTo("first name")) + } + + @Test + fun `setting first name to null will set it to null`() { + profile.firstName = null + assertThat(profile.firstName, nullValue()) + } + + @Test + fun `setting first name to empty string will set it to null`() { + profile.firstName = "" + assertThat(profile.firstName, nullValue()) + } + + @Test + fun `middle name is initialized with null`() { + assertThat(profile.middleName, nullValue()) + } + + @Test + fun `setting middle name to value will set it to value`() { + profile.middleName = "middle name" + assertThat(profile.middleName, equalTo("middle name")) + } + + @Test + fun `setting middle name to null will set it to null`() { + profile.middleName = null + assertThat(profile.middleName, nullValue()) + } + + @Test + fun `setting middle name to empty string will set it to null`() { + profile.middleName = "" + assertThat(profile.middleName, nullValue()) + } + + @Test + fun `last name is initialized with null`() { + assertThat(profile.lastName, nullValue()) + } + + @Test + fun `setting last name to value will set it to value`() { + profile.lastName = "last name" + assertThat(profile.lastName, equalTo("last name")) + } + + @Test + fun `setting last name to null will set it to null`() { + profile.lastName = null + assertThat(profile.lastName, nullValue()) + } + + @Test + fun `setting last name to empty string will set it to null`() { + profile.lastName = "" + assertThat(profile.lastName, nullValue()) + } + + @Test fun `new fields are initialized with an empty string`() { val newField = profile.addField("testField") assertThat(newField.value, equalTo("")) -- 2.7.4