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 ac1db122ad2 Add more test cases on HiveConnectionPropertiesParserTest 
(#38370)
ac1db122ad2 is described below

commit ac1db122ad250df9b56b640c0781461423adaa24
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Mar 7 23:03:05 2026 +0800

    Add more test cases on HiveConnectionPropertiesParserTest (#38370)
---
 .../HiveConnectionPropertiesParserTest.java        | 93 +++++++++++++++-------
 1 file changed, 63 insertions(+), 30 deletions(-)

diff --git 
a/database/connector/dialect/hive/src/test/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveConnectionPropertiesParserTest.java
 
b/database/connector/dialect/hive/src/test/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveConnectionPropertiesParserTest.java
index 6c0d73db6a2..b7a3ae040b2 100644
--- 
a/database/connector/dialect/hive/src/test/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveConnectionPropertiesParserTest.java
+++ 
b/database/connector/dialect/hive/src/test/java/org/apache/shardingsphere/database/connector/hive/jdbcurl/HiveConnectionPropertiesParserTest.java
@@ -18,6 +18,8 @@
 package org.apache.shardingsphere.database.connector.hive.jdbcurl;
 
 import org.apache.hive.jdbc.JdbcUriParseException;
+import org.apache.hive.jdbc.Utils;
+import org.apache.hive.jdbc.Utils.JdbcConnectionParams;
 import 
org.apache.shardingsphere.database.connector.core.jdbcurl.parser.ConnectionProperties;
 import 
org.apache.shardingsphere.database.connector.core.jdbcurl.parser.ConnectionPropertiesParser;
 import 
org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
@@ -26,57 +28,88 @@ import 
org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.infra.util.props.PropertiesBuilder;
 import org.apache.shardingsphere.infra.util.props.PropertiesBuilder.Property;
 import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtensionContext;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.Arguments;
-import org.junit.jupiter.params.provider.ArgumentsProvider;
-import org.junit.jupiter.params.provider.ArgumentsSource;
-import org.junit.jupiter.params.support.ParameterDeclarations;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.MockedStatic;
 
+import java.util.Collections;
 import java.util.Properties;
 import java.util.stream.Stream;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
 
 class HiveConnectionPropertiesParserTest {
     
-    private final ConnectionPropertiesParser parser = 
DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class, 
TypedSPILoader.getService(DatabaseType.class, "Hive"));
+    private final DatabaseType databaseType = 
TypedSPILoader.getService(DatabaseType.class, "Hive");
+    
+    private final ConnectionPropertiesParser parser = 
DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class, 
databaseType);
     
     @ParameterizedTest(name = "{0}")
-    @ArgumentsSource(NewConstructorTestCaseArgumentsProvider.class)
-    void assertNewConstructor(final String name, final String url, final 
String hostname, final int port, final String catalog, final String schema, 
final Properties queryProps) {
+    @MethodSource("parseSuccessArguments")
+    void assertParseWithValidUrl(final String name, final String url, final 
String expectedHostname, final int expectedPort, final String expectedCatalog, 
final Properties expectedQueryProps) {
         ConnectionProperties actual = parser.parse(url, null, null);
-        assertThat(actual.getHostname(), is(hostname));
-        assertThat(actual.getPort(), is(port));
-        assertThat(actual.getCatalog(), is(catalog));
-        assertThat(actual.getSchema(), is(schema));
-        assertThat(actual.getQueryProperties(), is(queryProps));
+        assertThat(actual.getHostname(), is(expectedHostname));
+        assertThat(actual.getPort(), is(expectedPort));
+        assertThat(actual.getCatalog(), is(expectedCatalog));
+        assertNull(actual.getSchema());
+        assertThat(actual.getQueryProperties(), is(expectedQueryProps));
+    }
+    
+    @ParameterizedTest(name = "{0}")
+    @MethodSource("parseJdbcUriParseExceptionArguments")
+    void assertParseWithInvalidUrl(final String name, final String url) {
+        assertThrows(JdbcUriParseException.class, () -> parser.parse(url, 
null, null));
     }
     
     @Test
-    void assertNewConstructorFailure() {
-        assertThrows(JdbcUriParseException.class, () -> 
parser.parse("jdbc:hive2://localhost:10000;principal=test", null, null));
-        assertThrows(JdbcUriParseException.class, () -> 
parser.parse("jdbc:hive2://localhost:10000;principal=hive/[email protected]",
 null, null));
-        assertThrows(JdbcUriParseException.class, () -> 
parser.parse("jdbc:hive2://localhost:10000test", null, null));
+    void assertParseWithEmbeddedMode() {
         assertThrows(RuntimeException.class, () -> 
parser.parse("jdbc:hive2://", null, null));
     }
     
-    private static final class NewConstructorTestCaseArgumentsProvider 
implements ArgumentsProvider {
-        
-        @Override
-        public Stream<? extends Arguments> provideArguments(final 
ParameterDeclarations parameters, final ExtensionContext context) {
-            return Stream.of(
-                    Arguments.of("simple_first", 
"jdbc:hive2://localhost:10001/default", "localhost", 10001, "default", null, 
new Properties()),
-                    Arguments.of("simple_second", 
"jdbc:hive2://localhost/notdefault", "localhost", 10000, "notdefault", null, 
new Properties()),
-                    Arguments.of("simple_third", "jdbc:hive2://foo:1243", 
"foo", 1243, "default", null, new Properties()),
-                    Arguments.of("complex", 
"jdbc:hive2://server:10002/db;user=foo;password=bar?transportMode=http;httpPath=hs2",
-                            "server", 10002, "db", null, 
PropertiesBuilder.build(
-                                    new Property("user", "foo"),
-                                    new Property("password", "bar"),
-                                    new Property("transportMode", "http"),
-                                    new Property("httpPath", "hs2"))));
+    @Test
+    void assertParseWithNullHostAndNonZeroPort() {
+        JdbcConnectionParams jdbcConnectionParams = 
mock(JdbcConnectionParams.class);
+        when(jdbcConnectionParams.getHost()).thenReturn(null);
+        when(jdbcConnectionParams.getPort()).thenReturn(10000);
+        when(jdbcConnectionParams.getDbName()).thenReturn("default");
+        
when(jdbcConnectionParams.getSessionVars()).thenReturn(Collections.singletonMap("sessionKey",
 "sessionValue"));
+        
when(jdbcConnectionParams.getHiveConfs()).thenReturn(Collections.singletonMap("hiveConfKey",
 "hiveConfValue"));
+        
when(jdbcConnectionParams.getHiveVars()).thenReturn(Collections.singletonMap("hiveVarKey",
 "hiveVarValue"));
+        try (MockedStatic<Utils> mockedUtils = mockStatic(Utils.class, 
CALLS_REAL_METHODS)) {
+            mockedUtils.when(() -> Utils.parseURL(anyString(), 
any(Properties.class))).thenReturn(jdbcConnectionParams);
+            ConnectionProperties actualConnectionProperties = 
parser.parse("jdbc:hive2://branch/null-host-port/default", null, null);
+            assertNull(actualConnectionProperties.getHostname());
+            assertThat(actualConnectionProperties.getPort(), is(10000));
+            assertThat(actualConnectionProperties.getCatalog(), is("default"));
+            assertNull(actualConnectionProperties.getSchema());
+            assertThat(actualConnectionProperties.getQueryProperties(),
+                    is(PropertiesBuilder.build(new Property("sessionKey", 
"sessionValue"), new Property("hiveConfKey", "hiveConfValue"), new 
Property("hiveVarKey", "hiveVarValue"))));
         }
     }
+    
+    private static Stream<Arguments> parseSuccessArguments() {
+        return Stream.of(
+                Arguments.of("simple_first", 
"jdbc:hive2://localhost:10001/default", "localhost", 10001, "default", new 
Properties()),
+                Arguments.of("simple_second", 
"jdbc:hive2://localhost/notdefault", "localhost", 10000, "notdefault", new 
Properties()),
+                Arguments.of("simple_third", "jdbc:hive2://foo:1243", "foo", 
1243, "default", new Properties()),
+                Arguments.of("complex", 
"jdbc:hive2://server:10002/db;user=foo;password=bar?transportMode=http;httpPath=hs2",
 "server", 10002, "db",
+                        PropertiesBuilder.build(new Property("user", "foo"), 
new Property("password", "bar"), new Property("transportMode", "http"), new 
Property("httpPath", "hs2"))));
+    }
+    
+    private static Stream<Arguments> parseJdbcUriParseExceptionArguments() {
+        return Stream.of(
+                Arguments.of("principal_without_realm", 
"jdbc:hive2://localhost:10000;principal=test"),
+                Arguments.of("principal_with_realm", 
"jdbc:hive2://localhost:10000;principal=hive/[email protected]"),
+                Arguments.of("missing_separator", 
"jdbc:hive2://localhost:10000test"));
+    }
 }

Reply via email to