🐛 Prevent empty names in profile
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 19 Mar 2019 17:16:58 +0000 (18:16 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 19 Mar 2019 17:16:58 +0000 (18:16 +0100)
src/main/java/net/pterodactylus/sone/data/Profile.java
src/test/kotlin/net/pterodactylus/sone/data/ProfileTest.kt

index 76e0acf..8ea09f7 100644 (file)
@@ -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;
        }
 
index 5c82574..54ba0e0 100644 (file)
@@ -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(""))