Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. package io.github.steveswinsburg;
  2.  
  3. import static org.junit.Assert.assertEquals;
  4.  
  5. import java.sql.Date;
  6. import java.sql.Timestamp;
  7. import java.time.ZoneOffset;
  8. import java.util.Calendar;
  9. import java.util.TimeZone;
  10.  
  11. import org.junit.Test;
  12.  
  13. import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
  14. import ca.uhn.fhir.model.primitive.DateTimeDt;
  15.  
  16. /**
  17. * Some tests for {@link DateTimeDt} to help me troubleshoot a timezone issue
  18. */
  19. public class DateTimeDtTest {
  20.  
  21. /**
  22. * Ensure that a string version of an offset can be mapped
  23. */
  24. @Test
  25. public void testOffset() {
  26.  
  27. ZoneOffset offset = ZoneOffset.of("-0300");
  28.  
  29. assertEquals("Didn't map offset correctly", "-03:00", offset.getId());
  30. }
  31.  
  32. /**
  33. * Ensure that a string version of an offset can be mapped into a timezone
  34. */
  35. @Test
  36. public void testTimezoneFromOffset() {
  37.  
  38. ZoneOffset offset = ZoneOffset.of("-0300");
  39. TimeZone timezone = TimeZone.getTimeZone(offset);
  40.  
  41. assertEquals("Didn't map timezone correctly", "GMT-03:00", timezone.getDisplayName());
  42. }
  43.  
  44. /**
  45. * A {@link DateTimeDt} created with the basic constructor uses the default timezone
  46. */
  47. @Test
  48. public void testDateInDefaultTimezone() {
  49.  
  50. Date date = new Date();
  51. TimeZone timezone = TimeZone.getDefault();
  52. DateTimeDt fhirDate = new DateTimeDt(date);
  53.  
  54. assertEquals("Timezone expected was not default", timezone, fhirDate.getTimeZone());
  55. }
  56.  
  57. /**
  58. * A {@link DateTimeDt} created with the full constructor should use the provided timezone
  59. */
  60. @Test
  61. public void testDateInProvidedTimezone() {
  62.  
  63. Date date = new Date();
  64. TimeZone timezone = TimeZone.getTimeZone("America/Los_Angeles");
  65. DateTimeDt fhirDate = new DateTimeDt(date, TemporalPrecisionEnum.SECOND, timezone);
  66.  
  67. assertEquals("Timezone was not as expected", timezone, fhirDate.getTimeZone());
  68. }
  69.  
  70. /**
  71. * A {@link DateTimeDt} created with the full constructor and a timezone created from a string offset, should use the provided timezone and map it across correctly
  72. */
  73. @Test
  74. public void testDateInProvidedTimezoneFromOffset() {
  75.  
  76. Date date = new Date();
  77.  
  78. ZoneOffset offset = ZoneOffset.of("-0300");
  79. TimeZone timezone = TimeZone.getTimeZone(offset);
  80. DateTimeDt fhirDate = new DateTimeDt(date, TemporalPrecisionEnum.SECOND, timezone);
  81.  
  82. assertEquals("Timezone was not as expected", timezone, fhirDate.getTimeZone());
  83. }
  84.  
  85. /**
  86. * A {@link DateTimeDt} created with the full constructor using a timestamp instead of a date, and a timezone created from a string offset, should use the provided timezone and map it across correctly
  87. */
  88. @Test
  89. public void testDateInProvidedTimezoneFromTimestamp() {
  90.  
  91. Timestamp timestamp = new Timestamp();
  92.  
  93. ZoneOffset offset = ZoneOffset.of("-0300");
  94. TimeZone timezone = TimeZone.getTimeZone(offset);
  95. DateTimeDt fhirDate = new DateTimeDt(timestamp, TemporalPrecisionEnum.SECOND, timezone);
  96.  
  97. assertEquals("Timezone was not as expected", timezone, fhirDate.getTimeZone());
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement