Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLTypeUnspecifiedSQLParameter;
import org.apache.shardingsphere.database.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;

/**
* Binary protocol value for unspecified for PostgreSQL.
Expand All @@ -28,7 +27,7 @@ public final class PostgreSQLUnspecifiedBinaryProtocolValue implements PostgreSQ

@Override
public int getColumnLength(final PostgreSQLPacketPayload payload, final Object value) {
throw new UnsupportedSQLOperationException("getColumnLength");
return value.toString().getBytes(payload.getCharset()).length;
}

@Override
Expand All @@ -41,6 +40,6 @@ public Object read(final PostgreSQLPacketPayload payload, final int parameterVal

@Override
public void write(final PostgreSQLPacketPayload payload, final Object value) {
throw new UnsupportedSQLOperationException("write");
payload.writeStringEOF(value.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,69 @@
import org.apache.shardingsphere.database.protocol.postgresql.packet.ByteBufTestUtils;
import org.apache.shardingsphere.database.protocol.postgresql.packet.command.query.extended.bind.PostgreSQLTypeUnspecifiedSQLParameter;
import org.apache.shardingsphere.database.protocol.postgresql.payload.PostgreSQLPacketPayload;
import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.nio.charset.StandardCharsets;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isA;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
class PostgreSQLUnspecifiedBinaryProtocolValueTest {

@Mock
private ByteBuf byteBuf;

private PostgreSQLPacketPayload payload;

@BeforeEach
void setup() {
payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
}

@Test
void assertGetColumnLength() {
assertThrows(UnsupportedSQLOperationException.class, () -> new PostgreSQLUnspecifiedBinaryProtocolValue().getColumnLength(new PostgreSQLPacketPayload(null, StandardCharsets.UTF_8), "val"));
PostgreSQLUnspecifiedBinaryProtocolValue actual = new PostgreSQLUnspecifiedBinaryProtocolValue();
assertThat(actual.getColumnLength(payload, "val"), is(3));
assertThat(actual.getColumnLength(payload, new PostgreSQLTypeUnspecifiedSQLParameter("test")), is(4));
}

@Test
void assertGetColumnLengthWithMultiByteCharset() {
PostgreSQLUnspecifiedBinaryProtocolValue actual = new PostgreSQLUnspecifiedBinaryProtocolValue();
assertThat(actual.getColumnLength(payload, "中文"), is(6));
}

@Test
void assertRead() {
String timestampStr = "2020-08-23 15:57:03+08";
int expectedLength = 4 + timestampStr.length();
ByteBuf byteBuf = ByteBufTestUtils.createByteBuf(expectedLength);
byteBuf.writeInt(timestampStr.length());
byteBuf.writeCharSequence(timestampStr, StandardCharsets.ISO_8859_1);
byteBuf.readInt();
PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
Object actual = new PostgreSQLUnspecifiedBinaryProtocolValue().read(payload, timestampStr.length());
ByteBuf readBuf = ByteBufTestUtils.createByteBuf(expectedLength);
readBuf.writeInt(timestampStr.length());
readBuf.writeCharSequence(timestampStr, StandardCharsets.ISO_8859_1);
readBuf.readInt();
PostgreSQLPacketPayload readPayload = new PostgreSQLPacketPayload(readBuf, StandardCharsets.UTF_8);
Object actual = new PostgreSQLUnspecifiedBinaryProtocolValue().read(readPayload, timestampStr.length());
assertThat(actual, isA(PostgreSQLTypeUnspecifiedSQLParameter.class));
assertThat(actual.toString(), is(timestampStr));
assertThat(byteBuf.readerIndex(), is(expectedLength));
assertThat(readBuf.readerIndex(), is(expectedLength));
}

@Test
void assertWrite() {
assertThrows(UnsupportedSQLOperationException.class, () -> new PostgreSQLUnspecifiedBinaryProtocolValue().write(mock(PostgreSQLPacketPayload.class), "val"));
new PostgreSQLUnspecifiedBinaryProtocolValue().write(payload, "val");
verify(byteBuf).writeBytes("val".getBytes(StandardCharsets.UTF_8));
}

@Test
void assertWriteWithTypeUnspecifiedParameter() {
new PostgreSQLUnspecifiedBinaryProtocolValue().write(payload, new PostgreSQLTypeUnspecifiedSQLParameter("test"));
verify(byteBuf).writeBytes("test".getBytes(StandardCharsets.UTF_8));
}
}
Loading