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 6a2fba5da2c add test And fix NPE (#19233)
6a2fba5da2c is described below
commit 6a2fba5da2c783ec1f8b2d6e5dc0adaf4e9cf26b
Author: skai <[email protected]>
AuthorDate: Fri Jul 15 22:35:32 2022 +0800
add test And fix NPE (#19233)
* add test And fix NPE
* add license
Co-authored-by: skai <[email protected]>
---
.../api/ShardingSphereDataSourceFactory.java | 11 ++-
.../api/ShardingSphereDataSourceFactoryTest.java | 81 ++++++++++++++++++
.../YamlShardingSphereDataSourceFactoryTest.java | 99 ++++++++++++++++++++++
.../yaml/configWithDataSourceWithRules.yaml | 44 ++++++++++
.../yaml/configWithoutDataSourceWithRules.yaml | 36 ++++++++
.../test/resources/yaml/configWithoutRules.yaml | 21 +++++
6 files changed, 289 insertions(+), 3 deletions(-)
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactory.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactory.java
index 75da0040d3b..57a22ad39ff 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactory.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactory.java
@@ -29,6 +29,7 @@ import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
+import java.util.LinkedList;
import java.util.Map;
import java.util.Properties;
@@ -47,7 +48,7 @@ public final class ShardingSphereDataSourceFactory {
* @throws SQLException SQL exception
*/
public static DataSource createDataSource(final String databaseName, final
ModeConfiguration modeConfig) throws SQLException {
- return new
ShardingSphereDataSource(Strings.isNullOrEmpty(databaseName) ?
DefaultDatabase.LOGIC_NAME : databaseName, modeConfig);
+ return new
ShardingSphereDataSource(getDatabaseNameOrDefault(databaseName), modeConfig);
}
/**
@@ -74,7 +75,7 @@ public final class ShardingSphereDataSourceFactory {
*/
public static DataSource createDataSource(final String databaseName, final
ModeConfiguration modeConfig,
final Map<String, DataSource>
dataSourceMap, final Collection<RuleConfiguration> configs, final Properties
props) throws SQLException {
- return new
ShardingSphereDataSource(Strings.isNullOrEmpty(databaseName) ?
DefaultDatabase.LOGIC_NAME : databaseName, modeConfig, dataSourceMap, configs,
props);
+ return new
ShardingSphereDataSource(getDatabaseNameOrDefault(databaseName), modeConfig,
dataSourceMap, null == configs ? new LinkedList<>() : configs, props);
}
/**
@@ -105,7 +106,7 @@ public final class ShardingSphereDataSourceFactory {
*/
public static DataSource createDataSource(final String databaseName, final
ModeConfiguration modeConfig,
final DataSource dataSource,
final Collection<RuleConfiguration> configs, final Properties props) throws
SQLException {
- return createDataSource(databaseName, modeConfig,
Collections.singletonMap(Strings.isNullOrEmpty(databaseName) ?
DefaultDatabase.LOGIC_NAME : databaseName, dataSource), configs, props);
+ return createDataSource(databaseName, modeConfig,
Collections.singletonMap(getDatabaseNameOrDefault(databaseName), dataSource),
configs, props);
}
/**
@@ -177,4 +178,8 @@ public final class ShardingSphereDataSourceFactory {
public static DataSource createDataSource(final DataSource dataSource,
final Collection<RuleConfiguration> configs, final Properties props) throws
SQLException {
return createDataSource((ModeConfiguration) null, dataSource, configs,
props);
}
+
+ private static String getDatabaseNameOrDefault(final String databaseName) {
+ return Strings.isNullOrEmpty(databaseName) ?
DefaultDatabase.LOGIC_NAME : databaseName;
+ }
}
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactoryTest.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactoryTest.java
new file mode 100644
index 00000000000..e689a277a87
--- /dev/null
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactoryTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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.api;
+
+import java.lang.reflect.Field;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Properties;
+import javax.sql.DataSource;
+import
org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
+import org.apache.shardingsphere.infra.database.DefaultDatabase;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+import lombok.SneakyThrows;
+
+public final class ShardingSphereDataSourceFactoryTest {
+
+ private final ModeConfiguration modeConfig = new
ModeConfiguration("Standalone", null, false);
+
+ @Test
+ public void assertCreateDataSourceWithDatabaseName() throws SQLException {
+ DataSource testDataSource =
ShardingSphereDataSourceFactory.createDataSource("test_db", null);
+ assertTrue(testDataSource instanceof ShardingSphereDataSource);
+ assertThat(getDatabaseName(testDataSource), is("test_db"));
+ DataSource testDataSource1 =
ShardingSphereDataSourceFactory.createDataSource(modeConfig);
+ assertTrue(testDataSource1 instanceof ShardingSphereDataSource);
+ assertThat(getDatabaseName(testDataSource1),
is(DefaultDatabase.LOGIC_NAME));
+ DataSource testDataSource2 =
ShardingSphereDataSourceFactory.createDataSource("", null);
+ assertTrue(testDataSource2 instanceof ShardingSphereDataSource);
+ assertThat(getDatabaseName(testDataSource2),
is(DefaultDatabase.LOGIC_NAME));
+ DataSource testDataSource3 =
ShardingSphereDataSourceFactory.createDataSource(new HashMap<String,
DataSource>(), new LinkedList<>(), new Properties());
+ assertThat(getDatabaseName(testDataSource3),
is(DefaultDatabase.LOGIC_NAME));
+ DataSource testDataSource4 =
ShardingSphereDataSourceFactory.createDataSource(new MockedDataSource(), new
LinkedList<>(), new Properties());
+ assertThat(getDatabaseName(testDataSource4),
is(DefaultDatabase.LOGIC_NAME));
+ DataSource testDataSource5 =
ShardingSphereDataSourceFactory.createDataSource("test_db5", new
MockedDataSource(), new LinkedList<>(), new Properties());
+ assertTrue(testDataSource5 instanceof ShardingSphereDataSource);
+ assertThat(getDatabaseName(testDataSource5), is("test_db5"));
+ DataSource testDataSource6 =
ShardingSphereDataSourceFactory.createDataSource("test_db6", new
HashMap<String, DataSource>(), new LinkedList<>(), new Properties());
+ assertTrue(testDataSource6 instanceof ShardingSphereDataSource);
+ assertThat(getDatabaseName(testDataSource6), is("test_db6"));
+ DataSource testDataSource7 =
ShardingSphereDataSourceFactory.createDataSource("test_db6", modeConfig, new
HashMap<String, DataSource>(), null, null);
+ assertTrue(testDataSource7 instanceof ShardingSphereDataSource);
+ assertThat(getDatabaseName(testDataSource7), is("test_db6"));
+ }
+
+
+ /**
+ * get database name.
+ *
+ * @param shardingSphereDataSource dataSource
+ * @return databaseName
+ */
+ @SneakyThrows(ReflectiveOperationException.class)
+ public static String getDatabaseName(final DataSource
shardingSphereDataSource) {
+ Field field =
ShardingSphereDataSource.class.getDeclaredField("databaseName");
+ field.setAccessible(true);
+ return (String) field.get(shardingSphereDataSource);
+ }
+}
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactoryTest.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactoryTest.java
new file mode 100644
index 00000000000..8fa6d0ca4a8
--- /dev/null
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactoryTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.api.yaml;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.URL;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
+import javax.sql.DataSource;
+import
org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactoryTest;
+import
org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
+import org.junit.Test;
+
+public final class YamlShardingSphereDataSourceFactoryTest {
+
+ @Test
+ public void assertCreateDataSourceWithFile() throws Exception {
+ URL url =
YamlShardingSphereDataSourceFactoryTest.class.getResource("/yaml/configWithDataSourceWithRules.yaml");
+ assertNotNull(url);
+ File yamlFile = new File(url.toURI());
+ DataSource dataSource =
YamlShardingSphereDataSourceFactory.createDataSource(yamlFile);
+ assertNotNull(dataSource);
+ assertTrue(dataSource instanceof ShardingSphereDataSource);
+
assertThat(ShardingSphereDataSourceFactoryTest.getDatabaseName(dataSource),
is("logic_db"));
+
+ }
+
+ @Test
+ public void assertCreateDataSourceWithBytes() throws SQLException,
IOException {
+ URL url =
YamlShardingSphereDataSourceFactoryTest.class.getResource("/yaml/configWithDataSourceWithRules.yaml");
+ assertNotNull(url);
+ StringBuilder yamlContent = new StringBuilder();
+ try (
+ FileReader fileReader = new FileReader(url.getFile());
+ BufferedReader reader = new BufferedReader(fileReader)) {
+ String line;
+ while (null != (line = reader.readLine())) {
+ yamlContent.append(line).append(System.lineSeparator());
+ }
+ }
+
+ DataSource dataSource =
YamlShardingSphereDataSourceFactory.createDataSource(yamlContent.toString().getBytes());
+ assertNotNull(dataSource);
+ assertTrue(dataSource instanceof ShardingSphereDataSource);
+
assertThat(ShardingSphereDataSourceFactoryTest.getDatabaseName(dataSource),
is("logic_db"));
+ }
+
+ @Test
+ public void assertCreateDataSourceWithoutDataSource() throws Exception {
+ URL url =
YamlShardingSphereDataSourceFactoryTest.class.getResource("/yaml/configWithoutDataSourceWithRules.yaml");
+ assertNotNull(url);
+ File yamlFile = new File(url.toURI());
+ Map<String, DataSource> dataSourceMap = new HashMap<>();
+ dataSourceMap.put("ds_0", new
MockedDataSource("jdbc:mock:://localhost:3306/logic_ds_01", "root", "root"));
+ dataSourceMap.put("ds_1", new
MockedDataSource("jdbc:mock:://localhost:3306/logic_ds_01", "root", "root"));
+ DataSource dataSource =
YamlShardingSphereDataSourceFactory.createDataSource(dataSourceMap, yamlFile);
+ assertNotNull(dataSource);
+ assertTrue(dataSource instanceof ShardingSphereDataSource);
+
assertThat(ShardingSphereDataSourceFactoryTest.getDatabaseName(dataSource),
is("logic_db"));
+ }
+
+ @Test
+ public void assertCreateDataSourceWithOnlyDataSource() throws Exception {
+ URL url =
YamlShardingSphereDataSourceFactoryTest.class.getResource("/yaml/configWithoutRules.yaml");
+ assertNotNull(url);
+ File yamlFile = new File(url.toURI());
+ MockedDataSource mockedDataSource = new
MockedDataSource("jdbc:mock:://localhost:3306/logic_ds_01", "root", "root");
+ DataSource dataSource =
YamlShardingSphereDataSourceFactory.createDataSource(mockedDataSource,
yamlFile);
+ assertNotNull(dataSource);
+ assertTrue(dataSource instanceof ShardingSphereDataSource);
+
assertThat(ShardingSphereDataSourceFactoryTest.getDatabaseName(dataSource),
is("logic_db"));
+
+ }
+}
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/yaml/configWithDataSourceWithRules.yaml
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/yaml/configWithDataSourceWithRules.yaml
new file mode 100644
index 00000000000..b280e56cf5a
--- /dev/null
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/yaml/configWithDataSourceWithRules.yaml
@@ -0,0 +1,44 @@
+#
+# 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.
+#
+
+databaseName: logic_db
+
+dataSources:
+ ds_0:
+ dataSourceClassName: org.apache.shardingsphere.test.mock.MockedDataSource
+ url: jdbc:mock:://localhost:3306/logic_ds_01
+ ds_1:
+ dataSourceClassName: org.apache.shardingsphere.test.mock.MockedDataSource
+ url: jdbc:mock:://localhost:3306/logic_ds_02
+
+rules:
+- !SHARDING
+ autoTables:
+ t_order:
+ actualDataSources: ds_0,ds_1
+ shardingStrategy:
+ standard:
+ shardingColumn: order_id
+ shardingAlgorithmName: auto-mod
+ shardingAlgorithms:
+ auto-mod:
+ type: MOD
+ props:
+ sharding-count: 4
+
+mode:
+ type: Standalone
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/yaml/configWithoutDataSourceWithRules.yaml
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/yaml/configWithoutDataSourceWithRules.yaml
new file mode 100644
index 00000000000..06e87ef5733
--- /dev/null
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/yaml/configWithoutDataSourceWithRules.yaml
@@ -0,0 +1,36 @@
+#
+# 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.
+#
+
+databaseName: logic_db
+
+rules:
+ - !SHARDING
+ autoTables:
+ t_order:
+ actualDataSources: ds_0,ds_1
+ shardingStrategy:
+ standard:
+ shardingColumn: order_id
+ shardingAlgorithmName: auto-mod
+ shardingAlgorithms:
+ auto-mod:
+ type: MOD
+ props:
+ sharding-count: 4
+
+mode:
+ type: Standalone
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/yaml/configWithoutRules.yaml
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/yaml/configWithoutRules.yaml
new file mode 100644
index 00000000000..492d3f89b0c
--- /dev/null
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/yaml/configWithoutRules.yaml
@@ -0,0 +1,21 @@
+#
+# 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.
+#
+
+databaseName: logic_db
+
+mode:
+ type: Standalone