You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/12/13 09:30:40 UTC
[GitHub] [shardingsphere] huanghao495430759 opened a new pull request #8602: Issue#8592
huanghao495430759 opened a new pull request #8602:
URL: https://github.com/apache/shardingsphere/pull/8602
Fixes #8592.
Add test case for FrontDatabaseProtocolTypeFactory
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] terrymanu commented on a change in pull request #8602: Issue#8592
Posted by GitBox <gi...@apache.org>.
terrymanu commented on a change in pull request #8602:
URL: https://github.com/apache/shardingsphere/pull/8602#discussion_r542117799
##########
File path: shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.proxy.frontend.protocol;
+
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.auth.Authentication;
+import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
+import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.lang.reflect.Field;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class FrontDatabaseProtocolTypeFactoryTest {
+
+ @Test(expected = ShardingSphereConfigurationException.class)
+ public void assertGetDatabaseTypeFromMetaDataContextsThrowShardingSphereConfigurationException() {
+ StandardMetaDataContexts standardMetaDataContexts = new StandardMetaDataContexts(Collections.emptyMap(), mock(ExecutorEngine.class), mock(Authentication.class),
+ new ConfigurationProperties(new Properties()));
+ setMetaDataContexts(standardMetaDataContexts);
+ assertTrue(standardMetaDataContexts.getMetaDataMap().isEmpty());
+ FrontDatabaseProtocolTypeFactory.getDatabaseType();
+ }
+
+ @Test
+ public void assertGetDatabaseTypeInstanceOfMySQLDatabaseTypeFromMetaDataContextsSchemaName() {
+ StandardMetaDataContexts standardMetaDataContexts = new StandardMetaDataContexts(getShardingSphereMetaData(), mock(ExecutorEngine.class), mock(Authentication.class),
+ new ConfigurationProperties(new Properties()));
+ setMetaDataContexts(standardMetaDataContexts);
+ assertTrue(!standardMetaDataContexts.getMetaDataMap().isEmpty());
+ String configuredDatabaseType = standardMetaDataContexts.getProps().getValue(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE);
+ assertTrue(configuredDatabaseType.isEmpty());
+ assertTrue(standardMetaDataContexts.getAllSchemaNames().contains("mysql"));
+ DatabaseType databaseType = FrontDatabaseProtocolTypeFactory.getDatabaseType();
+ assertThat(databaseType, instanceOf(DatabaseType.class));
+ assertThat(databaseType.getName(), is("MySQL"));
+ }
+
+ @Test
+ public void assertGetDatabaseTypeOfPostgreSQLDatabaseTypeFromMetaDataContextsProps() {
+ Properties properties = new Properties();
+ properties.setProperty(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE.getKey(), "PostgreSQL");
+ StandardMetaDataContexts standardMetaDataContexts = new StandardMetaDataContexts(getShardingSphereMetaData(), mock(ExecutorEngine.class), mock(Authentication.class),
+ new ConfigurationProperties(properties));
+ setMetaDataContexts(standardMetaDataContexts);
+ assertTrue(!standardMetaDataContexts.getMetaDataMap().isEmpty());
+ String configuredDatabaseType = standardMetaDataContexts.getProps().getValue(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE);
+ assertThat(configuredDatabaseType, is("PostgreSQL"));
+ assertTrue(standardMetaDataContexts.getAllSchemaNames().contains("mysql"));
+ DatabaseType databaseType = FrontDatabaseProtocolTypeFactory.getDatabaseType();
+ assertThat(databaseType, instanceOf(DatabaseType.class));
+ assertThat(databaseType.getName(), is("PostgreSQL"));
+ assertThat(standardMetaDataContexts.getMetaData("mysql").getResource().getDatabaseType(), instanceOf(MySQLDatabaseType.class));
+ }
+
+ @SneakyThrows
+ private void setMetaDataContexts(final StandardMetaDataContexts standardMetaDataContexts) {
+ Field field = ProxyContext.getInstance().getClass().getDeclaredField("metaDataContexts");
+ field.setAccessible(true);
+ field.set(ProxyContext.getInstance(), standardMetaDataContexts);
+ }
+
+ private Map<String, ShardingSphereMetaData> getShardingSphereMetaData() {
+ ShardingSphereResource shardingSphereResource = mock(ShardingSphereResource.class);
+ when(shardingSphereResource.getDatabaseType()).thenReturn(new MySQLDatabaseType());
+ ShardingSphereMetaData shardingSphereMetaData = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS);
+ when(shardingSphereMetaData.getResource()).thenReturn(shardingSphereResource);
+ Map<String, ShardingSphereMetaData> metaDataMap = Collections.singletonMap("mysql", shardingSphereMetaData);
+ return metaDataMap;
Review comment:
Return value should name as `result`
##########
File path: shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.proxy.frontend.protocol;
+
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.auth.Authentication;
+import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
+import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.lang.reflect.Field;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
Review comment:
If there is no @Mock annotation, the @RunWith(MockitoJUnitRunner.class) is unnecessary
##########
File path: shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.proxy.frontend.protocol;
+
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.auth.Authentication;
+import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
+import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.lang.reflect.Field;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class FrontDatabaseProtocolTypeFactoryTest {
+
+ @Test(expected = ShardingSphereConfigurationException.class)
+ public void assertGetDatabaseTypeFromMetaDataContextsThrowShardingSphereConfigurationException() {
+ StandardMetaDataContexts standardMetaDataContexts = new StandardMetaDataContexts(Collections.emptyMap(), mock(ExecutorEngine.class), mock(Authentication.class),
+ new ConfigurationProperties(new Properties()));
+ setMetaDataContexts(standardMetaDataContexts);
+ assertTrue(standardMetaDataContexts.getMetaDataMap().isEmpty());
+ FrontDatabaseProtocolTypeFactory.getDatabaseType();
+ }
+
+ @Test
+ public void assertGetDatabaseTypeInstanceOfMySQLDatabaseTypeFromMetaDataContextsSchemaName() {
+ StandardMetaDataContexts standardMetaDataContexts = new StandardMetaDataContexts(getShardingSphereMetaData(), mock(ExecutorEngine.class), mock(Authentication.class),
+ new ConfigurationProperties(new Properties()));
+ setMetaDataContexts(standardMetaDataContexts);
+ assertTrue(!standardMetaDataContexts.getMetaDataMap().isEmpty());
+ String configuredDatabaseType = standardMetaDataContexts.getProps().getValue(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE);
+ assertTrue(configuredDatabaseType.isEmpty());
+ assertTrue(standardMetaDataContexts.getAllSchemaNames().contains("mysql"));
+ DatabaseType databaseType = FrontDatabaseProtocolTypeFactory.getDatabaseType();
+ assertThat(databaseType, instanceOf(DatabaseType.class));
+ assertThat(databaseType.getName(), is("MySQL"));
+ }
+
+ @Test
+ public void assertGetDatabaseTypeOfPostgreSQLDatabaseTypeFromMetaDataContextsProps() {
+ Properties properties = new Properties();
+ properties.setProperty(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE.getKey(), "PostgreSQL");
+ StandardMetaDataContexts standardMetaDataContexts = new StandardMetaDataContexts(getShardingSphereMetaData(), mock(ExecutorEngine.class), mock(Authentication.class),
+ new ConfigurationProperties(properties));
+ setMetaDataContexts(standardMetaDataContexts);
+ assertTrue(!standardMetaDataContexts.getMetaDataMap().isEmpty());
+ String configuredDatabaseType = standardMetaDataContexts.getProps().getValue(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE);
+ assertThat(configuredDatabaseType, is("PostgreSQL"));
+ assertTrue(standardMetaDataContexts.getAllSchemaNames().contains("mysql"));
+ DatabaseType databaseType = FrontDatabaseProtocolTypeFactory.getDatabaseType();
+ assertThat(databaseType, instanceOf(DatabaseType.class));
+ assertThat(databaseType.getName(), is("PostgreSQL"));
+ assertThat(standardMetaDataContexts.getMetaData("mysql").getResource().getDatabaseType(), instanceOf(MySQLDatabaseType.class));
+ }
+
+ @SneakyThrows
Review comment:
Please add exception that for @SneakyThrows, please just make it narrow.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] terrymanu merged pull request #8602: Issue#8592
Posted by GitBox <gi...@apache.org>.
terrymanu merged pull request #8602:
URL: https://github.com/apache/shardingsphere/pull/8602
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [shardingsphere] codecov-io commented on pull request #8602: Issue#8592
Posted by GitBox <gi...@apache.org>.
codecov-io commented on pull request #8602:
URL: https://github.com/apache/shardingsphere/pull/8602#issuecomment-744004606
# [Codecov](https://codecov.io/gh/apache/shardingsphere/pull/8602?src=pr&el=h1) Report
> Merging [#8602](https://codecov.io/gh/apache/shardingsphere/pull/8602?src=pr&el=desc) (b6377f5) into [master](https://codecov.io/gh/apache/shardingsphere/commit/7b120d577636d71a798f17ed7b80bfabe19e2911?el=desc) (7b120d5) will **increase** coverage by `0.03%`.
> The diff coverage is `n/a`.
[![Impacted file tree graph](https://codecov.io/gh/apache/shardingsphere/pull/8602/graphs/tree.svg?width=650&height=150&src=pr&token=ZvlXpWa7so)](https://codecov.io/gh/apache/shardingsphere/pull/8602?src=pr&el=tree)
```diff
@@ Coverage Diff @@
## master #8602 +/- ##
============================================
+ Coverage 72.12% 72.15% +0.03%
Complexity 600 600
============================================
Files 1554 1554
Lines 24746 24746
Branches 4364 4364
============================================
+ Hits 17847 17856 +9
+ Misses 5727 5718 -9
Partials 1172 1172
```
| [Impacted Files](https://codecov.io/gh/apache/shardingsphere/pull/8602?src=pr&el=tree) | Coverage Δ | Complexity Δ | |
|---|---|---|---|
| [...y/backend/text/metadata/rdl/RDLBackendHandler.java](https://codecov.io/gh/apache/shardingsphere/pull/8602/diff?src=pr&el=tree#diff-c2hhcmRpbmdzcGhlcmUtcHJveHkvc2hhcmRpbmdzcGhlcmUtcHJveHktYmFja2VuZC9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2hhcmRpbmdzcGhlcmUvcHJveHkvYmFja2VuZC90ZXh0L21ldGFkYXRhL3JkbC9SRExCYWNrZW5kSGFuZGxlci5qYXZh) | `63.63% <0.00%> (ø)` | `0.00% <0.00%> (ø%)` | |
| [...end/protocol/FrontDatabaseProtocolTypeFactory.java](https://codecov.io/gh/apache/shardingsphere/pull/8602/diff?src=pr&el=tree#diff-c2hhcmRpbmdzcGhlcmUtcHJveHkvc2hhcmRpbmdzcGhlcmUtcHJveHktZnJvbnRlbmQvc2hhcmRpbmdzcGhlcmUtcHJveHktZnJvbnRlbmQtY29yZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvc2hhcmRpbmdzcGhlcmUvcHJveHkvZnJvbnRlbmQvcHJvdG9jb2wvRnJvbnREYXRhYmFzZVByb3RvY29sVHlwZUZhY3RvcnkuamF2YQ==) | `100.00% <0.00%> (+100.00%)` | `0.00% <0.00%> (ø%)` | |
------
[Continue to review full report at Codecov](https://codecov.io/gh/apache/shardingsphere/pull/8602?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/apache/shardingsphere/pull/8602?src=pr&el=footer). Last update [7b120d5...b6377f5](https://codecov.io/gh/apache/shardingsphere/pull/8602?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org