-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Trunk 5452 - Adaption of equals for CohortMembership #2791
Conversation
Do you mind making the commit message better than simply "resolving ticket id"? In other words, from the commit message, i should tell what the commit is about, without having to first look at the ticket. |
@dkayiwa I adapted the commit message. In the past I always did it like this. THat's why I did not do it in the first place. |
Can you have the commit message include the ticket id as advised at? https://wiki.openmrs.org/display/docs/Pull+Request+Tips |
@dkayiwa added message and issue number |
public void equal_shouldReturnTrueIfObjectReferenceAreTheSame() { | ||
CohortMembership cohortMembership = new CohortMembership(12); | ||
|
||
boolean result = cohortMembership.equals(cohortMembership); |
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.
I guess we could just assert without the temporary variable.
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.
I thought it is more OpenMRS agree on style to first execute the function under test and then perform the assertion? Anyways I changed it
// A null argument to equal should not lead to an exception | ||
CohortMembership cohortMembership = new CohortMembership(12); | ||
|
||
boolean result = cohortMembership.equals(null); |
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.
I guess we could just assert without the temporary result variable.
|
||
/** | ||
* Indicates if a given cohortMembership object is equal to this one | ||
* |
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.
Did you just forget to add the @SInCE annotation?
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.
Yes. I just added it
Date startDate1 = new Date(1545140935); | ||
Date endDate1 = new Date(1577836800); | ||
|
||
Date startDate2 = new Date(1545140935); |
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.
I guess we can avoid duplicating the date value.
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.
Are you sure? Because the issue is that then we may do not see if equal() of Date works as aspected
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.
Anyways I adapted it
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.
i mean duplicating 1545140935
cohortMembershipTwo.setStartDate(startDate2); | ||
cohortMembershipTwo.setEndDate(endDate2); | ||
|
||
boolean result = cohortMembershipOne.equals(cohortMembershipTwo); |
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.
I guess we could just directly assert
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.
I did so in the new commit
cohortMembershipTwo.setStartDate(startDate2); | ||
cohortMembershipTwo.setEndDate(endDate2); | ||
|
||
boolean result = cohortMembershipOne.equals(cohortMembershipTwo); |
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.
I guess we could just directly assert
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.
I did so in the new commit
859a803
to
9293850
Compare
public boolean equals(CohortMembership otherCohortMembership) { | ||
if(this == otherCohortMembership) return true; | ||
|
||
return otherCohortMembership != null && this.endDate.equals(otherCohortMembership.getEndDate()) && this.startDate.equals(otherCohortMembership.getStartDate()) |
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.
What happens if any of these properties is null?
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.
You are right! I did not took this into account. I changed the code and added two new tests. Are two tests alright or should I add further test cases dealing with null values?
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.
Those tests would not hurt. 😊
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.
All possible cases should be tested now!
b569d7c
to
3464633
Compare
if(this == otherCohortMembership) return true; | ||
|
||
return otherCohortMembership != null && | ||
((endDate != null ) ? this.endDate.equals(otherCohortMembership.getEndDate()) : otherCohortMembership.getEndDate() == null) |
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.
Any reason why you sometimes use endDate and others you use this.endDate?
At first appearance, it looked as if endDate is a local variable different from this.endDate
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." is not necessary since only the class attributes exist with this name (no local variable).
At first I thought it would make it more obvious that "this" class member is checked for equality with the attribute. But since the statement is already quite long to avoid NullPointerExceptions I think the better way to go is to remove the "this" in this method.
This was done in the new commit
@fruether is this of any help? http://www.javapractices.com/topic/TopicAction.do?Id=17 |
I am not sure if I got correctly what the intension of this link is. Regarding the comparrission guideline: "possibly-null object fields : use both == and equals" since -> I used equals() but not explicitly ==. But this should not be a problem since equals() should check if both references are equal and the null object case is also taken care of |
} | ||
|
||
@Test | ||
public void equal_shouldReturnTrueIfBothPatientIdIsNull() { |
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.
Should return true or false?
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.
Fixed it
} | ||
|
||
@Test | ||
public void equal_shouldReturnFalseIfEndDateIsNull() { |
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 method name seems to suggest that this would return true if the end date was not null
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.
Fixed it
} | ||
|
||
@Test | ||
public void equal_shouldReturnFalseIfStartDateIsNull() { |
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 method name seems to suggest that this would return true if start date was not null
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.
Fixed it
} | ||
|
||
@Test | ||
public void equal_shouldReturnTrueIfObjectsAreTheSameIncludingNullValues() { |
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.
What does "including null values" mean?
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 thing is I want to distinguish the equal tests that contain Null values in their objects and that have all attributes set properly.
That is why I wanted to add a phrase to point out that the test actually tests if equal works when some attributes are null in both objects.
Which name do you think would make the most sense:
- equal_shouldWorkWhenNullValuesExistInObject()
- equal_shouldReturnTrueWhenBothPatientIdsAreNull()
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 second one is more precise.
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.
Done
} | ||
|
||
@Test | ||
public void equal_shouldReturnTrueWhenBothPatientIdsAreNull() { |
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.
Does this mean it will return true if both patient ids are null even if the other fields are different?
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.
@dkayiwa I corrected the name
} | ||
|
||
@Test | ||
public void equal_shouldReturnTrueIfAllDatesAreNull() { |
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.
Should return true if all dates are null even if the patients are different?
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.
When you say "all dates" does it also include dateCreated, dateChanged and dateVoided?
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.
@dkayiwa I corrected the name
CohortMembership cohortMembershipOne = new CohortMembership(13); | ||
CohortMembership cohortMembershipTwo = new CohortMembership(12); | ||
|
||
Date startDate1 = new Date(1577836801); |
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.
Should we duplicate 1577836801?
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.
Done
} | ||
|
||
@Test | ||
public void equal_shouldReturnFalseIfOnlyOneStartDateIsNull() { |
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.
Should return false when one start date is null, even if all the other fields are the same? Or did you want to mean something else?
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.
equal_shouldReturnFalseIfOnlyStartDateOfOneObjectIsNull
} | ||
|
||
@Test | ||
public void equal_shouldReturnFalseIfOnlyOneEndDateIsNull() { |
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.
Should return false when end date is null, even if all the other fields are the same? Or did you want to mean something else?
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.
Pointed out now that it is only False if end Date is null for one object:
equal_shouldReturnFalseIfOnlyEndDateOfOneObjectIsNull
767dcfe
to
5cab65c
Compare
CohortMembership cohortMembershipOne = new CohortMembership(); | ||
CohortMembership cohortMembershipTwo = new CohortMembership(); | ||
|
||
Date startDate1 = new Date(1545140935); |
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 wee need to duplicate 1545140935?
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.
I will make a variable out of it. And yeah I think it is helpful for testing purposes to duplicate it
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.
how would duplicating it be helpful?
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 alternative would be to say: startDate2 = startDate1 correct?
But if I am doing so the objects point to the same area and the reference is already equal. I want to test/make sure that the equal() function of the Date type is executed. Hence to verify, if both dates have the same content. That is why I need to duplicate the value to create a new object which is equal but not the same
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.
Assigning that value to a constant always helps to avoid the duplication 😊
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.
What do you mean? I can't follow your right now :(
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.
If you wanted to change that value from 1545140935 to 1545140936, would also change it for the second date?
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.
Yes. it should change the value of both startDate's. The one of object1 and object2. Because I want to test if it fails, when only the endDate is different in both objects
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 fact that you have to change the same value in more than one place is a design smell.
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.
I fixed it for patientId now as well. Now only values are hardcoded that are only used for the creation of one objects. Thanks for the clarification
} | ||
|
||
@Test | ||
public void equal_shouldReturnFalseIfOnlyEndDateOfOneObjectIsNull() { |
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.
end date of one object or two objects?
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.
Secondly, would it return true of the patient ids were the same?
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.
(1) Only EndDate of one object is null. That is why both objects are not equal since the EndDate is different
(2) I fixed it. It was actually a mistake of me
} | ||
|
||
@Test | ||
public void equal_shouldReturnFalseIfOnlyStartDateOfOneObjectIsNull() { |
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.
Only start date is null or even end date of one object?
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.
Fixxd it
5ab97f6
to
7937ef6
Compare
CohortMembership cohortMembershipOne = new CohortMembership(); | ||
CohortMembership cohortMembershipTwo = new CohortMembership(); | ||
|
||
long timestampStart = 1545140935; |
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.
Did you get a chance to look at our naming convention for constants? https://wiki.openmrs.org/display/docs/Java+Conventions
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.
Secondly, would you like this to be later on assigned to another value?
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.
"Any constants should be written in uppercase letters." I made it upper case and new words seperated by _
public void equal_shouldReturnTrueIfPatientIdIsEqualAndStarDateAndEndDateAreBothNull() { | ||
|
||
// Creating two logical identical CohortMemberships to test equal method | ||
int patientId = 12; |
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.
Same questions as below
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.
made it final
…mrs#2790) TRUNK-5452 - Adding equal method to CohoerMembership class (openmrs#2791) TRUNK-5266: Remove unecessary keywords (openmrs#2795) TRUNK-5467 Use diamond operator where possible (openmrs#2799) TRUNK-5469 Use simpler call to get a String's substring (openmrs#2801) TRUNK-5470 Simplify detection of global error (openmrs#2802)
Description of what I changed
In this function I added the equal method to CohortMembership accordingly to the ticket description (https://issues.openmrs.org/browse/TRUNK-5452). In addition, I added several test cases to CohortMembershipTest to prove that the method behaves in the intended way. A related talk topic can be found here: Talk Thread
Issue I worked on
see https://issues.openmrs.org/browse/TRUNK-5452
Checklist: I completed these to help reviewers :)
My pull request only contains ONE single commit
(the number above, next to the 'Commits' tab is 1).
No? -> read here on how to squash multiple commits into one
[x ] My IDE is configured to follow the code style of this project.
No? Unsure? -> configure your IDE, format the code and add the changes with
git add . && git commit --amend
[ x] I have added tests to cover my changes. (If you refactored
existing code that was well tested you do not have to add tests)
No? -> write tests and add them to this commit
git add . && git commit --amend
[ x] I ran
mvn clean package
right before creating this pull request andadded all formatting changes to my commit.
No? -> execute above command
[ x] All new and existing tests passed.
No? -> figure out why and add the fix to your commit. It is your responsibility to make sure your code works.
[x ] My pull request is based on the latest changes of the master branch.
No? Unsure? -> execute command
git pull --rebase upstream master