Add warnign to DateType
This commit is contained in:
parent
22f796fa7c
commit
b80897d16c
|
@ -39,6 +39,8 @@ import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.Validate;
|
||||||
|
|
||||||
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
|
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.
|
* 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 theYear The year, e.g. 2015
|
||||||
* @param theMonth The month, e.g. 0 for January
|
* @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) {
|
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"));
|
GregorianCalendar retVal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
|
||||||
retVal.set(Calendar.YEAR, theYear);
|
retVal.set(Calendar.YEAR, theYear);
|
||||||
retVal.set(Calendar.MONTH, theMonth);
|
retVal.set(Calendar.MONTH, theMonth);
|
||||||
|
@ -165,6 +176,7 @@ public class DateType extends BaseDateTimeType {
|
||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String fhirType() {
|
public String fhirType() {
|
||||||
return "date";
|
return "date";
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package ca.uhn.fhir.model;
|
package ca.uhn.fhir.model;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
@ -14,17 +16,49 @@ import ca.uhn.fhir.util.TestUtil;
|
||||||
public class DateTypeTest {
|
public class DateTypeTest {
|
||||||
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(DateTypeTest.class);
|
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(DateTypeTest.class);
|
||||||
|
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public static void afterClassClearContext() {
|
public static void afterClassClearContext() {
|
||||||
TestUtil.clearAllStaticFieldsForUnitTest();
|
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
|
@Test
|
||||||
public void testPrecision() {
|
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();
|
final Calendar cal = Calendar.getInstance();
|
||||||
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
|
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||||
|
|
Loading…
Reference in New Issue