Skip to content

Commit

Permalink
Use MockedDataSource instead of mock(DataSource.class) (#28635)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Oct 4, 2023
1 parent b5b5c32 commit 14d2274
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.shardingsphere.readwritesplitting.exception.checker.DuplicateDataSourceException;
import org.apache.shardingsphere.readwritesplitting.exception.checker.InvalidWeightLoadBalancerConfigurationException;
import org.apache.shardingsphere.readwritesplitting.exception.checker.MissingRequiredWriteDataSourceNameException;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -127,10 +128,10 @@ private ReadwriteSplittingDataSourceRuleConfiguration createDataSourceRuleConfig

private Map<String, DataSource> mockDataSources() {
Map<String, DataSource> result = new LinkedHashMap<>(2, 1F);
result.put("read_ds_0", mock(DataSource.class));
result.put("read_ds_1", mock(DataSource.class));
result.put("write_ds_0", mock(DataSource.class));
result.put("write_ds_1", mock(DataSource.class));
result.put("read_ds_0", new MockedDataSource());
result.put("read_ds_1", new MockedDataSource());
result.put("write_ds_0", new MockedDataSource());
result.put("write_ds_1", new MockedDataSource());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

import static org.mockito.Mockito.mock;

class ShadowRuleConfigurationCheckerTest {

@Test
Expand All @@ -43,42 +41,21 @@ void assertCheck() {

private Map<String, DataSource> createDataSourceMap() {
Map<String, DataSource> result = new LinkedHashMap<>(2, 1F);
result.put("ds", mock(DataSource.class));
result.put("ds_shadow", mock(DataSource.class));
result.put("ds", new MockedDataSource());
result.put("ds_shadow", new MockedDataSource());
return result;
}

private ShadowRuleConfiguration createShadowRuleConfiguration() {
ShadowRuleConfiguration result = new ShadowRuleConfiguration();
result.setShadowAlgorithms(createShadowAlgorithmConfigurations());
result.setDataSources(createDataSources());
result.setTables(createTables());
return result;
}

private Map<String, AlgorithmConfiguration> createShadowAlgorithmConfigurations() {
Map<String, AlgorithmConfiguration> result = new LinkedHashMap<>();
result.put("user-id-insert-match-algorithm", createAlgorithmConfiguration());
result.setShadowAlgorithms(Collections.singletonMap("user-id-insert-match-algorithm", createAlgorithmConfiguration()));
result.setDataSources(Collections.singleton(new ShadowDataSourceConfiguration("shadow-data-source", "ds", "ds_shadow")));
result.setTables(Collections.singletonMap("t_order", new ShadowTableConfiguration(new LinkedList<>(), new LinkedList<>(Collections.singleton("user-id-insert-match-algorithm")))));
return result;
}

private AlgorithmConfiguration createAlgorithmConfiguration() {
return new AlgorithmConfiguration("user-id-insert-match-algorithm",
PropertiesBuilder.build(new Property("column", "shadow"), new Property("operation", "insert"), new Property("regex", "[1]")));
}

private Map<String, ShadowTableConfiguration> createTables() {
Map<String, ShadowTableConfiguration> result = new LinkedHashMap<>();
Collection<String> dataSourceNames = new LinkedList<>();
Collection<String> shadowAlgorithmNames = new LinkedList<>();
shadowAlgorithmNames.add("user-id-insert-match-algorithm");
result.put("t_order", new ShadowTableConfiguration(dataSourceNames, shadowAlgorithmNames));
return result;
}

private Collection<ShadowDataSourceConfiguration> createDataSources() {
Collection<ShadowDataSourceConfiguration> result = new LinkedList<>();
result.add(new ShadowDataSourceConfiguration("shadow-data-source", "ds", "ds_shadow"));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -47,7 +47,6 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(AutoMockExtension.class)
Expand All @@ -60,7 +59,7 @@ class GenericSchemaBuilderTest {
void setUp() {
DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
material = new GenericSchemaBuilderMaterial(databaseType, Collections.singletonMap(DefaultDatabase.LOGIC_NAME, databaseType),
Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mock(DataSource.class)),
Collections.singletonMap(DefaultDatabase.LOGIC_NAME, new MockedDataSource()),
Collections.singleton(new TableContainedFixtureRule()), new ConfigurationProperties(new Properties()), DefaultDatabase.LOGIC_NAME);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

class DataSourcePoolDestroyerTest {

@Test
void assertAsyncDestroyWithoutAutoCloseableDataSource() {
assertDoesNotThrow(() -> new DataSourcePoolDestroyer(mock(DataSource.class)).asyncDestroy());
assertDoesNotThrow(() -> new DataSourcePoolDestroyer(new MockedDataSource()).asyncDestroy());
}

@Test
Expand Down

0 comments on commit 14d2274

Please sign in to comment.