Skip to content

Commit

Permalink
[TRUNK-3652] Natural ordering of Patient State is not consistent with…
Browse files Browse the repository at this point in the history
… equals

applying auto format
  • Loading branch information
k-joseph authored and wluyima committed Aug 16, 2013
1 parent 8c10360 commit 55d85e8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/src/main/java/org/openmrs/PatientState.java
Expand Up @@ -192,13 +192,18 @@ public void setId(Integer id) {
* @see java.lang.Comparable#compareTo(java.lang.Object)
* @should return positive if startDates equal and this endDate null
* @should return negative if this startDate null
* @should pass if two states have the same start date, end date and uuid
* @should return positive or negative if two states have the same start date and end date but different uuids
*/
@Override
public int compareTo(PatientState o) {
int result = OpenmrsUtil.compareWithNullAsEarliest(getStartDate(), o.getStartDate());
if (result == 0) {
result = OpenmrsUtil.compareWithNullAsLatest(getEndDate(), o.getEndDate());
}
if (result == 0) {
result = OpenmrsUtil.compareWithNullAsGreatest(getUuid(), o.getUuid());
}
return result;
}
}
53 changes: 53 additions & 0 deletions api/src/test/java/org/openmrs/PatientStateTest.java
Expand Up @@ -14,10 +14,12 @@
package org.openmrs;

import java.util.Date;
import java.util.UUID;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openmrs.test.Verifies;

public class PatientStateTest {

Expand All @@ -31,6 +33,10 @@ public class PatientStateTest {

private Date leftOutOfRange;

private String uuid2;

private String uuid1;

@Before
public void before() {
inRange = new Date();
Expand Down Expand Up @@ -259,4 +265,51 @@ public void compareTo_shouldReturnNegativeIfThisStartDateNull() throws Exception
Assert.assertTrue(result < 0);
}

/**
* @see PatientState#compareTo(PatientState)
*/
@Test
@Verifies(value = "pass if two states have the same start date, end date and uuid", method = "compareTo(PatientState)")
public void compareTo_shouldPassIfTwoStatesHaveTheSameStartDateEndDateAndUuid() throws Exception {

PatientState patientState = new PatientState();
patientState.setStartDate(leftRange);
patientState.setEndDate(rightRange);
patientState.setUuid(uuid1);
patientState.setVoided(false);

PatientState patientState2 = new PatientState();
patientState2.setStartDate(leftRange);
patientState2.setEndDate(rightRange);
patientState2.setUuid(uuid1);
patientState2.setVoided(false);

Assert.assertTrue(patientState.compareTo(patientState2) == 0);
}

/**
* @see PatientState#compareTo(PatientState)
*/
@Test
@Verifies(value = "return positive or negative if two states have the same start date and end date but different uuids", method = "compareTo(PatientState)")
public void compareTo_shouldReturnPositiveOrNegativeIfTwoStatesHaveTheSameStartDatesEndDatesAndUuids() throws Exception {
uuid1 = UUID.randomUUID().toString();
uuid2 = UUID.randomUUID().toString();

PatientState patientState = new PatientState();
patientState.setStartDate(leftRange);
patientState.setEndDate(rightRange);
patientState.setUuid(uuid1);
patientState.setVoided(false);

PatientState patientState2 = new PatientState();
patientState2.setStartDate(leftRange);
patientState2.setEndDate(rightRange);
patientState2.setUuid(uuid2);
patientState2.setVoided(false);

int result = (patientState.compareTo(patientState2));

Assert.assertTrue(result <= -1 || result >= 1);
}
}

0 comments on commit 55d85e8

Please sign in to comment.