This is the most crazy thing I have seen in java (1.6):
Set<ActionPlan> actionPlans = assessment.getActionPlans();
//getActionPlans() returns a java.util.HashSet<ActionPlan>
ActionPlan actionPlan = actionPlans.iterator().next();
assertTrue(actionPlan1.equals(actionPlan));
assertEquals(actionPlan1.hashCode(), actionPlan.hashCode());
assertTrue(actionPlans.contains(actionPlan1));
The first two asserts pass but the last one fails.
I'm not giving you details on the ActionPlan and Assessment classes because it shouldn't matter. The contains method fails where the equals and hash don't.
I'm not saying that java is broken or anything, there is probably something funny going on in my code.
Note that I'm an experienced java programmer and I'm aware of the dos and don't for implementing equals and hashCode. So if something is missing in my code it's not something obvious.
Has anyone ever seen something that puzzling?
EDIT
I did some research in my code and I now think the problem is in hibernate. I have logged the hashCode of the ActionPlan object, after creation, and at different parts of the code until the failing assert is getting called. It does not change.
Also I have checked the class returned by assessment.getActionPlans() and it is:
org.hibernate.collection.internal.PersistentSet
I'm tempted to believe that this implementation of Set does not use equals or hashcode properly.
Does anyone have insight on that?