|
24 | 24 | import java.sql.Date; |
25 | 25 | import java.sql.Time; |
26 | 26 | import java.sql.Timestamp; |
| 27 | +import java.time.Instant; |
27 | 28 | import java.time.LocalDateTime; |
| 29 | +import java.time.OffsetDateTime; |
| 30 | +import java.time.ZoneId; |
| 31 | +import java.time.ZonedDateTime; |
28 | 32 | import java.util.Calendar; |
| 33 | +import java.util.Objects; |
29 | 34 | import java.util.TimeZone; |
30 | 35 | import java.util.concurrent.TimeUnit; |
31 | 36 | import java.util.function.Supplier; |
@@ -198,6 +203,82 @@ public void testShouldGetTimestampReturnValidTimestampWithCalendar( |
198 | 203 | }); |
199 | 204 | } |
200 | 205 |
|
| 206 | + @ParameterizedTest |
| 207 | + @MethodSource("data") |
| 208 | + public void testShouldGetObjectReturnValidLocalDateTime( |
| 209 | + Supplier<TimeStampVector> vectorSupplier, String vectorType, String timeZone) |
| 210 | + throws Exception { |
| 211 | + setup(vectorSupplier); |
| 212 | + final String expectedTimeZone = Objects.requireNonNullElse(timeZone, "UTC"); |
| 213 | + |
| 214 | + accessorIterator.iterate( |
| 215 | + vector, |
| 216 | + (accessor, currentRow) -> { |
| 217 | + final LocalDateTime value = accessor.getObject(LocalDateTime.class); |
| 218 | + |
| 219 | + assertThat( |
| 220 | + value, equalTo(getZonedDateTime(currentRow, expectedTimeZone).toLocalDateTime())); |
| 221 | + assertThat(accessor.wasNull(), is(false)); |
| 222 | + }); |
| 223 | + } |
| 224 | + |
| 225 | + @ParameterizedTest |
| 226 | + @MethodSource("data") |
| 227 | + public void testShouldGetObjectReturnValidInstant( |
| 228 | + Supplier<TimeStampVector> vectorSupplier, String vectorType, String timeZone) |
| 229 | + throws Exception { |
| 230 | + setup(vectorSupplier); |
| 231 | + final String expectedTimeZone = Objects.requireNonNullElse(timeZone, "UTC"); |
| 232 | + |
| 233 | + accessorIterator.iterate( |
| 234 | + vector, |
| 235 | + (accessor, currentRow) -> { |
| 236 | + final Instant value = accessor.getObject(Instant.class); |
| 237 | + |
| 238 | + assertThat(value, equalTo(getZonedDateTime(currentRow, expectedTimeZone).toInstant())); |
| 239 | + assertThat(accessor.wasNull(), is(false)); |
| 240 | + }); |
| 241 | + } |
| 242 | + |
| 243 | + @ParameterizedTest |
| 244 | + @MethodSource("data") |
| 245 | + public void testShouldGetObjectReturnValidOffsetDateTime( |
| 246 | + Supplier<TimeStampVector> vectorSupplier, String vectorType, String timeZone) |
| 247 | + throws Exception { |
| 248 | + setup(vectorSupplier); |
| 249 | + final String expectedTimeZone = Objects.requireNonNullElse(timeZone, "UTC"); |
| 250 | + |
| 251 | + accessorIterator.iterate( |
| 252 | + vector, |
| 253 | + (accessor, currentRow) -> { |
| 254 | + final OffsetDateTime value = accessor.getObject(OffsetDateTime.class); |
| 255 | + final OffsetDateTime vectorValue = |
| 256 | + getZonedDateTime(currentRow, expectedTimeZone).toOffsetDateTime(); |
| 257 | + assertThat(value, equalTo(vectorValue)); |
| 258 | + assertThat(value.getOffset(), equalTo(vectorValue.getOffset())); |
| 259 | + assertThat(accessor.wasNull(), is(false)); |
| 260 | + }); |
| 261 | + } |
| 262 | + |
| 263 | + @ParameterizedTest |
| 264 | + @MethodSource("data") |
| 265 | + public void testShouldGetObjectReturnValidZonedDateTime( |
| 266 | + Supplier<TimeStampVector> vectorSupplier, String vectorType, String timeZone) |
| 267 | + throws Exception { |
| 268 | + setup(vectorSupplier); |
| 269 | + final String expectedTimeZone = Objects.requireNonNullElse(timeZone, "UTC"); |
| 270 | + |
| 271 | + accessorIterator.iterate( |
| 272 | + vector, |
| 273 | + (accessor, currentRow) -> { |
| 274 | + final ZonedDateTime value = accessor.getObject(ZonedDateTime.class); |
| 275 | + |
| 276 | + assertThat(value, equalTo(getZonedDateTime(currentRow, expectedTimeZone))); |
| 277 | + assertThat(value.getZone(), equalTo(ZoneId.of(expectedTimeZone))); |
| 278 | + assertThat(accessor.wasNull(), is(false)); |
| 279 | + }); |
| 280 | + } |
| 281 | + |
201 | 282 | @ParameterizedTest |
202 | 283 | @MethodSource("data") |
203 | 284 | public void testShouldGetTimestampReturnNull(Supplier<TimeStampVector> vectorSupplier) { |
@@ -319,6 +400,24 @@ private Timestamp getTimestampForVector(int currentRow, String timeZone) { |
319 | 400 | return expectedTimestamp; |
320 | 401 | } |
321 | 402 |
|
| 403 | + private ZonedDateTime getZonedDateTime(int currentRow, String timeZone) { |
| 404 | + Object object = vector.getObject(currentRow); |
| 405 | + TimeZone tz = TimeZone.getTimeZone(timeZone); |
| 406 | + ZonedDateTime expectedTimestamp = null; |
| 407 | + if (object instanceof LocalDateTime) { |
| 408 | + expectedTimestamp = ((LocalDateTime) object).atZone(tz.toZoneId()); |
| 409 | + } else if (object instanceof Long) { |
| 410 | + TimeUnit timeUnit = getTimeUnitForVector(vector); |
| 411 | + long millis = timeUnit.toMillis((Long) object); |
| 412 | + long offset = tz.getOffset(millis); |
| 413 | + // TODO: should we actually add the offset here? I'm not completely sure how the value is |
| 414 | + // stored in the vector |
| 415 | + LocalDateTime local = new Timestamp(millis + offset).toLocalDateTime(); |
| 416 | + expectedTimestamp = ZonedDateTime.of(local, tz.toZoneId()); |
| 417 | + } |
| 418 | + return expectedTimestamp; |
| 419 | + } |
| 420 | + |
322 | 421 | @ParameterizedTest |
323 | 422 | @MethodSource("data") |
324 | 423 | public void testShouldGetObjectClass(Supplier<TimeStampVector> vectorSupplier) throws Exception { |
|
0 commit comments