From: David ‘Bombe’ Roden Date: Sun, 20 Nov 2016 10:36:48 +0000 (+0100) Subject: Add equals() and hashCode() to JSON return object X-Git-Tag: 0.9.7^2~412 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=9298284b0cf616691f42282d8098fc34d4853175;p=Sone.git Add equals() and hashCode() to JSON return object --- diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/JsonReturnObject.java b/src/main/java/net/pterodactylus/sone/web/ajax/JsonReturnObject.java index ca29f3c..9ee0048 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/JsonReturnObject.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/JsonReturnObject.java @@ -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); + } + } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonReturnObjectTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonReturnObjectTest.kt index 4acb8aa..55d1ba1 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonReturnObjectTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/ajax/JsonReturnObjectTest.kt @@ -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(null))) + } + + @Test + fun `object is not equal to object of different class`() { + assertThat(JsonReturnObject(true), not(equalTo("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())) + } + }