Rework tests for #381
This commit is contained in:
parent
a3484f84c1
commit
4cfabfe4a6
|
@ -223,7 +223,6 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
|
|||
theTarget.append(string);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Date parse(String theValue) throws DataFormatException {
|
||||
Calendar cal = new GregorianCalendar(0, 0, 0);
|
||||
|
@ -265,7 +264,7 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
|
|||
} else {
|
||||
time = theValue.substring(11, offsetIdx);
|
||||
String offsetString = theValue.substring(offsetIdx);
|
||||
setTimeZone(offsetString);
|
||||
setTimeZone(theValue, offsetString);
|
||||
cal.setTimeZone(getTimeZone());
|
||||
}
|
||||
int timeLength = time.length();
|
||||
|
@ -312,111 +311,6 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
|
|||
setPrecision(precision);
|
||||
return cal.getTime();
|
||||
|
||||
// try {
|
||||
// if (theValue.length() == 4 && ourYearPattern.matcher(theValue).matches()) {
|
||||
// if (!isPrecisionAllowed(YEAR)) {
|
||||
// ourLog.debug("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support YEAR precision): " + theValue);
|
||||
// }
|
||||
// setPrecision(YEAR);
|
||||
// clearTimeZone();
|
||||
// return ((ourYearFormat).parse(theValue));
|
||||
// } else if (theValue.length() == 6 && ourYearMonthPattern.matcher(theValue).matches()) {
|
||||
// // Eg. 198401 (allow this just to be lenient)
|
||||
// if (!isPrecisionAllowed(MONTH)) {
|
||||
// ourLog.debug("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support DAY precision): " + theValue);
|
||||
// }
|
||||
// setPrecision(MONTH);
|
||||
// clearTimeZone();
|
||||
// return ((ourYearMonthNoDashesFormat).parse(theValue));
|
||||
// } else if (theValue.length() == 7 && ourYearDashMonthPattern.matcher(theValue).matches()) {
|
||||
// // E.g. 1984-01 (this is valid according to the spec)
|
||||
// if (!isPrecisionAllowed(MONTH)) {
|
||||
// ourLog.debug("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support MONTH precision): " + theValue);
|
||||
// }
|
||||
// setPrecision(MONTH);
|
||||
// clearTimeZone();
|
||||
// return ((ourYearMonthFormat).parse(theValue));
|
||||
// } else if (theValue.length() == 8 && ourYearMonthDayPattern.matcher(theValue).matches()) {
|
||||
// // Eg. 19840101 (allow this just to be lenient)
|
||||
// if (!isPrecisionAllowed(DAY)) {
|
||||
// ourLog.debug("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support DAY precision): " + theValue);
|
||||
// }
|
||||
// setPrecision(DAY);
|
||||
// clearTimeZone();
|
||||
// return ((ourYearMonthDayNoDashesFormat).parse(theValue));
|
||||
// } else if (theValue.length() == 10 && ourYearDashMonthDashDayPattern.matcher(theValue).matches()) {
|
||||
// // E.g. 1984-01-01 (this is valid according to the spec)
|
||||
// if (!isPrecisionAllowed(DAY)) {
|
||||
// ourLog.debug("Invalid date/time string (datatype " + getClass().getSimpleName() + " does not support DAY precision): " + theValue);
|
||||
// }
|
||||
// setPrecision(DAY);
|
||||
// clearTimeZone();
|
||||
// return ((ourYearMonthDayFormat).parse(theValue));
|
||||
// } else if (theValue.length() >= 18) { // date and time with possible time zone
|
||||
// char timeSeparator = theValue.charAt(10);
|
||||
// if (timeSeparator != 'T') {
|
||||
// throw new DataFormatException("Invalid date/time string: " + theValue);
|
||||
// }
|
||||
// int dotIndex = theValue.indexOf('.', 18);
|
||||
// boolean hasMillis = dotIndex > -1;
|
||||
//
|
||||
// if (!hasMillis && !isPrecisionAllowed(SECOND)) {
|
||||
// ourLog.debug("Invalid date/time string (data type does not support SECONDS precision): " + theValue);
|
||||
// } else if (hasMillis && !isPrecisionAllowed(MILLI)) {
|
||||
// ourLog.debug("Invalid date/time string (data type " + getClass().getSimpleName() + " does not support MILLIS precision):" + theValue);
|
||||
// }
|
||||
//
|
||||
// Date retVal;
|
||||
// if (hasMillis) {
|
||||
// String value = theValue;
|
||||
//
|
||||
// /*
|
||||
// * If we have more than 3 digits of precision after the decimal point, we
|
||||
// * only parse the first 3 since Java Dates don't support more than that and
|
||||
// * FastDateFormat gets confused
|
||||
// */
|
||||
// int offsetIndex = getOffsetIndex(theValue);
|
||||
// if (offsetIndex >= 24) {
|
||||
// value = theValue.substring(0, 23) + theValue.substring(offsetIndex);
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// if (hasOffset(value)) {
|
||||
// retVal = ourYearMonthDayTimeMilliZoneFormat.parse(value);
|
||||
// } else if (value.endsWith("Z")) {
|
||||
// retVal = ourYearMonthDayTimeMilliUTCZFormat.parse(value);
|
||||
// } else {
|
||||
// retVal = ourYearMonthDayTimeMilliFormat.parse(value);
|
||||
// }
|
||||
// } catch (ParseException p2) {
|
||||
// throw new DataFormatException("Invalid data/time string (" + p2.getMessage() + "): " + theValue);
|
||||
// }
|
||||
// setTimeZone(theValue);
|
||||
// setPrecision(TemporalPrecisionEnum.MILLI);
|
||||
// } else {
|
||||
// try {
|
||||
// if (hasOffset(theValue)) {
|
||||
// retVal = ourYearMonthDayTimeZoneFormat.parse(theValue);
|
||||
// } else if (theValue.endsWith("Z")) {
|
||||
// retVal = ourYearMonthDayTimeUTCZFormat.parse(theValue);
|
||||
// } else {
|
||||
// retVal = ourYearMonthDayTimeFormat.parse(theValue);
|
||||
// }
|
||||
// } catch (ParseException p2) {
|
||||
// throw new DataFormatException("Invalid data/time string (" + p2.getMessage() + "): " + theValue);
|
||||
// }
|
||||
//
|
||||
// setTimeZone(theValue);
|
||||
// setPrecision(TemporalPrecisionEnum.SECOND);
|
||||
// }
|
||||
//
|
||||
// return retVal;
|
||||
// } else {
|
||||
// throw new DataFormatException("Invalid date/time string (invalid length): " + theValue);
|
||||
// }
|
||||
// } catch (ParseException e) {
|
||||
// throw new DataFormatException("Invalid date string (" + e.getMessage() + "): " + theValue);
|
||||
// }
|
||||
}
|
||||
|
||||
private int parseInt(String theValue, String theSubstring, int theLowerBound, int theUpperBound) {
|
||||
|
@ -447,16 +341,22 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
|
|||
updateStringValue();
|
||||
}
|
||||
|
||||
private BaseDateTimeDt setTimeZone(String theValueString) {
|
||||
private BaseDateTimeDt setTimeZone(String theWholeValue, String theValue) {
|
||||
|
||||
if (isBlank(theValueString)) {
|
||||
throw new DataFormatException("Invalid time zone offset string: \"" + theValueString + "\"");
|
||||
}
|
||||
if (isBlank(theValue)) {
|
||||
throwBadDateFormat(theWholeValue);
|
||||
} else if (theValue.charAt(0) == 'Z') {
|
||||
clearTimeZone();
|
||||
if (theValueString.charAt(0) == 'Z') {
|
||||
setTimeZoneZulu(true);
|
||||
} else if (theValue.length() != 5) {
|
||||
throwBadDateFormat(theWholeValue, "Timezone offset must be in the form \"Z\", \"-HH:mm\", or \"+HH:mm\"");
|
||||
} else if (theValue.charAt(3) != ':' || !(theValue.charAt(0) == '+' || theValue.charAt(0) == '-')) {
|
||||
throwBadDateFormat(theWholeValue, "Timezone offset must be in the form \"Z\", \"-HH:mm\", or \"+HH:mm\"");
|
||||
} else {
|
||||
setTimeZone(TimeZone.getTimeZone("GMT" + theValueString));
|
||||
parseInt(theWholeValue, theValue.substring(0, 2), 0, 23);
|
||||
parseInt(theWholeValue, theValue.substring(3, 5), 0, 59);
|
||||
clearTimeZone();
|
||||
setTimeZone(TimeZone.getTimeZone("GMT" + theValue));
|
||||
}
|
||||
|
||||
return this;
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
package ca.uhn.fhir.model.api;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import ca.uhn.fhir.model.primitive.InstantDt;
|
||||
|
||||
public class InstantDtTest {
|
||||
|
||||
@Test
|
||||
public void testParseHandlesMillis() {
|
||||
InstantDt dt = new InstantDt();
|
||||
dt.setValueAsString("2015-06-22T15:44:32.831-04:00");
|
||||
Date date = dt.getValue();
|
||||
|
||||
InstantDt dt2 = new InstantDt();
|
||||
dt2.setValue(date);
|
||||
dt2.setTimeZoneZulu(true);
|
||||
String string = dt2.getValueAsString();
|
||||
|
||||
assertEquals("2015-06-22T19:44:32.831Z", string);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,306 +0,0 @@
|
|||
package ca.uhn.fhir.model.primitive;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.apache.commons.lang3.time.FastDateFormat;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import ca.uhn.fhir.context.FhirContext;
|
||||
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
|
||||
import ca.uhn.fhir.model.dstu.resource.Condition;
|
||||
import ca.uhn.fhir.model.dstu.resource.Patient;
|
||||
import ca.uhn.fhir.parser.DataFormatException;
|
||||
import ca.uhn.fhir.util.TestUtil;
|
||||
import ca.uhn.fhir.validation.ValidationResult;
|
||||
|
||||
public class BaseDateTimeDtTest {
|
||||
private SimpleDateFormat myDateInstantParser;
|
||||
private FastDateFormat myDateInstantZoneParser;
|
||||
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseDateTimeDtTest.class);
|
||||
|
||||
private static FhirContext ourCtx = FhirContext.forDstu1();
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
myDateInstantParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
myDateInstantZoneParser = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSSZ", TimeZone.getTimeZone("GMT-02:00"));
|
||||
}
|
||||
|
||||
/**
|
||||
* See #101
|
||||
*/
|
||||
@Test
|
||||
public void testPrecision() throws Exception {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(myDateInstantParser.parse("2012-01-02 22:31:02.333"));
|
||||
cal.setTimeZone(TimeZone.getTimeZone("EST"));
|
||||
|
||||
Patient patient = new Patient();
|
||||
patient.setBirthDate(cal.getTime(), TemporalPrecisionEnum.DAY);
|
||||
String out = ourCtx.newXmlParser().encodeResourceToString(patient);
|
||||
assertThat(out, containsString("<birthDate value=\"2012-01-02\"/>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseDate() {
|
||||
new DateDt("2012-03-31");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setTimezoneToZulu() {
|
||||
DateTimeDt dt = new DateTimeDt(new Date(816411488000L));
|
||||
// assertEquals("1995-11-14T23:58:08", dt.getValueAsString());
|
||||
dt.setTimeZoneZulu(true);
|
||||
assertEquals("1995-11-15T04:58:08Z", dt.getValueAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateTimeInLocalTimezone() {
|
||||
DateTimeDt dt = DateTimeDt.withCurrentTime();
|
||||
String str = dt.getValueAsString();
|
||||
char offset = str.charAt(19);
|
||||
if (offset != '+' && offset != '-' && offset != 'Z') {
|
||||
fail("No timezone provided: " + str);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInstantInLocalTimezone() {
|
||||
InstantDt dt = InstantDt.withCurrentTime();
|
||||
String str = dt.getValueAsString();
|
||||
char offset = str.charAt(23);
|
||||
if (offset != '+' && offset != '-' && offset != 'Z') {
|
||||
fail("No timezone provided: " + str);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for #57
|
||||
*/
|
||||
@Test
|
||||
public void testDateParsesWithInvalidPrecision() {
|
||||
Condition c = new Condition();
|
||||
c.setDateAsserted(new DateDt());
|
||||
c.getDateAsserted().setValueAsString("2001-01-02T11:13:33");
|
||||
assertEquals(TemporalPrecisionEnum.SECOND, c.getDateAsserted().getPrecision());
|
||||
|
||||
String encoded = ourCtx.newXmlParser().encodeResourceToString(c);
|
||||
Assert.assertThat(encoded, Matchers.containsString("value=\"2001-01-02T11:13:33\""));
|
||||
|
||||
c = ourCtx.newXmlParser().parseResource(Condition.class, encoded);
|
||||
|
||||
assertEquals("2001-01-02T11:13:33", c.getDateAsserted().getValueAsString());
|
||||
assertEquals(TemporalPrecisionEnum.SECOND, c.getDateAsserted().getPrecision());
|
||||
|
||||
ValidationResult outcome = ourCtx.newValidator().validateWithResult(c);
|
||||
String outcomeStr = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(outcome.getOperationOutcome());
|
||||
ourLog.info(outcomeStr);
|
||||
|
||||
assertThat(outcomeStr, containsString("date-primitive"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for #57
|
||||
*/
|
||||
@Test
|
||||
public void testConstructorRejectsInvalidPrecision() {
|
||||
try {
|
||||
new DateDt("2001-01-02T11:13:33");
|
||||
fail();
|
||||
} catch (DataFormatException e) {
|
||||
assertThat(e.getMessage(), containsString("precision"));
|
||||
}
|
||||
try {
|
||||
new InstantDt("2001-01-02");
|
||||
fail();
|
||||
} catch (DataFormatException e) {
|
||||
assertThat(e.getMessage(), containsString("precision"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* See #381
|
||||
*/
|
||||
@Test
|
||||
public void testParseFailsForInvalidDate() {
|
||||
try {
|
||||
DateTimeDt dt = new DateTimeDt("9999-13-01");
|
||||
fail(dt.getValue().toString());
|
||||
} catch (DataFormatException e) {
|
||||
// good
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void testFormats() throws Exception {
|
||||
Date instant = myDateInstantParser.parse("2001-02-03 13:01:02.555");
|
||||
for (FastDateFormat next : BaseDateTimeDt.getFormatters()) {
|
||||
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(instant);
|
||||
String value = next.format(cal);
|
||||
ourLog.info("String: {}", value);
|
||||
|
||||
DateTimeDt dt = new DateTimeDt(value);
|
||||
String reEncoded = next.format(dt.getValue());
|
||||
|
||||
assertEquals(value, reEncoded);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseDay() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("2013-02-03");
|
||||
|
||||
assertEquals("2013-02-03", myDateInstantParser.format(dt.getValue()).substring(0, 10));
|
||||
assertEquals("2013-02-03", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertNull(dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.DAY, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test(expected=DataFormatException.class)
|
||||
public void testParseMalformatted() throws DataFormatException {
|
||||
new DateTimeDt("20120102");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseMilli() throws DataFormatException {
|
||||
InstantDt dt = new InstantDt();
|
||||
dt.setValueAsString("2013-02-03T11:22:33.234");
|
||||
|
||||
assertEquals("2013-02-03 11:22:33.234", myDateInstantParser.format(dt.getValue()).substring(0, 23));
|
||||
assertEquals("2013-02-03T11:22:33.234", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertNull(dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.MILLI, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseMilliZone() throws DataFormatException {
|
||||
InstantDt dt = new InstantDt();
|
||||
dt.setValueAsString("2013-02-03T11:22:33.234-02:00");
|
||||
|
||||
assertEquals("2013-02-03 11:22:33.234-0200", myDateInstantZoneParser.format(dt.getValue()));
|
||||
assertEquals("2013-02-03T11:22:33.234-02:00", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertEquals(TimeZone.getTimeZone("GMT-02:00"), dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.MILLI, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseMilliZulu() throws DataFormatException {
|
||||
InstantDt dt = new InstantDt();
|
||||
dt.setValueAsString("2013-02-03T11:22:33.234Z");
|
||||
|
||||
assertEquals("2013-02-03 09:22:33.234-0200", myDateInstantZoneParser.format(dt.getValue()));
|
||||
assertEquals("2013-02-03T11:22:33.234Z", dt.getValueAsString());
|
||||
assertEquals(true, dt.isTimeZoneZulu());
|
||||
assertEquals("GMT", dt.getTimeZone().getID());
|
||||
assertEquals(TemporalPrecisionEnum.MILLI, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseMonth() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("2013-02");
|
||||
|
||||
ourLog.info("Date: {}", dt.getValue());
|
||||
assertEquals("2013-02", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertNull(dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.MONTH, dt.getPrecision());
|
||||
|
||||
assertEquals("2013-02", myDateInstantParser.format(dt.getValue()).substring(0, 7));
|
||||
}
|
||||
|
||||
@Test(expected=DataFormatException.class)
|
||||
public void testParseMonthNoDashes() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("201302");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseSecond() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("2013-02-03T11:22:33");
|
||||
|
||||
assertEquals("2013-02-03 11:22:33", myDateInstantParser.format(dt.getValue()).substring(0, 19));
|
||||
assertEquals("2013-02-03T11:22:33", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertNull(dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.SECOND, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseSecondulu() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("2013-02-03T11:22:33Z");
|
||||
|
||||
assertEquals("2013-02-03T11:22:33Z", dt.getValueAsString());
|
||||
assertEquals(true, dt.isTimeZoneZulu());
|
||||
assertEquals("GMT", dt.getTimeZone().getID());
|
||||
assertEquals(TemporalPrecisionEnum.SECOND, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseSecondZone() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("2013-02-03T11:22:33-02:00");
|
||||
|
||||
assertEquals("2013-02-03T11:22:33-02:00", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertEquals(TimeZone.getTimeZone("GMT-02:00"), dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.SECOND, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseYear() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("2013");
|
||||
|
||||
assertEquals("2013", myDateInstantParser.format(dt.getValue()).substring(0, 4));
|
||||
assertEquals("2013", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertNull(dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.YEAR, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetValueByString() {
|
||||
InstantDt i = new InstantDt();
|
||||
i.setValueAsString("2014-06-20T20:22:09Z");
|
||||
|
||||
assertNotNull(i.getValue());
|
||||
assertNotNull(i.getValueAsString());
|
||||
|
||||
assertEquals(1403295729000L, i.getValue().getTime());
|
||||
assertEquals("2014-06-20T20:22:09Z", i.getValueAsString());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClassClearContext() {
|
||||
TestUtil.clearAllStaticFieldsForUnitTest();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,44 +1,134 @@
|
|||
package ca.uhn.fhir.model.primitive;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.either;
|
||||
import static org.hamcrest.Matchers.endsWith;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.apache.commons.lang3.time.FastDateFormat;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import ca.uhn.fhir.context.FhirContext;
|
||||
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
|
||||
import ca.uhn.fhir.model.dstu2.resource.Condition;
|
||||
import ca.uhn.fhir.model.dstu2.resource.Patient;
|
||||
import ca.uhn.fhir.parser.DataFormatException;
|
||||
import ca.uhn.fhir.util.TestUtil;
|
||||
import ca.uhn.fhir.validation.ValidationResult;
|
||||
|
||||
public class BaseDateTimeDtDstu2Test {
|
||||
private static FhirContext ourCtx = FhirContext.forDstu2();
|
||||
private static Locale ourDefaultLocale;
|
||||
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseDateTimeDtDstu2Test.class);
|
||||
|
||||
|
||||
|
||||
private SimpleDateFormat myDateInstantParser;
|
||||
|
||||
@AfterClass
|
||||
public static void afterClassClearContext() {
|
||||
TestUtil.clearAllStaticFieldsForUnitTest();
|
||||
}
|
||||
|
||||
private FastDateFormat myDateInstantZoneParser;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
myDateInstantParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
myDateInstantZoneParser = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss.SSSZ", TimeZone.getTimeZone("GMT-02:00"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void setTimezoneToZulu() {
|
||||
DateTimeDt dt = new DateTimeDt(new Date(816411488000L));
|
||||
// assertEquals("1995-11-14T23:58:08", dt.getValueAsString());
|
||||
dt.setTimeZoneZulu(true);
|
||||
assertEquals("1995-11-15T04:58:08Z", dt.getValueAsString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for #57
|
||||
*/
|
||||
@Test
|
||||
public void testConstructorRejectsInvalidPrecision() {
|
||||
try {
|
||||
new DateDt("2001-01-02T11:13:33");
|
||||
fail();
|
||||
} catch (DataFormatException e) {
|
||||
assertThat(e.getMessage(), containsString("precision"));
|
||||
}
|
||||
try {
|
||||
new InstantDt("2001-01-02");
|
||||
fail();
|
||||
} catch (DataFormatException e) {
|
||||
assertThat(e.getMessage(), containsString("precision"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for #57
|
||||
*/
|
||||
@Test
|
||||
public void testDateParsesWithInvalidPrecision() {
|
||||
Condition c = new Condition();
|
||||
c.setDateRecorded(new DateDt());
|
||||
c.getDateRecordedElement().setValueAsString("2001-01-02T11:13:33");
|
||||
assertEquals(TemporalPrecisionEnum.SECOND, c.getDateRecordedElement().getPrecision());
|
||||
|
||||
String encoded = ourCtx.newXmlParser().encodeResourceToString(c);
|
||||
Assert.assertThat(encoded, Matchers.containsString("value=\"2001-01-02T11:13:33\""));
|
||||
|
||||
c = ourCtx.newXmlParser().parseResource(Condition.class, encoded);
|
||||
|
||||
assertEquals("2001-01-02T11:13:33", c.getDateRecordedElement().getValueAsString());
|
||||
assertEquals(TemporalPrecisionEnum.SECOND, c.getDateRecordedElement().getPrecision());
|
||||
|
||||
ValidationResult outcome = ourCtx.newValidator().validateWithResult(c);
|
||||
String outcomeStr = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(outcome.toOperationOutcome());
|
||||
ourLog.info(outcomeStr);
|
||||
|
||||
assertThat(outcomeStr, containsString("date-primitive"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateTimeInLocalTimezone() {
|
||||
DateTimeDt dt = DateTimeDt.withCurrentTime();
|
||||
String str = dt.getValueAsString();
|
||||
char offset = str.charAt(19);
|
||||
if (offset != '+' && offset != '-' && offset != 'Z') {
|
||||
fail("No timezone provided: " + str);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncodeOffset() throws Exception {
|
||||
String offset = InstantDt.withCurrentTime().setTimeZone(TimeZone.getTimeZone("America/Toronto")).getValueAsString();
|
||||
assertThat(offset, either(endsWith("-05:00")).or(endsWith("-04:00")));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testInstantInLocalTimezone() {
|
||||
InstantDt dt = InstantDt.withCurrentTime();
|
||||
String str = dt.getValueAsString();
|
||||
char offset = str.charAt(23);
|
||||
if (offset != '+' && offset != '-' && offset != 'Z') {
|
||||
fail("No timezone provided: " + str);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLargePrecision() {
|
||||
DateTimeDt dt = new DateTimeDt("2014-03-06T22:09:58.9121174+04:30");
|
||||
|
@ -48,32 +138,247 @@ public class BaseDateTimeDtDstu2Test {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testEncodeOffset() throws Exception {
|
||||
myDateInstantParser.parse("2011-01-01 11:11:11.0");
|
||||
myDateInstantParser.setTimeZone(TimeZone.getTimeZone("America/Toronto"));
|
||||
String offset = InstantDt.withCurrentTime().setTimeZone(TimeZone.getTimeZone("America/Toronto")).getValueAsString();
|
||||
assertThat(offset, endsWith("-05:00"));
|
||||
public void testMinutePrecisionEncode() throws Exception {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
|
||||
cal.set(1990, Calendar.JANUARY, 3, 3, 22, 11);
|
||||
|
||||
DateTimeDt date = new DateTimeDt();
|
||||
date.setValue(cal.getTime(), TemporalPrecisionEnum.MINUTE);
|
||||
date.setTimeZone(TimeZone.getTimeZone("EST"));
|
||||
assertEquals("1990-01-02T21:22-05:00", date.getValueAsString());
|
||||
|
||||
date.setTimeZoneZulu(true);
|
||||
assertEquals("1990-01-03T02:22Z", date.getValueAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseDate() {
|
||||
new DateDt("2012-03-31");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseDay() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("2013-02-03");
|
||||
|
||||
assertEquals("2013-02-03", myDateInstantParser.format(dt.getValue()).substring(0, 10));
|
||||
assertEquals("2013-02-03", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertNull(dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.DAY, dt.getPrecision());
|
||||
}
|
||||
|
||||
/**
|
||||
* See #381
|
||||
*/
|
||||
@Test
|
||||
public void testParseFailsForInvalidDate() {
|
||||
try {
|
||||
DateTimeDt dt = new DateTimeDt("9999-13-01");
|
||||
fail(dt.getValue().toString());
|
||||
} catch (DataFormatException e) {
|
||||
// good
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseHandlesMillis() {
|
||||
InstantDt dt = new InstantDt();
|
||||
dt.setValueAsString("2015-06-22T15:44:32.831-04:00");
|
||||
Date date = dt.getValue();
|
||||
|
||||
InstantDt dt2 = new InstantDt();
|
||||
dt2.setValue(date);
|
||||
dt2.setTimeZoneZulu(true);
|
||||
String string = dt2.getValueAsString();
|
||||
|
||||
assertEquals("2015-06-22T19:44:32.831Z", string);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDateTimeFormatsInvalid() {
|
||||
// Bad timezone
|
||||
verifyFails("1974-12-01T00:00:00A");
|
||||
verifyFails("1974-12-01T00:00:00=00:00");
|
||||
verifyFails("1974-12-01T00:00:00+");
|
||||
verifyFails("1974-12-01T00:00:00+25:00");
|
||||
verifyFails("1974-12-01T00:00:00+00:61");
|
||||
verifyFails("1974-12-01T00:00:00+00 401");
|
||||
verifyFails("1974-12-01T00:00:00+0");
|
||||
verifyFails("1974-12-01T00:00:00+01");
|
||||
verifyFails("1974-12-01T00:00:00+011");
|
||||
verifyFails("1974-12-01T00:00:00+0110");
|
||||
|
||||
// Out of range
|
||||
verifyFails("1974-12-25T25:00:00Z");
|
||||
verifyFails("1974-12-25T24:00:00Z");
|
||||
verifyFails("1974-12-25T23:60:00Z");
|
||||
verifyFails("1974-12-25T23:59:60Z");
|
||||
|
||||
// Invalid Separators
|
||||
verifyFails("1974-12-25T23 59:00Z");
|
||||
verifyFails("1974-12-25T23:59 00Z");
|
||||
|
||||
// Invalid length
|
||||
verifyFails("1974-12-25T2Z");
|
||||
verifyFails("1974-12-25T22:Z");
|
||||
verifyFails("1974-12-25T22:1Z");
|
||||
verifyFails("1974-12-25T22:11:Z");
|
||||
verifyFails("1974-12-25T22:11:1Z");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testParseInvalid() {
|
||||
public void testDateFormatsInvalid() {
|
||||
// No spaces in dates
|
||||
verifyFails("1974 12-25");
|
||||
verifyFails("1974-12 25");
|
||||
|
||||
// No letters
|
||||
verifyFails("A974-12-25");
|
||||
verifyFails("1974-A2-25");
|
||||
verifyFails("1974-12-A5");
|
||||
|
||||
// Date shouldn't have a time zone
|
||||
verifyFails("1974-12-25Z");
|
||||
verifyFails("1974-12-25+10:00");
|
||||
|
||||
// Out of range
|
||||
verifyFails("1974-13-25");
|
||||
verifyFails("1974-12-32");
|
||||
verifyFails("2015-02-29");
|
||||
verifyFails("-016-02-01");
|
||||
verifyFails("2016--2-01");
|
||||
verifyFails("2016-02--1");
|
||||
|
||||
// Invalid length
|
||||
verifyFails("2");
|
||||
verifyFails("20");
|
||||
verifyFails("201");
|
||||
verifyFails("2016-0");
|
||||
verifyFails("2016-02-0");
|
||||
}
|
||||
|
||||
|
||||
private void verifyFails(String input) {
|
||||
try {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("1974-12-25+10:00");
|
||||
dt.setValueAsString(input);
|
||||
fail();
|
||||
} catch (ca.uhn.fhir.parser.DataFormatException e) {
|
||||
assertEquals("Invalid date/time format: \"1974-12-25+10:00\"", e.getMessage());
|
||||
assertThat(e.getMessage(), containsString("Invalid date/time format: \"" + input + "\""));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseInvalidZoneOffset() {
|
||||
try {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("1974-12-25Z");
|
||||
new DateTimeDt("2010-01-01T00:00:00.1234-09:00Z");
|
||||
fail();
|
||||
} catch (ca.uhn.fhir.parser.DataFormatException e) {
|
||||
assertEquals("Invalid date/time format: \"1974-12-25Z\"", e.getMessage());
|
||||
} catch (DataFormatException e) {
|
||||
assertEquals("Invalid FHIR date/time string: 2010-01-01T00:00:00.1234-09:00Z", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = DataFormatException.class)
|
||||
public void testParseMalformatted() throws DataFormatException {
|
||||
new DateTimeDt("20120102");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testParseMilli() throws DataFormatException {
|
||||
InstantDt dt = new InstantDt();
|
||||
dt.setValueAsString("2013-02-03T11:22:33.234");
|
||||
|
||||
assertEquals("2013-02-03 11:22:33.234", myDateInstantParser.format(dt.getValue()).substring(0, 23));
|
||||
assertEquals("2013-02-03T11:22:33.234", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertNull(dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.MILLI, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseMilliZone() throws DataFormatException {
|
||||
InstantDt dt = new InstantDt();
|
||||
dt.setValueAsString("2013-02-03T11:22:33.234-02:00");
|
||||
|
||||
assertEquals("2013-02-03 11:22:33.234-0200", myDateInstantZoneParser.format(dt.getValue()));
|
||||
assertEquals("2013-02-03T11:22:33.234-02:00", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertEquals(TimeZone.getTimeZone("GMT-02:00"), dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.MILLI, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseMilliZulu() throws DataFormatException {
|
||||
InstantDt dt = new InstantDt();
|
||||
dt.setValueAsString("2013-02-03T11:22:33.234Z");
|
||||
|
||||
assertEquals("2013-02-03 09:22:33.234-0200", myDateInstantZoneParser.format(dt.getValue()));
|
||||
assertEquals("2013-02-03T11:22:33.234Z", dt.getValueAsString());
|
||||
assertEquals(true, dt.isTimeZoneZulu());
|
||||
assertEquals("GMT", dt.getTimeZone().getID());
|
||||
assertEquals(TemporalPrecisionEnum.MILLI, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseMonth() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("2013-02");
|
||||
|
||||
ourLog.info("Date: {}", dt.getValue());
|
||||
assertEquals("2013-02", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertNull(dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.MONTH, dt.getPrecision());
|
||||
|
||||
assertEquals("2013-02", myDateInstantParser.format(dt.getValue()).substring(0, 7));
|
||||
}
|
||||
|
||||
@Test(expected = DataFormatException.class)
|
||||
public void testParseMonthNoDashes() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("201302");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseSecond() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("2013-02-03T11:22:33");
|
||||
|
||||
assertEquals("2013-02-03 11:22:33", myDateInstantParser.format(dt.getValue()).substring(0, 19));
|
||||
assertEquals("2013-02-03T11:22:33", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertNull(dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.SECOND, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseSecondulu() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("2013-02-03T11:22:33Z");
|
||||
|
||||
assertEquals("2013-02-03T11:22:33Z", dt.getValueAsString());
|
||||
assertEquals(true, dt.isTimeZoneZulu());
|
||||
assertEquals("GMT", dt.getTimeZone().getID());
|
||||
assertEquals(TemporalPrecisionEnum.SECOND, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseSecondZone() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("2013-02-03T11:22:33-02:00");
|
||||
|
||||
assertEquals("2013-02-03T11:22:33-02:00", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertEquals(TimeZone.getTimeZone("GMT-02:00"), dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.SECOND, dt.getPrecision());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseTimeZoneOffsetCorrectly0millis() {
|
||||
myDateInstantParser.setTimeZone(TimeZone.getTimeZone("America/Toronto"));
|
||||
|
@ -134,16 +439,6 @@ public class BaseDateTimeDtDstu2Test {
|
|||
assertEquals("2010-01-01T09:00:00.123Z", dt.getValueAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseInvalidZoneOffset() {
|
||||
try {
|
||||
new DateTimeDt("2010-01-01T00:00:00.1234-09:00Z");
|
||||
fail();
|
||||
} catch (DataFormatException e) {
|
||||
assertEquals("Invalid FHIR date/time string: 2010-01-01T00:00:00.1234-09:00Z", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseTimeZoneOffsetCorrectly4millis() {
|
||||
myDateInstantParser.setTimeZone(TimeZone.getTimeZone("America/Toronto"));
|
||||
|
@ -174,6 +469,33 @@ public class BaseDateTimeDtDstu2Test {
|
|||
assertEquals("2010-01-01T09:00:00.12345Z", dt.getValueAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseYear() throws DataFormatException {
|
||||
DateTimeDt dt = new DateTimeDt();
|
||||
dt.setValueAsString("2013");
|
||||
|
||||
assertEquals("2013", myDateInstantParser.format(dt.getValue()).substring(0, 4));
|
||||
assertEquals("2013", dt.getValueAsString());
|
||||
assertEquals(false, dt.isTimeZoneZulu());
|
||||
assertNull(dt.getTimeZone());
|
||||
assertEquals(TemporalPrecisionEnum.YEAR, dt.getPrecision());
|
||||
}
|
||||
|
||||
/**
|
||||
* See #101
|
||||
*/
|
||||
@Test
|
||||
public void testPrecision() throws Exception {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTime(myDateInstantParser.parse("2012-01-02 22:31:02.333"));
|
||||
cal.setTimeZone(TimeZone.getTimeZone("EST"));
|
||||
|
||||
Patient patient = new Patient();
|
||||
patient.setBirthDate(cal.getTime(), TemporalPrecisionEnum.DAY);
|
||||
String out = ourCtx.newXmlParser().encodeResourceToString(patient);
|
||||
assertThat(out, containsString("<birthDate value=\"2012-01-02\"/>"));
|
||||
}
|
||||
|
||||
/**
|
||||
* See HAPI #101 - https://github.com/jamesagnew/hapi-fhir/issues/101
|
||||
*/
|
||||
|
@ -190,21 +512,6 @@ public class BaseDateTimeDtDstu2Test {
|
|||
assertEquals("2012-01-02", date.getValueAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMinutePrecisionEncode() throws Exception {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
|
||||
cal.set(1990, Calendar.JANUARY, 3, 3, 22, 11);
|
||||
|
||||
DateTimeDt date = new DateTimeDt();
|
||||
date.setValue(cal.getTime(), TemporalPrecisionEnum.MINUTE);
|
||||
date.setTimeZone(TimeZone.getTimeZone("EST"));
|
||||
assertEquals("1990-01-02T21:22-05:00", date.getValueAsString());
|
||||
|
||||
date.setTimeZoneZulu(true);
|
||||
assertEquals("1990-01-03T02:22Z", date.getValueAsString());
|
||||
}
|
||||
|
||||
/**
|
||||
* See HAPI #101 - https://github.com/jamesagnew/hapi-fhir/issues/101
|
||||
*/
|
||||
|
@ -221,6 +528,18 @@ public class BaseDateTimeDtDstu2Test {
|
|||
assertEquals("2012-01-02", date.getValueAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetValueByString() {
|
||||
InstantDt i = new InstantDt();
|
||||
i.setValueAsString("2014-06-20T20:22:09Z");
|
||||
|
||||
assertNotNull(i.getValue());
|
||||
assertNotNull(i.getValueAsString());
|
||||
|
||||
assertEquals(1403295729000L, i.getValue().getTime());
|
||||
assertEquals("2014-06-20T20:22:09Z", i.getValueAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToHumanDisplay() {
|
||||
DateTimeDt dt = new DateTimeDt("2012-01-05T12:00:00-08:00");
|
||||
|
@ -234,6 +553,11 @@ public class BaseDateTimeDtDstu2Test {
|
|||
Locale.setDefault(ourDefaultLocale);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClassClearContext() {
|
||||
TestUtil.clearAllStaticFieldsForUnitTest();
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
/*
|
||||
|
|
Loading…
Reference in New Issue