From ecf20b30fdbaaca41801bf718168c7b6bcaaa8b8 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Mon, 16 Dec 2024 10:27:36 +0800 Subject: [PATCH 1/2] Use mockito Plugins instead of ReflectionUtils in test cases --- .../command/query/extended/PortalTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PortalTest.java b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PortalTest.java index 17343047332e8..f22a792248b36 100644 --- a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PortalTest.java +++ b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/command/query/extended/PortalTest.java @@ -40,7 +40,6 @@ import org.apache.shardingsphere.infra.session.connection.ConnectionContext; import org.apache.shardingsphere.infra.session.query.QueryContext; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; -import org.apache.shardingsphere.infra.util.reflection.ReflectionUtils; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; @@ -66,6 +65,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Answers; import org.mockito.Mock; +import org.mockito.internal.configuration.plugins.Plugins; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; @@ -83,7 +83,6 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyString; @@ -148,8 +147,9 @@ void assertGetName() throws SQLException { assertThat(portal.getName(), is("")); } + @SuppressWarnings("unchecked") @Test - void assertExecuteSelectStatementAndReturnAllRows() throws SQLException { + void assertExecuteSelectStatementAndReturnAllRows() throws SQLException, ReflectiveOperationException { QueryResponseHeader responseHeader = mock(QueryResponseHeader.class); QueryHeader queryHeader = new QueryHeader("schema", "table", "columnLabel", "columnName", Types.VARCHAR, "columnTypeName", 0, 0, false, false, false, false); QueryHeader intColumnQueryHeader = new QueryHeader("schema", "table", "columnLabel", "columnName", Types.INTEGER, "columnTypeName", 0, 0, false, false, false, false); @@ -168,9 +168,9 @@ void assertExecuteSelectStatementAndReturnAllRows() throws SQLException { portal.bind(); PostgreSQLPacket portalDescription = portal.describe(); assertThat(portalDescription, instanceOf(PostgreSQLRowDescriptionPacket.class)); - Optional> columnDescriptions = ReflectionUtils.getFieldValue(portalDescription, "columnDescriptions"); - assertTrue(columnDescriptions.isPresent()); - Iterator columnDescriptionIterator = columnDescriptions.get().iterator(); + Collection columnDescriptions = (Collection) Plugins.getMemberAccessor() + .get(PostgreSQLRowDescriptionPacket.class.getDeclaredField("columnDescriptions"), portalDescription); + Iterator columnDescriptionIterator = columnDescriptions.iterator(); PostgreSQLColumnDescription textColumnDescription = columnDescriptionIterator.next(); PostgreSQLColumnDescription intColumnDescription = columnDescriptionIterator.next(); assertThat(textColumnDescription.getDataFormat(), is(PostgreSQLValueFormat.TEXT.getCode())); From 349704c02e026d3bc5794270fc5f7b2164ee6d16 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Mon, 16 Dec 2024 10:35:12 +0800 Subject: [PATCH 2/2] Use mockito Plugins instead of ReflectionUtils in test cases --- .../dumper/OpenGaussIncrementalDumperTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kernel/data-pipeline/dialect/opengauss/src/test/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/incremental/dumper/OpenGaussIncrementalDumperTest.java b/kernel/data-pipeline/dialect/opengauss/src/test/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/incremental/dumper/OpenGaussIncrementalDumperTest.java index afda0e2a3ae52..600cd5ccf889b 100644 --- a/kernel/data-pipeline/dialect/opengauss/src/test/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/incremental/dumper/OpenGaussIncrementalDumperTest.java +++ b/kernel/data-pipeline/dialect/opengauss/src/test/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/incremental/dumper/OpenGaussIncrementalDumperTest.java @@ -17,8 +17,8 @@ package org.apache.shardingsphere.data.pipeline.opengauss.ingest.incremental.dumper; -import org.apache.shardingsphere.infra.util.reflection.ReflectionUtils; import org.junit.jupiter.api.Test; +import org.mockito.internal.configuration.plugins.Plugins; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -27,15 +27,15 @@ class OpenGaussIncrementalDumperTest { @Test - void assertGetVersion() throws NoSuchMethodException { + void assertGetVersion() throws ReflectiveOperationException { OpenGaussIncrementalDumper dumper = mock(OpenGaussIncrementalDumper.class); - int version = ReflectionUtils.invokeMethod(OpenGaussIncrementalDumper.class.getDeclaredMethod("parseMajorVersion", String.class), dumper, + int version = (int) Plugins.getMemberAccessor().invoke(OpenGaussIncrementalDumper.class.getDeclaredMethod("parseMajorVersion", String.class), dumper, "(openGauss 3.1.0 build ) compiled at 2023-02-17 16:13:51 commit 0 last mr on x86_64-unknown-linux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit"); assertThat(version, is(3)); OpenGaussIncrementalDumper mock = mock(OpenGaussIncrementalDumper.class); - version = ReflectionUtils.invokeMethod(OpenGaussIncrementalDumper.class.getDeclaredMethod("parseMajorVersion", String.class), mock, "(openGauss 5.0.1 build )"); + version = (int) Plugins.getMemberAccessor().invoke(OpenGaussIncrementalDumper.class.getDeclaredMethod("parseMajorVersion", String.class), mock, "(openGauss 5.0.1 build )"); assertThat(version, is(5)); - version = ReflectionUtils.invokeMethod(OpenGaussIncrementalDumper.class.getDeclaredMethod("parseMajorVersion", String.class), mock, "not match"); + version = (int) Plugins.getMemberAccessor().invoke(OpenGaussIncrementalDumper.class.getDeclaredMethod("parseMajorVersion", String.class), mock, "not match"); assertThat(version, is(2)); } }