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/08 06:56:41 UTC

[shardingsphere] branch master updated: Add test cases for RawJDBCExecutor and SQLExecutor (#6681)

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 fe6c6bf  Add test cases for RawJDBCExecutor and SQLExecutor (#6681)
fe6c6bf is described below

commit fe6c6bff6ab3e4d9e18566ce958de77a44500c33
Author: Andrew <zh...@aliyun.com>
AuthorDate: Sat Aug 8 14:56:26 2020 +0800

    Add test cases for RawJDBCExecutor and SQLExecutor (#6681)
    
    * Add test cases for RawJDBCExecutor and SQLExecutor
    
    * Coverage increased to 100%
---
 .../sql/raw/execute/RawJDBCExecutorTest.java       | 108 +++++++++++++++++++++
 .../resourced/jdbc/executor/SQLExecutorTest.java   |  72 ++++++++++++++
 2 files changed, 180 insertions(+)

diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/raw/execute/RawJDBCExecutorTest.java b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/raw/execute/RawJDBCExecutorTest.java
new file mode 100644
index 0000000..ad03e74
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/raw/execute/RawJDBCExecutorTest.java
@@ -0,0 +1,108 @@
+/*
+ * 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.infra.executor.sql.raw.execute;
+
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.executor.kernel.ExecutorKernel;
+import org.apache.shardingsphere.infra.executor.sql.QueryResult;
+import org.apache.shardingsphere.infra.executor.sql.raw.execute.result.query.ExecuteQueryResult;
+import org.apache.shardingsphere.infra.executor.sql.raw.execute.result.update.ExecuteUpdateResult;
+import org.apache.shardingsphere.infra.executor.sql.resourced.jdbc.executor.ExecutorExceptionHandler;
+import org.junit.Test;
+
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class RawJDBCExecutorTest {
+    
+    @Test
+    @SneakyThrows(value = SQLException.class)
+    public void assertExecuteForResultEmpty() {
+        ExecutorKernel kernel = mock(ExecutorKernel.class);
+        RawJDBCExecutor executor = new RawJDBCExecutor(kernel, true);
+        boolean actual = executor.execute(null, null);
+        assertThat(actual, is(false));
+    }
+    
+    @Test
+    @SneakyThrows(value = SQLException.class)
+    public void assertExecuteForExecuteQueryResult() {
+        ExecutorKernel kernel = mock(ExecutorKernel.class);
+        when(kernel.execute(any(), any(), any(), anyBoolean())).thenReturn(Collections.singletonList(new ExecuteQueryResult(null, null)));
+        RawJDBCExecutor executor = new RawJDBCExecutor(kernel, true);
+        boolean actual = executor.execute(null, null);
+        assertThat(actual, is(true));
+    }
+    
+    @Test
+    @SneakyThrows(value = SQLException.class)
+    public void assertExecuteQueryForExecuteQueryResult() {
+        ExecutorKernel kernel = mock(ExecutorKernel.class);
+        ExecuteQueryResult executeQueryResult = mock(ExecuteQueryResult.class);
+        QueryResult queryResult = mock(QueryResult.class);
+        when(executeQueryResult.getQueryResult()).thenReturn(queryResult);
+        when(kernel.execute(any(), any(), any(), anyBoolean())).thenReturn(Collections.singletonList(executeQueryResult));
+        RawJDBCExecutor executor = new RawJDBCExecutor(kernel, true);
+        List<QueryResult> actual = executor.executeQuery(null, null);
+        assertThat(actual, is(Collections.singletonList(queryResult)));
+    }
+    
+    @Test
+    @SneakyThrows(value = SQLException.class)
+    public void assertExecuteUpdate() {
+        ExecutorKernel kernel = mock(ExecutorKernel.class);
+        ExecuteUpdateResult executeUpdateResult1 = new ExecuteUpdateResult(1, 2);
+        ExecuteUpdateResult executeUpdateResult2 = new ExecuteUpdateResult(3, 4);
+        when(kernel.execute(any(), any(), any(), anyBoolean())).thenReturn(Arrays.asList(executeUpdateResult1, executeUpdateResult2));
+        RawJDBCExecutor executor = new RawJDBCExecutor(kernel, true);
+        int actual = executor.executeUpdate(null, null);
+        assertThat(actual, is(4));
+    }
+    
+    @Test
+    @SneakyThrows(value = SQLException.class)
+    public void assertExecuteNotThrownSQLException() {
+        ExecutorKernel kernel = mock(ExecutorKernel.class);
+        when(kernel.execute(any(), any(), any(), anyBoolean())).thenThrow(new SQLException("TestSQLException"));
+        RawJDBCExecutor rawJDBCExecutor = new RawJDBCExecutor(kernel, false);
+        ExecutorExceptionHandler.setExceptionThrown(false);
+        boolean actual = rawJDBCExecutor.execute(Collections.EMPTY_LIST, null);
+        assertThat(actual, is(false));
+    }
+    
+    @Test
+    public void assertExecuteSQLException() {
+        try {
+            ExecutorKernel kernel = mock(ExecutorKernel.class);
+            when(kernel.execute(any(), any(), any(), anyBoolean())).thenThrow(new SQLException("TestSQLException"));
+            RawJDBCExecutor rawJDBCExecutor = new RawJDBCExecutor(kernel, false);
+            rawJDBCExecutor.execute(Collections.EMPTY_LIST, null);
+        } catch (SQLException e) {
+            assertThat(e.getMessage(), is("TestSQLException"));
+        }
+    }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/resourced/jdbc/executor/SQLExecutorTest.java b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/resourced/jdbc/executor/SQLExecutorTest.java
new file mode 100644
index 0000000..03a0b58
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/resourced/jdbc/executor/SQLExecutorTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.infra.executor.sql.resourced.jdbc.executor;
+
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.executor.kernel.ExecutorKernel;
+import org.junit.Test;
+
+import java.sql.SQLException;
+import java.util.Collections;
+import java.util.List;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyCollection;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class SQLExecutorTest {
+    
+    @Test
+    @SneakyThrows(value = SQLException.class)
+    public void assertExecute() {
+        ExecutorKernel kernel = mock(ExecutorKernel.class);
+        when(kernel.execute(anyCollection(), any(), any(), anyBoolean())).thenReturn(Collections.singletonList("test"));
+        SQLExecutor sqlExecutor = new SQLExecutor(kernel, false);
+        List actual1 = sqlExecutor.execute(Collections.EMPTY_LIST, null);
+        assertThat(actual1, is(Collections.singletonList("test")));
+        List actual2 = sqlExecutor.execute(Collections.EMPTY_LIST, null, null);
+        assertThat(actual2, is(Collections.singletonList("test")));
+    }
+    
+    @Test
+    public void assertExecuteSQLException() {
+        try {
+            ExecutorKernel kernel = mock(ExecutorKernel.class);
+            when(kernel.execute(anyCollection(), any(), any(), anyBoolean())).thenThrow(new SQLException("TestSQLException"));
+            SQLExecutor sqlExecutor = new SQLExecutor(kernel, false);
+            sqlExecutor.execute(Collections.EMPTY_LIST, null);
+        } catch (SQLException e) {
+            assertThat(e.getMessage(), is("TestSQLException"));
+        }
+    }
+    
+    @Test
+    @SneakyThrows(value = SQLException.class)
+    public void assertExecuteNotThrownSQLException() {
+        ExecutorKernel kernel = mock(ExecutorKernel.class);
+        when(kernel.execute(anyCollection(), any(), any(), anyBoolean())).thenThrow(new SQLException("TestSQLException"));
+        SQLExecutor sqlExecutor = new SQLExecutor(kernel, false);
+        ExecutorExceptionHandler.setExceptionThrown(false);
+        List actual = sqlExecutor.execute(Collections.EMPTY_LIST, null);
+        assertThat(actual, is(Collections.EMPTY_LIST));
+    }
+}