You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ch...@apache.org on 2023/08/18 08:17:58 UTC

[shardingsphere] branch master updated: Refactor SQLExceptionTransformEngineTest (#28157)

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

chengzhang 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 2bff3e25802 Refactor SQLExceptionTransformEngineTest (#28157)
2bff3e25802 is described below

commit 2bff3e25802d5a6edf678b6403c060ac5d83a1df
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Aug 18 16:17:51 2023 +0800

    Refactor SQLExceptionTransformEngineTest (#28157)
    
    * Refactor SQLExceptionTransformEngineTest
    
    * Refactor SQLExceptionTransformEngineTest
    
    * Refactor SQLExceptionTransformEngineTest
---
 infra/exception/dialect/core/pom.xml               |  7 +++
 .../dialect/SQLExceptionTransformEngineTest.java   | 54 +++++++++++++++++-----
 .../DatabaseProtocolExceptionExceptionFixture.java | 27 -----------
 .../fixture/SQLDialectExceptionMapperFixture.java  |  4 +-
 4 files changed, 52 insertions(+), 40 deletions(-)

diff --git a/infra/exception/dialect/core/pom.xml b/infra/exception/dialect/core/pom.xml
index c75642dc8d0..07ccc70a24a 100644
--- a/infra/exception/dialect/core/pom.xml
+++ b/infra/exception/dialect/core/pom.xml
@@ -32,5 +32,12 @@
             <artifactId>shardingsphere-infra-common</artifactId>
             <version>${project.version}</version>
         </dependency>
+        
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-test-fixture-database</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/SQLExceptionTransformEngineTest.java b/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/SQLExceptionTransformEngineTest.java
index b372fa88045..05f1b893f47 100644
--- a/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/SQLExceptionTransformEngineTest.java
+++ b/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/SQLExceptionTransformEngineTest.java
@@ -18,9 +18,10 @@
 package org.apache.shardingsphere.infra.exception.dialect;
 
 import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
-import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.DatabaseCreateExistsException;
-import org.apache.shardingsphere.infra.exception.dialect.fixture.DatabaseProtocolExceptionExceptionFixture;
-import org.apache.shardingsphere.infra.hint.SQLHintDataSourceNotExistsException;
+import org.apache.shardingsphere.infra.exception.core.external.server.ShardingSphereServerException;
+import org.apache.shardingsphere.infra.exception.core.external.sql.ShardingSphereSQLException;
+import org.apache.shardingsphere.infra.exception.dialect.exception.SQLDialectException;
+import org.apache.shardingsphere.infra.exception.dialect.exception.protocol.DatabaseProtocolException;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.junit.jupiter.api.Test;
 
@@ -28,17 +29,48 @@ import java.sql.SQLException;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class SQLExceptionTransformEngineTest {
     
+    private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+    
+    @Test
+    void assertToSQLExceptionWithSQLException() {
+        SQLException cause = new SQLException();
+        assertThat(SQLExceptionTransformEngine.toSQLException(cause, databaseType), is(cause));
+    }
+    
+    @Test
+    void assertToSQLExceptionWithShardingSphereSQLException() {
+        ShardingSphereSQLException cause = mock(ShardingSphereSQLException.class);
+        SQLException expected = new SQLException();
+        when(cause.toSQLException()).thenReturn(expected);
+        assertThat(SQLExceptionTransformEngine.toSQLException(cause, databaseType), is(expected));
+    }
+    
+    @Test
+    void assertToSQLExceptionWithDatabaseProtocolException() {
+        DatabaseProtocolException cause = mock(DatabaseProtocolException.class);
+        when(cause.getMessage()).thenReturn("No reason");
+        assertThat(SQLExceptionTransformEngine.toSQLException(cause, databaseType).getMessage(), is("Database protocol exception: No reason"));
+    }
+    
+    @Test
+    void assertToSQLExceptionWithSQLDialectException() {
+        assertThat(SQLExceptionTransformEngine.toSQLException(mock(SQLDialectException.class), databaseType).getMessage(), is("Dialect exception"));
+    }
+    
+    @Test
+    void assertToSQLExceptionWithShardingSphereServerException() {
+        ShardingSphereServerException cause = mock(ShardingSphereServerException.class);
+        when(cause.getMessage()).thenReturn("No reason");
+        assertThat(SQLExceptionTransformEngine.toSQLException(cause, databaseType).getMessage(), is("No reason"));
+    }
+    
     @Test
-    void assertSQLExceptionTransformEngineConvertException() {
-        assertThat(SQLException.class, is(SQLExceptionTransformEngine.toSQLException(new SQLException(), null).getClass()));
-        assertThat(SQLException.class, is(SQLExceptionTransformEngine.toSQLException(new SQLHintDataSourceNotExistsException("only for test"), null).getClass()));
-        assertThat(SQLException.class,
-                is(SQLExceptionTransformEngine.toSQLException(new DatabaseProtocolExceptionExceptionFixture("only for test"), TypedSPILoader.getService(DatabaseType.class, "MySQL")).getClass()));
-        assertThat(SQLException.class,
-                is(SQLExceptionTransformEngine.toSQLException(new DatabaseCreateExistsException("only for test"), TypedSPILoader.getService(DatabaseType.class, "MySQL")).getClass()));
-        assertThat(SQLException.class, is(SQLExceptionTransformEngine.toSQLException(new RuntimeException("only for test"), null).getClass()));
+    void assertToSQLExceptionWithOtherException() {
+        assertThat(SQLExceptionTransformEngine.toSQLException(new Exception("No reason"), databaseType).getMessage(), is("Unknown exception: No reason"));
     }
 }
diff --git a/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/fixture/DatabaseProtocolExceptionExceptionFixture.java b/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/fixture/DatabaseProtocolExceptionExceptionFixture.java
deleted file mode 100644
index ac29f25dbf6..00000000000
--- a/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/fixture/DatabaseProtocolExceptionExceptionFixture.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * 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.exception.dialect.fixture;
-
-import org.apache.shardingsphere.infra.exception.dialect.exception.protocol.DatabaseProtocolException;
-
-public final class DatabaseProtocolExceptionExceptionFixture extends DatabaseProtocolException {
-    
-    public DatabaseProtocolExceptionExceptionFixture(final String reason) {
-        super(reason);
-    }
-}
diff --git a/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/fixture/SQLDialectExceptionMapperFixture.java b/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/fixture/SQLDialectExceptionMapperFixture.java
index c0187a0ba09..052b09e6404 100644
--- a/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/fixture/SQLDialectExceptionMapperFixture.java
+++ b/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/fixture/SQLDialectExceptionMapperFixture.java
@@ -26,11 +26,11 @@ public final class SQLDialectExceptionMapperFixture implements SQLDialectExcepti
     
     @Override
     public SQLException convert(final SQLDialectException sqlDialectException) {
-        return new SQLException("only for test");
+        return new SQLException("Dialect exception");
     }
     
     @Override
     public String getDatabaseType() {
-        return "MySQL";
+        return "FIXTURE";
     }
 }