-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. Rename |
||
|
||
private Long id; | ||
private String field1; | ||
|
||
@Override | ||
public DiffResult<TypeTestEntityClass> diff(TypeTestEntityClass obj) { | ||
return new ReflectionDiffBuilder(this, obj, SHORT_STYLE, false).build(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always use blocks ( |
||
if (obj == null || getClass() != obj.getClass()) return false; | ||
TypeTestEntityClass that = (TypeTestEntityClass) obj; | ||
return new EqualsBuilder().append(id, that.id).build(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return new HashCodeBuilder(17, 37).append(id).toHashCode(); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
private static class TypeTestClass implements Diffable<TypeTestClass> { | ||
private static int staticField; | ||
|
@@ -184,4 +209,18 @@ public void testGetExcludeFieldNamesWithNullValuesInExcludedFieldNames() { | |
assertEquals("charField", excludeFieldNames[0]); | ||
} | ||
|
||
@Test | ||
public void testEqualsObjectDiff() { | ||
final TypeTestEntityClass firstObject = new TypeTestEntityClass(); | ||
firstObject.id = 1L; | ||
firstObject.field1 = "a"; | ||
|
||
final TypeTestEntityClass secondObject = new TypeTestEntityClass(); | ||
secondObject.id = 1L; | ||
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 commentThe 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 |
||
} | ||
|
||
} |
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