[OLINGO-796] Add support for java.sql.time

This commit is contained in:
Christian Holzer 2015-12-08 16:01:35 +01:00
parent 5004f4b1ff
commit a47e9f6ad2
3 changed files with 46 additions and 4 deletions

View File

@ -18,6 +18,7 @@
*/ */
package org.apache.olingo.commons.core.edm.primitivetype; package org.apache.olingo.commons.core.edm.primitivetype;
import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
@ -140,6 +141,8 @@ public final class EdmDateTimeOffset extends SingletonPrimitiveType {
Timestamp timestamp = new Timestamp(dateTimeValue.getTimeInMillis()); Timestamp timestamp = new Timestamp(dateTimeValue.getTimeInMillis());
timestamp.setNanos(nanoSeconds); timestamp.setNanos(nanoSeconds);
return returnType.cast(timestamp); return returnType.cast(timestamp);
} else if(returnType.isAssignableFrom(Time.class)) {
return returnType.cast(new Time(dateTimeValue.getTimeInMillis()));
} else { } else {
throw new ClassCastException("unsupported return type " + returnType.getSimpleName()); throw new ClassCastException("unsupported return type " + returnType.getSimpleName());
} }
@ -202,7 +205,10 @@ public final class EdmDateTimeOffset extends SingletonPrimitiveType {
*/ */
protected static <T> Calendar createDateTime(final T value) throws EdmPrimitiveTypeException { protected static <T> Calendar createDateTime(final T value) throws EdmPrimitiveTypeException {
Calendar dateTimeValue; Calendar dateTimeValue;
if (value instanceof Date) { if(value instanceof Time) {
dateTimeValue = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
dateTimeValue.setTimeInMillis(((Time) value).getTime());
} else if (value instanceof Date) {
// Although java.util.Date, as stated in its documentation, // Although java.util.Date, as stated in its documentation,
// "is intended to reflect coordinated universal time (UTC)", // "is intended to reflect coordinated universal time (UTC)",
// its getName() method uses the default time zone. And so do we. // its getName() method uses the default time zone. And so do we.

View File

@ -21,6 +21,7 @@ package org.apache.olingo.commons.core.edm.primitivetype;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
@ -76,6 +77,10 @@ public class EdmDateTimeOffsetTest extends PrimitiveTypeBaseTest {
assertEquals("1969-12-31T23:59:59.9Z", instance.valueToString(-100L, null, null, 1, 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)); assertEquals("1969-12-31T23:59:59.98Z", instance.valueToString(-20L, null, null, 2, null, null));
assertEquals("2012-02-29T23:32:03.007Z", instance.valueToString(new Time(millis), null, null, 3, null, null));
assertEquals("1969-12-31T23:59:59.9Z", instance.valueToString(new Time(-100L), null, null, 1, null, null));
assertEquals("1969-12-31T23:59:59.98Z", instance.valueToString(new Time(-20L), null, null, 2, null, null));
final Date date = new Date(millis); final Date date = new Date(millis);
final String time = date.toString().substring(11, 19); final String time = date.toString().substring(11, 19);
assertTrue(instance.valueToString(date, null, null, 3, null, null).contains(time)); assertTrue(instance.valueToString(date, null, null, 3, null, null).contains(time));
@ -129,6 +134,13 @@ public class EdmDateTimeOffsetTest extends PrimitiveTypeBaseTest {
assertEquals(Long.valueOf(120L), instance.valueOfString("1970-01-01T00:00:00.12", null, null, 2, null, null, assertEquals(Long.valueOf(120L), instance.valueOfString("1970-01-01T00:00:00.12", null, null, 2, null, null,
Long.class)); Long.class));
assertEquals(new Time(120000L), instance.valueOfString("1970-01-01T00:02", null, null, null, null, null,
Time.class));
assertEquals(new Time(12L), instance.valueOfString("1970-01-01T00:00:00.012", null, null, 3, null, null,
Time.class));
assertEquals(new Time(120L), instance.valueOfString("1970-01-01T00:00:00.12", null, null, 2, null, null,
Time.class));
expectFacetsErrorInValueOfString(instance, "2012-02-29T23:32:02.9Z", null, null, null, null, null); expectFacetsErrorInValueOfString(instance, "2012-02-29T23:32:02.9Z", null, null, null, null, null);
expectFacetsErrorInValueOfString(instance, "2012-02-29T23:32:02.9Z", null, null, 0, null, null); expectFacetsErrorInValueOfString(instance, "2012-02-29T23:32:02.9Z", null, null, 0, null, null);
expectContentErrorInValueOfString(instance, "2012-02-29T23:32:02X"); expectContentErrorInValueOfString(instance, "2012-02-29T23:32:02X");

View File

@ -20,6 +20,7 @@ package org.apache.olingo.commons.core.edm.primitivetype;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.sql.Time;
import java.util.Calendar; import java.util.Calendar;
import java.util.TimeZone; import java.util.TimeZone;
@ -61,6 +62,29 @@ public class EdmTimeOfDayTest extends PrimitiveTypeBaseTest {
expectTypeErrorInValueToString(instance, 0); expectTypeErrorInValueToString(instance, 0);
} }
@Test
public void toTimeObject() throws Exception {
Calendar dateTime = Calendar.getInstance();
dateTime.clear();
dateTime.setTimeZone(TimeZone.getTimeZone("GMT"));
dateTime.set(Calendar.HOUR, 12);
Time timeValue = instance.valueOfString("12:00:00", null, null, null, null, null, Time.class);
assertEquals(dateTime.getTimeInMillis(), timeValue.getTime());
}
@Test
public void fromTimeObject() throws Exception {
Calendar dateTime = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
dateTime.clear();
dateTime.set(Calendar.HOUR, 5);
dateTime.set(Calendar.MINUTE, 59);
dateTime.set(Calendar.SECOND, 23);
Time time = new Time(dateTime.getTimeInMillis());
assertEquals("05:59:23", instance.valueToString(time, null, null, null, null, null));
}
@Test @Test
public void valueOfString() throws Exception { public void valueOfString() throws Exception {
Calendar dateTime = Calendar.getInstance(); Calendar dateTime = Calendar.getInstance();