Skip to content

Commit

Permalink
Refactor YamlShardingSphereDataSourceFactoryTest (apache#19250)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Jul 15, 2022
1 parent 6a2fba5 commit d06cae9
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -75,7 +75,7 @@ public static DataSource createDataSource(final ModeConfiguration modeConfig) th
*/
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);
}

/**
Expand Down Expand Up @@ -106,7 +106,7 @@ public static DataSource createDataSource(final ModeConfiguration modeConfig,
*/
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);
}

/**
Expand Down Expand Up @@ -179,7 +179,7 @@ public static DataSource createDataSource(final DataSource dataSource, final Col
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -50,32 +50,25 @@ public void assertCreateDataSourceWithDatabaseName() throws SQLException {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,3 @@ rules:
type: MOD
props:
sharding-count: 4

mode:
type: Standalone

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected void configure() {
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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();

Expand Down

0 comments on commit d06cae9

Please sign in to comment.