You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/07/15 15:12:27 UTC
[shardingsphere] branch master updated: Refactor YamlShardingSphereDataSourceFactoryTest (#19250)
This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 d06cae9cf4b Refactor YamlShardingSphereDataSourceFactoryTest (#19250)
d06cae9cf4b is described below
commit d06cae9cf4bd795b7dd24f0f93397c75ee686532
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Jul 15 23:12:17 2022 +0800
Refactor YamlShardingSphereDataSourceFactoryTest (#19250)
---
.../api/ShardingSphereDataSourceFactory.java | 8 +-
.../api/ShardingSphereDataSourceFactoryTest.java | 35 +++----
.../YamlShardingSphereDataSourceFactoryTest.java | 110 +++++++++++----------
.../factory/config-for-factory-test.yaml} | 3 -
.../yaml/configWithoutDataSourceWithRules.yaml | 36 -------
.../test/resources/yaml/configWithoutRules.yaml | 21 ----
.../container/database/OpenGaussContainer.java | 2 +-
.../jaxb/cases/domain/SQLParserTestCases.java | 4 +-
8 files changed, 77 insertions(+), 142 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 57a22ad39ff..a35d414f07a 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
@@ -48,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(getDatabaseNameOrDefault(databaseName), modeConfig);
+ return new ShardingSphereDataSource(getDatabaseName(databaseName), modeConfig);
}
/**
@@ -75,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(getDatabaseNameOrDefault(databaseName), modeConfig, dataSourceMap, null == configs ? new LinkedList<>() : configs, props);
+ return new ShardingSphereDataSource(getDatabaseName(databaseName), modeConfig, dataSourceMap, null == configs ? new LinkedList<>() : configs, props);
}
/**
@@ -106,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(getDatabaseNameOrDefault(databaseName), dataSource), configs, props);
+ return createDataSource(databaseName, modeConfig, Collections.singletonMap(getDatabaseName(databaseName), dataSource), configs, props);
}
/**
@@ -179,7 +179,7 @@ public final class ShardingSphereDataSourceFactory {
return createDataSource((ModeConfiguration) null, dataSource, configs, props);
}
- private static String getDatabaseNameOrDefault(final String databaseName) {
+ private static String getDatabaseName(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
index e689a277a87..b8400a69b68 100644
--- 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
@@ -17,24 +17,24 @@
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 lombok.SneakyThrows;
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 javax.sql.DataSource;
+import java.lang.reflect.Field;
+import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Properties;
+
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);
@@ -50,32 +50,25 @@ public final class ShardingSphereDataSourceFactoryTest {
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());
+ DataSource testDataSource3 = ShardingSphereDataSourceFactory.createDataSource(new HashMap<>(), 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());
+ DataSource testDataSource6 = ShardingSphereDataSourceFactory.createDataSource("test_db6", new HashMap<>(), 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);
+ DataSource testDataSource7 = ShardingSphereDataSourceFactory.createDataSource("test_db6", modeConfig, new HashMap<>(), 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) {
+ private static String getDatabaseName(final DataSource dataSource) {
Field field = ShardingSphereDataSource.class.getDeclaredField("databaseName");
field.setAccessible(true);
- return (String) field.get(shardingSphereDataSource);
+ return (String) field.get(dataSource);
}
}
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
index 8fa6d0ca4a8..7b2d0b96196 100644
--- 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
@@ -17,83 +17,85 @@
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 lombok.SneakyThrows;
+import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
+import org.junit.Test;
+import javax.sql.DataSource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
+import java.lang.reflect.Field;
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;
+import java.util.Objects;
-public final class YamlShardingSphereDataSourceFactoryTest {
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+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"));
-
+ assertDataSource(YamlShardingSphereDataSourceFactory.createDataSource(new File(getYamlFileUrl().toURI())));
}
-
+
@Test
public void assertCreateDataSourceWithBytes() throws SQLException, IOException {
- URL url = YamlShardingSphereDataSourceFactoryTest.class.getResource("/yaml/configWithDataSourceWithRules.yaml");
- assertNotNull(url);
- StringBuilder yamlContent = new StringBuilder();
+ assertDataSource(YamlShardingSphereDataSourceFactory.createDataSource(readFile(getYamlFileUrl()).getBytes()));
+ }
+
+ @Test
+ public void assertCreateDataSourceWithFileForExternalDataSources() throws Exception {
+ Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
+ dataSourceMap.put("ds_0", new MockedDataSource());
+ dataSourceMap.put("ds_1", new MockedDataSource());
+ assertDataSource(YamlShardingSphereDataSourceFactory.createDataSource(dataSourceMap, new File(getYamlFileUrl().toURI())));
+ }
+
+ @Test
+ public void assertCreateDataSourceWithFileForExternalSingleDataSource() throws Exception {
+ assertDataSource(YamlShardingSphereDataSourceFactory.createDataSource(new MockedDataSource(), new File(getYamlFileUrl().toURI())));
+ }
+
+ @Test
+ public void assertCreateDataSourceWithBytesForExternalDataSources() throws Exception {
+ Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
+ dataSourceMap.put("ds_0", new MockedDataSource());
+ dataSourceMap.put("ds_1", new MockedDataSource());
+ assertDataSource(YamlShardingSphereDataSourceFactory.createDataSource(dataSourceMap, readFile(getYamlFileUrl()).getBytes()));
+ }
+
+ @Test
+ public void assertCreateDataSourceWithBytesForExternalSingleDataSource() throws Exception {
+ assertDataSource(YamlShardingSphereDataSourceFactory.createDataSource(new MockedDataSource(), readFile(getYamlFileUrl()).getBytes()));
+ }
+
+ private URL getYamlFileUrl() {
+ return Objects.requireNonNull(YamlShardingSphereDataSourceFactoryTest.class.getResource("/config/factory/config-for-factory-test.yaml"));
+ }
+
+ private String readFile(final URL url) throws IOException {
+ StringBuilder result = 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());
+ result.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"));
+ return result.toString();
}
-
- @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"));
-
+
+ @SneakyThrows(ReflectiveOperationException.class)
+ private void assertDataSource(final DataSource dataSource) {
+ Field field = ShardingSphereDataSource.class.getDeclaredField("databaseName");
+ field.setAccessible(true);
+ assertThat((String) field.get(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/config/factory/config-for-factory-test.yaml
similarity index 98%
rename from shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/yaml/configWithDataSourceWithRules.yaml
rename to shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/config/factory/config-for-factory-test.yaml
index b280e56cf5a..b819bb3f53a 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/yaml/configWithDataSourceWithRules.yaml
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/config/factory/config-for-factory-test.yaml
@@ -39,6 +39,3 @@ rules:
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
deleted file mode 100644
index 06e87ef5733..00000000000
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/yaml/configWithoutDataSourceWithRules.yaml
+++ /dev/null
@@ -1,36 +0,0 @@
-#
-# 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
deleted file mode 100644
index 492d3f89b0c..00000000000
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/resources/yaml/configWithoutRules.yaml
+++ /dev/null
@@ -1,21 +0,0 @@
-#
-# 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
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/framework/container/database/OpenGaussContainer.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/framework/container/database/OpenGaussContainer.java
index f7a6d96952d..54d72fc61e5 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/framework/container/database/OpenGaussContainer.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/framework/container/database/OpenGaussContainer.java
@@ -49,7 +49,7 @@ public final class OpenGaussContainer extends DatabaseContainer {
addEnv("GS_PASSWORD", password);
withClasspathResourceMapping("/env/postgresql/postgresql.conf", "/usr/local/opengauss/share/postgresql/postgresql.conf.sample", BindMode.READ_ONLY);
withClasspathResourceMapping("/env/postgresql/initdb.sql", "/docker-entrypoint-initdb.d/", BindMode.READ_ONLY);
-
+
withPrivilegedMode(true);
withExposedPorts(port);
setWaitStrategy(new JDBCConnectionWaitStrategy(() -> DriverManager.getConnection(DataSourceEnvironment.getURL(DATABASE_TYPE, "localhost", getFirstMappedPort(), "postgres"),
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/SQLParserTestCases.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/SQLParserTestCases.java
index c50480d21b6..2449f6dcb9b 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/SQLParserTestCases.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/SQLParserTestCases.java
@@ -1273,10 +1273,10 @@ public final class SQLParserTestCases {
@XmlElement(name = "alter-sql-parser-rule")
private final List<AlterSQLParserRuleStatementTestCase> alterSQLParserRuleTestCases = new LinkedList<>();
-
+
@XmlElement(name = "alter-local-transaction-rule")
private final List<AlterLocalTransactionRuleStatementTestCase> alterLocalTransactionRuleTestCases = new LinkedList<>();
-
+
@XmlElement(name = "alter-xa-transaction-rule")
private final List<AlterXATransactionRuleStatementTestCase> alterXATransactionRuleTestCases = new LinkedList<>();