This is an automated email from the ASF dual-hosted git repository.

benw pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tapestry-5.git


The following commit(s) were added to refs/heads/master by this push:
     new e0345b6d5 TAP5-2780: Fixing JSONObject to honour equals/hashCode 
contract (#48)
e0345b6d5 is described below

commit e0345b6d5a358c96178c437ad819f6ef0a71bf7e
Author: Manuel Koller <kolle...@proton.me>
AuthorDate: Sun Jun 23 08:31:06 2024 +0200

    TAP5-2780: Fixing JSONObject to honour equals/hashCode contract (#48)
    
    Added tests for both JSONObject and JSONArray. The latter required no 
change to get the test to pass.
    
    Co-authored-by: Manuel Koller <m.kol...@albourne.com>
---
 .../java/org/apache/tapestry5/json/JSONObject.java  |  5 +++++
 .../src/test/groovy/json/specs/JSONArraySpec.groovy | 19 +++++++++++++++++++
 .../test/groovy/json/specs/JSONObjectSpec.groovy    | 21 +++++++++++++++++++++
 3 files changed, 45 insertions(+)

diff --git 
a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java 
b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java
index b9ac448bd..321085ed5 100644
--- a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java
+++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java
@@ -893,6 +893,11 @@ public final class JSONObject extends JSONCollection 
implements Map<String, Obje
         return nameValuePairs.equals(other.nameValuePairs);
     }
 
+    @Override
+    public int hashCode() {
+        return nameValuePairs.hashCode();
+    }
+
     /**
      * Returns a Map of the keys and values of the JSONObject. The returned 
map is unmodifiable.
      * Note that changes to the JSONObject will be reflected in the map. In 
addition, null values in the JSONObject
diff --git a/tapestry-json/src/test/groovy/json/specs/JSONArraySpec.groovy 
b/tapestry-json/src/test/groovy/json/specs/JSONArraySpec.groovy
index 35b76584a..52419cc7e 100644
--- a/tapestry-json/src/test/groovy/json/specs/JSONArraySpec.groovy
+++ b/tapestry-json/src/test/groovy/json/specs/JSONArraySpec.groovy
@@ -265,6 +265,25 @@ class JSONArraySpec extends Specification {
         array1 != array2
     }
 
+    def "array hashCode"() {
+        when:
+
+        def array1 = new JSONArray(1, 2, 3)
+        def array2 = new JSONArray(1, 2, 3)
+
+        then:
+
+        array1.hashCode() == array2.hashCode()
+
+        when:
+
+        array2.put 9, "stuff"
+
+        then:
+
+        array1.hashCode() != array2.hashCode()
+    }
+
     def "pretty print"() {
         def array = new JSONArray("fred", "barney")
 
diff --git a/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy 
b/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy
index 8c1516702..4d07bc605 100644
--- a/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy
+++ b/tapestry-json/src/test/groovy/json/specs/JSONObjectSpec.groovy
@@ -612,6 +612,27 @@ class JSONObjectSpec extends Specification {
         obj1 != obj2
     }
 
+    def "hashCode() implementation"() {
+        def json = /{ "key" : 99 }/
+
+        when:
+
+        def obj1 = new JSONObject(json)
+        def obj2 = new JSONObject(json)
+
+        then:
+
+        obj1.hashCode() == obj2.hashCode()
+
+        when:
+
+        obj2.put("screw", "the pooch")
+
+        then:
+
+        obj1.hashCode() != obj2.hashCode()
+    }
+
     def "escaped characters in the JSON are parsed correctly"() {
         when:
 

Reply via email to