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 859292b3154 Add coverage for oracle schema option and jdbc instance
judger (#37586)
859292b3154 is described below
commit 859292b31543ac91904fbb66f0ebf46ef50e912e
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Dec 30 21:16:04 2025 +0800
Add coverage for oracle schema option and jdbc instance judger (#37586)
* Add full coverage for connector registry and dialect utilities
* Add coverage for oracle schema option and jdbc instance judger
---
.../judger/DatabaseInstanceJudgeEngineTest.java | 68 ++++++++++++++++++++++
.../database/datatype/DataTypeLoaderTest.java | 63 ++++++++++++++++++++
.../database/option/OracleSchemaOptionTest.java | 63 ++++++++++++++++++++
3 files changed, 194 insertions(+)
diff --git
a/database/connector/core/src/test/java/org/apache/shardingsphere/database/connector/core/jdbcurl/judger/DatabaseInstanceJudgeEngineTest.java
b/database/connector/core/src/test/java/org/apache/shardingsphere/database/connector/core/jdbcurl/judger/DatabaseInstanceJudgeEngineTest.java
new file mode 100644
index 00000000000..07618367b8e
--- /dev/null
+++
b/database/connector/core/src/test/java/org/apache/shardingsphere/database/connector/core/jdbcurl/judger/DatabaseInstanceJudgeEngineTest.java
@@ -0,0 +1,68 @@
+/*
+ * 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.database.connector.core.jdbcurl.judger;
+
+import
org.apache.shardingsphere.database.connector.core.jdbcurl.parser.ConnectionProperties;
+import
org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+
+import java.util.Optional;
+import java.util.Properties;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(DatabaseTypedSPILoader.class)
+class DatabaseInstanceJudgeEngineTest {
+
+ @Mock
+ private DatabaseType databaseType;
+
+ private DatabaseInstanceJudgeEngine engine;
+
+ @BeforeEach
+ void setUp() {
+ engine = new DatabaseInstanceJudgeEngine(databaseType);
+ }
+
+ @Test
+ void assertIsInSameDatabaseInstanceWithDialectJudger() {
+ DialectDatabaseInstanceJudger dialectDatabaseInstanceJudger =
mock(DialectDatabaseInstanceJudger.class);
+
when(DatabaseTypedSPILoader.findService(DialectDatabaseInstanceJudger.class,
databaseType)).thenReturn(Optional.of(dialectDatabaseInstanceJudger));
+ ConnectionProperties connectionProps1 = new
ConnectionProperties("hostA", 3306, "catalog1", "schema1", new Properties());
+ ConnectionProperties connectionProps2 = new
ConnectionProperties("hostB", 3306, "catalog2", "schema2", new Properties());
+
when(dialectDatabaseInstanceJudger.isInSameDatabaseInstance(connectionProps1,
connectionProps2)).thenReturn(true);
+ assertTrue(engine.isInSameDatabaseInstance(connectionProps1,
connectionProps2));
+ }
+
+ @Test
+ void assertIsInSameDatabaseInstanceWithDefaultJudger() {
+
when(DatabaseTypedSPILoader.findService(DialectDatabaseInstanceJudger.class,
databaseType)).thenReturn(Optional.empty());
+ ConnectionProperties connectionProps1 = new
ConnectionProperties("hostC", 5432, "catalog3", "schema3", new Properties());
+ ConnectionProperties connectionProps2 = new
ConnectionProperties("hostC", 5432, "catalog4", "schema4", new Properties());
+ assertTrue(engine.isInSameDatabaseInstance(connectionProps1,
connectionProps2));
+ }
+}
diff --git
a/database/connector/core/src/test/java/org/apache/shardingsphere/database/connector/core/metadata/database/datatype/DataTypeLoaderTest.java
b/database/connector/core/src/test/java/org/apache/shardingsphere/database/connector/core/metadata/database/datatype/DataTypeLoaderTest.java
new file mode 100644
index 00000000000..b617b814833
--- /dev/null
+++
b/database/connector/core/src/test/java/org/apache/shardingsphere/database/connector/core/metadata/database/datatype/DataTypeLoaderTest.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.database.connector.core.metadata.database.datatype;
+
+import
org.apache.shardingsphere.database.connector.core.metadata.database.metadata.DialectDatabaseMetaData;
+import
org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.Collections;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+class DataTypeLoaderTest {
+
+ private final DataTypeLoader dataTypeLoader = new DataTypeLoader();
+
+ @Test
+ void assertLoad() throws SQLException {
+ ResultSet resultSet = mock(ResultSet.class);
+ when(resultSet.next()).thenReturn(true, true, false);
+ when(resultSet.getString("TYPE_NAME")).thenReturn("int", "varchar");
+ when(resultSet.getInt("DATA_TYPE")).thenReturn(Types.INTEGER,
Types.VARCHAR);
+ DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
+ when(databaseMetaData.getTypeInfo()).thenReturn(resultSet);
+ DialectDatabaseMetaData dialectDatabaseMetaData =
mock(DialectDatabaseMetaData.class, RETURNS_DEEP_STUBS);
+
when(dialectDatabaseMetaData.getDataTypeOption().getExtraDataTypes()).thenReturn(Collections.singletonMap("EXTRA_TYPE",
Types.OTHER));
+ DatabaseType databaseType = mock(DatabaseType.class);
+ try (MockedStatic<DatabaseTypedSPILoader> mockedStatic =
mockStatic(DatabaseTypedSPILoader.class)) {
+ mockedStatic.when(() ->
DatabaseTypedSPILoader.getService(DialectDatabaseMetaData.class,
databaseType)).thenReturn(dialectDatabaseMetaData);
+ Map<String, Integer> actual =
dataTypeLoader.load(databaseMetaData, databaseType);
+ assertThat(actual.get("INT"), is(Types.INTEGER));
+ assertThat(actual.get("varchar"), is(Types.VARCHAR));
+ assertThat(actual.get("EXTRA_TYPE"), is(Types.OTHER));
+ }
+ }
+}
diff --git
a/database/connector/dialect/oracle/src/test/java/org/apache/shardingsphere/database/connector/oracle/metadata/database/option/OracleSchemaOptionTest.java
b/database/connector/dialect/oracle/src/test/java/org/apache/shardingsphere/database/connector/oracle/metadata/database/option/OracleSchemaOptionTest.java
new file mode 100644
index 00000000000..38b9c03cab9
--- /dev/null
+++
b/database/connector/dialect/oracle/src/test/java/org/apache/shardingsphere/database/connector/oracle/metadata/database/option/OracleSchemaOptionTest.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.database.connector.oracle.metadata.database.option;
+
+import org.junit.jupiter.api.Test;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class OracleSchemaOptionTest {
+
+ @Test
+ void assertIsSchemaAvailable() {
+ assertFalse(new OracleSchemaOption().isSchemaAvailable());
+ }
+
+ @Test
+ void assertGetSchemaWithUserName() throws SQLException {
+ Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+ when(connection.getMetaData().getUserName()).thenReturn("scott");
+ assertThat(new OracleSchemaOption().getSchema(connection),
is("SCOTT"));
+ }
+
+ @Test
+ void assertGetSchemaWhenSQLExceptionThrown() throws SQLException {
+ Connection connection = mock(Connection.class);
+ when(connection.getMetaData()).thenThrow(SQLException.class);
+ assertNull(new OracleSchemaOption().getSchema(connection));
+ }
+
+ @Test
+ void assertGetDefaultSchema() {
+ assertFalse(new OracleSchemaOption().getDefaultSchema().isPresent());
+ }
+
+ @Test
+ void assertGetDefaultSystemSchema() {
+ assertFalse(new
OracleSchemaOption().getDefaultSystemSchema().isPresent());
+ }
+}