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/24 07:59:57 UTC

[shardingsphere] branch master updated: DALBackendHandlerFactoryTest added (#7581)

This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 6e0ab99  DALBackendHandlerFactoryTest added (#7581)
6e0ab99 is described below

commit 6e0ab99324bb0e657eccc880bce13a9ef19d28a9
Author: sluk3r <sl...@qq.com>
AuthorDate: Thu Sep 24 15:59:45 2020 +0800

    DALBackendHandlerFactoryTest added (#7581)
    
    * TransactionBackendHandlerFactoryTest added
    
    * DALBackendHandlerFactoryTest added
    
    * use static import of MatcherAssert
    
    Co-authored-by: wangxichun <wa...@jd.com>
---
 .../text/admin/DALBackendHandlerFactoryTest.java   | 103 +++++++++++++++++++++
 .../TransactionBackendHandlerFactoryTest.java      |   3 +-
 2 files changed, 104 insertions(+), 2 deletions(-)

diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DALBackendHandlerFactoryTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DALBackendHandlerFactoryTest.java
new file mode 100644
index 0000000..182ed02
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/DALBackendHandlerFactoryTest.java
@@ -0,0 +1,103 @@
+/*
+ * 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.backend.text.admin;
+
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.SetStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowDatabasesStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLUseStatement;
+import org.hamcrest.Matcher;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public final class DALBackendHandlerFactoryTest {
+    
+    @Test
+    public void assertShowTablesBackendHandlerReturnedWhenMySQLShowTablesStatement() {
+        MySQLShowTablesStatement mySQLShowTablesStatement = mock(MySQLShowTablesStatement.class);
+        BackendConnection backendConnection = mock(BackendConnection.class);
+        TextProtocolBackendHandler textProtocolBackendHandler = DALBackendHandlerFactory.newInstance("", mySQLShowTablesStatement, backendConnection);
+        assertThat(textProtocolBackendHandler, instanceOf(ShowTablesBackendHandler.class));
+        ShowTablesBackendHandler showTablesBackendHandler = (ShowTablesBackendHandler) textProtocolBackendHandler;
+        assertFieldOfInstance(showTablesBackendHandler, "sqlStatement", is(mySQLShowTablesStatement));
+        assertFieldOfInstance(showTablesBackendHandler, "sql", is(""));
+        assertFieldOfInstance(showTablesBackendHandler, "backendConnection", is(backendConnection));
+    }
+    
+    @Test
+    public void assertUseDatabaseBackendHandlerReturnedWhenMySQLUseStatement() {
+        MySQLUseStatement mySQLUseStatement = mock(MySQLUseStatement.class);
+        BackendConnection backendConnection = mock(BackendConnection.class);
+        TextProtocolBackendHandler textProtocolBackendHandler = DALBackendHandlerFactory.newInstance("", mySQLUseStatement, backendConnection);
+        assertThat(textProtocolBackendHandler, instanceOf(UseDatabaseBackendHandler.class));
+        UseDatabaseBackendHandler useDatabaseBackendHandler = (UseDatabaseBackendHandler) textProtocolBackendHandler;
+        assertFieldOfInstance(useDatabaseBackendHandler, "useStatement", is(mySQLUseStatement));
+        assertFieldOfInstance(useDatabaseBackendHandler, "backendConnection", is(backendConnection));
+    }
+    
+    @Test
+    public void assertShowDatabasesBackendHandlerReturnedWhenMySQLShowDatabasesStatement() {
+        BackendConnection backendConnection = mock(BackendConnection.class);
+        TextProtocolBackendHandler textProtocolBackendHandler = DALBackendHandlerFactory.newInstance("", mock(MySQLShowDatabasesStatement.class), backendConnection);
+        assertThat(textProtocolBackendHandler, instanceOf(ShowDatabasesBackendHandler.class));
+        ShowDatabasesBackendHandler showDatabasesBackendHandler = (ShowDatabasesBackendHandler) textProtocolBackendHandler;
+        assertFieldOfInstance(showDatabasesBackendHandler, "backendConnection", is(backendConnection));
+    }
+    
+    @Test
+    public void assertBroadcastBackendHandlerReturnedWhenSetStatement() {
+        SetStatement setStatement = mock(SetStatement.class);
+        BackendConnection backendConnection = mock(BackendConnection.class);
+        TextProtocolBackendHandler textProtocolBackendHandler = DALBackendHandlerFactory.newInstance("", setStatement, backendConnection);
+        assertThat(textProtocolBackendHandler, instanceOf(BroadcastBackendHandler.class));
+        BroadcastBackendHandler broadcastBackendHandler = (BroadcastBackendHandler) textProtocolBackendHandler;
+        assertFieldOfInstance(broadcastBackendHandler, "sqlStatement", is(setStatement));
+        assertFieldOfInstance(broadcastBackendHandler, "sql", is(""));
+        assertFieldOfInstance(broadcastBackendHandler, "backendConnection", is(backendConnection));
+    }
+    
+    @Test
+    public void assertUnicastBackendHandlerReturnedWhenOtherDALStatement() {
+        DALStatement dalStatement = mock(DALStatement.class);
+        BackendConnection backendConnection = mock(BackendConnection.class);
+        TextProtocolBackendHandler textProtocolBackendHandler = DALBackendHandlerFactory.newInstance("", dalStatement, backendConnection);
+        assertThat(textProtocolBackendHandler, instanceOf(UnicastBackendHandler.class));
+        UnicastBackendHandler unicastBackendHandler = (UnicastBackendHandler) textProtocolBackendHandler;
+        assertFieldOfInstance(unicastBackendHandler, "sqlStatement", is(dalStatement));
+        assertFieldOfInstance(unicastBackendHandler, "sql", is(""));
+        assertFieldOfInstance(unicastBackendHandler, "backendConnection", is(backendConnection));
+    }
+    
+    @SneakyThrows
+    private <S, T> void assertFieldOfInstance(final S classInstance, final String fieldName, final Matcher<T> matcher) {
+        Field field = classInstance.getClass().getDeclaredField(fieldName);
+        field.setAccessible(true);
+        T value = (T) field.get(classInstance);
+        assertThat(value, matcher);
+    }
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/transaction/TransactionBackendHandlerFactoryTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/transaction/TransactionBackendHandlerFactoryTest.java
index 9474cfc..f3199b4 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/transaction/TransactionBackendHandlerFactoryTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/transaction/TransactionBackendHandlerFactoryTest.java
@@ -27,7 +27,6 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.RollbackSta
 import org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.TCLStatement;
 import org.apache.shardingsphere.transaction.core.TransactionOperationType;
 import org.hamcrest.Matcher;
-import org.hamcrest.MatcherAssert;
 import org.junit.Test;
 import org.mockito.Answers;
 
@@ -70,7 +69,7 @@ public final class TransactionBackendHandlerFactoryTest {
         Field field = classInstance.getClass().getDeclaredField(fieldName);
         field.setAccessible(true);
         T value = (T) field.get(classInstance);
-        MatcherAssert.assertThat(value, matcher);
+        assertThat(value, matcher);
     }
     
     @SneakyThrows