[OLINGO-1408] Applied code formatter

This commit is contained in:
Michael Bolz 2019-11-25 20:49:09 +01:00
parent 932af8fb5d
commit 302f991e5b
7 changed files with 623 additions and 646 deletions

View File

@ -225,7 +225,7 @@ public class AsyncRequestWrapperImpl<R extends ODataResponse> extends AbstractRe
int parseReplyAfter(String value) {
if (value == null || value.isEmpty()) {
return DEFAULT_RETRY_AFTER;
}
try {
int n = Integer.parseInt(value);
if (n < 0) {

View File

@ -35,80 +35,80 @@ import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
*/
public final class EdmDate extends SingletonPrimitiveType {
private static final EdmDate INSTANCE = new EdmDate();
private static final EdmDate INSTANCE = new EdmDate();
public static EdmDate getInstance() {
return INSTANCE;
}
public static EdmDate getInstance() {
return INSTANCE;
}
@Override
public Class<?> getDefaultType() {
return Calendar.class;
}
@Override
public Class<?> getDefaultType() {
return Calendar.class;
}
@SuppressWarnings("unchecked")
@Override
protected <T> T internalValueOfString(final String value, final Boolean isNullable, final Integer maxLength,
final Integer precision, final Integer scale, final Boolean isUnicode, final Class<T> returnType)
throws EdmPrimitiveTypeException {
LocalDate date;
try {
date = LocalDate.parse(value);
} catch (DateTimeParseException ex) {
throw new EdmPrimitiveTypeException("The literal '" + value + "' has illegal content.");
}
@SuppressWarnings("unchecked")
@Override
protected <T> T internalValueOfString(final String value, final Boolean isNullable, final Integer maxLength,
final Integer precision, final Integer scale, final Boolean isUnicode, final Class<T> returnType)
throws EdmPrimitiveTypeException {
LocalDate date;
try {
date = LocalDate.parse(value);
} catch (DateTimeParseException ex) {
throw new EdmPrimitiveTypeException("The literal '" + value + "' has illegal content.");
}
// appropriate types
if (returnType.isAssignableFrom(LocalDate.class)) {
return (T) date;
} else if (returnType.isAssignableFrom(java.sql.Date.class)) {
return (T) java.sql.Date.valueOf(date);
}
// appropriate types
if (returnType.isAssignableFrom(LocalDate.class)) {
return (T) date;
} else if (returnType.isAssignableFrom(java.sql.Date.class)) {
return (T) java.sql.Date.valueOf(date);
}
// inappropriate types, which need to be supported for backward compatibility
ZonedDateTime zdt = LocalDateTime.of(date, LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault());
if (returnType.isAssignableFrom(Calendar.class)) {
return (T) GregorianCalendar.from(zdt);
} else if (returnType.isAssignableFrom(Long.class)) {
return (T) Long.valueOf(zdt.toInstant().toEpochMilli());
} else if (returnType.isAssignableFrom(java.sql.Date.class)) {
throw new EdmPrimitiveTypeException("The value type " + returnType + " is not supported.");
} else if (returnType.isAssignableFrom(java.sql.Timestamp.class)) {
return (T) java.sql.Timestamp.from(zdt.toInstant());
} else if (returnType.isAssignableFrom(java.util.Date.class)) {
return (T) java.util.Date.from(zdt.toInstant());
} else {
throw new EdmPrimitiveTypeException("The value type " + returnType + " is not supported.");
}
}
// inappropriate types, which need to be supported for backward compatibility
ZonedDateTime zdt = LocalDateTime.of(date, LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault());
if (returnType.isAssignableFrom(Calendar.class)) {
return (T) GregorianCalendar.from(zdt);
} else if (returnType.isAssignableFrom(Long.class)) {
return (T) Long.valueOf(zdt.toInstant().toEpochMilli());
} else if (returnType.isAssignableFrom(java.sql.Date.class)) {
throw new EdmPrimitiveTypeException("The value type " + returnType + " is not supported.");
} else if (returnType.isAssignableFrom(java.sql.Timestamp.class)) {
return (T) java.sql.Timestamp.from(zdt.toInstant());
} else if (returnType.isAssignableFrom(java.util.Date.class)) {
return (T) java.util.Date.from(zdt.toInstant());
} else {
throw new EdmPrimitiveTypeException("The value type " + returnType + " is not supported.");
}
}
@Override
protected <T> String internalValueToString(final T value, final Boolean isNullable, final Integer maxLength,
final Integer precision, final Integer scale, final Boolean isUnicode) throws EdmPrimitiveTypeException {
// appropriate types
if (value instanceof LocalDate) {
return value.toString();
} else if(value instanceof java.sql.Date) {
return value.toString();
}
// inappropriate types, which need to be supported for backward compatibility
if (value instanceof GregorianCalendar) {
GregorianCalendar calendar = (GregorianCalendar) value;
return calendar.toZonedDateTime().toLocalDate().toString();
}
long millis;
if (value instanceof Long) {
millis = (Long)value;
} else if (value instanceof java.util.Date) {
millis = ((java.util.Date)value).getTime();
} else {
throw new EdmPrimitiveTypeException("The value type " + value.getClass() + " is not supported.");
}
ZonedDateTime zdt = Instant.ofEpochMilli(millis).atZone(ZoneId.systemDefault());
return zdt.toLocalDate().toString();
}
@Override
protected <T> String internalValueToString(final T value, final Boolean isNullable, final Integer maxLength,
final Integer precision, final Integer scale, final Boolean isUnicode) throws EdmPrimitiveTypeException {
// appropriate types
if (value instanceof LocalDate) {
return value.toString();
} else if (value instanceof java.sql.Date) {
return value.toString();
}
// inappropriate types, which need to be supported for backward compatibility
if (value instanceof GregorianCalendar) {
GregorianCalendar calendar = (GregorianCalendar) value;
return calendar.toZonedDateTime().toLocalDate().toString();
}
long millis;
if (value instanceof Long) {
millis = (Long) value;
} else if (value instanceof java.util.Date) {
millis = ((java.util.Date) value).getTime();
} else {
throw new EdmPrimitiveTypeException("The value type " + value.getClass() + " is not supported.");
}
ZonedDateTime zdt = Instant.ofEpochMilli(millis).atZone(ZoneId.systemDefault());
return zdt.toLocalDate().toString();
}
}

View File

@ -38,144 +38,142 @@ import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
*/
public final class EdmDateTimeOffset extends SingletonPrimitiveType {
private static final ZoneId ZULU = ZoneId.of("Z");
private static final ZoneId ZULU = ZoneId.of("Z");
private static final Pattern PATTERN = Pattern.compile("(-?\\p{Digit}{4,})-(\\p{Digit}{2})-(\\p{Digit}{2})"
+ "T(\\p{Digit}{2}):(\\p{Digit}{2})(?::(\\p{Digit}{2})(\\.(\\p{Digit}{0,12}?)0*)?)?"
+ "(Z|([-+]\\p{Digit}{2}:\\p{Digit}{2}))?");
private static final Pattern PATTERN = Pattern.compile("(-?\\p{Digit}{4,})-(\\p{Digit}{2})-(\\p{Digit}{2})"
+ "T(\\p{Digit}{2}):(\\p{Digit}{2})(?::(\\p{Digit}{2})(\\.(\\p{Digit}{0,12}?)0*)?)?"
+ "(Z|([-+]\\p{Digit}{2}:\\p{Digit}{2}))?");
private static final EdmDateTimeOffset INSTANCE = new EdmDateTimeOffset();
private static final EdmDateTimeOffset INSTANCE = new EdmDateTimeOffset();
public static EdmDateTimeOffset getInstance() {
return INSTANCE;
}
public static EdmDateTimeOffset getInstance() {
return INSTANCE;
}
@Override
public Class<?> getDefaultType() {
return Timestamp.class;
}
@Override
public Class<?> getDefaultType() {
return Timestamp.class;
}
@Override
protected <T> T internalValueOfString(final String value, final Boolean isNullable, final Integer maxLength,
final Integer precision, final Integer scale, final Boolean isUnicode, final Class<T> returnType)
throws EdmPrimitiveTypeException {
try {
ZonedDateTime zdt = parseZonedDateTime(value);
@Override
protected <T> T internalValueOfString(final String value, final Boolean isNullable, final Integer maxLength,
final Integer precision, final Integer scale, final Boolean isUnicode, final Class<T> returnType)
throws EdmPrimitiveTypeException {
try {
ZonedDateTime zdt = parseZonedDateTime(value);
return convertZonedDateTime(zdt, returnType);
} catch (DateTimeParseException ex) {
throw new EdmPrimitiveTypeException("The literal '" + value + "' has illegal content.", ex);
} catch (final ClassCastException e) {
throw new EdmPrimitiveTypeException("The value type " + returnType + " is not supported.", e);
}
}
return convertZonedDateTime(zdt, returnType);
} catch (DateTimeParseException ex) {
throw new EdmPrimitiveTypeException("The literal '" + value + "' has illegal content.", ex);
} catch (final ClassCastException e) {
throw new EdmPrimitiveTypeException("The value type " + returnType + " is not supported.", e);
}
}
private static ZonedDateTime parseZonedDateTime(final String value) {
ZonedDateTime zdt;
try {
// ISO-8601 conform pattern
zdt = ZonedDateTime.parse(value);
} catch (DateTimeParseException ex) {
// for backward compatibility - allow patterns that don't specify a time zone
final Matcher matcher = PATTERN.matcher(value);
if (matcher.matches() && matcher.group(9) == null) {
zdt = ZonedDateTime.parse(value + "Z");
} else {
throw ex;
}
}
return zdt;
}
private static ZonedDateTime parseZonedDateTime(final String value) {
ZonedDateTime zdt;
try {
// ISO-8601 conform pattern
zdt = ZonedDateTime.parse(value);
} catch (DateTimeParseException ex) {
// for backward compatibility - allow patterns that don't specify a time zone
final Matcher matcher = PATTERN.matcher(value);
if (matcher.matches() && matcher.group(9) == null) {
zdt = ZonedDateTime.parse(value + "Z");
} else {
throw ex;
}
}
return zdt;
}
@SuppressWarnings("unchecked")
private static <T> T convertZonedDateTime(ZonedDateTime zdt, Class<T> returnType) {
if (returnType == ZonedDateTime.class) {
return (T) zdt;
} else if (returnType == Instant.class) {
return (T) zdt.toInstant();
} else if (returnType.isAssignableFrom(Timestamp.class)) {
return (T) Timestamp.from(zdt.toInstant());
} else if (returnType.isAssignableFrom(java.util.Date.class)) {
return (T) java.util.Date.from(zdt.toInstant());
} else if (returnType.isAssignableFrom(java.sql.Time.class)) {
return (T) new java.sql.Time(zdt.toInstant().truncatedTo(ChronoUnit.SECONDS).toEpochMilli());
} else if (returnType.isAssignableFrom(java.sql.Date.class)) {
return (T) new java.sql.Date(zdt.toInstant().truncatedTo(ChronoUnit.SECONDS).toEpochMilli());
} else if (returnType.isAssignableFrom(Long.class)) {
return (T) Long.valueOf(zdt.toInstant().toEpochMilli());
} else if (returnType.isAssignableFrom(Calendar.class)) {
return (T) GregorianCalendar.from(zdt);
} else {
throw new ClassCastException("unsupported return type " + returnType.getSimpleName());
}
}
@SuppressWarnings("unchecked")
private static <T> T convertZonedDateTime(ZonedDateTime zdt, Class<T> returnType) {
if (returnType == ZonedDateTime.class) {
return (T) zdt;
} else if (returnType == Instant.class) {
return (T) zdt.toInstant();
} else if (returnType.isAssignableFrom(Timestamp.class)) {
return (T) Timestamp.from(zdt.toInstant());
} else if (returnType.isAssignableFrom(java.util.Date.class)) {
return (T) java.util.Date.from(zdt.toInstant());
} else if (returnType.isAssignableFrom(java.sql.Time.class)) {
return (T) new java.sql.Time(zdt.toInstant().truncatedTo(ChronoUnit.SECONDS).toEpochMilli());
} else if (returnType.isAssignableFrom(java.sql.Date.class)) {
return (T) new java.sql.Date(zdt.toInstant().truncatedTo(ChronoUnit.SECONDS).toEpochMilli());
} else if (returnType.isAssignableFrom(Long.class)) {
return (T) Long.valueOf(zdt.toInstant().toEpochMilli());
} else if (returnType.isAssignableFrom(Calendar.class)) {
return (T) GregorianCalendar.from(zdt);
} else {
throw new ClassCastException("Unsupported return type " + returnType.getSimpleName());
}
}
@Override
protected <T> String internalValueToString(final T value, final Boolean isNullable, final Integer maxLength,
final Integer precision, final Integer scale, final Boolean isUnicode) throws EdmPrimitiveTypeException {
ZonedDateTime zdt = createZonedDateTime(value);
@Override
protected <T> String internalValueToString(final T value, final Boolean isNullable, final Integer maxLength,
final Integer precision, final Integer scale, final Boolean isUnicode) throws EdmPrimitiveTypeException {
ZonedDateTime zdt = createZonedDateTime(value);
return format(zdt.toLocalDateTime(), zdt.getOffset(), zdt.getNano());
}
return format(zdt.toLocalDateTime(), zdt.getOffset(), zdt.getNano());
}
private static <T> ZonedDateTime createZonedDateTime(final T value) throws EdmPrimitiveTypeException {
if (value instanceof ZonedDateTime) {
return (ZonedDateTime) value;
}
private static <T> ZonedDateTime createZonedDateTime(final T value) throws EdmPrimitiveTypeException {
if (value instanceof ZonedDateTime) {
return (ZonedDateTime) value;
}
if (value instanceof Instant) {
return ((Instant) value).atZone(ZULU);
}
if (value instanceof Instant) {
return ((Instant) value).atZone(ZULU);
}
if (value instanceof GregorianCalendar) {
GregorianCalendar calendar = (GregorianCalendar) value;
ZonedDateTime zdt = calendar.toZonedDateTime();
ZoneId normalizedZoneId = calendar.getTimeZone().toZoneId().normalized();
return zdt.withZoneSameInstant(normalizedZoneId);
}
if (value instanceof GregorianCalendar) {
GregorianCalendar calendar = (GregorianCalendar) value;
ZonedDateTime zdt = calendar.toZonedDateTime();
ZoneId normalizedZoneId = calendar.getTimeZone().toZoneId().normalized();
return zdt.withZoneSameInstant(normalizedZoneId);
}
return convertToInstant(value).atZone(ZULU);
}
return convertToInstant(value).atZone(ZULU);
}
private static String format(LocalDateTime dateTime, ZoneOffset offset, int nanos) {
String str = dateTime.toString();
if (nanos > 0) {
str = removeTrailingZeros(str);
}
str = str + offset.toString();
private static String format(LocalDateTime dateTime, ZoneOffset offset, int nanos) {
String str = dateTime.toString();
if (nanos > 0) {
str = removeTrailingZeros(str);
}
return str + offset.toString();
}
return str;
}
private static String removeTrailingZeros(String str) {
char[] chars = str.toCharArray();
int trailingZeros = 0;
for (int i = chars.length - 1; i >= 0 && chars[i] == '0'; i--) {
trailingZeros++;
}
return str.substring(0, chars.length - trailingZeros);
}
private static String removeTrailingZeros(String str) {
char[] chars = str.toCharArray();
int trailingZeros = 0;
for (int i = chars.length - 1; i >= 0 && chars[i] == '0'; i--) {
trailingZeros++;
}
return str.substring(0, chars.length - trailingZeros);
}
/**
* Creates an {@link Instant} from the given value.
*
* @param value the value as {@link Instant}, {@link java.util.Date},
* {@link java.sql.Timestamp}, {@link Long} or
* {@link GregorianCalendar}
* @return the value as {@link Instant}
* @throws EdmPrimitiveTypeException if the type of the value is not supported
*/
private static <T> Instant convertToInstant(final T value) throws EdmPrimitiveTypeException {
if (value instanceof java.sql.Time || value instanceof java.sql.Date) {
throw new EdmPrimitiveTypeException("The value type " + value.getClass() + " is not supported.");
} else if (value instanceof java.util.Date) {
return ((java.util.Date) value).toInstant();
} else if (value instanceof Timestamp) {
return ((Timestamp) value).toInstant();
} else if (value instanceof Long) {
return Instant.ofEpochMilli((Long) value);
} else {
throw new EdmPrimitiveTypeException("The value type " + value.getClass() + " is not supported.");
}
}
/**
* Creates an {@link Instant} from the given value.
*
* @param value the value as {@link Instant}, {@link java.util.Date},
* {@link java.sql.Timestamp}, {@link Long} or
* {@link GregorianCalendar}
* @return the value as {@link Instant}
* @throws EdmPrimitiveTypeException if the type of the value is not supported
*/
private static <T> Instant convertToInstant(final T value) throws EdmPrimitiveTypeException {
if (value instanceof java.sql.Time || value instanceof java.sql.Date) {
throw new EdmPrimitiveTypeException("The value type " + value.getClass() + " is not supported.");
} else if (value instanceof java.util.Date) {
return ((java.util.Date) value).toInstant();
} else if (value instanceof Timestamp) {
return ((Timestamp) value).toInstant();
} else if (value instanceof Long) {
return Instant.ofEpochMilli((Long) value);
} else {
throw new EdmPrimitiveTypeException("The value type " + value.getClass() + " is not supported.");
}
}
}

View File

@ -33,101 +33,81 @@ import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
public final class EdmTimeOfDay extends SingletonPrimitiveType {
private static final LocalDate EPOCH = LocalDate.ofEpochDay(0l);
private static final EdmTimeOfDay INSTANCE = new EdmTimeOfDay();
private static final LocalDate EPOCH = LocalDate.ofEpochDay(0l);
private static final EdmTimeOfDay INSTANCE = new EdmTimeOfDay();
public static EdmTimeOfDay getInstance() {
return INSTANCE;
}
public static EdmTimeOfDay getInstance() {
return INSTANCE;
}
@Override
public Class<?> getDefaultType() {
return Calendar.class;
}
@Override
public Class<?> getDefaultType() {
return Calendar.class;
}
@SuppressWarnings("unchecked")
@Override
protected <T> T internalValueOfString(final String value, final Boolean isNullable, final Integer maxLength,
final Integer precision, final Integer scale, final Boolean isUnicode, final Class<T> returnType)
throws EdmPrimitiveTypeException {
LocalTime time;
try {
time = LocalTime.parse(value);
} catch (DateTimeParseException ex) {
throw new EdmPrimitiveTypeException("The literal '" + value + "' has illegal content.");
}
@SuppressWarnings("unchecked")
@Override
protected <T> T internalValueOfString(final String value, final Boolean isNullable, final Integer maxLength,
final Integer precision, final Integer scale, final Boolean isUnicode, final Class<T> returnType)
throws EdmPrimitiveTypeException {
LocalTime time;
try {
time = LocalTime.parse(value);
} catch (DateTimeParseException ex) {
throw new EdmPrimitiveTypeException("The literal '" + value + "' has illegal content.");
}
// appropriate types
if (returnType.isAssignableFrom(LocalTime.class)) {
return (T) time;
} else if (returnType.isAssignableFrom(java.sql.Time.class)) {
return (T) java.sql.Time.valueOf(time);
}
// appropriate types
if (returnType.isAssignableFrom(LocalTime.class)) {
return (T) time;
} else if (returnType.isAssignableFrom(java.sql.Time.class)) {
return (T) java.sql.Time.valueOf(time);
}
// inappropriate types, which need to be supported for backward compatibility
ZonedDateTime zdt = LocalDateTime.of(EPOCH, time).atZone(ZoneId.systemDefault());
if (returnType.isAssignableFrom(Calendar.class)) {
return (T) GregorianCalendar.from(zdt);
} else if (returnType.isAssignableFrom(Long.class)) {
return (T) Long.valueOf(zdt.toInstant().toEpochMilli());
} else if (returnType.isAssignableFrom(java.sql.Date.class)) {
throw new EdmPrimitiveTypeException("The value type " + returnType + " is not supported.");
} else if (returnType.isAssignableFrom(Timestamp.class)) {
return (T) Timestamp.from(zdt.toInstant());
} else if (returnType.isAssignableFrom(java.util.Date.class)) {
return (T) java.util.Date.from(zdt.toInstant());
} else {
throw new EdmPrimitiveTypeException("The value type " + returnType + " is not supported.");
}
}
// inappropriate types, which need to be supported for backward compatibility
ZonedDateTime zdt = LocalDateTime.of(EPOCH, time).atZone(ZoneId.systemDefault());
if (returnType.isAssignableFrom(Calendar.class)) {
return (T) GregorianCalendar.from(zdt);
} else if (returnType.isAssignableFrom(Long.class)) {
return (T) Long.valueOf(zdt.toInstant().toEpochMilli());
} else if (returnType.isAssignableFrom(java.sql.Date.class)) {
throw new EdmPrimitiveTypeException("The value type " + returnType + " is not supported.");
} else if (returnType.isAssignableFrom(Timestamp.class)) {
return (T) Timestamp.from(zdt.toInstant());
} else if (returnType.isAssignableFrom(java.util.Date.class)) {
return (T) java.util.Date.from(zdt.toInstant());
} else {
throw new EdmPrimitiveTypeException("The value type " + returnType + " is not supported.");
}
}
@Override
protected <T> String internalValueToString(final T value, final Boolean isNullable, final Integer maxLength,
final Integer precision, final Integer scale, final Boolean isUnicode) throws EdmPrimitiveTypeException {
// appropriate types
if (value instanceof LocalTime) {
return value.toString();
} else if(value instanceof java.sql.Time) {
return value.toString();
}
// inappropriate types, which need to be supported for backward compatibility
if (value instanceof GregorianCalendar) {
GregorianCalendar calendar = (GregorianCalendar) value;
return calendar.toZonedDateTime().toLocalTime().toString();
}
long millis;
if (value instanceof Long) {
millis = (Long)value;
} else if (value instanceof java.util.Date) {
millis = ((java.util.Date)value).getTime();
} else {
throw new EdmPrimitiveTypeException("The value type " + value.getClass() + " is not supported.");
}
ZonedDateTime zdt = Instant.ofEpochMilli(millis).atZone(ZoneId.systemDefault());
return zdt.toLocalTime().toString();
@Override
protected <T> String internalValueToString(final T value, final Boolean isNullable, final Integer maxLength,
final Integer precision, final Integer scale, final Boolean isUnicode) throws EdmPrimitiveTypeException {
// appropriate types
if (value instanceof LocalTime) {
return value.toString();
} else if (value instanceof java.sql.Time) {
return value.toString();
}
//
// final Calendar dateTimeValue = EdmDateTimeOffset.createDateTime(value, true);
//
// StringBuilder result = new StringBuilder();
// EdmDateTimeOffset.appendTwoDigits(result, dateTimeValue.get(Calendar.HOUR_OF_DAY));
// result.append(':');
// EdmDateTimeOffset.appendTwoDigits(result, dateTimeValue.get(Calendar.MINUTE));
// result.append(':');
// EdmDateTimeOffset.appendTwoDigits(result, dateTimeValue.get(Calendar.SECOND));
//
// final int fractionalSecs = value instanceof Timestamp ? ((Timestamp) value).getNanos()
// : dateTimeValue.get(Calendar.MILLISECOND);
// try {
// EdmDateTimeOffset.appendFractionalSeconds(result, fractionalSecs, value instanceof Timestamp, precision);
// } catch (final IllegalArgumentException e) {
// throw new EdmPrimitiveTypeException("The value '" + value + "' does not match the facets' constraints.", e);
// }
//
// return result.toString();
}
// inappropriate types, which need to be supported for backward compatibility
if (value instanceof GregorianCalendar) {
GregorianCalendar calendar = (GregorianCalendar) value;
return calendar.toZonedDateTime().toLocalTime().toString();
}
long millis;
if (value instanceof Long) {
millis = (Long) value;
} else if (value instanceof java.util.Date) {
millis = ((java.util.Date) value).getTime();
} else {
throw new EdmPrimitiveTypeException("The value type " + value.getClass() + " is not supported.");
}
ZonedDateTime zdt = Instant.ofEpochMilli(millis).atZone(ZoneId.systemDefault());
return zdt.toLocalTime().toString();
}
}

View File

@ -30,79 +30,79 @@ import org.junit.Test;
public class EdmDateTest extends PrimitiveTypeBaseTest {
private final EdmPrimitiveType instance = EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Date);
private final EdmPrimitiveType instance = EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Date);
@Test
public void toUriLiteral() throws Exception {
assertEquals("2009-12-26", instance.toUriLiteral("2009-12-26"));
assertEquals("-2009-12-26", instance.toUriLiteral("-2009-12-26"));
}
@Test
public void toUriLiteral() throws Exception {
assertEquals("2009-12-26", instance.toUriLiteral("2009-12-26"));
assertEquals("-2009-12-26", instance.toUriLiteral("-2009-12-26"));
}
@Test
public void fromUriLiteral() throws Exception {
assertEquals("2009-12-26", instance.fromUriLiteral("2009-12-26"));
assertEquals("-2009-12-26", instance.fromUriLiteral("-2009-12-26"));
}
@Test
public void fromUriLiteral() throws Exception {
assertEquals("2009-12-26", instance.fromUriLiteral("2009-12-26"));
assertEquals("-2009-12-26", instance.fromUriLiteral("-2009-12-26"));
}
@Test
public void valueToString() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
setTimeZone(dateTime, "GMT-11:30");
dateTime.set(2012, 1, 29, 13, 0, 0);
assertEquals("2012-02-29", instance.valueToString(dateTime, null, null, null, null, null));
@Test
public void valueToString() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
setTimeZone(dateTime, "GMT-11:30");
dateTime.set(2012, 1, 29, 13, 0, 0);
assertEquals("2012-02-29", instance.valueToString(dateTime, null, null, null, null, null));
Long millis = 1330558323007L;
millis -= TimeZone.getDefault().getOffset(millis);
assertEquals("2012-02-29", instance.valueToString(millis, null, null, null, null, null));
Long millis = 1330558323007L;
millis -= TimeZone.getDefault().getOffset(millis);
assertEquals("2012-02-29", instance.valueToString(millis, null, null, null, null, null));
assertEquals("1969-12-31", instance.valueToString(new java.util.Date(-43200000), null, null, null, null, null));
assertEquals("1969-12-31",
instance.valueToString(java.sql.Date.valueOf("1969-12-31"), null, null, null, null, null));
assertEquals("1969-12-31",
instance.valueToString(LocalDate.parse("1969-12-31"), null, null, null, null, null));
assertEquals("1969-12-31", instance.valueToString(new java.util.Date(-43200000), null, null, null, null, null));
assertEquals("1969-12-31",
instance.valueToString(java.sql.Date.valueOf("1969-12-31"), null, null, null, null, null));
assertEquals("1969-12-31",
instance.valueToString(LocalDate.parse("1969-12-31"), null, null, null, null, null));
// TODO support for years beyond 9999
// TODO support for years beyond 9999
// dateTime.set(Calendar.YEAR, 12344);
// assertEquals("12344-02-29", instance.valueToString(dateTime, null, null, null, null, null));
expectTypeErrorInValueToString(instance, 0);
}
expectTypeErrorInValueToString(instance, 0);
}
@Test
public void valueOfString() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
dateTime.set(2012, 1, 29);
assertEqualCalendar(dateTime,
instance.valueOfString("2012-02-29", null, null, null, null, null, Calendar.class));
assertEquals(Long.valueOf(dateTime.getTimeInMillis()),
instance.valueOfString("2012-02-29", null, null, null, null, null, Long.class));
assertEquals(dateTime.getTime(),
instance.valueOfString("2012-02-29", null, null, null, null, null, java.util.Date.class));
assertEquals(java.sql.Date.valueOf("2012-02-29"),
instance.valueOfString("2012-02-29", null, null, null, null, null, java.sql.Date.class));
assertEquals(LocalDate.parse("2012-02-29"),
instance.valueOfString("2012-02-29", null, null, null, null, null, LocalDate.class));
@Test
public void valueOfString() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
dateTime.set(2012, 1, 29);
assertEqualCalendar(dateTime,
instance.valueOfString("2012-02-29", null, null, null, null, null, Calendar.class));
assertEquals(Long.valueOf(dateTime.getTimeInMillis()),
instance.valueOfString("2012-02-29", null, null, null, null, null, Long.class));
assertEquals(dateTime.getTime(),
instance.valueOfString("2012-02-29", null, null, null, null, null, java.util.Date.class));
assertEquals(java.sql.Date.valueOf("2012-02-29"),
instance.valueOfString("2012-02-29", null, null, null, null, null, java.sql.Date.class));
assertEquals(LocalDate.parse("2012-02-29"),
instance.valueOfString("2012-02-29", null, null, null, null, null, LocalDate.class));
// TODO support for years beyond 9999
// dateTime.set(Calendar.YEAR, 12344);
// Calendar result = instance.valueOfString("12344-02-29", null, null, null,
// null, null, Calendar.class);
// this.assertEqualCalendar(dateTime, result);
// TODO support for years beyond 9999
// dateTime.set(Calendar.YEAR, 12344);
// Calendar result = instance.valueOfString("12344-02-29", null, null, null,
// null, null, Calendar.class);
// this.assertEqualCalendar(dateTime, result);
// TODO: Clarify whether negative years are really needed.
// dateTime.set(-1, 1, 28);
// assertEquals(dateTime, instance.valueOfString("-0001-02-28", null,
// Calendar.class));
// TODO: Clarify whether negative years are really needed.
// dateTime.set(-1, 1, 28);
// assertEquals(dateTime, instance.valueOfString("-0001-02-28", null,
// Calendar.class));
expectContentErrorInValueOfString(instance, "2012-02-29T23:32:02");
expectContentErrorInValueOfString(instance, "2012-02-30");
expectContentErrorInValueOfString(instance, "20120229");
expectContentErrorInValueOfString(instance, "2012-02-1");
expectContentErrorInValueOfString(instance, "2012-2-12");
expectContentErrorInValueOfString(instance, "123-02-03");
expectContentErrorInValueOfString(instance, "2012-02-29T23:32:02");
expectContentErrorInValueOfString(instance, "2012-02-30");
expectContentErrorInValueOfString(instance, "20120229");
expectContentErrorInValueOfString(instance, "2012-02-1");
expectContentErrorInValueOfString(instance, "2012-2-12");
expectContentErrorInValueOfString(instance, "123-02-03");
expectTypeErrorInValueOfString(instance, "2012-02-29");
}
expectTypeErrorInValueOfString(instance, "2012-02-29");
}
}

View File

@ -35,208 +35,208 @@ import org.junit.Test;
public class EdmDateTimeOffsetTest extends PrimitiveTypeBaseTest {
final EdmPrimitiveType instance = EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.DateTimeOffset);
final EdmPrimitiveType instance = EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.DateTimeOffset);
@Test
public void toUriLiteral() throws Exception {
assertEquals("2009-12-26T21:23:38Z", instance.toUriLiteral("2009-12-26T21:23:38Z"));
assertEquals("2002-10-10T12:00:00-05:00", instance.toUriLiteral("2002-10-10T12:00:00-05:00"));
}
@Test
public void toUriLiteral() throws Exception {
assertEquals("2009-12-26T21:23:38Z", instance.toUriLiteral("2009-12-26T21:23:38Z"));
assertEquals("2002-10-10T12:00:00-05:00", instance.toUriLiteral("2002-10-10T12:00:00-05:00"));
}
@Test
public void fromUriLiteral() throws Exception {
assertEquals("2009-12-26T21:23:38Z", instance.fromUriLiteral("2009-12-26T21:23:38Z"));
assertEquals("2002-10-10T12:00:00-05:00", instance.fromUriLiteral("2002-10-10T12:00:00-05:00"));
}
@Test
public void fromUriLiteral() throws Exception {
assertEquals("2009-12-26T21:23:38Z", instance.fromUriLiteral("2009-12-26T21:23:38Z"));
assertEquals("2002-10-10T12:00:00-05:00", instance.fromUriLiteral("2002-10-10T12:00:00-05:00"));
}
@Test
public void valueToStringFromInstant() throws Exception {
Instant instant = Instant.parse("2012-02-29T01:02:03Z");
assertEquals("2012-02-29T01:02:03Z", instance.valueToString(instant, null, null, null, null, null));
assertEquals("2012-02-29T01:02:03Z", instance.valueToString(instant, null, null, 0, null, null));
assertEquals("2012-02-29T01:02:03Z", instance.valueToString(instant, null, null, 5, null, null));
}
@Test
public void valueToStringFromInstant() throws Exception {
Instant instant = Instant.parse("2012-02-29T01:02:03Z");
assertEquals("2012-02-29T01:02:03Z", instance.valueToString(instant, null, null, null, null, null));
assertEquals("2012-02-29T01:02:03Z", instance.valueToString(instant, null, null, 0, null, null));
assertEquals("2012-02-29T01:02:03Z", instance.valueToString(instant, null, null, 5, null, null));
}
@Test
public void valueToStringFromZonedDateTime() throws Exception {
ZonedDateTime zdt = ZonedDateTime.parse("2012-02-28T23:32:03-01:30");
@Test
public void valueToStringFromZonedDateTime() throws Exception {
ZonedDateTime zdt = ZonedDateTime.parse("2012-02-28T23:32:03-01:30");
assertEquals("2012-02-28T23:32:03-01:30", instance.valueToString(zdt, null, null, null, null, null));
assertEquals("2012-02-28T23:32:03-01:30", instance.valueToString(zdt, null, null, null, null, null));
zdt = zdt.withZoneSameInstant(ZoneId.of("GMT+11:00"));
assertEquals("2012-02-29T12:02:03+11:00", instance.valueToString(zdt, null, null, null, null, null));
zdt = zdt.withZoneSameInstant(ZoneId.of("GMT+11:00"));
assertEquals("2012-02-29T12:02:03+11:00", instance.valueToString(zdt, null, null, null, null, null));
zdt = zdt.plus(123, ChronoUnit.MILLIS);
zdt = zdt.plus(123, ChronoUnit.MILLIS);
assertEquals("2012-02-29T12:02:03.123+11:00", instance.valueToString(zdt, null, null, null, null, null));
assertEquals("2012-02-29T12:02:03.123+11:00", instance.valueToString(zdt, null, null, 3, null, null));
zdt = zdt.plus(456789, ChronoUnit.NANOS);
assertEquals("2012-02-29T12:02:03.123456789+11:00", instance.valueToString(zdt, null, null, 9, null, null));
}
assertEquals("2012-02-29T12:02:03.123+11:00", instance.valueToString(zdt, null, null, null, null, null));
assertEquals("2012-02-29T12:02:03.123+11:00", instance.valueToString(zdt, null, null, 3, null, null));
@Test
public void valueToStringFromCalendar() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
setTimeZone(dateTime, "GMT");
dateTime.set(2012, 1, 29, 1, 2, 3);
assertEquals("2012-02-29T01:02:03Z", instance.valueToString(dateTime, null, null, null, null, null));
assertEquals("2012-02-29T01:02:03Z", instance.valueToString(dateTime, null, null, 0, null, null));
assertEquals("2012-02-29T01:02:03Z", instance.valueToString(dateTime, null, null, 5, null, null));
zdt = zdt.plus(456789, ChronoUnit.NANOS);
assertEquals("2012-02-29T12:02:03.123456789+11:00", instance.valueToString(zdt, null, null, 9, null, null));
}
setTimeZone(dateTime, "GMT-1:30");
assertEquals("2012-02-28T23:32:03-01:30", instance.valueToString(dateTime, null, null, null, null, null));
@Test
public void valueToStringFromCalendar() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
setTimeZone(dateTime, "GMT");
dateTime.set(2012, 1, 29, 1, 2, 3);
assertEquals("2012-02-29T01:02:03Z", instance.valueToString(dateTime, null, null, null, null, null));
assertEquals("2012-02-29T01:02:03Z", instance.valueToString(dateTime, null, null, 0, null, null));
assertEquals("2012-02-29T01:02:03Z", instance.valueToString(dateTime, null, null, 5, null, null));
setTimeZone(dateTime, "GMT+11:00");
assertEquals("2012-02-29T12:02:03+11:00", instance.valueToString(dateTime, null, null, null, null, null));
setTimeZone(dateTime, "GMT-1:30");
assertEquals("2012-02-28T23:32:03-01:30", instance.valueToString(dateTime, null, null, null, null, null));
dateTime.set(Calendar.MILLISECOND, 503);
assertEquals("2012-02-29T12:02:03.503+11:00", instance.valueToString(dateTime, null, null, null, null, null));
assertEquals("2012-02-29T12:02:03.503+11:00", instance.valueToString(dateTime, null, null, 3, null, null));
setTimeZone(dateTime, "GMT+11:00");
assertEquals("2012-02-29T12:02:03+11:00", instance.valueToString(dateTime, null, null, null, null, null));
dateTime.set(Calendar.MILLISECOND, 530);
assertEquals("2012-02-29T12:02:03.53+11:00", instance.valueToString(dateTime, null, null, null, null, null));
assertEquals("2012-02-29T12:02:03.53+11:00", instance.valueToString(dateTime, null, null, 3, null, null));
dateTime.set(Calendar.MILLISECOND, 503);
assertEquals("2012-02-29T12:02:03.503+11:00", instance.valueToString(dateTime, null, null, null, null, null));
assertEquals("2012-02-29T12:02:03.503+11:00", instance.valueToString(dateTime, null, null, 3, null, null));
dateTime.set(Calendar.MILLISECOND, 53);
assertEquals("2012-02-29T12:02:03.053+11:00", instance.valueToString(dateTime, null, null, null, null, null));
assertEquals("2012-02-29T12:02:03.053+11:00", instance.valueToString(dateTime, null, null, 3, null, null));
}
dateTime.set(Calendar.MILLISECOND, 530);
assertEquals("2012-02-29T12:02:03.53+11:00", instance.valueToString(dateTime, null, null, null, null, null));
assertEquals("2012-02-29T12:02:03.53+11:00", instance.valueToString(dateTime, null, null, 3, null, null));
@Test
public void valueToStringFromLong() throws Exception {
Long millis = 1330558323000L;
assertEquals("2012-02-29T23:32:03Z", instance.valueToString(millis, null, null, null, null, null));
millis = 1330558323007L;
assertEquals("2012-02-29T23:32:03.007Z", instance.valueToString(millis, null, null, null, null, null));
assertEquals("2012-02-29T23:32:03.007Z", instance.valueToString(millis, null, null, 3, null, null));
assertEquals("1969-12-31T23:59:59.9Z", instance.valueToString(-100L, null, null, 1, null, null));
assertEquals("1969-12-31T23:59:59.98Z", instance.valueToString(-20L, null, null, 2, null, null));
}
dateTime.set(Calendar.MILLISECOND, 53);
assertEquals("2012-02-29T12:02:03.053+11:00", instance.valueToString(dateTime, null, null, null, null, null));
assertEquals("2012-02-29T12:02:03.053+11:00", instance.valueToString(dateTime, null, null, 3, null, null));
}
@Test
public void valueToStringFromJavaUtilDate() throws Exception {
final Long millis = 1330558323007L;
final Date date = new Date(millis);
assertEquals("2012-02-29T23:32:03.007Z", instance.valueToString(date, null, null, null, null, null));
assertEquals("2012-02-29T23:32:03.007Z", instance.valueToString(date, null, null, 3, null, null));
}
@Test
public void valueToStringFromLong() throws Exception {
Long millis = 1330558323000L;
assertEquals("2012-02-29T23:32:03Z", instance.valueToString(millis, null, null, null, null, null));
millis = 1330558323007L;
assertEquals("2012-02-29T23:32:03.007Z", instance.valueToString(millis, null, null, null, null, null));
assertEquals("2012-02-29T23:32:03.007Z", instance.valueToString(millis, null, null, 3, null, null));
assertEquals("1969-12-31T23:59:59.9Z", instance.valueToString(-100L, null, null, 1, null, null));
assertEquals("1969-12-31T23:59:59.98Z", instance.valueToString(-20L, null, null, 2, null, null));
}
@Test
public void valueToStringFromTimestamp() throws Exception {
Timestamp timestamp = new Timestamp(0);
timestamp.setNanos(120);
assertEquals("1970-01-01T00:00:00.00000012Z", instance.valueToString(timestamp, null, null, null, null, null));
assertEquals("1970-01-01T00:00:00.00000012Z", instance.valueToString(timestamp, null, null, 8, null, null));
@Test
public void valueToStringFromJavaUtilDate() throws Exception {
final Long millis = 1330558323007L;
final Date date = new Date(millis);
assertEquals("2012-02-29T23:32:03.007Z", instance.valueToString(date, null, null, null, null, null));
assertEquals("2012-02-29T23:32:03.007Z", instance.valueToString(date, null, null, 3, null, null));
}
}
@Test
public void valueToStringFromTimestamp() throws Exception {
Timestamp timestamp = new Timestamp(0);
timestamp.setNanos(120);
assertEquals("1970-01-01T00:00:00.00000012Z", instance.valueToString(timestamp, null, null, null, null, null));
assertEquals("1970-01-01T00:00:00.00000012Z", instance.valueToString(timestamp, null, null, 8, null, null));
@Test
public void valueToStringFromInvalidTypes() throws Exception {
expectTypeErrorInValueToString(instance, Integer.valueOf(0));
expectTypeErrorInValueToString(instance, Time.valueOf("12:13:14"));
expectTypeErrorInValueToString(instance, java.sql.Date.valueOf("2019-10-25"));
}
}
@Test
public void valueOfStringToInstant() throws Exception {
Instant instant = Instant.parse("2012-02-29T01:02:03Z");
assertEquals(instant,
instance.valueOfString("2012-02-29T01:02:03Z", null, null, null, null, null, Instant.class));
assertEquals(instant,
instance.valueOfString("2012-02-29T01:02:03", null, null, null, null, null, Instant.class));
}
@Test
public void valueToStringFromInvalidTypes() throws Exception {
expectTypeErrorInValueToString(instance, Integer.valueOf(0));
expectTypeErrorInValueToString(instance, Time.valueOf("12:13:14"));
expectTypeErrorInValueToString(instance, java.sql.Date.valueOf("2019-10-25"));
}
@Test
public void valueOfStringToZonedDateTime() throws Exception {
ZonedDateTime zdt = ZonedDateTime.parse("2012-02-29T01:02:03-01:30");
assertEquals(zdt,
instance.valueOfString("2012-02-29T01:02:03-01:30", null, null, null, null, null, ZonedDateTime.class));
}
@Test
public void valueOfStringToInstant() throws Exception {
Instant instant = Instant.parse("2012-02-29T01:02:03Z");
assertEquals(instant,
instance.valueOfString("2012-02-29T01:02:03Z", null, null, null, null, null, Instant.class));
assertEquals(instant,
instance.valueOfString("2012-02-29T01:02:03", null, null, null, null, null, Instant.class));
}
@Test
public void valueOfStringToCalendar() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
setTimeZone(dateTime, "GMT");
dateTime.set(2012, 1, 29, 1, 2, 3);
assertEqualCalendar(dateTime,
instance.valueOfString("2012-02-29T01:02:03Z", null, null, null, null, null, Calendar.class));
assertEqualCalendar(dateTime,
instance.valueOfString("2012-02-29T01:02:03", null, null, null, null, null, Calendar.class));
@Test
public void valueOfStringToZonedDateTime() throws Exception {
ZonedDateTime zdt = ZonedDateTime.parse("2012-02-29T01:02:03-01:30");
assertEquals(zdt,
instance.valueOfString("2012-02-29T01:02:03-01:30", null, null, null, null, null, ZonedDateTime.class));
}
dateTime.clear();
setTimeZone(dateTime, "GMT-01:30");
dateTime.set(2012, 1, 29, 1, 2, 3);
assertEquals(dateTime.getTime(),
instance.valueOfString("2012-02-29T01:02:03-01:30", null, null, null, null, null, Date.class));
@Test
public void valueOfStringToCalendar() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
setTimeZone(dateTime, "GMT");
dateTime.set(2012, 1, 29, 1, 2, 3);
assertEqualCalendar(dateTime,
instance.valueOfString("2012-02-29T01:02:03Z", null, null, null, null, null, Calendar.class));
assertEqualCalendar(dateTime,
instance.valueOfString("2012-02-29T01:02:03", null, null, null, null, null, Calendar.class));
dateTime.clear();
setTimeZone(dateTime, "GMT+11:00");
dateTime.set(2012, 1, 29, 1, 2, 3);
assertEqualCalendar(dateTime,
instance.valueOfString("2012-02-29T01:02:03+11:00", null, null, null, null, null, Calendar.class));
dateTime.add(Calendar.MILLISECOND, 7);
assertEqualCalendar(dateTime,
instance.valueOfString("2012-02-29T01:02:03.007+11:00", null, null, 3, null, null, Calendar.class));
}
dateTime.clear();
setTimeZone(dateTime, "GMT-01:30");
dateTime.set(2012, 1, 29, 1, 2, 3);
assertEquals(dateTime.getTime(),
instance.valueOfString("2012-02-29T01:02:03-01:30", null, null, null, null, null, Date.class));
@Test
public void valueOfStringToTimestamp() throws Exception {
assertEquals(530000001, instance
.valueOfString("2012-02-29T01:02:03.530000001+11:00", null, null, 9, null, null, Timestamp.class)
.getNanos());
}
dateTime.clear();
setTimeZone(dateTime, "GMT+11:00");
dateTime.set(2012, 1, 29, 1, 2, 3);
assertEqualCalendar(dateTime,
instance.valueOfString("2012-02-29T01:02:03+11:00", null, null, null, null, null, Calendar.class));
dateTime.add(Calendar.MILLISECOND, 7);
assertEqualCalendar(dateTime,
instance.valueOfString("2012-02-29T01:02:03.007+11:00", null, null, 3, null, null, Calendar.class));
}
@Test
public void valueOfStringToLong() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
setTimeZone(dateTime, "GMT");
dateTime.set(2012, 1, 29, 1, 2, 3);
assertEquals(Long.valueOf(dateTime.getTimeInMillis()),
instance.valueOfString("2012-02-29T01:02:03+00:00", null, null, null, null, null, Long.class));
assertEquals(Long.valueOf(120000L),
instance.valueOfString("1970-01-01T00:02Z", null, null, null, null, null, Long.class));
assertEquals(Long.valueOf(120000L),
instance.valueOfString("1970-01-01T00:02", null, null, null, null, null, Long.class));
assertEquals(Long.valueOf(12L),
instance.valueOfString("1970-01-01T00:00:00.012", null, null, 3, null, null, Long.class));
assertEquals(Long.valueOf(120L),
instance.valueOfString("1970-01-01T00:00:00.12", null, null, 2, null, null, Long.class));
}
@Test
public void valueOfStringToTimestamp() throws Exception {
assertEquals(530000001, instance
.valueOfString("2012-02-29T01:02:03.530000001+11:00", null, null, 9, null, null, Timestamp.class)
.getNanos());
}
@Test
public void valueOfStringToJavaSqlTime() throws Exception {
assertEquals(new Time(120000L),
instance.valueOfString("1970-01-01T00:02", null, null, null, null, null, Time.class));
// java.sql.Time does not keep track of milliseconds.
assertEquals(new Time(0),
instance.valueOfString("1970-01-01T00:00:00.012", null, null, 3, null, null, Time.class));
assertEquals(new Time(0),
instance.valueOfString("1970-01-01T00:00:00.12", null, null, 2, null, null, Time.class));
}
@Test
public void valueOfStringToLong() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
setTimeZone(dateTime, "GMT");
dateTime.set(2012, 1, 29, 1, 2, 3);
assertEquals(Long.valueOf(dateTime.getTimeInMillis()),
instance.valueOfString("2012-02-29T01:02:03+00:00", null, null, null, null, null, Long.class));
assertEquals(Long.valueOf(120000L),
instance.valueOfString("1970-01-01T00:02Z", null, null, null, null, null, Long.class));
assertEquals(Long.valueOf(120000L),
instance.valueOfString("1970-01-01T00:02", null, null, null, null, null, Long.class));
assertEquals(Long.valueOf(12L),
instance.valueOfString("1970-01-01T00:00:00.012", null, null, 3, null, null, Long.class));
assertEquals(Long.valueOf(120L),
instance.valueOfString("1970-01-01T00:00:00.12", null, null, 2, null, null, Long.class));
}
@Test
public void valueOfStringToJavaSqlDate() throws Exception {
assertEquals(new java.sql.Date(120000L),
instance.valueOfString("1970-01-01T00:02", null, null, null, null, null, java.sql.Date.class));
// java.sql.Time does not keep track of milliseconds.
assertEquals(new java.sql.Date(0),
instance.valueOfString("1970-01-01T00:00:00.012", null, null, 3, null, null, java.sql.Date.class));
assertEquals(new java.sql.Date(0),
instance.valueOfString("1970-01-01T00:00:00.12", null, null, 2, null, null, java.sql.Date.class));
}
@Test
public void valueOfStringInvalidData() throws Exception {
expectContentErrorInValueOfString(instance, "2012-02-29T23:32:02X");
expectContentErrorInValueOfString(instance, "2012-02-29T23:32:02+24:00");
expectContentErrorInValueOfString(instance, "2012-02-30T01:02:03");
expectContentErrorInValueOfString(instance, "2012-02-29T23:32:02.0000000000000");
@Test
public void valueOfStringToJavaSqlTime() throws Exception {
assertEquals(new Time(120000L),
instance.valueOfString("1970-01-01T00:02", null, null, null, null, null, Time.class));
// java.sql.Time does not keep track of milliseconds.
assertEquals(new Time(0),
instance.valueOfString("1970-01-01T00:00:00.012", null, null, 3, null, null, Time.class));
assertEquals(new Time(0),
instance.valueOfString("1970-01-01T00:00:00.12", null, null, 2, null, null, Time.class));
}
expectTypeErrorInValueOfString(instance, "2012-02-29T01:02:03Z");
}
@Test
public void valueOfStringToJavaSqlDate() throws Exception {
assertEquals(new java.sql.Date(120000L),
instance.valueOfString("1970-01-01T00:02", null, null, null, null, null, java.sql.Date.class));
// java.sql.Time does not keep track of milliseconds.
assertEquals(new java.sql.Date(0),
instance.valueOfString("1970-01-01T00:00:00.012", null, null, 3, null, null, java.sql.Date.class));
assertEquals(new java.sql.Date(0),
instance.valueOfString("1970-01-01T00:00:00.12", null, null, 2, null, null, java.sql.Date.class));
}
@Test
public void valueOfStringInvalidData() throws Exception {
expectContentErrorInValueOfString(instance, "2012-02-29T23:32:02X");
expectContentErrorInValueOfString(instance, "2012-02-29T23:32:02+24:00");
expectContentErrorInValueOfString(instance, "2012-02-30T01:02:03");
expectContentErrorInValueOfString(instance, "2012-02-29T23:32:02.0000000000000");
expectTypeErrorInValueOfString(instance, "2012-02-29T01:02:03Z");
}
}

View File

@ -23,7 +23,6 @@ import static org.junit.Assert.assertEquals;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
@ -37,154 +36,154 @@ import org.junit.Test;
public class EdmTimeOfDayTest extends PrimitiveTypeBaseTest {
private final EdmPrimitiveType instance = EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.TimeOfDay);
private final EdmPrimitiveType instance = EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.TimeOfDay);
@Test
public void toUriLiteral() throws Exception {
assertEquals("11:12", instance.toUriLiteral("11:12"));
assertEquals("11:12:13.012", instance.toUriLiteral("11:12:13.012"));
}
@Test
public void toUriLiteral() throws Exception {
assertEquals("11:12", instance.toUriLiteral("11:12"));
assertEquals("11:12:13.012", instance.toUriLiteral("11:12:13.012"));
}
@Test
public void fromUriLiteral() throws Exception {
assertEquals("11:12", instance.fromUriLiteral("11:12"));
assertEquals("11:12:13.012", instance.fromUriLiteral("11:12:13.012"));
}
@Test
public void fromUriLiteral() throws Exception {
assertEquals("11:12", instance.fromUriLiteral("11:12"));
assertEquals("11:12:13.012", instance.fromUriLiteral("11:12:13.012"));
}
@Test
public void valueToString() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
setTimeZone(dateTime, "GMT+11:30");
dateTime.set(1, 2, 3, 4, 5, 6);
assertEquals("04:05:06", instance.valueToString(dateTime, null, null, null, null, null));
@Test
public void valueToString() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
setTimeZone(dateTime, "GMT+11:30");
dateTime.set(1, 2, 3, 4, 5, 6);
assertEquals("04:05:06", instance.valueToString(dateTime, null, null, null, null, null));
dateTime.add(Calendar.MILLISECOND, 42);
assertEquals("04:05:06.042", instance.valueToString(dateTime, null, null, null, null, null));
assertEquals("04:05:06.042", instance.valueToString(dateTime, null, null, 3, null, null));
assertEquals("04:05:06.042", instance.valueToString(dateTime, null, null, 4, null, null));
dateTime.add(Calendar.MILLISECOND, 42);
assertEquals("04:05:06.042", instance.valueToString(dateTime, null, null, null, null, null));
assertEquals("04:05:06.042", instance.valueToString(dateTime, null, null, 3, null, null));
assertEquals("04:05:06.042", instance.valueToString(dateTime, null, null, 4, null, null));
Calendar dateTime2 = Calendar.getInstance();
dateTime2.clear();
setTimeZone(dateTime, TimeZone.getDefault());
dateTime2.set(Calendar.HOUR, 5);
dateTime2.set(Calendar.MINUTE, 59);
dateTime2.set(Calendar.SECOND, 23);
final java.sql.Time time = new java.sql.Time(dateTime2.getTimeInMillis());
assertEquals("05:59:23", instance.valueToString(time, null, null, null, null, null));
Calendar dateTime2 = Calendar.getInstance();
dateTime2.clear();
setTimeZone(dateTime, TimeZone.getDefault());
dateTime2.set(Calendar.HOUR, 5);
dateTime2.set(Calendar.MINUTE, 59);
dateTime2.set(Calendar.SECOND, 23);
assertEquals("05:59:23", instance.valueToString(dateTime2.getTimeInMillis(), null, null, null, null, null));
final java.sql.Time time = new java.sql.Time(dateTime2.getTimeInMillis());
assertEquals("05:59:23", instance.valueToString(time, null, null, null, null, null));
assertEquals("05:59:23", instance.valueToString(dateTime2.getTimeInMillis(), null, null, null, null, null));
// Timestamp timestamp = new Timestamp(0);
// timestamp.setNanos(42);
expectTypeErrorInValueToString(instance, 0);
}
expectTypeErrorInValueToString(instance, 0);
}
@Test
public void valueToStringFromJavaUtilDate() throws Exception {
LocalTime time = LocalTime.parse("04:05:06");
ZonedDateTime zdt = ZonedDateTime.of(LocalDate.ofEpochDay(0), time, ZoneId.systemDefault());
long millis = zdt.toInstant().toEpochMilli();
java.util.Date javaUtilDate = new java.util.Date(millis);
assertEquals("04:05:06", instance.valueToString(javaUtilDate, null, null, null, null, null));
java.sql.Timestamp javaSqlTimestamp = new java.sql.Timestamp(millis);
assertEquals("04:05:06", instance.valueToString(javaSqlTimestamp, null, null, null, null, null));
}
@Test
public void valueToStringFromJavaUtilDate() throws Exception {
LocalTime time = LocalTime.parse("04:05:06");
ZonedDateTime zdt = ZonedDateTime.of(LocalDate.ofEpochDay(0), time, ZoneId.systemDefault());
long millis = zdt.toInstant().toEpochMilli();
@Test
public void valueToStringFromLocalTime() throws Exception {
LocalTime time = LocalTime.parse("04:05:06");
assertEquals("04:05:06", instance.valueToString(time, null, null, null, null, null));
}
@Test
public void valueToStringFromJavaSqlTime() throws Exception {
java.sql.Time time = java.sql.Time.valueOf("04:05:06");
assertEquals("04:05:06", instance.valueToString(time, null, null, null, null, null));
}
@Test
public void valueOfString() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
java.util.Date javaUtilDate = new java.util.Date(millis);
assertEquals("04:05:06", instance.valueToString(javaUtilDate, null, null, null, null, null));
assertEqualCalendar(dateTime, instance.valueOfString("00:00", null, null, null, null, null, Calendar.class));
assertEqualCalendar(dateTime, instance.valueOfString("00:00:00", null, null, null, null, null, Calendar.class));
assertEqualCalendar(dateTime,
instance.valueOfString("00:00:00.000000000", null, null, null, null, null, Calendar.class));
java.sql.Timestamp javaSqlTimestamp = new java.sql.Timestamp(millis);
assertEquals("04:05:06", instance.valueToString(javaSqlTimestamp, null, null, null, null, null));
}
final Time timeValue = instance.valueOfString("00:00:00.999", null, null, 3, null, null, Time.class);
assertEquals(dateTime.getTimeInMillis(), timeValue.getTime());
@Test
public void valueToStringFromLocalTime() throws Exception {
LocalTime time = LocalTime.parse("04:05:06");
assertEquals("04:05:06", instance.valueToString(time, null, null, null, null, null));
}
dateTime.set(Calendar.MILLISECOND, 999);
assertEqualCalendar(dateTime,
instance.valueOfString("00:00:00.999", null, null, 3, null, null, Calendar.class));
assertEqualCalendar(dateTime,
instance.valueOfString("00:00:00.999", null, null, 3, null, null, Calendar.class));
assertEquals(Long.valueOf(dateTime.getTimeInMillis()),
instance.valueOfString("00:00:00.999", null, null, 3, null, null, Long.class));
@Test
public void valueToStringFromJavaSqlTime() throws Exception {
java.sql.Time time = java.sql.Time.valueOf("04:05:06");
assertEquals("04:05:06", instance.valueToString(time, null, null, null, null, null));
}
final Timestamp timestamp = instance.valueOfString("00:00:00.999888777", null, null, 9, null, null,
Timestamp.class);
assertEquals(dateTime.getTimeInMillis(), timestamp.getTime());
assertEquals(999888777, timestamp.getNanos());
@Test
public void valueOfString() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
assertEqualCalendar(dateTime, instance.valueOfString("00:00", null, null, null, null, null, Calendar.class));
assertEqualCalendar(dateTime, instance.valueOfString("00:00:00", null, null, null, null, null, Calendar.class));
assertEqualCalendar(dateTime,
instance.valueOfString("00:00:00.000000000", null, null, null, null, null, Calendar.class));
final Time timeValue = instance.valueOfString("00:00:00.999", null, null, 3, null, null, Time.class);
assertEquals(dateTime.getTimeInMillis(), timeValue.getTime());
dateTime.set(Calendar.MILLISECOND, 999);
assertEqualCalendar(dateTime,
instance.valueOfString("00:00:00.999", null, null, 3, null, null, Calendar.class));
assertEqualCalendar(dateTime,
instance.valueOfString("00:00:00.999", null, null, 3, null, null, Calendar.class));
assertEquals(Long.valueOf(dateTime.getTimeInMillis()),
instance.valueOfString("00:00:00.999", null, null, 3, null, null, Long.class));
final Timestamp timestamp = instance.valueOfString("00:00:00.999888777", null, null, 9, null, null,
Timestamp.class);
assertEquals(dateTime.getTimeInMillis(), timestamp.getTime());
assertEquals(999888777, timestamp.getNanos());
// expectUnconvertibleErrorInValueOfString(instance, "11:12:13.1234", Calendar.class);
// expectUnconvertibleErrorInValueOfString(instance, "11:12:13.0123456789", Timestamp.class);
expectContentErrorInValueOfString(instance, "24:32:02");
expectContentErrorInValueOfString(instance, "011:12:13");
expectContentErrorInValueOfString(instance, "11:12:13:14");
expectContentErrorInValueOfString(instance, "111213");
expectContentErrorInValueOfString(instance, "1:2:3");
expectContentErrorInValueOfString(instance, "11:12:13.0.1");
expectContentErrorInValueOfString(instance, "24:32:02");
expectContentErrorInValueOfString(instance, "011:12:13");
expectContentErrorInValueOfString(instance, "11:12:13:14");
expectContentErrorInValueOfString(instance, "111213");
expectContentErrorInValueOfString(instance, "1:2:3");
expectContentErrorInValueOfString(instance, "11:12:13.0.1");
// expectContentErrorInValueOfString(instance, "11:12:13.");
expectContentErrorInValueOfString(instance, "11:12:13.0000000000000");
expectContentErrorInValueOfString(instance, "11:12:13.0000000000000");
expectTypeErrorInValueOfString(instance, "11:12:13");
}
@Test
public void valueOfStringToLocalTime() throws Exception {
LocalTime time = LocalTime.parse("04:05:06");
assertEquals(time, instance.valueOfString("04:05:06", null, null, null, null, null, LocalTime.class));
time = time.plus(123, ChronoUnit.MILLIS);
assertEquals(time, instance.valueOfString("04:05:06.123", null, null, null, null, null, LocalTime.class));
time = time.plus(456789, ChronoUnit.NANOS);
assertEquals(time, instance.valueOfString("04:05:06.123456789", null, null, null, null, null, LocalTime.class));
}
@Test
public void valueOfStringToJavaSqlTime() throws Exception {
java.sql.Time time = java.sql.Time.valueOf("04:05:06");
assertEquals(time, instance.valueOfString("04:05:06", null, null, null, null, null, java.sql.Time.class));
}
@Test
public void valueOfStringToJavaUtilDateTime() throws Exception {
LocalTime time = LocalTime.parse("04:05:06");
ZonedDateTime zdt = ZonedDateTime.of(LocalDate.ofEpochDay(0), time, ZoneId.systemDefault());
long millis = zdt.toInstant().toEpochMilli();
java.util.Date javaUtilDate = new java.util.Date(millis);
assertEquals(javaUtilDate, instance.valueOfString("04:05:06", null, null, null, null, null, java.util.Date.class));
}
@Test
public void testRoundTripTime() throws Exception {
java.sql.Time time = instance.valueOfString("04:05:06.002", true, 4000, 3, 0, true, java.sql.Time.class);
String val = instance.valueToString(time, true, 4000, 3, 0, true);
assertEquals("04:05:06", val);
}
expectTypeErrorInValueOfString(instance, "11:12:13");
}
@Test
public void tests() throws Exception {
instance.validate("12:34:55", null, null, null, null, null);
}
@Test
public void valueOfStringToLocalTime() throws Exception {
LocalTime time = LocalTime.parse("04:05:06");
assertEquals(time, instance.valueOfString("04:05:06", null, null, null, null, null, LocalTime.class));
time = time.plus(123, ChronoUnit.MILLIS);
assertEquals(time, instance.valueOfString("04:05:06.123", null, null, null, null, null, LocalTime.class));
time = time.plus(456789, ChronoUnit.NANOS);
assertEquals(time, instance.valueOfString("04:05:06.123456789", null, null, null, null, null, LocalTime.class));
}
@Test
public void valueOfStringToJavaSqlTime() throws Exception {
java.sql.Time time = java.sql.Time.valueOf("04:05:06");
assertEquals(time, instance.valueOfString("04:05:06", null, null, null, null, null, java.sql.Time.class));
}
@Test
public void valueOfStringToJavaUtilDateTime() throws Exception {
LocalTime time = LocalTime.parse("04:05:06");
ZonedDateTime zdt = ZonedDateTime.of(LocalDate.ofEpochDay(0), time, ZoneId.systemDefault());
long millis = zdt.toInstant().toEpochMilli();
java.util.Date javaUtilDate = new java.util.Date(millis);
assertEquals(javaUtilDate, instance.valueOfString("04:05:06", null, null, null, null, null, java.util.Date.class));
}
@Test
public void testRoundTripTime() throws Exception {
java.sql.Time time = instance.valueOfString("04:05:06.002", true, 4000, 3, 0, true, java.sql.Time.class);
String val = instance.valueToString(time, true, 4000, 3, 0, true);
assertEquals("04:05:06", val);
}
@Test
public void tests() throws Exception {
instance.validate("12:34:55", null, null, null, null, null);
}
}