[ 
https://issues.apache.org/jira/browse/GROOVY-6151?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15983593#comment-15983593
 ] 

Michael Lipp commented on GROOVY-6151:
--------------------------------------

It took me quite some time to find this bug report using search engines. 
Therefore I'd like to add some "keywords" to increase its visibility.

What this bug report effectively means is that groovy fails to properly check 
instances of classes for equality that inherit from HashMap (or LinkedHashMap) 
and override the "equals" method. (Most likely the problem will show up with 
any class that implements Map and overrides equals.) The wrong behavior occurs 
not only when using the == operator, but also if you explicitly call "equals" 
in a groovy script (like "derived1.equals(derived2)").

Call me unlucky, but I fell into this trap when writing my first Spock tests. I 
was about to remove Spock completely from my project again, because nothing 
seemed to make sense. Eventually, I added a Java class that provides a static 
method that invokes the equals method. Calling this "real" equals invocation 
from groovy finally gave me the proper result. 

Honestly, it shatters my believe in the usability of groovy that this major bug 
has remained unattended for 4 years!

> Equal on class (extends Map)
> ----------------------------
>
>                 Key: GROOVY-6151
>                 URL: https://issues.apache.org/jira/browse/GROOVY-6151
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 2.1.3
>            Reporter: Grzegorz Karawacki
>         Attachments: Main.groovy, TestError.groovy
>
>
> The problem occurs for classes inherit from class Map:
> {code:title=TestError.groovy|borderStyle=solid}
> class TestError extends HashMap {
>     String id
>     String getId() {
>         return id
>     }
>     void setId(String id) {
>         this.id = id
>     }
>     boolean equals(Object o) {
>         if (this.is(o)) return true
>         if (getClass() != o.class) return false
>         if (!super.equals(o)) return false
>         TestError testError = (TestError) o
>         if (id != testError.id) return false
>         return true
>     }
>     int hashCode() {
>         int result = super.hashCode()
>         result = 31 * result + (id != null ? id.hashCode() : 0)
>         return result
>     }
> }
> {code}
> {code:title=Main.groovy|borderStyle=solid}
> TestError t1 = new TestError()
> t1.setId("1")
> TestError t2 = new TestError()
> t2.setId("2")
> println(t1.getId())
> println(t2.getId())
> println(t1.equals(t2))
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to