You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2023/03/03 15:26:14 UTC
[shardingsphere] branch master updated: Add ConstructionMockSettings (#24445)
This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 0026563c4ae Add ConstructionMockSettings (#24445)
0026563c4ae is described below
commit 0026563c4ae50d2428231acc441875343ac48c20
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Mar 3 23:25:56 2023 +0800
Add ConstructionMockSettings (#24445)
* Add ConstructionMockSettings
* Add ConstructionMockSettings
---
.../data/DatabaseBackendHandlerFactoryTest.java | 10 +++-----
.../netty/ServerHandlerInitializerTest.java | 14 +++++-----
.../admin/MySQLComResetConnectionExecutorTest.java | 16 ++++++------
.../it/mysql/external/ExternalMySQLParserIT.java | 6 ++++-
.../external/ExternalPostgreSQLParserIT.java | 7 +++--
.../test/mock/AutoMockExtension.java | 30 ++++++++++++++++++++++
.../test/mock/ConstructionMockSettings.java | 26 +++++++++++++++----
7 files changed, 80 insertions(+), 29 deletions(-)
diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/DatabaseBackendHandlerFactoryTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/DatabaseBackendHandlerFactoryTest.java
index d75e00b9d27..68de781f9ab 100644
--- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/DatabaseBackendHandlerFactoryTest.java
+++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/DatabaseBackendHandlerFactoryTest.java
@@ -30,10 +30,10 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dal.DALStatement;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
+import org.apache.shardingsphere.test.mock.ConstructionMockSettings;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.MockedConstruction;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
@@ -43,11 +43,11 @@ import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.when;
@ExtendWith(AutoMockExtension.class)
@StaticMockSettings(ProxyContext.class)
+@ConstructionMockSettings(DatabaseConnector.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public final class DatabaseBackendHandlerFactoryTest {
@@ -76,10 +76,8 @@ public final class DatabaseBackendHandlerFactoryTest {
ConnectionSession connectionSession = mockConnectionSession();
ContextManager contextManager = mockContextManager();
when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
- try (MockedConstruction<DatabaseConnector> ignored = mockConstruction(DatabaseConnector.class)) {
- DatabaseBackendHandler actual = DatabaseBackendHandlerFactory.newInstance(new QueryContext(sqlStatementContext, sql, Collections.emptyList()), connectionSession, false);
- assertThat(actual, instanceOf(DatabaseConnector.class));
- }
+ DatabaseBackendHandler actual = DatabaseBackendHandlerFactory.newInstance(new QueryContext(sqlStatementContext, sql, Collections.emptyList()), connectionSession, false);
+ assertThat(actual, instanceOf(DatabaseConnector.class));
}
private static SQLStatementContext<SQLStatement> mockSQLStatementContext() {
diff --git a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializerTest.java b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializerTest.java
index c2727e570d4..254d67bda4b 100644
--- a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializerTest.java
+++ b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/netty/ServerHandlerInitializerTest.java
@@ -22,15 +22,18 @@ import io.netty.channel.socket.SocketChannel;
import org.apache.shardingsphere.db.protocol.codec.PacketCodec;
import org.apache.shardingsphere.db.protocol.netty.ChannelAttrInitializer;
import org.apache.shardingsphere.test.fixture.infra.database.type.MockedDatabaseType;
-import org.junit.Test;
-import org.mockito.MockedConstruction;
+import org.apache.shardingsphere.test.mock.AutoMockExtension;
+import org.apache.shardingsphere.test.mock.ConstructionMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+@ExtendWith(AutoMockExtension.class)
+@ConstructionMockSettings(FrontendChannelInboundHandler.class)
public final class ServerHandlerInitializerTest {
@Test
@@ -38,10 +41,7 @@ public final class ServerHandlerInitializerTest {
SocketChannel channel = mock(SocketChannel.class);
ChannelPipeline pipeline = mock(ChannelPipeline.class);
when(channel.pipeline()).thenReturn(pipeline);
- ServerHandlerInitializer initializer = new ServerHandlerInitializer(new MockedDatabaseType());
- try (MockedConstruction<FrontendChannelInboundHandler> ignored = mockConstruction(FrontendChannelInboundHandler.class)) {
- initializer.initChannel(channel);
- }
+ new ServerHandlerInitializer(new MockedDatabaseType()).initChannel(channel);
verify(pipeline).addLast(any(ChannelAttrInitializer.class));
verify(pipeline).addLast(any(PacketCodec.class));
verify(pipeline).addLast(any(FrontendChannelLimitationInboundHandler.class));
diff --git a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/MySQLComResetConnectionExecutorTest.java b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/MySQLComResetConnectionExecutorTest.java
index d9d00abd79d..ba08ee233a0 100644
--- a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/MySQLComResetConnectionExecutorTest.java
+++ b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/admin/MySQLComResetConnectionExecutorTest.java
@@ -25,9 +25,11 @@ import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.proxy.backend.session.ServerPreparedStatementRegistry;
import org.apache.shardingsphere.proxy.backend.session.transaction.TransactionStatus;
import org.apache.shardingsphere.proxy.frontend.mysql.command.query.binary.MySQLServerPreparedStatement;
+import org.apache.shardingsphere.test.mock.AutoMockExtension;
+import org.apache.shardingsphere.test.mock.ConstructionMockSettings;
import org.apache.shardingsphere.transaction.api.TransactionType;
-import org.junit.Test;
-import org.mockito.MockedConstruction;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import java.sql.SQLException;
import java.util.Collection;
@@ -36,12 +38,13 @@ import java.util.Collections;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+@ExtendWith(AutoMockExtension.class)
+@ConstructionMockSettings(BackendTransactionManager.class)
public final class MySQLComResetConnectionExecutorTest {
@Test
@@ -53,10 +56,7 @@ public final class MySQLComResetConnectionExecutorTest {
when(connectionSession.getServerPreparedStatementRegistry()).thenReturn(new ServerPreparedStatementRegistry());
int statementId = 1;
connectionSession.getServerPreparedStatementRegistry().addPreparedStatement(statementId, new MySQLServerPreparedStatement("", null, Collections.emptyList()));
- Collection<DatabasePacket<?>> actual;
- try (MockedConstruction<BackendTransactionManager> ignored = mockConstruction(BackendTransactionManager.class)) {
- actual = new MySQLComResetConnectionExecutor(connectionSession).execute();
- }
+ Collection<DatabasePacket<?>> actual = new MySQLComResetConnectionExecutor(connectionSession).execute();
assertThat(actual.size(), is(1));
assertThat(actual.iterator().next(), instanceOf(MySQLOKPacket.class));
verify(connectionSession).setAutoCommit(true);
diff --git a/sql-parser/dialect/mysql/src/test/java/org/apache/shardingsphere/test/it/sql/parser/it/mysql/external/ExternalMySQLParserIT.java b/sql-parser/dialect/mysql/src/test/java/org/apache/shardingsphere/test/it/sql/parser/it/mysql/external/ExternalMySQLParserIT.java
index d1027a8b67c..e81ffd77b57 100644
--- a/sql-parser/dialect/mysql/src/test/java/org/apache/shardingsphere/test/it/sql/parser/it/mysql/external/ExternalMySQLParserIT.java
+++ b/sql-parser/dialect/mysql/src/test/java/org/apache/shardingsphere/test/it/sql/parser/it/mysql/external/ExternalMySQLParserIT.java
@@ -20,6 +20,10 @@ package org.apache.shardingsphere.test.it.sql.parser.it.mysql.external;
import org.apache.shardingsphere.test.it.sql.parser.external.ExternalSQLParserIT;
import org.apache.shardingsphere.test.it.sql.parser.external.ExternalSQLParserITSettings;
-@ExternalSQLParserITSettings(value = "MySQL", caseURL = "https://github.com/mysql/mysql-server/tree/8.0/mysql-test/t", resultURL = "https://github.com/mysql/mysql-server/tree/8.0/mysql-test/r")
+@ExternalSQLParserITSettings(value = "MySQL", caseURL = ExternalMySQLParserIT.CASE_URL, resultURL = ExternalMySQLParserIT.RESULT_URL)
public final class ExternalMySQLParserIT extends ExternalSQLParserIT {
+
+ static final String CASE_URL = "https://github.com/mysql/mysql-server/tree/8.0/mysql-test/t";
+
+ static final String RESULT_URL = "https://github.com/mysql/mysql-server/tree/8.0/mysql-test/r";
}
diff --git a/sql-parser/dialect/postgresql/src/test/java/org/apache/shardingsphere/test/it/sql/parser/it/postgresql/external/ExternalPostgreSQLParserIT.java b/sql-parser/dialect/postgresql/src/test/java/org/apache/shardingsphere/test/it/sql/parser/it/postgresql/external/ExternalPostgreSQLParserIT.java
index 746f3e9a24c..0e3dea729f7 100644
--- a/sql-parser/dialect/postgresql/src/test/java/org/apache/shardingsphere/test/it/sql/parser/it/postgresql/external/ExternalPostgreSQLParserIT.java
+++ b/sql-parser/dialect/postgresql/src/test/java/org/apache/shardingsphere/test/it/sql/parser/it/postgresql/external/ExternalPostgreSQLParserIT.java
@@ -20,7 +20,10 @@ package org.apache.shardingsphere.test.it.sql.parser.it.postgresql.external;
import org.apache.shardingsphere.test.it.sql.parser.external.ExternalSQLParserIT;
import org.apache.shardingsphere.test.it.sql.parser.external.ExternalSQLParserITSettings;
-@ExternalSQLParserITSettings(value = "PostgreSQL",
- caseURL = "https://github.com/postgres/postgres/tree/master/src/test/regress/sql", resultURL = "https://github.com/postgres/postgres/tree/master/src/test/regress/expected")
+@ExternalSQLParserITSettings(value = "PostgreSQL", caseURL = ExternalPostgreSQLParserIT.CASE_URL, resultURL = ExternalPostgreSQLParserIT.RESULT_URL)
public final class ExternalPostgreSQLParserIT extends ExternalSQLParserIT {
+
+ static final String CASE_URL = "https://github.com/postgres/postgres/tree/master/src/test/regress/sql";
+
+ static final String RESULT_URL = "https://github.com/postgres/postgres/tree/master/src/test/regress/expected";
}
diff --git a/test/util/src/main/java/org/apache/shardingsphere/test/mock/AutoMockExtension.java b/test/util/src/main/java/org/apache/shardingsphere/test/mock/AutoMockExtension.java
index fad6847f133..ce41f4877cb 100644
--- a/test/util/src/main/java/org/apache/shardingsphere/test/mock/AutoMockExtension.java
+++ b/test/util/src/main/java/org/apache/shardingsphere/test/mock/AutoMockExtension.java
@@ -20,12 +20,14 @@ package org.apache.shardingsphere.test.mock;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
+import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import java.util.Collection;
import java.util.LinkedList;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.mockStatic;
/**
@@ -35,8 +37,15 @@ public final class AutoMockExtension implements BeforeEachCallback, AfterEachCal
private final Collection<MockedStatic<?>> mockedStatics = new LinkedList<>();
+ private final Collection<MockedConstruction<?>> mockedConstructions = new LinkedList<>();
+
@Override
public void beforeEach(final ExtensionContext context) {
+ mockStatics(context);
+ mockConstructions(context);
+ }
+
+ private void mockStatics(final ExtensionContext context) {
StaticMockSettings staticMockSettings = context.getRequiredTestClass().getAnnotation(StaticMockSettings.class);
if (null != staticMockSettings) {
for (Class<?> each : staticMockSettings.value()) {
@@ -45,11 +54,32 @@ public final class AutoMockExtension implements BeforeEachCallback, AfterEachCal
}
}
+ private void mockConstructions(final ExtensionContext context) {
+ ConstructionMockSettings constructionMockSettings = context.getRequiredTestClass().getAnnotation(ConstructionMockSettings.class);
+ if (null != constructionMockSettings) {
+ for (Class<?> each : constructionMockSettings.value()) {
+ mockedConstructions.add(mockConstruction(each));
+ }
+ }
+ }
+
@Override
public void afterEach(final ExtensionContext context) {
+ cleanMockedStatics();
+ cleanMockedConstructions();
+ }
+
+ private void cleanMockedStatics() {
for (MockedStatic<?> each : mockedStatics) {
each.close();
}
mockedStatics.clear();
}
+
+ private void cleanMockedConstructions() {
+ for (MockedConstruction<?> each : mockedConstructions) {
+ each.close();
+ }
+ mockedConstructions.clear();
+ }
}
diff --git a/sql-parser/dialect/mysql/src/test/java/org/apache/shardingsphere/test/it/sql/parser/it/mysql/external/ExternalMySQLParserIT.java b/test/util/src/main/java/org/apache/shardingsphere/test/mock/ConstructionMockSettings.java
similarity index 59%
copy from sql-parser/dialect/mysql/src/test/java/org/apache/shardingsphere/test/it/sql/parser/it/mysql/external/ExternalMySQLParserIT.java
copy to test/util/src/main/java/org/apache/shardingsphere/test/mock/ConstructionMockSettings.java
index d1027a8b67c..b6994445ffe 100644
--- a/sql-parser/dialect/mysql/src/test/java/org/apache/shardingsphere/test/it/sql/parser/it/mysql/external/ExternalMySQLParserIT.java
+++ b/test/util/src/main/java/org/apache/shardingsphere/test/mock/ConstructionMockSettings.java
@@ -15,11 +15,27 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.test.it.sql.parser.it.mysql.external;
+package org.apache.shardingsphere.test.mock;
-import org.apache.shardingsphere.test.it.sql.parser.external.ExternalSQLParserIT;
-import org.apache.shardingsphere.test.it.sql.parser.external.ExternalSQLParserITSettings;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.junit.jupiter.MockitoExtension;
-@ExternalSQLParserITSettings(value = "MySQL", caseURL = "https://github.com/mysql/mysql-server/tree/8.0/mysql-test/t", resultURL = "https://github.com/mysql/mysql-server/tree/8.0/mysql-test/r")
-public final class ExternalMySQLParserIT extends ExternalSQLParserIT {
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * Construction mock settings.
+ */
+@ExtendWith(MockitoExtension.class)
+@Inherited
+@Retention(RetentionPolicy.RUNTIME)
+public @interface ConstructionMockSettings {
+
+ /**
+ * Mock classes.
+ *
+ * @return mock classes
+ */
+ Class<?>[] value();
}