You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ki...@apache.org on 2020/10/19 04:09:15 UTC

[shardingsphere] branch master updated: Split DatabaseMetaDataDialectHandlerTest related test cases with diff dialect (#7838)

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

kimmking 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 e997777  Split DatabaseMetaDataDialectHandlerTest related test cases with diff dialect (#7838)
e997777 is described below

commit e997777e715eacce8c03822b4305c59f19b02fc2
Author: xiaoyu <54...@qq.com>
AuthorDate: Mon Oct 19 12:08:48 2020 +0800

    Split DatabaseMetaDataDialectHandlerTest related test cases with diff dialect (#7838)
    
    * Split DatabaseMetaDataDialectHandlerTest related test cases with diff dialect
    
    * Split DatabaseMetaDataDialectHandlerTest related test cases with diff dialect
---
 ...AbstractDatabaseMetaDataDialectHandlerTest.java | 63 ++++++++++++++++++++++
 .../MySQLDatabaseMetaDataDialectHandlerTest.java   | 50 +++++++++++++++++
 2 files changed, 113 insertions(+)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/model/physical/jdbc/handler/AbstractDatabaseMetaDataDialectHandlerTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/model/physical/jdbc/handler/AbstractDatabaseMetaDataDialectHandlerTest.java
new file mode 100644
index 0000000..322ef6a
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/model/physical/jdbc/handler/AbstractDatabaseMetaDataDialectHandlerTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.metadata.model.physical.jdbc.handler;
+
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.SQLException;
+import lombok.Getter;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.sql.parser.sql.common.constant.QuoteCharacter;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+@Getter
+@RunWith(MockitoJUnitRunner.class)
+public abstract class AbstractDatabaseMetaDataDialectHandlerTest {
+    
+    protected static final String DATABASE_NAME = "demo_ds";
+    
+    protected static final String TABLE_NAME_PATTERN = "t_order_0";
+    
+    @Mock
+    private Connection connection;
+    
+    @Mock
+    private DatabaseMetaData databaseMetaData;
+    
+    protected QuoteCharacter getQuoteCharacter(final DatabaseType databaseType) {
+        return DatabaseMetaDataDialectHandlerFactory.findHandler(databaseType).map(DatabaseMetaDataDialectHandler::getQuoteCharacter).orElse(QuoteCharacter.NONE);
+    }
+    
+    protected String formatTableNamePattern(final DatabaseType databaseType) {
+        return DatabaseMetaDataDialectHandlerFactory.findHandler(databaseType).map(handler -> handler.formatTableNamePattern(TABLE_NAME_PATTERN)).orElse(TABLE_NAME_PATTERN);
+    }
+    
+    protected String getSchema(final DatabaseType databaseType) {
+        return DatabaseMetaDataDialectHandlerFactory.findHandler(databaseType).map(handler -> handler.getSchema(connection)).orElse(getSchema(connection));
+    }
+    
+    private String getSchema(final Connection connection) {
+        try {
+            return connection.getSchema();
+        } catch (final SQLException ignored) {
+            return null;
+        }
+    }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/model/physical/jdbc/handler/MySQLDatabaseMetaDataDialectHandlerTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/model/physical/jdbc/handler/MySQLDatabaseMetaDataDialectHandlerTest.java
new file mode 100644
index 0000000..a138d5c
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/model/physical/jdbc/handler/MySQLDatabaseMetaDataDialectHandlerTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.metadata.model.physical.jdbc.handler;
+
+import java.sql.SQLException;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.sql.parser.sql.common.constant.QuoteCharacter;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.when;
+
+public final class MySQLDatabaseMetaDataDialectHandlerTest extends AbstractDatabaseMetaDataDialectHandlerTest {
+    
+    @Test
+    public void assertGetSchema() throws SQLException {
+        when(getConnection().getSchema()).thenReturn(DATABASE_NAME);
+        String mysqlSchema = getSchema(new MySQLDatabaseType());
+        assertThat(mysqlSchema, is(DATABASE_NAME));
+    }
+    
+    @Test
+    public void assertFormatTableNamePattern() {
+        String mysqlTableNamePattern = formatTableNamePattern(new MySQLDatabaseType());
+        assertThat(mysqlTableNamePattern, is(TABLE_NAME_PATTERN));
+    }
+    
+    @Test
+    public void assertGetQuoteCharacter() {
+        QuoteCharacter mysqlQuoteCharacter = getQuoteCharacter(new MySQLDatabaseType());
+        assertThat(mysqlQuoteCharacter.getStartDelimiter(), is("`"));
+        assertThat(mysqlQuoteCharacter.getEndDelimiter(), is("`"));
+    }
+}