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

sunnianjun 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 b62311338e4 Add ShardingSphereURLTest (#30135)
b62311338e4 is described below

commit b62311338e4b902add91aef963fecc6265d22a22
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Feb 15 18:35:07 2024 +0800

    Add ShardingSphereURLTest (#30135)
    
    * Add ShardingSphereURLTest
    
    * Refactor DriverDataSourceCache
    
    * Refactor DriverDataSourceCache
---
 .../jdbc/core/driver/DriverDataSourceCache.java    |  9 ++--
 .../core/driver/url/ShardingSphereURLTest.java     | 59 ++++++++++++++++++++++
 2 files changed, 63 insertions(+), 5 deletions(-)

diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/DriverDataSourceCache.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/DriverDataSourceCache.java
index a6be030c7b9..a45cc99716a 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/DriverDataSourceCache.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/driver/DriverDataSourceCache.java
@@ -46,15 +46,14 @@ public final class DriverDataSourceCache {
         if (dataSourceMap.containsKey(url)) {
             return dataSourceMap.get(url);
         }
-        return dataSourceMap.computeIfAbsent(url, driverUrl -> 
createDataSource(driverUrl.substring(urlPrefix.length())));
+        return dataSourceMap.computeIfAbsent(url, driverUrl -> 
createDataSource(ShardingSphereURL.parse(driverUrl.substring(urlPrefix.length()))));
     }
     
     @SuppressWarnings("unchecked")
-    private <T extends Throwable> DataSource createDataSource(final String 
url) throws T {
+    private <T extends Throwable> DataSource createDataSource(final 
ShardingSphereURL url) throws T {
         try {
-            ShardingSphereURL shardingSphereURL = ShardingSphereURL.parse(url);
-            ShardingSphereURLLoader urlLoader = 
TypedSPILoader.getService(ShardingSphereURLLoader.class, 
shardingSphereURL.getSourceType());
-            return 
YamlShardingSphereDataSourceFactory.createDataSource(urlLoader.getContent(shardingSphereURL));
+            ShardingSphereURLLoader urlLoader = 
TypedSPILoader.getService(ShardingSphereURLLoader.class, url.getSourceType());
+            return 
YamlShardingSphereDataSourceFactory.createDataSource(urlLoader.getContent(url));
         } catch (final IOException ex) {
             throw (T) new SQLException(ex);
         } catch (final SQLException ex) {
diff --git 
a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLTest.java
 
b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLTest.java
new file mode 100644
index 00000000000..276bca9abba
--- /dev/null
+++ 
b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/driver/url/ShardingSphereURLTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.driver.jdbc.core.driver.url;
+
+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 java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Stream;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class ShardingSphereURLTest {
+    
+    @ParameterizedTest(name = "{0}")
+    @ArgumentsSource(TestCaseArgumentsProvider.class)
+    void assertParse(final String url, final String expectedSourceType, final 
String expectedConfigurationSubject, final Map<String, String> 
expectedParameters) {
+        ShardingSphereURL actual = ShardingSphereURL.parse(url);
+        assertThat(actual.getSourceType(), is(expectedSourceType));
+        assertThat(actual.getConfigurationSubject(), 
is(expectedConfigurationSubject));
+        assertThat(actual.getParameters(), is(expectedParameters));
+    }
+    
+    private static class TestCaseArgumentsProvider implements 
ArgumentsProvider {
+        
+        @Override
+        public Stream<? extends Arguments> provideArguments(final 
ExtensionContext extensionContext) {
+            Map<String, String> multiParams = new HashMap<>();
+            multiParams.put("databaseName", "sharding_db");
+            multiParams.put("placeholder-type", "none");
+            return 
Stream.of(Arguments.of("absolutepath:/Users/shardingsphere/config.yaml", 
"absolutepath:", "/Users/shardingsphere/config.yaml", Collections.emptyMap()),
+                    
Arguments.of("absolutepath:C:\\Users\\shardingsphere\\config.yaml", 
"absolutepath:", "C:\\Users\\shardingsphere\\config.yaml", 
Collections.emptyMap()),
+                    
Arguments.of("absolutepath:/Users/configDirName?databaseName=sharding_db", 
"absolutepath:", "/Users/configDirName", 
Collections.singletonMap("databaseName", "sharding_db")),
+                    
Arguments.of("absolutepath:/Users/configDirName/?databaseName=sharding_db", 
"absolutepath:", "/Users/configDirName/", 
Collections.singletonMap("databaseName", "sharding_db")),
+                    
Arguments.of("classpath:config/shardingsphere/config.yml?databaseName=sharding_db&placeholder-type=none",
 "classpath:", "config/shardingsphere/config.yml", multiParams));
+        }
+    }
+}

Reply via email to