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 d5b567f  add rule check for shardingsphere-jdbc (#12404)
d5b567f is described below

commit d5b567f1142b447485e5783e32b58afc75f7820a
Author: zhaojinchao <[email protected]>
AuthorDate: Tue Sep 14 14:01:39 2021 +0800

    add rule check for shardingsphere-jdbc (#12404)
---
 .../core/datasource/ShardingSphereDataSource.java  |  8 ++++++
 .../driver/jdbc/adapter/WrapperAdapterTest.java    | 28 +++++++++++++++++++-
 .../UnsupportedOperationDataSourceTest.java        | 30 +++++++++++++++++++++-
 .../src/test/resources/application-jndi.properties |  3 ++-
 4 files changed, 66 insertions(+), 3 deletions(-)

diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
index 450a8c3..7381bf9 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
@@ -17,10 +17,12 @@
 
 package org.apache.shardingsphere.driver.jdbc.core.datasource;
 
+import com.google.common.base.Preconditions;
 import lombok.Getter;
 import 
org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedOperationDataSource;
 import org.apache.shardingsphere.driver.state.DriverStateContext;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import 
org.apache.shardingsphere.infra.config.checker.RuleConfigurationCheckerFactory;
 import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
 import org.apache.shardingsphere.infra.config.scope.GlobalRuleConfiguration;
 import org.apache.shardingsphere.infra.config.scope.SchemaRuleConfiguration;
@@ -56,10 +58,16 @@ public final class ShardingSphereDataSource extends 
AbstractUnsupportedOperation
     
     public ShardingSphereDataSource(final String schemaName, final 
ModeConfiguration modeConfig, final Map<String, DataSource> dataSourceMap,
                                     final Collection<RuleConfiguration> 
ruleConfigs, final Properties props) throws SQLException {
+        checkRuleConfiguration(schemaName, ruleConfigs);
         this.schemaName = schemaName;
         contextManager = createContextManager(schemaName, modeConfig, 
dataSourceMap, ruleConfigs, props, null == modeConfig || 
modeConfig.isOverwrite());
     }
     
+    private void checkRuleConfiguration(final String schemaName, final 
Collection<RuleConfiguration> ruleConfigs) {
+        Preconditions.checkArgument(null != ruleConfigs && 
!ruleConfigs.isEmpty(), "ShardingSphere rule configuration cannot be null or 
empty");
+        ruleConfigs.forEach(each -> 
RuleConfigurationCheckerFactory.newInstance(each).ifPresent(checker -> 
checker.check(schemaName, each)));
+    }
+    
     private ContextManager createContextManager(final String schemaName, final 
ModeConfiguration modeConfig, final Map<String, DataSource> dataSourceMap,
                                                 final 
Collection<RuleConfiguration> ruleConfigs, final Properties props, final 
boolean isOverwrite) throws SQLException {
         Map<String, Map<String, DataSource>> dataSourcesMap = 
Collections.singletonMap(schemaName, dataSourceMap);
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/WrapperAdapterTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/WrapperAdapterTest.java
index 4928717..e9d42d2 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/WrapperAdapterTest.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/WrapperAdapterTest.java
@@ -19,14 +19,21 @@ package org.apache.shardingsphere.driver.jdbc.adapter;
 
 import 
org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
 import 
org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.database.DefaultSchema;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import 
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
 import org.junit.Before;
 import org.junit.Test;
 
+import javax.sql.DataSource;
 import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
 import java.sql.SQLException;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 import java.util.logging.Logger;
 
@@ -36,6 +43,9 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public final class WrapperAdapterTest {
     
@@ -43,7 +53,23 @@ public final class WrapperAdapterTest {
     
     @Before
     public void setUp() throws SQLException {
-        shardingSphereDataSource = new 
ShardingSphereDataSource(DefaultSchema.LOGIC_NAME, null, 
Collections.emptyMap(), Collections.emptyList(), new Properties());
+        shardingSphereDataSource = new 
ShardingSphereDataSource(DefaultSchema.LOGIC_NAME, null, getDataSource(), 
getRuleConfigurations(), new Properties());
+    }
+    
+    private Map<String, DataSource> getDataSource() throws SQLException {
+        DataSource dataSource = mock(DataSource.class);
+        Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+        when(dataSource.getConnection()).thenReturn(connection);
+        when(connection.getMetaData()).thenReturn(mock(DatabaseMetaData.class, 
RETURNS_DEEP_STUBS));
+        
when(dataSource.getConnection().getMetaData().getURL()).thenReturn("jdbc:mysql://localhost:3306/test");
+        return Collections.singletonMap("ds", dataSource);
+    }
+    
+    private List<RuleConfiguration> getRuleConfigurations() {
+        ShardingRuleConfiguration shardingRuleConfig = new 
ShardingRuleConfiguration();
+        ShardingTableRuleConfiguration shardingTableRuleConfig = new 
ShardingTableRuleConfiguration("table", "ds" + "." + "table");
+        
shardingRuleConfig.setTables(Collections.singletonList(shardingTableRuleConfig));
+        return Collections.singletonList(shardingRuleConfig);
     }
     
     @Test
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationDataSourceTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationDataSourceTest.java
index c0be948..c6d3388 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationDataSourceTest.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/unsupported/UnsupportedOperationDataSourceTest.java
@@ -18,22 +18,50 @@
 package org.apache.shardingsphere.driver.jdbc.unsupported;
 
 import 
org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.database.DefaultSchema;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import 
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
 import org.junit.Before;
 import org.junit.Test;
 
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
 import java.sql.SQLException;
 import java.sql.SQLFeatureNotSupportedException;
 import java.util.Collections;
+import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
 public final class UnsupportedOperationDataSourceTest {
     
     private ShardingSphereDataSource shardingSphereDataSource;
     
     @Before
     public void setUp() throws SQLException {
-        shardingSphereDataSource = new 
ShardingSphereDataSource(DefaultSchema.LOGIC_NAME, null, 
Collections.emptyMap(), Collections.emptyList(), new Properties());
+        shardingSphereDataSource = new 
ShardingSphereDataSource(DefaultSchema.LOGIC_NAME, null, getDataSource(), 
getRuleConfigurations(), new Properties());
+    }
+    
+    private Map<String, DataSource> getDataSource() throws SQLException {
+        DataSource dataSource = mock(DataSource.class);
+        Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+        when(dataSource.getConnection()).thenReturn(connection);
+        when(connection.getMetaData()).thenReturn(mock(DatabaseMetaData.class, 
RETURNS_DEEP_STUBS));
+        
when(dataSource.getConnection().getMetaData().getURL()).thenReturn("jdbc:mysql://localhost:3306/test");
+        return Collections.singletonMap("ds", dataSource);
+    }
+    
+    private List<RuleConfiguration> getRuleConfigurations() {
+        ShardingRuleConfiguration shardingRuleConfig = new 
ShardingRuleConfiguration();
+        ShardingTableRuleConfiguration shardingTableRuleConfig = new 
ShardingTableRuleConfiguration("table", "ds" + "." + "table");
+        
shardingRuleConfig.setTables(Collections.singletonList(shardingTableRuleConfig));
+        return Collections.singletonList(shardingRuleConfig);
     }
     
     @Test(expected = SQLFeatureNotSupportedException.class)
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-jndi.properties
 
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-jndi.properties
index d3a4c4a..febc77a 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-jndi.properties
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-jndi.properties
@@ -19,4 +19,5 @@ spring.shardingsphere.datasource.names=jndi0,jndi1
 spring.shardingsphere.datasource.jndi0.jndi-name=java:comp/env/jdbc/jndi0
 spring.shardingsphere.datasource.jndi1.jndi-name=jdbc/jndi1
 
-spring.shardingsphere.rules.sharding.broadcast-tables=t_config
+spring.shardingsphere.rules.readwrite-splitting.data-sources.pr_ds.write-data-source-name=jndi0
+spring.shardingsphere.rules.readwrite-splitting.data-sources.pr_ds.read-data-source-names=jndi1

Reply via email to