This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 4c32b3f Recognize JNDI data source configuration in cluster mode
(#15238)
4c32b3f is described below
commit 4c32b3f05f34c1b89344025e072b7efacc208138
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Feb 3 16:29:47 2022 +0800
Recognize JNDI data source configuration in cluster mode (#15238)
---
.../infra/config/DatabaseAccessConfiguration.java | 33 ----------
.../infra/metadata/ShardingSphereMetaData.java | 20 +-----
.../metadata/resource/DataSourcesMetaData.java | 14 ++--
.../metadata/resource/DataSourcesMetaDataTest.java | 42 +++++++-----
.../adapter/AbstractDataSourceAdapterTest.java | 16 +----
.../datasource/ShardingSphereDataSourceTest.java | 45 +++----------
.../spring/boot/datasource/AopProxyUtils.java | 76 ++++++++++++++++++++++
.../boot/datasource/DataSourceMapSetter.java | 2 +-
.../mode/manager/ContextManagerTest.java | 31 ++++-----
.../distsql/rql/DataSourceQueryResultSetTest.java | 24 ++-----
.../shardingsphere/test/mock/MockedDataSource.java | 22 +++++--
11 files changed, 159 insertions(+), 166 deletions(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/DatabaseAccessConfiguration.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/DatabaseAccessConfiguration.java
deleted file mode 100644
index 6d2fd32..0000000
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/DatabaseAccessConfiguration.java
+++ /dev/null
@@ -1,33 +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.
- */
-
-package org.apache.shardingsphere.infra.config;
-
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-
-/**
- * Database access configuration.
- */
-@RequiredArgsConstructor
-@Getter
-public final class DatabaseAccessConfiguration {
-
- private final String url;
-
- private final String username;
-}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java
index 5ab024c..7a45380 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.infra.metadata;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.config.DatabaseAccessConfiguration;
import org.apache.shardingsphere.infra.config.schema.SchemaConfiguration;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRecognizer;
@@ -32,12 +31,9 @@ import
org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import javax.sql.DataSource;
import java.sql.Connection;
-import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Collection;
-import java.util.LinkedHashMap;
import java.util.Map;
-import java.util.Map.Entry;
import java.util.Optional;
/**
@@ -74,21 +70,9 @@ public final class ShardingSphereMetaData {
private static ShardingSphereResource createResource(final Map<String,
DataSource> dataSourceMap) throws SQLException {
DatabaseType databaseType =
DatabaseTypeRecognizer.getDatabaseType(dataSourceMap.values());
- DataSourcesMetaData dataSourceMetas = new
DataSourcesMetaData(databaseType,
createDatabaseAccessConfigurationMap(dataSourceMap));
+ DataSourcesMetaData dataSourcesMetaData = new
DataSourcesMetaData(databaseType, dataSourceMap);
CachedDatabaseMetaData cachedDatabaseMetaData =
createCachedDatabaseMetaData(dataSourceMap).orElse(null);
- return new ShardingSphereResource(dataSourceMap, dataSourceMetas,
cachedDatabaseMetaData, databaseType);
- }
-
- private static Map<String, DatabaseAccessConfiguration>
createDatabaseAccessConfigurationMap(final Map<String, DataSource>
dataSourceMap) throws SQLException {
- Map<String, DatabaseAccessConfiguration> result = new
LinkedHashMap<>(dataSourceMap.size(), 1);
- for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
- DataSource dataSource = entry.getValue();
- try (Connection connection = dataSource.getConnection()) {
- DatabaseMetaData metaData = connection.getMetaData();
- result.put(entry.getKey(), new
DatabaseAccessConfiguration(metaData.getURL(), metaData.getUserName()));
- }
- }
- return result;
+ return new ShardingSphereResource(dataSourceMap, dataSourcesMetaData,
cachedDatabaseMetaData, databaseType);
}
private static Optional<CachedDatabaseMetaData>
createCachedDatabaseMetaData(final Map<String, DataSource> dataSources) throws
SQLException {
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/resource/DataSourcesMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/resource/DataSourcesMetaData.java
index 1defcea..e179a52 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/resource/DataSourcesMetaData.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/resource/DataSourcesMetaData.java
@@ -19,14 +19,14 @@ package org.apache.shardingsphere.infra.metadata.resource;
import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.infra.config.DatabaseAccessConfiguration;
+import
org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesCreator;
+import javax.sql.DataSource;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.stream.Collectors;
/**
* Data sources meta data.
@@ -35,10 +35,12 @@ public final class DataSourcesMetaData {
private final Map<String, DataSourceMetaData> dataSourceMetaDataMap;
- public DataSourcesMetaData(final DatabaseType databaseType, final
Map<String, DatabaseAccessConfiguration> databaseAccessConfigs) {
- dataSourceMetaDataMap =
databaseAccessConfigs.entrySet().stream().collect(
- Collectors.toMap(Entry::getKey, entry ->
databaseType.getDataSourceMetaData(entry.getValue().getUrl(),
- entry.getValue().getUsername()), (oldValue,
currentValue) -> oldValue, LinkedHashMap::new));
+ public DataSourcesMetaData(final DatabaseType databaseType, final
Map<String, DataSource> dataSourceMap) {
+ dataSourceMetaDataMap = new LinkedHashMap<>(dataSourceMap.size(), 1);
+ for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
+ Map<String, Object> standardProps =
DataSourcePropertiesCreator.create(entry.getValue()).getConnectionPropertySynonyms().getStandardProperties();
+ dataSourceMetaDataMap.put(entry.getKey(),
databaseType.getDataSourceMetaData(standardProps.get("url").toString(),
standardProps.get("username").toString()));
+ }
}
/**
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/resource/DataSourcesMetaDataTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/resource/DataSourcesMetaDataTest.java
index b362197..a143c7f 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/resource/DataSourcesMetaDataTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/resource/DataSourcesMetaDataTest.java
@@ -17,10 +17,11 @@
package org.apache.shardingsphere.infra.metadata.resource;
-import org.apache.shardingsphere.infra.config.DatabaseAccessConfiguration;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
import org.junit.Test;
+import javax.sql.DataSource;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@@ -35,10 +36,10 @@ public final class DataSourcesMetaDataTest {
@Test
public void
assertGetAllInstanceDataSourceNamesForShardingRuleByDifferentDataSource() {
- Map<String, DatabaseAccessConfiguration>
databaseAccessConfigurationMap = new HashMap<>(2, 1);
- databaseAccessConfigurationMap.put("ds_0", new
DatabaseAccessConfiguration("jdbc:mysql://127.0.0.1:3306/db_0", "test"));
- databaseAccessConfigurationMap.put("ds_1", new
DatabaseAccessConfiguration("jdbc:mysql://127.0.0.1:3307/db_1", "test"));
- DataSourcesMetaData dataSourcesMetaData = new
DataSourcesMetaData(DatabaseTypeRegistry.getActualDatabaseType("MySQL"),
databaseAccessConfigurationMap);
+ Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
+ dataSourceMap.put("ds_0",
createDataSource("jdbc:mysql://127.0.0.1:3306/db_0"));
+ dataSourceMap.put("ds_1",
createDataSource("jdbc:mysql://127.0.0.1:3307/db_1"));
+ DataSourcesMetaData dataSourcesMetaData = new
DataSourcesMetaData(DatabaseTypeRegistry.getActualDatabaseType("MySQL"),
dataSourceMap);
Collection<String> allInstanceDataSourceNames =
dataSourcesMetaData.getAllInstanceDataSourceNames();
assertNotNull(allInstanceDataSourceNames);
assertThat(allInstanceDataSourceNames.size(), is(2));
@@ -47,10 +48,10 @@ public final class DataSourcesMetaDataTest {
@Test
public void
assertGetAllInstanceDataSourceNamesForShardingRuleBySameDataSource() {
- Map<String, DatabaseAccessConfiguration>
databaseAccessConfigurationMap = new HashMap<>(2, 1);
- databaseAccessConfigurationMap.put("ds_0", new
DatabaseAccessConfiguration("jdbc:mysql://127.0.0.1:3306/db_0", "test"));
- databaseAccessConfigurationMap.put("ds_1", new
DatabaseAccessConfiguration("jdbc:mysql://127.0.0.1:3306/db_1", "test"));
- DataSourcesMetaData dataSourcesMetaData = new
DataSourcesMetaData(DatabaseTypeRegistry.getActualDatabaseType("MySQL"),
databaseAccessConfigurationMap);
+ Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
+ dataSourceMap.put("ds_0",
createDataSource("jdbc:mysql://127.0.0.1:3306/db_0"));
+ dataSourceMap.put("ds_1",
createDataSource("jdbc:mysql://127.0.0.1:3306/db_1"));
+ DataSourcesMetaData dataSourcesMetaData = new
DataSourcesMetaData(DatabaseTypeRegistry.getActualDatabaseType("MySQL"),
dataSourceMap);
Collection<String> allInstanceDataSourceNames =
dataSourcesMetaData.getAllInstanceDataSourceNames();
assertNotNull(allInstanceDataSourceNames);
assertThat(allInstanceDataSourceNames.size(), is(1));
@@ -59,19 +60,26 @@ public final class DataSourcesMetaDataTest {
@Test
public void assertGetActualCatalogForShardingRule() {
- Map<String, DatabaseAccessConfiguration>
databaseAccessConfigurationMap = new HashMap<>(2, 1);
- databaseAccessConfigurationMap.put("ds_0", new
DatabaseAccessConfiguration("jdbc:mysql://127.0.0.1:3306/db_0", "test"));
- databaseAccessConfigurationMap.put("ds_1", new
DatabaseAccessConfiguration("jdbc:mysql://127.0.0.1:3306/db_1", "test"));
- DataSourcesMetaData dataSourcesMetaData = new
DataSourcesMetaData(DatabaseTypeRegistry.getActualDatabaseType("MySQL"),
databaseAccessConfigurationMap);
+ Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
+ dataSourceMap.put("ds_0",
createDataSource("jdbc:mysql://127.0.0.1:3306/db_0"));
+ dataSourceMap.put("ds_1",
createDataSource("jdbc:mysql://127.0.0.1:3306/db_1"));
+ DataSourcesMetaData dataSourcesMetaData = new
DataSourcesMetaData(DatabaseTypeRegistry.getActualDatabaseType("MySQL"),
dataSourceMap);
assertThat(dataSourcesMetaData.getDataSourceMetaData("ds_0").getCatalog(),
is("db_0"));
}
@Test
public void assertGetActualSchemaNameForShardingRuleForMysql() {
- Map<String, DatabaseAccessConfiguration>
databaseAccessConfigurationMap = new HashMap<>(2, 1);
- databaseAccessConfigurationMap.put("ds_0", new
DatabaseAccessConfiguration("jdbc:mysql://127.0.0.1:3306/db_0", "test"));
- databaseAccessConfigurationMap.put("ds_1", new
DatabaseAccessConfiguration("jdbc:mysql://127.0.0.1:3306/db_1", "test"));
- DataSourcesMetaData dataSourcesMetaData = new
DataSourcesMetaData(DatabaseTypeRegistry.getActualDatabaseType("MySQL"),
databaseAccessConfigurationMap);
+ Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
+ dataSourceMap.put("ds_0",
createDataSource("jdbc:mysql://127.0.0.1:3306/db_0"));
+ dataSourceMap.put("ds_1",
createDataSource("jdbc:mysql://127.0.0.1:3306/db_1"));
+ DataSourcesMetaData dataSourcesMetaData = new
DataSourcesMetaData(DatabaseTypeRegistry.getActualDatabaseType("MySQL"),
dataSourceMap);
assertNull(dataSourcesMetaData.getDataSourceMetaData("ds_0").getSchema());
}
+
+ private MockedDataSource createDataSource(final String url) {
+ MockedDataSource result = new MockedDataSource();
+ result.setUrl(url);
+ result.setUsername("test");
+ return result;
+ }
}
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractDataSourceAdapterTest.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractDataSourceAdapterTest.java
index 1f6ef08..fd3d071 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractDataSourceAdapterTest.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/adapter/AbstractDataSourceAdapterTest.java
@@ -22,16 +22,14 @@ import
org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.database.DefaultSchema;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
import org.junit.Before;
import org.junit.Test;
-import javax.sql.DataSource;
import java.io.PrintWriter;
-import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
-import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
@@ -39,9 +37,6 @@ import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public final class AbstractDataSourceAdapterTest {
@@ -49,14 +44,7 @@ public final class AbstractDataSourceAdapterTest {
@Before
public void setUp() throws SQLException {
- shardingSphereDataSource = new
ShardingSphereDataSource(DefaultSchema.LOGIC_NAME, null, getDataSource(),
getRuleConfigurations(), new Properties());
- }
-
- private Map<String, DataSource> getDataSource() throws SQLException {
- DataSource dataSource = mock(DataSource.class, RETURNS_DEEP_STUBS);
-
when(dataSource.getConnection().getMetaData()).thenReturn(mock(DatabaseMetaData.class,
RETURNS_DEEP_STUBS));
-
when(dataSource.getConnection().getMetaData().getURL()).thenReturn("jdbc:mysql://localhost:3306/test");
- return Collections.singletonMap("ds", dataSource);
+ shardingSphereDataSource = new
ShardingSphereDataSource(DefaultSchema.LOGIC_NAME, null,
Collections.singletonMap("ds", new MockedDataSource()),
getRuleConfigurations(), new Properties());
}
private Collection<RuleConfiguration> getRuleConfigurations() {
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
index 91f0d68..f988d30 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
@@ -23,17 +23,14 @@ import
org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.database.DefaultSchema;
import
org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
import org.apache.shardingsphere.infra.state.StateType;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
import org.apache.shardingsphere.transaction.core.TransactionTypeHolder;
import org.junit.After;
import org.junit.Test;
-import org.mockito.ArgumentMatchers;
import javax.sql.DataSource;
import java.sql.Connection;
-import java.sql.DatabaseMetaData;
-import java.sql.ResultSet;
import java.sql.SQLException;
-import java.sql.Statement;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
@@ -66,49 +63,25 @@ public final class ShardingSphereDataSourceTest {
@Test
public void assertNewConstructorWithAllArguments() throws SQLException {
- ShardingSphereDataSource actual =
createShardingSphereDataSource(mockDataSource());
+ Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+
when(connection.getMetaData().getURL()).thenReturn("jdbc:mock://127.0.0.1/foo_ds");
+ ShardingSphereDataSource actual = createShardingSphereDataSource(new
MockedDataSource(connection));
assertThat(actual.getSchemaName(), is(DefaultSchema.LOGIC_NAME));
assertNotNull(actual.getContextManager());
assertTrue(actual.getContextManager().getMetaDataContexts().getMetaDataMap().containsKey(DefaultSchema.LOGIC_NAME));
assertTrue(actual.getContextManager().getTransactionContexts().getEngines().containsKey(DefaultSchema.LOGIC_NAME));
assertThat(actual.getContextManager().getInstanceContext().getState().getCurrentState(),
is(StateType.OK));
assertThat(actual.getContextManager().getDataSourceMap(DefaultSchema.LOGIC_NAME).size(),
is(1));
- DataSource ds =
actual.getContextManager().getDataSourceMap(DefaultSchema.LOGIC_NAME).get("ds");
- assertNotNull(ds);
- assertThat(ds.getConnection().getMetaData().getURL(),
is("jdbc:h2:mem:demo_ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"));
+
assertThat(actual.getContextManager().getDataSourceMap(DefaultSchema.LOGIC_NAME).get("ds").getConnection().getMetaData().getURL(),
is("jdbc:mock://127.0.0.1/foo_ds"));
}
@Test
public void assertGetConnectionWithUsernameAndPassword() throws
SQLException {
- DataSource dataSource = mockDataSource();
- assertThat(((ShardingSphereConnection)
createShardingSphereDataSource(dataSource).getConnection("",
"")).getConnectionManager().getConnections("ds", 1,
ConnectionMode.MEMORY_STRICTLY).get(0),
- is(dataSource.getConnection()));
- }
-
- private DataSource mockDataSource() throws SQLException {
- DataSource result = mock(DataSource.class);
Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
- DatabaseMetaData databaseMetaData = mockDatabaseMetaData();
- Statement statement = mock(Statement.class);
- ResultSet resultSet = mock(ResultSet.class);
- when(resultSet.next()).thenReturn(false);
- when(statement.getResultSet()).thenReturn(resultSet);
- when(result.getConnection()).thenReturn(connection);
- when(connection.getMetaData()).thenReturn(databaseMetaData);
- when(statement.getConnection()).thenReturn(connection);
- when(connection.createStatement()).thenReturn(statement);
-
when(statement.executeQuery(ArgumentMatchers.any())).thenReturn(resultSet);
-
when(statement.getConnection().getMetaData().getTables(ArgumentMatchers.any(),
ArgumentMatchers.any(), ArgumentMatchers.any(),
ArgumentMatchers.any())).thenReturn(resultSet);
-
when(result.getConnection().getMetaData().getURL()).thenReturn("jdbc:h2:mem:demo_ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
- return result;
- }
-
- private DatabaseMetaData mockDatabaseMetaData() throws SQLException {
- DatabaseMetaData result = mock(DatabaseMetaData.class,
RETURNS_DEEP_STUBS);
- when(result.getColumns(null, null, "table_0",
"%")).thenReturn(mock(ResultSet.class));
- when(result.getPrimaryKeys(null, null,
"table_0")).thenReturn(mock(ResultSet.class));
- when(result.getIndexInfo(null, null, "table_0", false,
false)).thenReturn(mock(ResultSet.class));
- return result;
+
when(connection.getMetaData().getURL()).thenReturn("jdbc:mock://127.0.0.1/foo_ds");
+ assertThat(((ShardingSphereConnection) createShardingSphereDataSource(
+ new MockedDataSource(connection)).getConnection("",
"")).getConnectionManager().getConnections("ds", 1,
ConnectionMode.MEMORY_STRICTLY).get(0),
+ is(connection));
}
private ShardingSphereDataSource createShardingSphereDataSource(final
DataSource dataSource) throws SQLException {
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/datasource/AopProxyUtils.java
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/datasource/AopProxyUtils.java
new file mode 100644
index 0000000..1edb8bd
--- /dev/null
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/datasource/AopProxyUtils.java
@@ -0,0 +1,76 @@
+/*
+ * 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.
+ */
+
+package org.apache.shardingsphere.spring.boot.datasource;
+
+import lombok.SneakyThrows;
+import org.springframework.aop.framework.AdvisedSupport;
+import org.springframework.aop.framework.AopProxy;
+import org.springframework.aop.support.AopUtils;
+
+import java.lang.reflect.Field;
+
+/**
+ * Aop proxy utils.
+ */
+public final class AopProxyUtils {
+
+ /**
+ * Get target object.
+ *
+ * @param proxy proxy object
+ * @return target object
+ */
+ @SneakyThrows
+ public static Object getTarget(final Object proxy) {
+ if (AopUtils.isJdkDynamicProxy(proxy)) {
+ return getJdkDynamicProxyTargetObject(proxy);
+ }
+ if (AopUtils.isCglibProxy(proxy)) {
+ return getCglibProxyTargetObject(proxy);
+ }
+ return proxy;
+ }
+
+ private static Object getJdkDynamicProxyTargetObject(final Object proxy)
throws Exception {
+ AopProxy aopProxy = getJdkDynamicAopProxy(proxy);
+ Field advisedField = aopProxy.getClass().getDeclaredField("advised");
+ advisedField.setAccessible(true);
+ return ((AdvisedSupport)
advisedField.get(aopProxy)).getTargetSource().getTarget();
+ }
+
+ @SneakyThrows(ReflectiveOperationException.class)
+ private static AopProxy getJdkDynamicAopProxy(final Object proxy) {
+ Field hField = proxy.getClass().getSuperclass().getDeclaredField("h");
+ hField.setAccessible(true);
+ return (AopProxy) hField.get(proxy);
+ }
+
+ private static Object getCglibProxyTargetObject(final Object proxy) throws
Exception {
+ Object advisedInterceptor = getCglibAdvisedInterceptor(proxy);
+ Field advisedField =
advisedInterceptor.getClass().getDeclaredField("advised");
+ advisedField.setAccessible(true);
+ return ((AdvisedSupport)
advisedField.get(advisedInterceptor)).getTargetSource().getTarget();
+ }
+
+ @SneakyThrows(ReflectiveOperationException.class)
+ private static Object getCglibAdvisedInterceptor(final Object proxy) {
+ Field callbackField =
proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0");
+ callbackField.setAccessible(true);
+ return callbackField.get(proxy);
+ }
+}
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/datasource/DataSourceMapSetter.java
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/datasource/DataSourceMapSetter.java
index 39b242f..7b06b7b 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/datasource/DataSourceMapSetter.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/datasource/DataSourceMapSetter.java
@@ -101,6 +101,6 @@ public final class DataSourceMapSetter {
bean.setJndiName(jndiName);
bean.setProxyInterface(DataSource.class);
bean.afterPropertiesSet();
- return (DataSource) bean.getObject();
+ return (DataSource) AopProxyUtils.getTarget(bean.getObject());
}
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
index acdaac1..309f2d9 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java
@@ -233,17 +233,17 @@ public final class ContextManagerTest {
Properties dataSourceProps = new Properties();
dataSourceProps.put("jdbcUrl", "jdbc:mock://127.0.0.1/foo_ds");
Map<String, DataSourceProperties> result = new LinkedHashMap<>();
- result.put("test_ds_1", new
DataSourceProperties(MockedDataSource.class.getName(),
createProperties(dataSourceProps)));
- result.put("test_ds_2", new
DataSourceProperties(MockedDataSource.class.getName(),
createProperties(dataSourceProps)));
+ result.put("foo_ds_1", new
DataSourceProperties(MockedDataSource.class.getName(),
createProperties(dataSourceProps)));
+ result.put("foo_ds_2", new
DataSourceProperties(MockedDataSource.class.getName(),
createProperties(dataSourceProps)));
return result;
}
private void assertAddedDataSources(final Map<String, DataSource> actual) {
assertThat(contextManager.getMetaDataContexts().getMetaDataMap().get("test_schema").getResource().getDataSources().size(),
is(2));
- assertTrue(actual.containsKey("test_ds_1"));
- assertDataSource((MockedDataSource) actual.get("test_ds_1"));
- assertTrue(actual.containsKey("test_ds_2"));
- assertDataSource((MockedDataSource) actual.get("test_ds_2"));
+ assertTrue(actual.containsKey("foo_ds_1"));
+ assertDataSource((MockedDataSource) actual.get("foo_ds_1"));
+ assertTrue(actual.containsKey("foo_ds_2"));
+ assertDataSource((MockedDataSource) actual.get("foo_ds_2"));
}
@Test
@@ -263,28 +263,25 @@ public final class ContextManagerTest {
metaDataMap.put("test_schema", originalMetaData);
when(metaDataContexts.getMetaDataMap()).thenReturn(metaDataMap);
when(metaDataContexts.getMetaData("test_schema")).thenReturn(originalMetaData);
- Properties properties = new Properties();
- ConfigurationProperties configurationProperties = new
ConfigurationProperties(properties);
- when(metaDataContexts.getProps()).thenReturn(configurationProperties);
+ when(metaDataContexts.getProps()).thenReturn(new
ConfigurationProperties(new Properties()));
ShardingSphereRuleMetaData globalShardingSphereRuleMetaData =
mock(ShardingSphereRuleMetaData.class);
- List<RuleConfiguration> globalRuleConfigurations = new LinkedList<>();
-
when(globalShardingSphereRuleMetaData.getConfigurations()).thenReturn(globalRuleConfigurations);
+
when(globalShardingSphereRuleMetaData.getConfigurations()).thenReturn(new
LinkedList<>());
when(metaDataContexts.getGlobalRuleMetaData()).thenReturn(globalShardingSphereRuleMetaData);
when(metaDataContexts.getOptimizerContext()).thenReturn(mock(OptimizerContext.class));
when(metaDataContexts.getOptimizerContext().getFederationMetaData()).thenReturn(mock(FederationMetaData.class));
when(metaDataContexts.getOptimizerContext().getFederationMetaData().getSchemas()).thenReturn(new
LinkedHashMap<>());
when(metaDataContexts.getOptimizerContext().getParserContexts()).thenReturn(new
LinkedHashMap<>());
contextManager.alterResource("test_schema",
createToBeAlteredDataSourceProperties());
- assertAlteredDataSource((MockedDataSource)
contextManager.getMetaDataContexts().getMetaDataMap().get("test_schema").getResource().getDataSources().get("test_ds"));
+ assertAlteredDataSource((MockedDataSource)
contextManager.getMetaDataContexts().getMetaDataMap().get("test_schema").getResource().getDataSources().get("foo_ds"));
}
private Map<String, DataSourceProperties>
createToBeAlteredDataSourceProperties() {
Properties dataSourceProps = new Properties();
- dataSourceProps.put("jdbcUrl", "jdbc:mock://127.0.0.1/foo_ds");
+ dataSourceProps.put("url", "jdbc:mock://127.0.0.1/foo_ds");
dataSourceProps.put("username", "test");
dataSourceProps.put("password", "test");
Map<String, DataSourceProperties> result = new LinkedHashMap<>();
- result.put("test_ds", new
DataSourceProperties(MockedDataSource.class.getName(),
createProperties(dataSourceProps)));
+ result.put("foo_ds", new
DataSourceProperties(MockedDataSource.class.getName(),
createProperties(dataSourceProps)));
return result;
}
@@ -412,7 +409,7 @@ public final class ContextManagerTest {
when(testDataSource.getConnection().getMetaData()).thenReturn(mock(DatabaseMetaData.class));
when(testDataSource.getConnection().getMetaData().getURL()).thenReturn("jdbc:mock://127.0.0.1/foo_ds");
Map<String, DataSource> dataSources = new LinkedHashMap<>();
- dataSources.put("test_ds", testDataSource);
+ dataSources.put("foo_ds", testDataSource);
ShardingSphereResource originalResource =
mock(ShardingSphereResource.class);
when(originalResource.getDataSources()).thenReturn(dataSources);
ShardingSphereRuleMetaData originalRuleMetaData =
mock(ShardingSphereRuleMetaData.class);
@@ -435,9 +432,9 @@ public final class ContextManagerTest {
verify(schemaMetaDataPersistService,
times(1)).persist(eq("test_schema"), any(ShardingSphereSchema.class));
contextManager.reloadMetaData("test_schema", "test_table");
assertNotNull(contextManager.getMetaDataContexts().getMetaData("test_schema"));
- contextManager.reloadMetaData("test_schema", "test_table", "test_ds");
+ contextManager.reloadMetaData("test_schema", "test_table", "foo_ds");
assertNotNull(contextManager.getMetaDataContexts().getMetaData("test_schema").getSchema());
-
assertTrue(contextManager.getMetaDataContexts().getMetaData("test_schema").getResource().getDataSources().containsKey("test_ds"));
+
assertTrue(contextManager.getMetaDataContexts().getMetaData("test_schema").getResource().getDataSources().containsKey("foo_ds"));
}
private Map<String, Object> createProperties(final Properties
dataSourceProps) {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/DataSourceQueryResultSetTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/DataSourceQueryResultSetTest.java
index 8248a3b..160d5c5 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/DataSourceQueryResultSetTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/DataSourceQueryResultSetTest.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.proxy.backend.text.distsql.rql;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
-import org.apache.shardingsphere.infra.config.DatabaseAccessConfiguration;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.distsql.query.DistSQLResultSet;
@@ -33,11 +32,9 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
-import javax.sql.DataSource;
import java.util.Collection;
-import java.util.HashMap;
+import java.util.Collections;
import java.util.Iterator;
-import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -53,24 +50,11 @@ public final class DataSourceQueryResultSetTest {
@Before
public void before() {
DatabaseType databaseType = new MySQLDatabaseType();
- DataSourcesMetaData dataSourcesMetaData = new
DataSourcesMetaData(databaseType, createDatabaseAccessConfigurationMap());
- ShardingSphereResource resource = new
ShardingSphereResource(createDataSourceMap(), dataSourcesMetaData, null,
databaseType);
+ DataSourcesMetaData dataSourcesMetaData = new
DataSourcesMetaData(databaseType, Collections.singletonMap("foo_ds",
createDataSource()));
+ ShardingSphereResource resource = new
ShardingSphereResource(Collections.singletonMap("foo_ds", createDataSource()),
dataSourcesMetaData, null, databaseType);
when(shardingSphereMetaData.getResource()).thenReturn(resource);
}
- private Map<String, DatabaseAccessConfiguration>
createDatabaseAccessConfigurationMap() {
- Map<String, DatabaseAccessConfiguration> result = new HashMap<>(1, 1);
- result.put("ds_0", new
DatabaseAccessConfiguration("jdbc:mysql://localhost/demo_ds", "root"));
- return result;
- }
-
- private Map<String, DataSource> createDataSourceMap() {
- Map<String, DataSource> result = new HashMap<>(1, 1);
- MockedDataSource ds0 = createDataSource();
- result.put("ds_0", ds0);
- return result;
- }
-
private MockedDataSource createDataSource() {
MockedDataSource result = new MockedDataSource();
result.setUrl("jdbc:mysql://localhost/demo_ds");
@@ -88,7 +72,7 @@ public final class DataSourceQueryResultSetTest {
Collection<Object> actual = resultSet.getRowData();
assertThat(actual.size(), is(12));
Iterator<Object> rowData = actual.iterator();
- assertThat(rowData.next(), is("ds_0"));
+ assertThat(rowData.next(), is("foo_ds"));
assertThat(rowData.next(), is("MySQL"));
assertThat(rowData.next(), is("localhost"));
assertThat(rowData.next(), is(3306));
diff --git
a/shardingsphere-test/shardingsphere-test-common/src/main/java/org/apache/shardingsphere/test/mock/MockedDataSource.java
b/shardingsphere-test/shardingsphere-test-common/src/main/java/org/apache/shardingsphere/test/mock/MockedDataSource.java
index 0d6adf2..c752e3f 100644
---
a/shardingsphere-test/shardingsphere-test-common/src/main/java/org/apache/shardingsphere/test/mock/MockedDataSource.java
+++
b/shardingsphere-test/shardingsphere-test-common/src/main/java/org/apache/shardingsphere/test/mock/MockedDataSource.java
@@ -17,7 +17,9 @@
package org.apache.shardingsphere.test.mock;
+import lombok.AccessLevel;
import lombok.Getter;
+import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.sql.DataSource;
@@ -36,17 +38,18 @@ import static org.mockito.Mockito.when;
/**
* Mocked data source.
*/
+@NoArgsConstructor
@Getter
@Setter
public final class MockedDataSource implements DataSource {
private String driverClassName;
- private String url;
+ private String url = "jdbc:mock://127.0.0.1/foo_ds";
- private String username;
+ private String username = "root";
- private String password;
+ private String password = "root";
private Integer maxPoolSize;
@@ -56,11 +59,22 @@ public final class MockedDataSource implements DataSource {
private Properties jdbcUrlProperties;
+ @Getter(AccessLevel.NONE)
+ @Setter(AccessLevel.NONE)
+ private Connection connection;
+
+ public MockedDataSource(final Connection connection) {
+ this.connection = connection;
+ }
+
@SuppressWarnings("MagicConstant")
@Override
public Connection getConnection() throws SQLException {
+ if (null != connection) {
+ return connection;
+ }
Connection result = mock(Connection.class, RETURNS_DEEP_STUBS);
-
when(result.getMetaData().getURL()).thenReturn("jdbc:mock://127.0.0.1/foo_ds");
+ when(result.getMetaData().getURL()).thenReturn(url);
when(result.createStatement(anyInt(), anyInt(),
anyInt()).getConnection()).thenReturn(result);
return result;
}