|
| 1 | +/* |
| 2 | + * Copyright (c) 2011-2022 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 7 | + * which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 10 | + */ |
| 11 | + |
| 12 | +package io.vertx.oracleclient.test; |
| 13 | + |
| 14 | +import io.vertx.ext.unit.TestContext; |
| 15 | +import io.vertx.ext.unit.junit.VertxUnitRunner; |
| 16 | +import io.vertx.oracleclient.OraclePool; |
| 17 | +import io.vertx.oracleclient.test.junit.OracleRule; |
| 18 | +import io.vertx.sqlclient.PoolOptions; |
| 19 | +import io.vertx.sqlclient.Row; |
| 20 | +import io.vertx.sqlclient.Tuple; |
| 21 | +import io.vertx.sqlclient.desc.ColumnDescriptor; |
| 22 | +import org.junit.After; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.ClassRule; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | + |
| 28 | +import java.sql.JDBCType; |
| 29 | +import java.time.LocalDateTime; |
| 30 | +import java.time.OffsetDateTime; |
| 31 | +import java.time.ZoneOffset; |
| 32 | + |
| 33 | +@RunWith(VertxUnitRunner.class) |
| 34 | +public class OracleTemporalDataTypesTest extends OracleTestBase { |
| 35 | + |
| 36 | + @ClassRule |
| 37 | + public static OracleRule oracle = OracleRule.SHARED_INSTANCE; |
| 38 | + |
| 39 | + OraclePool pool; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() throws Exception { |
| 43 | + pool = OraclePool.pool(vertx, oracle.options(), new PoolOptions()); |
| 44 | + } |
| 45 | + |
| 46 | + @After |
| 47 | + public void tearDown(TestContext ctx) throws Exception { |
| 48 | + pool.close().onComplete(ctx.asyncAssertSuccess()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testEncodeDate(TestContext ctx) { |
| 53 | + testEncode(ctx, "test_date", LocalDateTime.class, LocalDateTime.of(2019, 11, 4, 0, 0)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testEncodeTimestamp(TestContext ctx) { |
| 58 | + testEncode(ctx, "test_timestamp", LocalDateTime.class, LocalDateTime.of(2018, 11, 4, 15, 13, 28)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testEncodeTimestampWithTimezone(TestContext ctx) { |
| 63 | + OffsetDateTime expected = OffsetDateTime.of(LocalDateTime.of(2019, 11, 4, 15, 13, 28), ZoneOffset.ofHoursMinutes(1, 2)); |
| 64 | + testEncode(ctx, "test_timestamp_with_timezone", OffsetDateTime.class, expected); |
| 65 | + } |
| 66 | + |
| 67 | + private <T> void testEncode(TestContext ctx, String columnName, Class<T> clazz, T expected) { |
| 68 | + pool |
| 69 | + .preparedQuery("UPDATE temporal_data_types SET " + columnName + " = ? WHERE id = 2") |
| 70 | + .execute(Tuple.of(expected)) |
| 71 | + .onComplete(ctx.asyncAssertSuccess(updateResult -> { |
| 72 | + pool |
| 73 | + .preparedQuery("SELECT " + columnName + " FROM temporal_data_types WHERE id = 2") |
| 74 | + .execute() |
| 75 | + .onComplete(ctx.asyncAssertSuccess(result -> { |
| 76 | + ctx.assertEquals(1, result.size()); |
| 77 | + Row row = result.iterator().next(); |
| 78 | + ctx.assertEquals(1, row.size()); |
| 79 | + ctx.assertEquals(expected, row.get(clazz, 0)); |
| 80 | + ctx.assertEquals(expected, row.get(clazz, columnName)); |
| 81 | + ctx.assertEquals(expected, row.getValue(0)); |
| 82 | + ctx.assertEquals(expected, row.getValue(columnName)); |
| 83 | + })); |
| 84 | + })); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testDecodeDate(TestContext ctx) { |
| 89 | + testDecode(ctx, "test_date", JDBCType.TIMESTAMP, LocalDateTime.class, LocalDateTime.of(2019, 11, 4, 0, 0)); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testDecodeTimestamp(TestContext ctx) { |
| 94 | + testDecode(ctx, "test_timestamp", JDBCType.TIMESTAMP, LocalDateTime.class, LocalDateTime.of(2018, 11, 4, 15, 13, 28)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testDecodeTimestampWithTimezone(TestContext ctx) { |
| 99 | + OffsetDateTime expected = OffsetDateTime.of(LocalDateTime.of(2019, 11, 4, 15, 13, 28), ZoneOffset.ofHoursMinutes(1, 2)); |
| 100 | + testDecode(ctx, "test_timestamp_with_timezone", JDBCType.TIMESTAMP_WITH_TIMEZONE, OffsetDateTime.class, expected); |
| 101 | + } |
| 102 | + |
| 103 | + private <T> void testDecode(TestContext ctx, String columnName, JDBCType jdbcType, Class<?> clazz, T expected) { |
| 104 | + pool |
| 105 | + .preparedQuery("SELECT " + columnName + " FROM temporal_data_types WHERE id = 1") |
| 106 | + .execute() |
| 107 | + .onComplete(ctx.asyncAssertSuccess(result -> { |
| 108 | + ctx.assertEquals(1, result.size()); |
| 109 | + Row row = result.iterator().next(); |
| 110 | + ctx.assertEquals(expected, row.get(clazz, 0)); |
| 111 | + ctx.assertEquals(expected, row.get(clazz, columnName)); |
| 112 | + ctx.assertEquals(expected, row.getValue(0)); |
| 113 | + ctx.assertEquals(expected, row.getValue(columnName)); |
| 114 | + ColumnDescriptor columnDescriptor = result.columnDescriptors().get(0); |
| 115 | + ctx.assertEquals(jdbcType, columnDescriptor.jdbcType()); |
| 116 | + ctx.assertNotNull(columnDescriptor); |
| 117 | + })); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testEncodeNull(TestContext ctx) { |
| 122 | + pool |
| 123 | + .preparedQuery("UPDATE temporal_data_types SET test_date = ?, test_timestamp = ?, test_timestamp_with_timezone = ? WHERE id = 2") |
| 124 | + .execute(Tuple.tuple().addValue(null).addValue(null).addValue(null)) |
| 125 | + .onComplete(ctx.asyncAssertSuccess(updateResult -> { |
| 126 | + pool |
| 127 | + .preparedQuery("SELECT * FROM temporal_data_types WHERE id = 2") |
| 128 | + .execute() |
| 129 | + .onComplete(ctx.asyncAssertSuccess(result -> { |
| 130 | + ctx.assertEquals(1, result.size()); |
| 131 | + Row row = result.iterator().next(); |
| 132 | + ctx.assertEquals(4, row.size()); |
| 133 | + ctx.assertEquals(2, row.getInteger(0)); |
| 134 | + for (int i = 1; i < row.size(); i++) { |
| 135 | + ctx.assertNull(row.getValue(i)); |
| 136 | + } |
| 137 | + })); |
| 138 | + })); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testDecodeNull(TestContext ctx) { |
| 143 | + pool |
| 144 | + .preparedQuery("SELECT test_date, test_timestamp, test_timestamp_with_timezone FROM temporal_data_types WHERE id = 3") |
| 145 | + .execute() |
| 146 | + .onComplete(ctx.asyncAssertSuccess(result -> { |
| 147 | + ctx.assertEquals(1, result.size()); |
| 148 | + Row row = result.iterator().next(); |
| 149 | + ctx.assertEquals(3, row.size()); |
| 150 | + for (int i = 0; i < row.size(); i++) { |
| 151 | + ctx.assertNull(row.getValue(i)); |
| 152 | + } |
| 153 | + })); |
| 154 | + } |
| 155 | +} |
0 commit comments