-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add an ability to compare equal by equals() method objects with ReflectionBuilder #1137
base: master
Are you sure you want to change the base?
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not add another constructor. This class provides a builder for new settings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test should assert what the difference is; IOW, make assertions on the other properties of the DiffResult
and Diff
instances.
@@ -29,6 +29,31 @@ private static final class TypeTestChildClass extends TypeTestClass { | |||
String field = "a"; | |||
} | |||
|
|||
@SuppressWarnings("unused") | |||
private static class TypeTestEntityClass implements Diffable<TypeTestEntityClass> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always use blocks ({...}
}
@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