ShardingSphereDataSource extends AbstractUnsupportedOperationDataSource implements AutoCloseable { @Override public ShardingSphereConnection getConnection() { return new ShardingSphereConnection(getDataSourceMap(), schemaContexts, transactionContexts, TransactionTypeHolder.get()); } ShardingSphereDataSource(final Map<String, DataSource> dataSourceMap, final Collection<RuleConfiguration> configurations, final Properties props); @Override ShardingSphereConnection getConnection(); @Override ShardingSphereConnection getConnection(final String username, final String password); Map<String, DataSource> getDataSourceMap(); @Override void close(); void close(final Collection<String> dataSourceNames);  }
@Test(expected = IllegalStateException.class) public void assertGetDatabaseProductNameWhenDataBaseProductNameDifferent() throws SQLException { DataSource dataSource1 = mockDataSource(DatabaseTypes.getActualDatabaseType("MySQL")); DataSource dataSource2 = mockDataSource(DatabaseTypes.getActualDatabaseType("H2")); Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1); dataSourceMap.put("ds1", dataSource1); dataSourceMap.put("ds2", dataSource2); assertDatabaseProductName(dataSourceMap, dataSource1.getConnection(), dataSource2.getConnection()); }
@Test public void assertGetDatabaseProductName() throws SQLException { DataSource dataSource1 = mockDataSource(DatabaseTypes.getActualDatabaseType("H2")); DataSource dataSource2 = mockDataSource(DatabaseTypes.getActualDatabaseType("H2")); DataSource dataSource3 = mockDataSource(DatabaseTypes.getActualDatabaseType("H2")); Map<String, DataSource> dataSourceMap = new HashMap<>(3, 1); dataSourceMap.put("ds1", dataSource1); dataSourceMap.put("ds2", dataSource2); dataSourceMap.put("ds3", dataSource3); assertDatabaseProductName(dataSourceMap, dataSource1.getConnection(), dataSource2.getConnection(), dataSource3.getConnection()); }
@Test public void assertGetDatabaseProductNameForPrimaryReplicaReplication() throws SQLException { DataSource dataSource1 = mockDataSource(DatabaseTypes.getActualDatabaseType("H2")); DataSource primaryDataSource = mockDataSource(DatabaseTypes.getActualDatabaseType("H2")); DataSource replicaDataSource = mockDataSource(DatabaseTypes.getActualDatabaseType("H2")); DataSource dataSource3 = mockDataSource(DatabaseTypes.getActualDatabaseType("H2")); Map<String, DataSource> dataSourceMap = new HashMap<>(4, 1); dataSourceMap.put("ds1", dataSource1); dataSourceMap.put("primaryDataSource", primaryDataSource); dataSourceMap.put("replicaDataSource", replicaDataSource); dataSourceMap.put("ds3", dataSource3); assertDatabaseProductName(dataSourceMap, dataSource1.getConnection(), primaryDataSource.getConnection(), replicaDataSource.getConnection()); }
@Test public void assertGetConnection() throws SQLException { DataSource dataSource = mockDataSource(DatabaseTypes.getActualDatabaseType("H2")); Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1); dataSourceMap.put("ds", dataSource); assertThat(createShardingSphereDataSource(dataSourceMap).getConnection().getConnection("ds"), is(dataSource.getConnection())); }