Add warnign to DateType

This commit is contained in:
James Agnew 2017-01-18 22:12:48 -05:00
parent 22f796fa7c
commit b80897d16c
2 changed files with 50 additions and 4 deletions

View File

@ -39,6 +39,8 @@ import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import org.apache.commons.lang.Validate;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
/**
@ -108,6 +110,10 @@ public class DateType extends BaseDateTimeType {
/**
* Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type.
* <p>
* <b>Use caution when using this constructor</b>: The month is 0-indexed but the day is 1-indexed
* in order to match the bahaviour of the Java {@link Calendar} type.
* </p>
*
* @param theYear The year, e.g. 2015
* @param theMonth The month, e.g. 0 for January
@ -118,6 +124,11 @@ public class DateType extends BaseDateTimeType {
}
private static GregorianCalendar toCalendarZulu(int theYear, int theMonth, int theDay) {
Validate.isTrue(theMonth >= 0, "theMonth must be between 0 and 11");
Validate.isTrue(theMonth <= 11, "theMonth must be between 0 and 11");
Validate.isTrue(theDay >= 1, "theMonth must be between 0 and 11");
Validate.isTrue(theDay <= 31, "theMonth must be between 0 and 11");
GregorianCalendar retVal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
retVal.set(Calendar.YEAR, theYear);
retVal.set(Calendar.MONTH, theMonth);
@ -165,6 +176,7 @@ public class DateType extends BaseDateTimeType {
return retVal;
}
@Override
public String fhirType() {
return "date";
}

View File

@ -1,6 +1,8 @@
package ca.uhn.fhir.model;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.Calendar;
import java.util.TimeZone;
@ -14,17 +16,49 @@ import ca.uhn.fhir.util.TestUtil;
public class DateTypeTest {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(DateTypeTest.class);
@AfterClass
public static void afterClassClearContext() {
TestUtil.clearAllStaticFieldsForUnitTest();
}
@Test
public void testDateType() {
DateType birthDate = new DateType(1974, 11, 25);
assertThat(birthDate.getYear(), is(1974));
assertThat(birthDate.getMonth(), is(11));
assertThat(birthDate.getDay(), is(25));
}
@SuppressWarnings("unused")
@Test
public void testDateTypeWithInvalidMonth() {
try {
new DateType(1974, 12, 25);
} catch (IllegalArgumentException e) {
assertEquals("theMonth must be between 0 and 11", e.getMessage());
}
try {
new DateType(1974, -1, 25);
} catch (IllegalArgumentException e) {
assertEquals("theMonth must be between 0 and 11", e.getMessage());
}
try {
new DateType(1974, 2, 0);
} catch (IllegalArgumentException e) {
assertEquals("theMonth must be between 0 and 11", e.getMessage());
}
try {
new DateType(1974, 2, 32);
} catch (IllegalArgumentException e) {
assertEquals("theMonth must be between 0 and 11", e.getMessage());
}
new DateType(1974, 1, 31);
}
@Test
public void testPrecision() {
// ourLog.info(""+ new TreeSet<String>(Arrays.asList(TimeZone.getAvailableIDs())));
// ourLog.info(""+ new TreeSet<String>(Arrays.asList(TimeZone.getAvailableIDs())));
final Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));