Advertisement
Guest User

Untitled

a guest
Aug 15th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.92 KB | None | 0 0
  1. /**
  2. * The contents of this file are subject to the OpenMRS Public License
  3. * Version 1.0 (the "License"); you may not use this file except in
  4. * compliance with the License. You may obtain a copy of the License at
  5. * http://license.openmrs.org
  6. *
  7. * Software distributed under the License is distributed on an "AS IS"
  8. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  9. * License for the specific language governing rights and limitations
  10. * under the License.
  11. *
  12. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  13. */
  14. package org.openmrs;
  15.  
  16. import java.util.Date;
  17. import java.util.UUID;
  18.  
  19. import org.junit.Assert;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. import org.openmrs.test.Verifies;
  23.  
  24. public class PatientStateTest {
  25.  
  26. private Date leftRange;
  27.  
  28. private Date inRange;
  29.  
  30. private Date rightRange;
  31.  
  32. private Date rightOutOfRange;
  33.  
  34. private Date leftOutOfRange;
  35.  
  36. private String uuid2;
  37.  
  38. private String uuid1;
  39.  
  40. @Before
  41. public void before() {
  42. inRange = new Date();
  43. leftRange = new Date(inRange.getTime() - 10000);
  44. rightRange = new Date(inRange.getTime() + 10000);
  45. rightOutOfRange = new Date(rightRange.getTime() + 10000);
  46. leftOutOfRange = new Date(leftRange.getTime() - 10000);
  47. }
  48.  
  49. /**
  50. * @see PatientState#getActive(Date)
  51. * @verifies return false if voided and date in range
  52. */
  53. @Test
  54. public void getActive_shouldReturnFalseIfVoidedAndDateInRange() throws Exception {
  55. //given
  56. PatientState patientState = new PatientState();
  57. patientState.setStartDate(leftRange);
  58. patientState.setEndDate(rightRange);
  59. patientState.setVoided(true);
  60.  
  61. //when
  62. boolean active = patientState.getActive(inRange);
  63.  
  64. //then
  65. Assert.assertFalse(active);
  66. }
  67.  
  68. /**
  69. * @see PatientState#getActive(Date)
  70. * @verifies return false if voided and date not in range
  71. */
  72. @Test
  73. public void getActive_shouldReturnFalseIfVoidedAndDateNotInRange() throws Exception {
  74. //given
  75. PatientState patientState = new PatientState();
  76. patientState.setStartDate(leftRange);
  77. patientState.setEndDate(rightRange);
  78. patientState.setVoided(true);
  79.  
  80. //when
  81. boolean active = patientState.getActive(rightOutOfRange);
  82.  
  83. //then
  84. Assert.assertFalse(active);
  85. }
  86.  
  87. /**
  88. * @see PatientState#getActive(Date)
  89. * @verifies return true if not voided and date in range
  90. */
  91. @Test
  92. public void getActive_shouldReturnTrueIfNotVoidedAndDateInRange() throws Exception {
  93. //given
  94. PatientState patientState = new PatientState();
  95. patientState.setStartDate(leftRange);
  96. patientState.setEndDate(rightRange);
  97. patientState.setVoided(false);
  98.  
  99. //when
  100. boolean active = patientState.getActive(inRange);
  101.  
  102. //then
  103. Assert.assertTrue(active);
  104. }
  105.  
  106. /**
  107. * @see PatientState#getActive(Date)
  108. * @verifies return true if not voided and date in range with null startDate
  109. */
  110. @Test
  111. public void getActive_shouldReturnTrueIfNotVoidedAndDateInRangeWithNullStartDate() throws Exception {
  112. //given
  113. PatientState patientState = new PatientState();
  114. patientState.setStartDate(null);
  115. patientState.setEndDate(rightRange);
  116. patientState.setVoided(false);
  117.  
  118. //when
  119. boolean active = patientState.getActive(inRange);
  120.  
  121. //then
  122. Assert.assertTrue(active);
  123. }
  124.  
  125. /**
  126. * @see PatientState#getActive(Date)
  127. * @verifies return true if not voided and date in range with null endDate
  128. */
  129. @Test
  130. public void getActive_shouldReturnTrueIfNotVoidedAndDateInRangeWithNullEndDate() throws Exception {
  131. //given
  132. PatientState patientState = new PatientState();
  133. patientState.setStartDate(leftRange);
  134. patientState.setEndDate(null);
  135. patientState.setVoided(false);
  136.  
  137. //when
  138. boolean active = patientState.getActive(inRange);
  139.  
  140. //then
  141. Assert.assertTrue(active);
  142. }
  143.  
  144. /**
  145. * @see PatientState#getActive(Date)
  146. * @verifies return true if not voided and both startDate and endDate nulled
  147. */
  148. @Test
  149. public void getActive_shouldReturnTrueIfNotVoidedAndBothStartDateAndEndDateNulled() throws Exception {
  150. //given
  151. PatientState patientState = new PatientState();
  152. patientState.setStartDate(null);
  153. patientState.setEndDate(null);
  154. patientState.setVoided(false);
  155.  
  156. //when
  157. boolean active = patientState.getActive(inRange);
  158.  
  159. //then
  160. Assert.assertTrue(active);
  161. }
  162.  
  163. /**
  164. * @see PatientState#getActive(Date)
  165. * @verifies compare with current date if date null
  166. */
  167. @Test
  168. public void getActive_shouldCompareWithCurrentDateIfDateNull() throws Exception {
  169. //given
  170. PatientState patientState = new PatientState();
  171. patientState.setStartDate(leftRange);
  172. patientState.setEndDate(leftRange);
  173. patientState.setVoided(false);
  174.  
  175. //when
  176. boolean active = patientState.getActive(null);
  177.  
  178. //then
  179. Assert.assertFalse(active);
  180. }
  181.  
  182. /**
  183. * @see PatientState#getActive(Date)
  184. * @verifies return false if not voided and date earlier than startDate
  185. */
  186. @Test
  187. public void getActive_shouldReturnFalseIfNotVoidedAndDateEarlierThanStartDate() throws Exception {
  188. //given
  189. PatientState patientState = new PatientState();
  190. patientState.setStartDate(leftRange);
  191. patientState.setEndDate(rightRange);
  192. patientState.setVoided(false);
  193.  
  194. //when
  195. boolean active = patientState.getActive(leftOutOfRange);
  196.  
  197. //then
  198. Assert.assertFalse(active);
  199. }
  200.  
  201. /**
  202. * @see PatientState#getActive(Date)
  203. * @verifies return false if not voided and date later than endDate
  204. */
  205. @Test
  206. public void getActive_shouldReturnFalseIfNotVoidedAndDateLaterThanEndDate() throws Exception {
  207. //given
  208. PatientState patientState = new PatientState();
  209. patientState.setStartDate(leftRange);
  210. patientState.setEndDate(rightRange);
  211. patientState.setVoided(false);
  212.  
  213. //when
  214. boolean active = patientState.getActive(rightOutOfRange);
  215.  
  216. //then
  217. Assert.assertFalse(active);
  218. }
  219.  
  220. /**
  221. * @see PatientState#compareTo(PatientState)
  222. * @verifies return positive if startDates equal and this endDate null
  223. */
  224. @Test
  225. public void compareTo_shouldReturnPositiveIfStartDatesEqualAndThisEndDateNull() throws Exception {
  226. //given
  227. PatientState patientState = new PatientState();
  228. patientState.setStartDate(leftRange);
  229. patientState.setEndDate(null);
  230. patientState.setVoided(false);
  231.  
  232. PatientState patientState2 = new PatientState();
  233. patientState2.setStartDate(leftRange);
  234. patientState2.setEndDate(rightRange);
  235. patientState2.setVoided(false);
  236.  
  237. //when
  238. int result = patientState.compareTo(patientState2);
  239.  
  240. //then
  241. Assert.assertTrue(result > 0);
  242. }
  243.  
  244. /**
  245. * @see PatientState#compareTo(PatientState)
  246. * @verifies return negative if this startDate null
  247. */
  248. @Test
  249. public void compareTo_shouldReturnNegativeIfThisStartDateNull() throws Exception {
  250. //given
  251. PatientState patientState = new PatientState();
  252. patientState.setStartDate(null);
  253. patientState.setEndDate(rightRange);
  254. patientState.setVoided(false);
  255.  
  256. PatientState patientState2 = new PatientState();
  257. patientState2.setStartDate(leftRange);
  258. patientState2.setEndDate(rightRange);
  259. patientState2.setVoided(false);
  260.  
  261. //when
  262. int result = patientState.compareTo(patientState2);
  263.  
  264. //then
  265. Assert.assertTrue(result < 0);
  266. }
  267.  
  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. uuid1 = UUID.randomUUID().toString();
  275. uuid2 = UUID.randomUUID().toString();
  276.  
  277. PatientState patientState = new PatientState();
  278. patientState.setStartDate(leftRange);
  279. patientState.setEndDate(rightRange);
  280. patientState.setUuid(uuid1);
  281. patientState.setVoided(false);
  282.  
  283. PatientState patientState2 = new PatientState();
  284. patientState2.setStartDate(leftRange);
  285. patientState2.setEndDate(rightRange);
  286. patientState2.setUuid(uuid1);
  287. patientState2.setVoided(false);
  288.  
  289. Assert.assertTrue(patientState.compareTo(patientState2) == 0);
  290. }
  291.  
  292. /**
  293. * @see PatientState#compareTo(PatientState)
  294. */
  295. @Test
  296. @Verifies(value = "return positive or negative if two states have the same start date and end date but different uuids", method = "compareTo(PatientState)")
  297. public void compareTo_shouldReturnPositiveOrNegativeIfTwoStatesHaveTheSameStartDatesEndDatesAndUuids() throws Exception {
  298. uuid1 = UUID.randomUUID().toString();
  299. uuid2 = UUID.randomUUID().toString();
  300.  
  301. PatientState patientState = new PatientState();
  302. patientState.setStartDate(leftRange);
  303. patientState.setEndDate(rightRange);
  304. patientState.setUuid(uuid1);
  305. patientState.setVoided(false);
  306.  
  307. PatientState patientState2 = new PatientState();
  308. patientState2.setStartDate(leftRange);
  309. patientState2.setEndDate(rightRange);
  310. patientState2.setUuid(uuid2);
  311. patientState2.setVoided(false);
  312.  
  313. int result = (patientState.compareTo(patientState2));
  314.  
  315. Assert.assertTrue(result <= -1 || result >= 1);
  316. }
  317. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement