Add an ability to compare equal by equals() method objects with ReflectionBuilder#1137
Add an ability to compare equal by equals() method objects with ReflectionBuilder#1137yurbar wants to merge 1 commit intoapache:masterfrom
Conversation
…ectionDiffBuilder Useful when need to compare JPA entities with the same identifier
|
@yurbar |
|
Just came here to create the same PR. I need to compare JPA Entities with the same ID but different fields. This is because I want to find the changes between different versions of the entities. Sadly the ReflectionDiffBuilder always uses the shortcut of comparig by equals() which is always true in this case. @garydgregory you cannot exclude the equals check this why because it's done before even creating the diff. |
|
I'll need to check back on this PR later this week. |
garydgregory
left a comment
There was a problem hiding this comment.
Hello @yurbar
Thank you the PR.
You did not reply to my previous question:
Why not use @DiffExclude or ReflectionDiffBuilder.Builder.setExcludeFieldNames(String...).
Please see my embedded comments.
New elements should have a Javadoc @since tag of 3.17.0.
| * @throws IllegalArgumentException | ||
| * if {@code lhs} or {@code rhs} is {@code null} | ||
| */ | ||
| public ReflectionDiffBuilder(final T lhs, final T rhs, final ToStringStyle style, boolean testTriviallyEqual) { |
There was a problem hiding this comment.
Do not add another constructor. This class provides a builder for new settings.
There was a problem hiding this comment.
The constructor is the purpose of this change. If I remove it, then the change becomes useless
| secondObject.field1 = "b"; | ||
|
|
||
| final DiffResult list = firstObject.diff(secondObject); | ||
| assertEquals(1, list.getNumberOfDiffs()); |
There was a problem hiding this comment.
This test should assert what the difference is; IOW, make assertions on the other properties of the DiffResult and Diff instances.
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| private static class TypeTestEntityClass implements Diffable<TypeTestEntityClass> { |
There was a problem hiding this comment.
Rename TypeTestEntityClass to EntityTestClass.
Add a class Javadoc that states the intent for the test fixture to emulate a JPA entity class.
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) return true; |
@DiffExclude just excludes particular field from output, but I need an ability to compare object which are equal by equals method, but different by other fields. |
Useful when need to compare JPA entities with the same identifier.
Example: OptimisticLockingException thrown and you need to recover and print what's a difference between two entities ts with the same identifiers. You can't do it with the ReflectionDiffBuilder now because it will print nothing because those objects are equals by equals() method. So with this change you can have such an ability for ReflectionDiffBuilder