|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.shardingsphere.database.protocol.postgresql.packet.command.query; |
| 19 | + |
| 20 | +import org.apache.shardingsphere.database.connector.core.resultset.ResultSetMapper; |
| 21 | +import org.apache.shardingsphere.database.connector.core.type.DatabaseType; |
| 22 | +import org.apache.shardingsphere.database.protocol.postgresql.payload.PostgreSQLPacketPayload; |
| 23 | +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 26 | +import org.mockito.ArgumentCaptor; |
| 27 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 28 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 29 | +import org.mockito.quality.Strictness; |
| 30 | + |
| 31 | +import java.nio.charset.StandardCharsets; |
| 32 | +import java.sql.ResultSet; |
| 33 | +import java.sql.ResultSetMetaData; |
| 34 | +import java.sql.Types; |
| 35 | +import java.time.OffsetDateTime; |
| 36 | +import java.util.Collections; |
| 37 | + |
| 38 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 39 | +import static org.hamcrest.Matchers.is; |
| 40 | +import static org.mockito.Mockito.mock; |
| 41 | +import static org.mockito.Mockito.verify; |
| 42 | +import static org.mockito.Mockito.when; |
| 43 | + |
| 44 | +@ExtendWith(MockitoExtension.class) |
| 45 | +@MockitoSettings(strictness = Strictness.LENIENT) |
| 46 | +class PostgreSQLDataRowPacketResultSetMapperITTest { |
| 47 | + |
| 48 | + @Test |
| 49 | + void assertTimestampTzValueFromResultSetMapperIsWrittenByDataRowPacketAsText() throws Exception { |
| 50 | + ResultSet resultSet = mock(ResultSet.class); |
| 51 | + ResultSetMetaData metaData = mock(ResultSetMetaData.class); |
| 52 | + when(resultSet.getMetaData()).thenReturn(metaData); |
| 53 | + |
| 54 | + when(metaData.getColumnType(1)).thenReturn(Types.TIMESTAMP_WITH_TIMEZONE); |
| 55 | + |
| 56 | + OffsetDateTime expected = OffsetDateTime.parse("2020-01-01T10:20:30+08:00"); |
| 57 | + when(resultSet.getObject(1)).thenReturn(expected); |
| 58 | + when(resultSet.wasNull()).thenReturn(false); |
| 59 | + |
| 60 | + DatabaseType postgreSQL = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"); |
| 61 | + Object mapped = new ResultSetMapper(postgreSQL).load(resultSet, 1); |
| 62 | + PostgreSQLPacketPayload payload = mock(PostgreSQLPacketPayload.class); |
| 63 | + when(payload.getCharset()).thenReturn(StandardCharsets.UTF_8); |
| 64 | + PostgreSQLDataRowPacket packet = new PostgreSQLDataRowPacket(Collections.singleton(mapped), Collections.singleton(Types.TIMESTAMP_WITH_TIMEZONE), "+05:30"); |
| 65 | + packet.write(payload); |
| 66 | + |
| 67 | + byte[] expectedBytes = "2020-01-01 07:50:30+05:30".getBytes(StandardCharsets.UTF_8); |
| 68 | + |
| 69 | + verify(payload).writeInt2(1); |
| 70 | + verify(payload).writeInt4(expectedBytes.length); |
| 71 | + |
| 72 | + ArgumentCaptor<byte[]> bytesCaptor = ArgumentCaptor.forClass(byte[].class); |
| 73 | + verify(payload).writeBytes(bytesCaptor.capture()); |
| 74 | + assertThat(bytesCaptor.getValue(), is(expectedBytes)); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments