From 45d0cc11b441163338c03eb5cb4d648006e39113 Mon Sep 17 00:00:00 2001 From: Yanjie Zhou Date: Mon, 24 Aug 2020 21:12:21 +0800 Subject: [PATCH] add test cases (#7030) * add test cases * add test cases * add test cases --- ...PostgreSQLInt2BinaryProtocolValueTest.java | 47 ++++++++++ ...PostgreSQLInt4BinaryProtocolValueTest.java | 47 ++++++++++ ...PostgreSQLInt8BinaryProtocolValueTest.java | 47 ++++++++++ ...stgreSQLStringBinaryProtocolValueTest.java | 66 ++++++++++++++ ...PostgreSQLTimeBinaryProtocolValueTest.java | 50 +++++++++++ .../PostgreSQLComDescribePacketTest.java | 45 ++++++++++ .../PostgreSQLComExecutePacketTest.java | 45 ++++++++++ .../parse/PostgreSQLComParsePacketTest.java | 59 +++++++++++++ .../PostgreSQLParseCompletePacketTest.java | 35 ++++++++ .../sync/PostgreSQLComSyncPacketTest.java | 43 ++++++++++ .../text/PostgreSQLComQueryPacketTest.java | 47 ++++++++++ .../text/PostgreSQLDataRowPacketTest.java | 86 +++++++++++++++++++ 12 files changed, 617 insertions(+) create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt2BinaryProtocolValueTest.java create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt4BinaryProtocolValueTest.java create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt8BinaryProtocolValueTest.java create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLStringBinaryProtocolValueTest.java create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLTimeBinaryProtocolValueTest.java create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/describe/PostgreSQLComDescribePacketTest.java create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/execute/PostgreSQLComExecutePacketTest.java create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLComParsePacketTest.java create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLParseCompletePacketTest.java create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/sync/PostgreSQLComSyncPacketTest.java create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/text/PostgreSQLComQueryPacketTest.java create mode 100644 shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/text/PostgreSQLDataRowPacketTest.java diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt2BinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt2BinaryProtocolValueTest.java new file mode 100644 index 0000000000..35645ee632 --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt2BinaryProtocolValueTest.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.protocol; + +import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public final class PostgreSQLInt2BinaryProtocolValueTest { + + @Mock + private PostgreSQLPacketPayload payload; + + @Test + public void assertNewInstance() { + PostgreSQLInt2BinaryProtocolValue actual = new PostgreSQLInt2BinaryProtocolValue(); + assertThat(actual.getColumnLength(null), equalTo(2)); + when(payload.readInt2()).thenReturn(1); + assertThat(actual.read(payload), is(1)); + actual.write(payload, 1); + verify(payload).writeInt2(1); + } +} diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt4BinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt4BinaryProtocolValueTest.java new file mode 100644 index 0000000000..de0423d77f --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt4BinaryProtocolValueTest.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.protocol; + +import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public final class PostgreSQLInt4BinaryProtocolValueTest { + + @Mock + private PostgreSQLPacketPayload payload; + + @Test + public void assertNewInstance() { + PostgreSQLInt4BinaryProtocolValue actual = new PostgreSQLInt4BinaryProtocolValue(); + assertThat(actual.getColumnLength(null), equalTo(4)); + when(payload.readInt4()).thenReturn(1); + assertThat(actual.read(payload), is(1)); + actual.write(payload, 1); + verify(payload).writeInt4(1); + } +} diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt8BinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt8BinaryProtocolValueTest.java new file mode 100644 index 0000000000..447293fb45 --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLInt8BinaryProtocolValueTest.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.protocol; + +import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public final class PostgreSQLInt8BinaryProtocolValueTest { + + @Mock + private PostgreSQLPacketPayload payload; + + @Test + public void assertNewInstance() { + PostgreSQLInt8BinaryProtocolValue actual = new PostgreSQLInt8BinaryProtocolValue(); + assertThat(actual.getColumnLength(null), equalTo(8)); + when(payload.readInt8()).thenReturn(1L); + assertThat(actual.read(payload), is(1L)); + actual.write(payload, 1L); + verify(payload).writeInt8(1L); + } +} diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLStringBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLStringBinaryProtocolValueTest.java new file mode 100644 index 0000000000..ee49bd637a --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLStringBinaryProtocolValueTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.protocol; + +import io.netty.buffer.ByteBuf; +import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public final class PostgreSQLStringBinaryProtocolValueTest { + + @InjectMocks + private PostgreSQLPacketPayload payload; + + @Mock + private ByteBuf byteBuf; + + @Test + public void assertNewInstance() { + when(byteBuf.readerIndex()).thenReturn(8); + doAnswer(new Answer() { + @Override + public ByteBuf answer(final InvocationOnMock invocation) throws Throwable { + ((byte[]) invocation.getArguments()[0])[0] = 97; + return byteBuf; + } + }).when(byteBuf).readBytes(any(byte[].class)); + PostgreSQLStringBinaryProtocolValue actual = new PostgreSQLStringBinaryProtocolValue(); + assertThat(actual.getColumnLength("str"), equalTo("str".length())); + when(payload.readInt4()).thenReturn(1); + assertThat(actual.read(payload), equalTo("a")); + verify(byteBuf).readerIndex(4); + actual.write(payload, "a"); + verify(byteBuf).writeBytes("a".getBytes()); + actual.write(payload, new byte[1]); + verify(byteBuf).writeBytes(new byte[1]); + } +} diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLTimeBinaryProtocolValueTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLTimeBinaryProtocolValueTest.java new file mode 100644 index 0000000000..c1582ed54e --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/bind/protocol/PostgreSQLTimeBinaryProtocolValueTest.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.protocol; + +import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import java.sql.Timestamp; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public final class PostgreSQLTimeBinaryProtocolValueTest { + + @Mock + private PostgreSQLPacketPayload payload; + + @Test + public void assertNewInstance() { + PostgreSQLTimeBinaryProtocolValue actual = new PostgreSQLTimeBinaryProtocolValue(); + assertThat(actual.getColumnLength(null), equalTo(8)); + when(payload.readInt8()).thenReturn(1L); + assertThat(actual.read(payload), is(1L)); + Timestamp timestamp = new Timestamp(System.currentTimeMillis()); + actual.write(payload, timestamp); + verify(payload).writeInt8(timestamp.getTime()); + } +} diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/describe/PostgreSQLComDescribePacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/describe/PostgreSQLComDescribePacketTest.java new file mode 100644 index 0000000000..2f3393add0 --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/describe/PostgreSQLComDescribePacketTest.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.describe; + +import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.verify; + +@RunWith(MockitoJUnitRunner.class) +public final class PostgreSQLComDescribePacketTest { + + @Mock + private PostgreSQLPacketPayload payload; + + @Test + public void assertNewInstance() { + PostgreSQLComDescribePacket actual = new PostgreSQLComDescribePacket(payload); + actual.write(payload); + assertThat(actual.getMessageType(), is('D')); + verify(payload).readInt4(); + verify(payload).readInt1(); + verify(payload).readStringNul(); + } +} diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/execute/PostgreSQLComExecutePacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/execute/PostgreSQLComExecutePacketTest.java new file mode 100644 index 0000000000..227b9dd4fe --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/execute/PostgreSQLComExecutePacketTest.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.execute; + +import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.verify; + +@RunWith(MockitoJUnitRunner.class) +public final class PostgreSQLComExecutePacketTest { + + @Mock + private PostgreSQLPacketPayload payload; + + @Test + public void assertNewInstance() { + PostgreSQLComExecutePacket actual = new PostgreSQLComExecutePacket(payload); + verify(payload, atLeast(2)).readInt4(); + verify(payload).readStringNul(); + actual.write(payload); + assertThat(actual.getMessageType(), is('E')); + } +} diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLComParsePacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLComParsePacketTest.java new file mode 100644 index 0000000000..665fcc21db --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLComParsePacketTest.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.parse; + +import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLColumnType; +import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.PostgreSQLBinaryStatementParameterType; +import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import java.util.List; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public final class PostgreSQLComParsePacketTest { + + @Mock + private PostgreSQLPacketPayload payload; + + @Test + public void assertNewInstance() { + when(payload.readInt2()).thenReturn(1); + when(payload.readInt4()).thenReturn(0); + when(payload.readStringNul()).thenReturn("sql"); + PostgreSQLComParsePacket actual = new PostgreSQLComParsePacket(payload); + actual.write(payload); + assertThat(actual.getMessageType(), is('P')); + assertThat(actual.getSql(), is("sql")); + assertThat(actual.getStatementId(), is("sql")); + List types = actual.getBinaryStatementParameterTypes(); + assertNotNull(types); + assertThat(types.size(), equalTo(1)); + assertThat(types.get(0).getColumnType(), is(PostgreSQLColumnType.POSTGRESQL_TYPE_UNSPECIFIED)); + assertTrue(actual.toString().startsWith("PostgreSQLComParsePacket(statementId=sql, sql=sql, binaryStatementParameterTypes=[")); + } +} diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLParseCompletePacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLParseCompletePacketTest.java new file mode 100644 index 0000000000..d316716918 --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/parse/PostgreSQLParseCompletePacketTest.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.parse; + +import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + +public final class PostgreSQLParseCompletePacketTest { + + @Test + public void assertNewInstance() { + PostgreSQLParseCompletePacket actual = new PostgreSQLParseCompletePacket(); + actual.write(mock(PostgreSQLPacketPayload.class)); + assertThat(actual.getMessageType(), is('1')); + } +} diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/sync/PostgreSQLComSyncPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/sync/PostgreSQLComSyncPacketTest.java new file mode 100644 index 0000000000..5bdb0051c2 --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/sync/PostgreSQLComSyncPacketTest.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.sync; + +import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.verify; + +@RunWith(MockitoJUnitRunner.class) +public final class PostgreSQLComSyncPacketTest { + + @Mock + private PostgreSQLPacketPayload payload; + + @Test + public void assertNewInstance() { + PostgreSQLComSyncPacket actual = new PostgreSQLComSyncPacket(payload); + actual.write(payload); + verify(payload).readInt4(); + assertThat(actual.getMessageType(), is('S')); + } +} diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/text/PostgreSQLComQueryPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/text/PostgreSQLComQueryPacketTest.java new file mode 100644 index 0000000000..68b764024f --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/text/PostgreSQLComQueryPacketTest.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.text; + +import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public final class PostgreSQLComQueryPacketTest { + + @Mock + private PostgreSQLPacketPayload payload; + + @Test + public void assertNewInstance() { + when(payload.readStringNul()).thenReturn("sql"); + PostgreSQLComQueryPacket actual = new PostgreSQLComQueryPacket(payload); + actual.write(payload); + verify(payload).readInt4(); + assertThat(actual.getSql(), is("sql")); + assertThat(actual.getMessageType(), is('Q')); + assertThat(actual.toString(), is("PostgreSQLComQueryPacket(sql=sql)")); + } +} diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/text/PostgreSQLDataRowPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/text/PostgreSQLDataRowPacketTest.java new file mode 100644 index 0000000000..7bbe296f4f --- /dev/null +++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/text/PostgreSQLDataRowPacketTest.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.text; + +import lombok.SneakyThrows; +import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import java.sql.SQLException; +import java.sql.SQLXML; +import java.util.Collections; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public final class PostgreSQLDataRowPacketTest { + + @Mock + private PostgreSQLPacketPayload payload; + + @Mock + private SQLXML sqlxml; + + @Test + public void assertWriteWithNull() { + PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList(null)); + actual.write(payload); + verify(payload).writeInt4(0xFFFFFFFF); + } + + @Test + public void assertWriteWithBytes() { + PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList(new byte[]{'a'})); + actual.write(payload); + verify(payload).writeInt4(new byte[]{'a'}.length); + verify(payload).writeBytes(new byte[]{'a'}); + } + + @Test + @SneakyThrows + public void assertWriteWithSQLXML() { + when(sqlxml.getString()).thenReturn("string"); + PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList(sqlxml)); + actual.write(payload); + verify(payload).writeInt4("string".getBytes().length); + verify(payload).writeStringEOF("string"); + } + + @Test + public void assertWriteWithString() { + PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList("str")); + assertThat(actual.getData(), is(Collections.singletonList("str"))); + actual.write(payload); + verify(payload).writeInt4("str".getBytes().length); + verify(payload).writeStringEOF("str"); + } + + @Test + @SneakyThrows + public void assertWriteWithSQLXML4Error() { + when(sqlxml.getString()).thenThrow(new SQLException("mock")); + PostgreSQLDataRowPacket actual = new PostgreSQLDataRowPacket(Collections.singletonList(sqlxml)); + actual.write(payload); + } +} -- GitLab