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

yx9o 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 83e2c27e894 Added unit tests for DatabaseNameUtils (#29976)
83e2c27e894 is described below

commit 83e2c27e894729e14c6e4bcb35cd15bdeb254afc
Author: Ashwin Deshpande <[email protected]>
AuthorDate: Sat Feb 3 21:46:51 2024 -0800

    Added unit tests for DatabaseNameUtils (#29976)
    
    Co-authored-by: Ashwin Deshpande <[email protected]>
---
 infra/distsql-handler/pom.xml                      |  6 ++
 .../handler/util/DatabaseNameUtilsTest.java        | 75 ++++++++++++++++++++++
 2 files changed, 81 insertions(+)

diff --git a/infra/distsql-handler/pom.xml b/infra/distsql-handler/pom.xml
index f9e18d90f14..40a9ad20c95 100644
--- a/infra/distsql-handler/pom.xml
+++ b/infra/distsql-handler/pom.xml
@@ -42,5 +42,11 @@
             <artifactId>shardingsphere-mode-core</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-test-util</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git 
a/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/util/DatabaseNameUtilsTest.java
 
b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/util/DatabaseNameUtilsTest.java
new file mode 100644
index 00000000000..30d118660e1
--- /dev/null
+++ 
b/infra/distsql-handler/src/test/java/org/apache/shardingsphere/distsql/handler/util/DatabaseNameUtilsTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.distsql.handler.util;
+
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.DatabaseSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.available.FromDatabaseAvailable;
+import 
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.test.mock.AutoMockExtension;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
+
+import java.util.Optional;
+
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.withSettings;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.CoreMatchers.is;
+
+@ExtendWith(AutoMockExtension.class)
+@MockitoSettings(strictness = Strictness.LENIENT)
+class DatabaseNameUtilsTest {
+    
+    private static final String CURRENT_DATABASE = "CURRENT_DATABASE";
+    
+    private static final String AVAILABLE_DATABASE = "AVAILABLE_DATABASE";
+    
+    @Mock
+    private SQLStatement sqlStatement;
+    
+    private FromDatabaseAvailable fromDatabaseAvailable = 
mock(FromDatabaseAvailable.class, 
withSettings().extraInterfaces(SQLStatement.class));
+    
+    @Mock
+    private DatabaseSegment databaseSegment;
+    
+    @Mock
+    private IdentifierValue identifierValue;
+    
+    @BeforeEach
+    void setup() {
+        
when(fromDatabaseAvailable.getDatabase()).thenReturn(Optional.of(databaseSegment));
+        when(databaseSegment.getIdentifier()).thenReturn(identifierValue);
+        when(identifierValue.getValue()).thenReturn(AVAILABLE_DATABASE);
+    }
+    
+    @Test
+    void assertDatabaseNameWhenAvailableInSqlStatement() {
+        assertThat(DatabaseNameUtils.getDatabaseName((SQLStatement) 
fromDatabaseAvailable, CURRENT_DATABASE), is(AVAILABLE_DATABASE));
+    }
+    
+    @Test
+    void assertDatabaseNameWhenNotAvailableInSqlStatement() {
+        assertThat(DatabaseNameUtils.getDatabaseName(sqlStatement, 
CURRENT_DATABASE), is(CURRENT_DATABASE));
+    }
+}

Reply via email to