You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/08/25 07:16:56 UTC
[shardingsphere] branch master updated: Add test case pg registry
and column desc packet (#7049)
This is an automated email from the ASF dual-hosted git repository.
zhangyonglun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new c4fc6d7 Add test case pg registry and column desc packet (#7049)
c4fc6d7 is described below
commit c4fc6d750bafaf87ecd4bd082e6f40be8a90673a
Author: xbkaishui <xb...@126.com>
AuthorDate: Tue Aug 25 15:16:43 2020 +0800
Add test case pg registry and column desc packet (#7049)
* add test case for pg codesc and constants
* fix check style error check
* fix mockito strict check
* remove un finish test case
* add pg test case statement registry and column desc packet
* remove blank line
---
.../codec/PostgreSQLPacketCodecEngineTest.java | 1 -
.../PostgreSQLUnsupportedCommandPacketTest.java | 49 ++++++++++++++++
.../query/PostgreSQLColumnDescriptionTest.java | 6 ++
.../query/PostgreSQLRowDescriptionPacketTest.java | 61 ++++++++++++++++++++
.../query/binary/BinaryStatementRegistryTest.java | 65 ++++++++++++++++++++++
...ConnectionScopeBinaryStatementRegistryTest.java | 55 ++++++++++++++++++
6 files changed, 236 insertions(+), 1 deletion(-)
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngineTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngineTest.java
index 77d6883..e630d44 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngineTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/codec/PostgreSQLPacketCodecEngineTest.java
@@ -97,5 +97,4 @@ public class PostgreSQLPacketCodecEngineTest {
public void assertCreatePacketPayload() {
assertThat(new PostgreSQLPacketCodecEngine().createPacketPayload(byteBuf).getByteBuf(), is(byteBuf));
}
-
}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/admin/PostgreSQLUnsupportedCommandPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/admin/PostgreSQLUnsupportedCommandPacketTest.java
new file mode 100644
index 0000000..7f0c8f9
--- /dev/null
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/admin/PostgreSQLUnsupportedCommandPacketTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.admin;
+
+import io.netty.buffer.ByteBuf;
+import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacketType;
+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;
+
+@RunWith(MockitoJUnitRunner.class)
+public class PostgreSQLUnsupportedCommandPacketTest {
+
+ @Mock
+ private ByteBuf byteBuf;
+
+ @Test
+ public void assertWrite() {
+ PostgreSQLUnsupportedCommandPacket rowPacket = new PostgreSQLUnsupportedCommandPacket(PostgreSQLCommandPacketType.AUTHENTICATION_OK.getValue());
+ rowPacket.write(new PostgreSQLPacketPayload(byteBuf));
+ assertThat(byteBuf.writerIndex(), is(0));
+ }
+
+ @Test
+ public void assertGetMessageType() {
+ PostgreSQLUnsupportedCommandPacket rowPacket = new PostgreSQLUnsupportedCommandPacket(PostgreSQLCommandPacketType.AUTHENTICATION_OK.getValue());
+ assertThat(rowPacket.getMessageType(), is(PostgreSQLCommandPacketType.AUTHENTICATION_OK.getValue()));
+ }
+}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLColumnDescriptionTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLColumnDescriptionTest.java
index 522c94b..99d7951 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLColumnDescriptionTest.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLColumnDescriptionTest.java
@@ -37,6 +37,12 @@ public final class PostgreSQLColumnDescriptionTest {
}
@Test
+ public void assertStringTypeOid() {
+ PostgreSQLColumnDescription description = new PostgreSQLColumnDescription("name", 1, Types.VARCHAR, 4, null);
+ assertThat(description.getTypeOID(), is(1043));
+ }
+
+ @Test
public void assertIntegerArrayTypeOid() throws SQLException {
ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class);
when(resultSetMetaData.getColumnTypeName(2)).thenReturn("_int4");
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLRowDescriptionPacketTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLRowDescriptionPacketTest.java
new file mode 100644
index 0000000..00aac43
--- /dev/null
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/PostgreSQLRowDescriptionPacketTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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;
+
+import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacketType;
+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.Types;
+import java.util.Arrays;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@RunWith(MockitoJUnitRunner.class)
+public class PostgreSQLRowDescriptionPacketTest {
+
+ @Mock
+ private PostgreSQLPacketPayload payload;
+
+ @Test
+ public void assertWrite() {
+ PostgreSQLColumnDescription description = new PostgreSQLColumnDescription("name", 1, Types.VARCHAR, 4, null);
+ PostgreSQLRowDescriptionPacket packet = new PostgreSQLRowDescriptionPacket(1, Arrays.asList(description));
+ packet.write(payload);
+ verify(payload, times(2)).writeInt2(1);
+ verify(payload).writeStringNul("name");
+ verify(payload).writeInt4(0);
+ verify(payload, times(2)).writeInt2(1);
+ verify(payload).writeInt4(1043);
+ verify(payload).writeInt2(4);
+ verify(payload).writeInt4(-1);
+ verify(payload).writeInt2(0);
+ }
+
+ @Test
+ public void getMessageType() {
+ PostgreSQLRowDescriptionPacket packet = new PostgreSQLRowDescriptionPacket(0, Arrays.asList());
+ assertThat(packet.getMessageType(), is(PostgreSQLCommandPacketType.ROW_DESCRIPTION.getValue()));
+ }
+}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/BinaryStatementRegistryTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/BinaryStatementRegistryTest.java
new file mode 100644
index 0000000..fe7ad09
--- /dev/null
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/BinaryStatementRegistryTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+
+public class BinaryStatementRegistryTest {
+
+ private final String sql = "SELECT * FROM tbl WHERE id=?";
+
+ private final String statementId = "stat-id";
+
+ @Before
+ public void init() {
+ BinaryStatementRegistry.getInstance().register(1);
+ ConnectionScopeBinaryStatementRegistry statementRegistry = BinaryStatementRegistry.getInstance().get(1);
+ statementRegistry.register(statementId, sql, 1, null);
+ }
+
+ @Test
+ public void assertRegisterIfAbsent() {
+ BinaryStatementRegistry.getInstance().register(2);
+ ConnectionScopeBinaryStatementRegistry actual = BinaryStatementRegistry.getInstance().get(2);
+ assertNull(actual.getBinaryStatement("stat-no-exist"));
+ }
+
+ @Test
+ public void assertRegisterIfPresent() {
+ ConnectionScopeBinaryStatementRegistry statementRegistry = BinaryStatementRegistry.getInstance().get(1);
+ PostgreSQLBinaryStatement statement = statementRegistry.getBinaryStatement(statementId);
+ assertThat(statement.getSql(), is(sql));
+ assertThat(statement.getParametersCount(), is(1));
+
+ BinaryStatementRegistry.getInstance().register(1);
+ statement = BinaryStatementRegistry.getInstance().get(1).getBinaryStatement(statementId);
+ assertNull(statement);
+ }
+
+ @Test
+ public void assertUnregister() {
+ BinaryStatementRegistry.getInstance().unregister(1);
+ ConnectionScopeBinaryStatementRegistry actual = BinaryStatementRegistry.getInstance().get(1);
+ assertNull(actual);
+ }
+}
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/ConnectionScopeBinaryStatementRegistryTest.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/ConnectionScopeBinaryStatementRegistryTest.java
new file mode 100644
index 0000000..09695d2
--- /dev/null
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/binary/ConnectionScopeBinaryStatementRegistryTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+
+public class ConnectionScopeBinaryStatementRegistryTest {
+
+ @Test
+ public void assertRegister() {
+ ConnectionScopeBinaryStatementRegistry statementRegistry = new ConnectionScopeBinaryStatementRegistry();
+ String statementId = "stat-id";
+ String sql = "select * from t_order";
+ statementRegistry.register(statementId, sql, 1, null);
+
+ PostgreSQLBinaryStatement binaryStatement = statementRegistry.getBinaryStatement(statementId);
+ assertThat(binaryStatement.getSql(), is(sql));
+ assertThat(binaryStatement.getParametersCount(), is(1));
+ }
+
+ @Test
+ public void assertGetBinaryStatementNotExists() {
+ ConnectionScopeBinaryStatementRegistry statementRegistry = new ConnectionScopeBinaryStatementRegistry();
+ PostgreSQLBinaryStatement binaryStatement = statementRegistry.getBinaryStatement("stat-no-exists");
+ assertNull(binaryStatement);
+ }
+
+ @Test
+ public void assertGetBinaryStatement() {
+ ConnectionScopeBinaryStatementRegistry statementRegistry = new ConnectionScopeBinaryStatementRegistry();
+ statementRegistry.register("stat-id", "", 1, null);
+ PostgreSQLBinaryStatement binaryStatement = statementRegistry.getBinaryStatement("stat-id");
+ assertNotNull(binaryStatement);
+ }
+}