Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbstractFieldValueChanger not working; POJOS with Set<> fail. #226

Open
wyattjsmith1 opened this issue Jun 20, 2018 · 2 comments
Open

AbstractFieldValueChanger not working; POJOS with Set<> fail. #226

wyattjsmith1 opened this issue Jun 20, 2018 · 2 comments

Comments

@wyattjsmith1
Copy link

If I have a POJO with a Set<> of anything, the tests will not pass if I am using Apache's EqualsBuilder because the hashcode will be the same with a null Set<> and an empty Set<>:

Class com.pojotester.bug.setBug.User has bad 'hashCode' method implementation.
The hashCode method should return different hash codes for non equal objects.
Current implementation returns same hash codes.
Object:
User(firstName=null, authorities=null)
and
User(firstName=null, authorities=[])
should have different hash codes:
23273
and
23273

I see that this was fixed in #153, but it is still happening to me. From what I see, that ticket changed empty sets to be sets with a single Object in SetValueChanger. Unfortunately, the method increaseValue is never being called according to my debugger, and therefore is not solving the problem. Here is a basic app that exposes the bug: https://github.com/wyattjsmith1/PojoTesterSetBug. Just run: ./gradlew test and look at the generated html failure. (I've never shared a git repo, so let me know if you can't access it)

Thanks for your help! I really like this project.

@wyattjsmith1
Copy link
Author

Slightly related, but I prefer to always return an empty collection if there is a null one saved. This causes an error with the getter method:

Class one.rizzo.backend.auth.domain.User has bad 'getter' method implementation.
The getter method for field 'private java.util.Set one.rizzo.backend.auth.domain.User.authorities' should return field value.
Current implementation returns different value.
Expected value:
[]
but was:
null

Not sure if this should be a separate ticket. Also, the expected value should be null, and the actual value is [].

@kgarnerncsu
Copy link

I tried this library today and had this problem with sets as well. In general, however, why are hash codes required to be different for different objects. That's not how hash codes are designed to work. An object containing a null set and empty set may generate the same hash code, but the equals method would not return true.

https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--
"It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants