Skip to content

Commit 7cb6711

Browse files
k-josephwluyima
authored andcommittedAug 16, 2013
[TRUNK-3652] Natural ordering of Patient State is not consistent with equals
applying auto format
1 parent ee41a74 commit 7cb6711

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
 

‎api/src/main/java/org/openmrs/PatientState.java

+5
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,18 @@ public void setId(Integer id) {
192192
* @see java.lang.Comparable#compareTo(java.lang.Object)
193193
* @should return positive if startDates equal and this endDate null
194194
* @should return negative if this startDate null
195+
* @should pass if two states have the same start date, end date and uuid
196+
* @should return positive or negative if two states have the same start date and end date but different uuids
195197
*/
196198
@Override
197199
public int compareTo(PatientState o) {
198200
int result = OpenmrsUtil.compareWithNullAsEarliest(getStartDate(), o.getStartDate());
199201
if (result == 0) {
200202
result = OpenmrsUtil.compareWithNullAsLatest(getEndDate(), o.getEndDate());
201203
}
204+
if (result == 0) {
205+
result = OpenmrsUtil.compareWithNullAsGreatest(getUuid(), o.getUuid());
206+
}
202207
return result;
203208
}
204209
}

‎api/src/test/java/org/openmrs/PatientStateTest.java

+53
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
package org.openmrs;
1515

1616
import java.util.Date;
17+
import java.util.UUID;
1718

1819
import org.junit.Assert;
1920
import org.junit.Before;
2021
import org.junit.Test;
22+
import org.openmrs.test.Verifies;
2123

2224
public class PatientStateTest {
2325

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

3234
private Date leftOutOfRange;
3335

36+
private String uuid2;
37+
38+
private String uuid1;
39+
3440
@Before
3541
public void before() {
3642
inRange = new Date();
@@ -259,4 +265,51 @@ public void compareTo_shouldReturnNegativeIfThisStartDateNull() throws Exception
259265
Assert.assertTrue(result < 0);
260266
}
261267

268+
/**
269+
* @see PatientState#compareTo(PatientState)
270+
*/
271+
@Test
272+
@Verifies(value = "pass if two states have the same start date, end date and uuid", method = "compareTo(PatientState)")
273+
public void compareTo_shouldPassIfTwoStatesHaveTheSameStartDateEndDateAndUuid() throws Exception {
274+
275+
PatientState patientState = new PatientState();
276+
patientState.setStartDate(leftRange);
277+
patientState.setEndDate(rightRange);
278+
patientState.setUuid(uuid1);
279+
patientState.setVoided(false);
280+
281+
PatientState patientState2 = new PatientState();
282+
patientState2.setStartDate(leftRange);
283+
patientState2.setEndDate(rightRange);
284+
patientState2.setUuid(uuid1);
285+
patientState2.setVoided(false);
286+
287+
Assert.assertTrue(patientState.compareTo(patientState2) == 0);
288+
}
289+
290+
/**
291+
* @see PatientState#compareTo(PatientState)
292+
*/
293+
@Test
294+
@Verifies(value = "return positive or negative if two states have the same start date and end date but different uuids", method = "compareTo(PatientState)")
295+
public void compareTo_shouldReturnPositiveOrNegativeIfTwoStatesHaveTheSameStartDatesEndDatesAndUuids() throws Exception {
296+
uuid1 = UUID.randomUUID().toString();
297+
uuid2 = UUID.randomUUID().toString();
298+
299+
PatientState patientState = new PatientState();
300+
patientState.setStartDate(leftRange);
301+
patientState.setEndDate(rightRange);
302+
patientState.setUuid(uuid1);
303+
patientState.setVoided(false);
304+
305+
PatientState patientState2 = new PatientState();
306+
patientState2.setStartDate(leftRange);
307+
patientState2.setEndDate(rightRange);
308+
patientState2.setUuid(uuid2);
309+
patientState2.setVoided(false);
310+
311+
int result = (patientState.compareTo(patientState2));
312+
313+
Assert.assertTrue(result <= -1 || result >= 1);
314+
}
262315
}

0 commit comments

Comments
 (0)
Please sign in to comment.