Add equals() and hashCode() to JSON return object
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 20 Nov 2016 10:36:48 +0000 (11:36 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 20 Nov 2016 13:47:31 +0000 (14:47 +0100)
src/main/java/net/pterodactylus/sone/web/ajax/JsonReturnObject.java
src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonReturnObjectTest.kt

index ca29f3c..9ee0048 100644 (file)
@@ -133,4 +133,18 @@ public class JsonReturnObject {
                return this;
        }
 
+       @Override
+       public int hashCode() {
+               return Boolean.valueOf(success).hashCode() ^ content.hashCode();
+       }
+
+       @Override
+       public boolean equals(Object object) {
+               if ((object == null) || (object.getClass() != getClass())) {
+                       return false;
+               }
+               JsonReturnObject other = (JsonReturnObject) object;
+               return (success == other.success) && content.equals(other.content);
+       }
+
 }
index 4acb8aa..55d1ba1 100644 (file)
@@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode
 import com.fasterxml.jackson.databind.node.TextNode
 import org.hamcrest.MatcherAssert.assertThat
 import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.not
 import org.junit.Test
 
 /**
@@ -87,4 +88,64 @@ class JsonReturnObjectTest {
                }))
        }
 
+       @Test
+       fun `successful object is not equal to unsuccessful object`() {
+               assertThat(JsonReturnObject(true), not(equalTo(JsonReturnObject(false))))
+       }
+
+       @Test
+       fun `objects with different content are not equal`() {
+               val firstObject = JsonReturnObject(true).apply {
+                       put("text", "text")
+               }
+               val secondObject = JsonReturnObject(true).apply {
+                       put("number", 123)
+               }
+               assertThat(firstObject, not(equalTo(secondObject)))
+       }
+
+       @Test
+       fun `object is not equal to null`() {
+           assertThat(JsonReturnObject(true), not(equalTo<Any?>(null)))
+       }
+
+       @Test
+       fun `object is not equal to object of different class`() {
+           assertThat(JsonReturnObject(true), not(equalTo<Any>("string")))
+       }
+
+       @Test
+       fun `equals is correctly implemented`() {
+               val firstObject = JsonReturnObject(true).apply {
+                       put("text", "text")
+                       put("int", 123)
+                       put("boolean", true)
+                       put("object", ObjectNode(JsonNodeFactory.instance))
+               }
+               val secondObject = JsonReturnObject(true).apply {
+                       put("text", "text")
+                       put("int", 123)
+                       put("boolean", true)
+                       put("object", ObjectNode(JsonNodeFactory.instance))
+               }
+               assertThat(firstObject, equalTo(secondObject))
+       }
+
+       @Test
+       fun `hash code of equal objects is equal`() {
+               val firstObject = JsonReturnObject(true).apply {
+                       put("text", "text")
+                       put("int", 123)
+                       put("boolean", true)
+                       put("object", ObjectNode(JsonNodeFactory.instance))
+               }
+               val secondObject = JsonReturnObject(true).apply {
+                       put("text", "text")
+                       put("int", 123)
+                       put("boolean", true)
+                       put("object", ObjectNode(JsonNodeFactory.instance))
+               }
+               assertThat(firstObject.hashCode(), equalTo(secondObject.hashCode()))
+       }
+
 }