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/09/13 05:38:17 UTC

[shardingsphere] branch master updated: Add more test cases for ShardingSphere-Proxy (#7321) (#7395)

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 82ebb45  Add more test cases for ShardingSphere-Proxy (#7321) (#7395)
82ebb45 is described below

commit 82ebb45d935828c013a9d19cc05a8a389fad98d8
Author: sandynz <42...@users.noreply.github.com>
AuthorDate: Sun Sep 13 13:37:52 2020 +0800

    Add more test cases for ShardingSphere-Proxy (#7321) (#7395)
---
 .../postgresql/PostgreSQLFrontendEngineTest.java   | 52 ++++++++++++++
 .../PostgreSQLCommandExecutorFactoryTest.java      | 80 ++++++++++++++++++++++
 .../PostgreSQLUnsupportedCommandExecutorTest.java  | 11 ++-
 3 files changed, 140 insertions(+), 3 deletions(-)

diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngineTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngineTest.java
new file mode 100644
index 0000000..80bcd70
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLFrontendEngineTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.proxy.frontend.postgresql;
+
+import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.BinaryStatementRegistry;
+import org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
+import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+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;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class PostgreSQLFrontendEngineTest {
+    
+    @Test
+    public void assertGetDatabaseType() {
+        String actual = new PostgreSQLFrontendEngine().getDatabaseType();
+        assertThat(actual, is(new PostgreSQLDatabaseType().getName()));
+    }
+    
+    @Test
+    public void assertRelease() {
+        BackendConnection backendConnection = mock(BackendConnection.class);
+        int connectionId = 1;
+        when(backendConnection.getConnectionId()).thenReturn(connectionId);
+        BinaryStatementRegistry registry = BinaryStatementRegistry.getInstance();
+        registry.register(connectionId);
+        assertNotNull(registry.get(connectionId));
+        PostgreSQLFrontendEngine frontendEngine = new PostgreSQLFrontendEngine();
+        frontendEngine.release(backendConnection);
+        assertNull(registry.get(connectionId));
+    }
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/PostgreSQLCommandExecutorFactoryTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/PostgreSQLCommandExecutorFactoryTest.java
new file mode 100644
index 0000000..5b4347e
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/PostgreSQLCommandExecutorFactoryTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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.proxy.frontend.postgresql.command;
+
+import java.util.Arrays;
+import java.util.Collection;
+import lombok.AllArgsConstructor;
+import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacket;
+import org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQLCommandPacketType;
+import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.bind.PostgreSQLComBindPacket;
+import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.binary.parse.PostgreSQLComParsePacket;
+import org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.text.PostgreSQLComQueryPacket;
+import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+import org.apache.shardingsphere.proxy.frontend.command.executor.CommandExecutor;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.generic.PostgreSQLComTerminationExecutor;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.generic.PostgreSQLUnsupportedCommandExecutor;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.binary.bind.PostgreSQLComBindExecutor;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.binary.describe.PostgreSQLComDescribeExecutor;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.binary.execute.PostgreSQLComExecuteExecutor;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.binary.parse.PostgreSQLComParseExecutor;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.binary.sync.PostgreSQLComSyncExecutor;
+import org.apache.shardingsphere.proxy.frontend.postgresql.command.query.text.PostgreSQLComQueryExecutor;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class PostgreSQLCommandExecutorFactoryTest {
+    
+    @Test
+    public void assertNewInstance() {
+        BackendConnection backendConnection = mock(BackendConnection.class);
+        when(backendConnection.getSchemaName()).thenReturn("schema");
+        Collection<InputOutput> inputOutputs = Arrays.asList(
+            new InputOutput(PostgreSQLCommandPacketType.QUERY, PostgreSQLComQueryPacket.class, PostgreSQLComQueryExecutor.class),
+            new InputOutput(PostgreSQLCommandPacketType.PARSE, PostgreSQLComParsePacket.class, PostgreSQLComParseExecutor.class),
+            new InputOutput(PostgreSQLCommandPacketType.BIND, PostgreSQLComBindPacket.class, PostgreSQLComBindExecutor.class),
+            new InputOutput(PostgreSQLCommandPacketType.DESCRIBE, null, PostgreSQLComDescribeExecutor.class),
+            new InputOutput(PostgreSQLCommandPacketType.EXECUTE, null, PostgreSQLComExecuteExecutor.class),
+            new InputOutput(PostgreSQLCommandPacketType.SYNC, null, PostgreSQLComSyncExecutor.class),
+            new InputOutput(PostgreSQLCommandPacketType.TERMINATE, null, PostgreSQLComTerminationExecutor.class),
+            new InputOutput(PostgreSQLCommandPacketType.CLOSE, null, PostgreSQLUnsupportedCommandExecutor.class)
+        );
+        for (InputOutput inputOutput : inputOutputs) {
+            Class<? extends PostgreSQLCommandPacket> commandPacketClass = inputOutput.commandPacketClass;
+            if (null == commandPacketClass) {
+                commandPacketClass = PostgreSQLCommandPacket.class;
+            }
+            CommandExecutor actual = PostgreSQLCommandExecutorFactory.newInstance(inputOutput.commandPacketType, mock(commandPacketClass), mock(BackendConnection.class));
+            assertThat(actual, instanceOf(inputOutput.resultClass));
+        }
+    }
+    
+    @AllArgsConstructor
+    private static final class InputOutput {
+        
+        private final PostgreSQLCommandPacketType commandPacketType;
+        
+        private final Class<? extends PostgreSQLCommandPacket> commandPacketClass;
+        
+        private final Class<? extends CommandExecutor> resultClass;
+    }
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/generic/PostgreSQLUnsupportedCommandExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/generic/PostgreSQLUnsupportedCommandExecutorTest.java
index 1cfa156..94b30e4 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/generic/PostgreSQLUnsupportedCommandExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/generic/PostgreSQLUnsupportedCommandExecutorTest.java
@@ -17,17 +17,22 @@
 
 package org.apache.shardingsphere.proxy.frontend.postgresql.command.generic;
 
+import java.util.Collection;
+import org.apache.shardingsphere.db.protocol.packet.DatabasePacket;
 import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLErrorResponsePacket;
 import org.junit.Test;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
 
 public final class PostgreSQLUnsupportedCommandExecutorTest {
     
     @Test
-    public void assertNewInstance() {
-        PostgreSQLUnsupportedCommandExecutor actual = new PostgreSQLUnsupportedCommandExecutor();
-        assertThat(actual.execute().iterator().next(), instanceOf(PostgreSQLErrorResponsePacket.class));
+    public void assertExecute() {
+        PostgreSQLUnsupportedCommandExecutor commandExecutor = new PostgreSQLUnsupportedCommandExecutor();
+        Collection<DatabasePacket<?>> actual = commandExecutor.execute();
+        assertThat(actual.size(), is(1));
+        assertThat(actual.iterator().next(), instanceOf(PostgreSQLErrorResponsePacket.class));
     }
 }