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

menghaoran 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 517bad4  Ignore DataSource property name which value is null (#13883)
517bad4 is described below

commit 517bad4a90316176bb2d9ba5cb13e6c4f93cca8b
Author: Hongsheng Zhong <[email protected]>
AuthorDate: Wed Dec 1 19:58:41 2021 +0800

    Ignore DataSource property name which value is null (#13883)
---
 .../datasource/creator/impl/AbstractDataSourceCreator.java  |  5 ++++-
 .../datasource/creator/DefaultDataSourceCreatorTest.java    | 11 +++++------
 .../datasource/creator/HikariDataSourceCreatorTest.java     | 13 +++++++++++++
 3 files changed, 22 insertions(+), 7 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/impl/AbstractDataSourceCreator.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/impl/AbstractDataSourceCreator.java
index a7b57c2..4edb1d0 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/impl/AbstractDataSourceCreator.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/impl/AbstractDataSourceCreator.java
@@ -108,7 +108,10 @@ public abstract class AbstractDataSourceCreator implements 
DataSourceCreator {
         for (Method each : allGetterMethods) {
             String propertyName = 
CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, 
each.getName().substring(GETTER_PREFIX.length()));
             if (GENERAL_CLASS_TYPE.contains(each.getReturnType()) && 
!SKIPPED_PROPERTY_NAMES.contains(propertyName)) {
-                result.put(propertyName, each.invoke(target));
+                Object propertyValue = each.invoke(target);
+                if (null != propertyValue) {
+                    result.put(propertyName, propertyValue);
+                }
             }
         }
         return result;
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/creator/DefaultDataSourceCreatorTest.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/creator/DefaultDataSourceCreatorTest.java
index f161335..116abd6 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/creator/DefaultDataSourceCreatorTest.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/creator/DefaultDataSourceCreatorTest.java
@@ -29,28 +29,27 @@ import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
 
 public final class DefaultDataSourceCreatorTest {
+    
     @Test
-    public void assertCreateDataSourceConfiguration() {
+    public void assertDataSourceConfigurationEquals() {
         DefaultDataSourceCreator defaultDataSourceCreator = new 
DefaultDataSourceCreator();
         DataSourceConfiguration generateDataSourceConfiguration = 
defaultDataSourceCreator.createDataSourceConfiguration(createDataSource());
-
         DataSourceConfiguration targetDataSourceConfiguration = 
createDataSourceConfiguration();
         assertThat(generateDataSourceConfiguration, 
is(targetDataSourceConfiguration));
     }
-
+    
     @Test
     public void assertCreateDataSource() {
         DefaultDataSourceCreator defaultDataSourceCreator = new 
DefaultDataSourceCreator();
         DataSource generateDataSource = 
defaultDataSourceCreator.createDataSource(createDataSourceConfiguration());
         assertThat(generateDataSource, instanceOf(HikariDataSource.class));
-
         HikariDataSource targetDataSource = (HikariDataSource) 
generateDataSource;
         assertThat(targetDataSource.getUsername(), is("root"));
         assertThat(targetDataSource.getPassword(), is("root"));
         assertThat(targetDataSource.getDriverClassName(), is("org.h2.Driver"));
         assertThat(targetDataSource.getJdbcUrl(), 
is("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"));
     }
-
+    
     private DataSource createDataSource() {
         HikariDataSource dataSource = new HikariDataSource();
         dataSource.setDriverClassName("org.h2.Driver");
@@ -59,7 +58,7 @@ public final class DefaultDataSourceCreatorTest {
         dataSource.setPassword("root");
         return dataSource;
     }
-
+    
     private DataSourceConfiguration createDataSourceConfiguration() {
         DataSourceConfiguration dataSourceConfiguration = new 
DataSourceConfiguration(HikariDataSource.class.getName());
         dataSourceConfiguration.getProps().put("jdbcUrl", 
"jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/creator/HikariDataSourceCreatorTest.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/creator/HikariDataSourceCreatorTest.java
index d23f0d5..39a0d8f 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/creator/HikariDataSourceCreatorTest.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/creator/HikariDataSourceCreatorTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.infra.datasource.creator;
 import com.zaxxer.hikari.HikariDataSource;
 import 
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
 import 
org.apache.shardingsphere.infra.config.datasource.creator.DataSourceCreator;
+import 
org.apache.shardingsphere.infra.config.datasource.creator.impl.DefaultDataSourceCreator;
 import 
org.apache.shardingsphere.infra.config.datasource.creator.impl.HikariDataSourceCreator;
 import org.junit.Test;
 
@@ -29,11 +30,23 @@ import java.util.Map;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
 
 public final class HikariDataSourceCreatorTest {
     
     @Test
+    public void assertCreateDataSourceConfigurationWithoutDriverClassName() {
+        HikariDataSource dataSource = new HikariDataSource();
+        
dataSource.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
+        dataSource.setUsername("root");
+        dataSource.setPassword("root");
+        DataSourceConfiguration dataSourceConfiguration = new 
DefaultDataSourceCreator().createDataSourceConfiguration(dataSource);
+        Map<String, Object> props = dataSourceConfiguration.getProps();
+        assertFalse(props.containsKey("driverClassName") && null == 
props.get("driverClassName"));
+    }
+    
+    @Test
     public void assertCreateDataSourceConfiguration() {
         DataSourceCreator dataSourceCreator = new HikariDataSourceCreator();
         DataSourceConfiguration configuration = 
dataSourceCreator.createDataSourceConfiguration(dataSourceCreator.createDataSource(createDataSourceConfiguration()));

Reply via email to