Skip to content

Commit

Permalink
Use mockito Plugins instead of ReflectionUtils in test cases (#34070)
Browse files Browse the repository at this point in the history
* Use mockito Plugins instead of ReflectionUtils in test cases

* Use mockito Plugins instead of ReflectionUtils in test cases
  • Loading branch information
terrymanu authored Dec 16, 2024
1 parent 257fcd9 commit 2108a52
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -168,9 +168,9 @@ void assertExecuteSelectStatementAndReturnAllRows() throws SQLException {
portal.bind();
PostgreSQLPacket portalDescription = portal.describe();
assertThat(portalDescription, instanceOf(PostgreSQLRowDescriptionPacket.class));
Optional<Collection<PostgreSQLColumnDescription>> columnDescriptions = ReflectionUtils.getFieldValue(portalDescription, "columnDescriptions");
assertTrue(columnDescriptions.isPresent());
Iterator<PostgreSQLColumnDescription> columnDescriptionIterator = columnDescriptions.get().iterator();
Collection<PostgreSQLColumnDescription> columnDescriptions = (Collection<PostgreSQLColumnDescription>) Plugins.getMemberAccessor()
.get(PostgreSQLRowDescriptionPacket.class.getDeclaredField("columnDescriptions"), portalDescription);
Iterator<PostgreSQLColumnDescription> columnDescriptionIterator = columnDescriptions.iterator();
PostgreSQLColumnDescription textColumnDescription = columnDescriptionIterator.next();
PostgreSQLColumnDescription intColumnDescription = columnDescriptionIterator.next();
assertThat(textColumnDescription.getDataFormat(), is(PostgreSQLValueFormat.TEXT.getCode()));
Expand Down

0 comments on commit 2108a52

Please sign in to comment.