DataSourceMapSetter { @SuppressWarnings("unchecked") public static Map<String, DataSource> getDataSourceMap(final Environment environment) { Map<String, DataSource> result = new LinkedHashMap<>(); Map<String, Object> dataSourceCommonProps = PropertyUtil.handle(environment, COMMON_PREFIX, Map.class); for (String each : getDataSourceNames(environment, PREFIX)) { try { result.put(each, getDataSource(environment, PREFIX, each, dataSourceCommonProps)); } catch (final ReflectiveOperationException ex) { throw new ShardingSphereException("Can't find data source type.", ex); } catch (final NamingException ex) { throw new ShardingSphereException("Can't find JNDI data source.", ex); } } return result; }  @SuppressWarnings("unchecked") static Map<String, DataSource> getDataSourceMap(final Environment environment);  }
@Test public void assetMergedAll() throws SQLException { MockEnvironment mockEnvironment = new MockEnvironment(); mockEnvironment.setProperty("spring.shardingsphere.datasource.names", "ds0,ds1"); mockEnvironment.setProperty("spring.shardingsphere.datasource.common.type", "org.apache.commons.dbcp2.BasicDataSource"); mockEnvironment.setProperty("spring.shardingsphere.datasource.common.driver-class-name", "org.h2.Driver"); mockEnvironment.setProperty("spring.shardingsphere.datasource.common.max-total", "100"); mockEnvironment.setProperty("spring.shardingsphere.datasource.common.username", "sa"); mockEnvironment.setProperty("spring.shardingsphere.datasource.common.password", ""); mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.url", "jdbc:h2:mem:ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL"); mockEnvironment.setProperty("spring.shardingsphere.datasource.ds1.url", "jdbc:h2:mem:ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL"); StandardEnvironment standardEnvironment = new StandardEnvironment(); standardEnvironment.merge(mockEnvironment); Map<String, DataSource> dataSourceMap = DataSourceMapSetter.getDataSourceMap(standardEnvironment); assertThat(dataSourceMap.size(), is(2)); assertThat(dataSourceMap.get("ds0").getConnection().getMetaData().getURL(), is("jdbc:h2:mem:ds")); assertThat(dataSourceMap.get("ds1").getConnection().getMetaData().getURL(), is("jdbc:h2:mem:ds")); }
@Test public void assetMergedReplaceAndAdd() throws SQLException { MockEnvironment mockEnvironment = new MockEnvironment(); mockEnvironment.setProperty("spring.shardingsphere.datasource.names", "ds0,ds1"); mockEnvironment.setProperty("spring.shardingsphere.datasource.common.type", "org.apache.commons.dbcp2.BasicDataSource"); mockEnvironment.setProperty("spring.shardingsphere.datasource.common.driver-class-name", "org.h2.Driver"); mockEnvironment.setProperty("spring.shardingsphere.datasource.common.max-total", "100"); mockEnvironment.setProperty("spring.shardingsphere.datasource.common.username", "Asa"); mockEnvironment.setProperty("spring.shardingsphere.datasource.common.password", "123"); mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.url", "jdbc:h2:mem:ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL"); mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.username", "sa"); mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.max-total", "50"); mockEnvironment.setProperty("spring.shardingsphere.datasource.ds0.password", ""); mockEnvironment.setProperty("spring.shardingsphere.datasource.ds1.url", "jdbc:h2:mem:ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL"); mockEnvironment.setProperty("spring.shardingsphere.datasource.ds1.username", "sa"); mockEnvironment.setProperty("spring.shardingsphere.datasource.ds1.max-total", "150"); mockEnvironment.setProperty("spring.shardingsphere.datasource.ds1.password", ""); StandardEnvironment standardEnvironment = new StandardEnvironment(); standardEnvironment.merge(mockEnvironment); Map<String, DataSource> dataSourceMap = DataSourceMapSetter.getDataSourceMap(standardEnvironment); assertThat(dataSourceMap.size(), is(2)); assertThat(dataSourceMap.get("ds0").getConnection().getMetaData().getUserName(), is("SA")); assertThat(dataSourceMap.get("ds1").getConnection().getMetaData().getUserName(), is("SA")); }