Skip to content

Commit

Permalink
Use Plugins.getMemberAccessor() instead of method reflect on test cas…
Browse files Browse the repository at this point in the history
…es (#23045)

* Refactor InsertValueContextTest

* Refactor OpenGaussAuthenticationAlgorithm

* Refactor OpenGaussAuthenticationAlgorithm

* Refactor MySQLBinlogRowsEventPacketTest

* Refactor OnDuplicateUpdateContextTest

* Refactor OnDuplicateUpdateContextTest

* Refactor MySQLIncrementalDumperTest

* Refactor PostgreSQLAuthenticationEngineTest

* Refactor PostgreSQLAuthenticationHandlerTest

* Refactor PostgreSQLMD5PasswordAuthenticatorTest

* Refactor ConsistencyCheckJobTest

* Use Plugins.getMemberAccessor() instead of method reflect on test cases
  • Loading branch information
terrymanu authored Dec 22, 2022
1 parent 45248a8 commit e3170a5
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -77,8 +77,8 @@ public void assertReadWriteRowV1WithoutNullValue() throws InvocationTargetExcept
assertFalse(actual.getColumnsPresentBitmap().isNullParameter(0));
assertNull(actual.getColumnsPresentBitmap2());
MySQLPacketPayload packetPayload = new MySQLPacketPayload(byteBuf, StandardCharsets.UTF_8);
Serializable[] result = (Serializable[]) invokeMethod(actual, "readRow", new Class[]{List.class, MySQLPacketPayload.class}, new Object[]{columnDefs, packetPayload});
assertThat(result[0], is(0L));
assertThat(((Serializable[]) Plugins.getMemberAccessor()
.invoke(MySQLBinlogRowsEventPacket.class.getDeclaredMethod("readRow", List.class, MySQLPacketPayload.class), actual, columnDefs, packetPayload))[0], is(0L));
}

private void assertBinlogRowsEventV1BeforeRows(final MySQLBinlogRowsEventPacket actual) {
Expand All @@ -87,11 +87,4 @@ private void assertBinlogRowsEventV1BeforeRows(final MySQLBinlogRowsEventPacket
verify(payload, never()).skipReserved(2);
assertThat(actual.getColumnNumber(), is(1));
}

private static Object invokeMethod(final Object target, final String methodName, final Class<?>[] parameterTypes,
final Object[] parameterValues) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method method = target.getClass().getDeclaredMethod(methodName, parameterTypes);
method.setAccessible(true);
return method.invoke(target, parameterValues);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import org.junit.Test;
import org.mockito.internal.configuration.plugins.Plugins;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -36,33 +36,27 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;

public final class InsertValueContextTest {

@SuppressWarnings("unchecked")
@Test
public void assertInstanceConstructedOk() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Collection<ExpressionSegment> assignments = Collections.emptyList();
List<Object> params = Collections.emptyList();
int parametersOffset = 0;
InsertValueContext insertValueContext = new InsertValueContext(assignments, params, parametersOffset);
Method getValueExpressionsMethod = InsertValueContext.class.getDeclaredMethod("getValueExpressions", Collection.class);
getValueExpressionsMethod.setAccessible(true);
List<ExpressionSegment> getValueExpressionsResult = (List<ExpressionSegment>) getValueExpressionsMethod.invoke(insertValueContext, new Object[]{assignments});
assertThat(insertValueContext.getValueExpressions(), is(getValueExpressionsResult));
Method getParametersMethod = InsertValueContext.class.getDeclaredMethod("getParameters", List.class, int.class);
getParametersMethod.setAccessible(true);
List<Object> getParametersResult = (List<Object>) getParametersMethod.invoke(insertValueContext, new Object[]{params, parametersOffset});
assertThat(insertValueContext.getParameters(), is(getParametersResult));
InsertValueContext insertValueContext = new InsertValueContext(Collections.emptyList(), Collections.emptyList(), 0);
assertThat(insertValueContext.getValueExpressions(), is(
Plugins.getMemberAccessor().invoke(InsertValueContext.class.getDeclaredMethod("getValueExpressions", Collection.class), insertValueContext, Collections.emptyList())));
assertThat(insertValueContext.getParameters(), is(
Plugins.getMemberAccessor().invoke(InsertValueContext.class.getDeclaredMethod("getParameters", List.class, int.class), insertValueContext, Collections.emptyList(), 0)));
}

@Test
public void assertGetLiteralValueWithParameterMarker() {
Collection<ExpressionSegment> assignments = makeParameterMarkerExpressionSegment();
String parameterValue = "test";
InsertValueContext insertValueContext = new InsertValueContext(assignments, Collections.singletonList(parameterValue), 0);
Object valueFromInsertValueContext = insertValueContext.getLiteralValue(0).get();
assertThat(valueFromInsertValueContext, is(parameterValue));
Optional<Object> valueFromInsertValueContext = insertValueContext.getLiteralValue(0);
assertTrue(valueFromInsertValueContext.isPresent());
assertThat(valueFromInsertValueContext.get(), is(parameterValue));
}

@Test
Expand All @@ -83,8 +77,9 @@ public void assertGetLiteralValueWhenLiteralExpressionSegment() {
Object literalObject = new Object();
Collection<ExpressionSegment> assignments = makeLiteralExpressionSegment(literalObject);
InsertValueContext insertValueContext = new InsertValueContext(assignments, Collections.emptyList(), 0);
Object valueFromInsertValueContext = insertValueContext.getLiteralValue(0).get();
assertThat(valueFromInsertValueContext, is(literalObject));
Optional<Object> valueFromInsertValueContext = insertValueContext.getLiteralValue(0);
assertTrue(valueFromInsertValueContext.isPresent());
assertThat(valueFromInsertValueContext.get(), is(literalObject));
}

private Collection<ExpressionSegment> makeLiteralExpressionSegment(final Object literalObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.SimpleExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import org.junit.Test;
import org.mockito.internal.configuration.plugins.Plugins;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -40,21 +40,13 @@

public final class OnDuplicateUpdateContextTest {

@SuppressWarnings("unchecked")
@Test
public void assertInstanceConstructedOk() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Collection<AssignmentSegment> assignments = Collections.emptyList();
List<Object> params = Collections.emptyList();
int parametersOffset = 0;
OnDuplicateUpdateContext onDuplicateUpdateContext = new OnDuplicateUpdateContext(assignments, params, parametersOffset);
Method getValueExpressionsMethod = OnDuplicateUpdateContext.class.getDeclaredMethod("getValueExpressions", Collection.class);
getValueExpressionsMethod.setAccessible(true);
List<ExpressionSegment> getValueExpressionsResult = (List<ExpressionSegment>) getValueExpressionsMethod.invoke(onDuplicateUpdateContext, new Object[]{assignments});
assertThat(onDuplicateUpdateContext.getValueExpressions(), is(getValueExpressionsResult));
Method getParametersMethod = OnDuplicateUpdateContext.class.getDeclaredMethod("getParameters", List.class, int.class);
getParametersMethod.setAccessible(true);
List<Object> getParametersResult = (List<Object>) getParametersMethod.invoke(onDuplicateUpdateContext, new Object[]{params, parametersOffset});
assertThat(onDuplicateUpdateContext.getParameters(), is(getParametersResult));
OnDuplicateUpdateContext onDuplicateUpdateContext = new OnDuplicateUpdateContext(Collections.emptyList(), Collections.emptyList(), 0);
assertThat(onDuplicateUpdateContext.getValueExpressions(), is(Plugins.getMemberAccessor()
.invoke(OnDuplicateUpdateContext.class.getDeclaredMethod("getValueExpressions", Collection.class), onDuplicateUpdateContext, Collections.emptyList())));
assertThat(onDuplicateUpdateContext.getParameters(), is(Plugins.getMemberAccessor()
.invoke(OnDuplicateUpdateContext.class.getDeclaredMethod("getParameters", List.class, int.class), onDuplicateUpdateContext, Collections.emptyList(), 0)));
}

@Test
Expand All @@ -63,8 +55,7 @@ public void assertGetValueWhenParameterMarker() {
String parameterValue1 = "test1";
String parameterValue2 = "test2";
List<Object> params = Arrays.asList(parameterValue1, parameterValue2);
int parametersOffset = 0;
OnDuplicateUpdateContext onDuplicateUpdateContext = new OnDuplicateUpdateContext(assignments, params, parametersOffset);
OnDuplicateUpdateContext onDuplicateUpdateContext = new OnDuplicateUpdateContext(assignments, params, 0);
Object valueFromInsertValueContext1 = onDuplicateUpdateContext.getValue(0);
assertThat(valueFromInsertValueContext1, is(parameterValue1));
Object valueFromInsertValueContext2 = onDuplicateUpdateContext.getValue(1);
Expand All @@ -83,8 +74,7 @@ private Collection<AssignmentSegment> createParameterMarkerExpressionAssignmentS
public void assertGetValueWhenLiteralExpressionSegment() {
Object literalObject = new Object();
Collection<AssignmentSegment> assignments = createLiteralExpressionSegment(literalObject);
List<Object> params = Collections.emptyList();
OnDuplicateUpdateContext onDuplicateUpdateContext = new OnDuplicateUpdateContext(assignments, params, 0);
OnDuplicateUpdateContext onDuplicateUpdateContext = new OnDuplicateUpdateContext(assignments, Collections.emptyList(), 0);
Object valueFromInsertValueContext = onDuplicateUpdateContext.getValue(0);
assertThat(valueFromInsertValueContext, is(literalObject));
}
Expand Down Expand Up @@ -115,8 +105,7 @@ private AssignmentSegment createAssignmentSegment(final BinaryOperationExpressio
public void assertGetColumn() {
Object literalObject = new Object();
Collection<AssignmentSegment> assignments = createLiteralExpressionSegment(literalObject);
List<Object> params = Collections.emptyList();
OnDuplicateUpdateContext onDuplicateUpdateContext = new OnDuplicateUpdateContext(assignments, params, 0);
OnDuplicateUpdateContext onDuplicateUpdateContext = new OnDuplicateUpdateContext(assignments, Collections.emptyList(), 0);
ColumnSegment column = onDuplicateUpdateContext.getColumn(0);
assertThat(column, is(assignments.iterator().next().getColumns().get(0)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.junit.MockitoJUnitRunner;

import javax.sql.DataSource;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -84,8 +82,7 @@ public void setUp() {
channel = new MultiplexMemoryPipelineChannel();
PipelineTableMetaDataLoader metaDataLoader = new StandardPipelineTableMetaDataLoader(dataSourceManager.getDataSource(dumperConfig.getDataSourceConfig()));
incrementalDumper = new MySQLIncrementalDumper(dumperConfig, new BinlogPosition("binlog-000001", 4L), channel, metaDataLoader);
PipelineColumnMetaData column = new PipelineColumnMetaData(1, "test", Types.INTEGER, "INTEGER", true, true, true);
when(pipelineTableMetaData.getColumnMetaData(anyInt())).thenReturn(column);
when(pipelineTableMetaData.getColumnMetaData(anyInt())).thenReturn(new PipelineColumnMetaData(1, "test", Types.INTEGER, "INTEGER", true, true, true));
}

private DumperConfiguration mockDumperConfiguration() {
Expand Down Expand Up @@ -114,79 +111,63 @@ public void tearDown() {
}

@Test
public void assertWriteRowsEvent() {
public void assertWriteRowsEvent() throws ReflectiveOperationException {
WriteRowsEvent rowsEvent = new WriteRowsEvent();
rowsEvent.setDatabaseName("");
rowsEvent.setTableName("t_order");
List<Serializable[]> rows = new ArrayList<>(1);
rows.add(new String[]{"1", "order"});
rowsEvent.setAfterRows(rows);
invokeMethod(incrementalDumper, "handleWriteRowsEvent", new Class[]{WriteRowsEvent.class, PipelineTableMetaData.class}, new Object[]{rowsEvent, pipelineTableMetaData});
List<Record> records = channel.fetchRecords(1, 0);
assertThat(records.size(), is(1));
assertThat(records.get(0), instanceOf(DataRecord.class));
assertThat(((DataRecord) records.get(0)).getType(), is(IngestDataChangeType.INSERT));
rowsEvent.setAfterRows(Collections.singletonList(new String[]{"1", "order"}));
Plugins.getMemberAccessor().invoke(
MySQLIncrementalDumper.class.getDeclaredMethod("handleWriteRowsEvent", WriteRowsEvent.class, PipelineTableMetaData.class), incrementalDumper, rowsEvent, pipelineTableMetaData);
List<Record> actual = channel.fetchRecords(1, 0);
assertThat(actual.size(), is(1));
assertThat(actual.get(0), instanceOf(DataRecord.class));
assertThat(((DataRecord) actual.get(0)).getType(), is(IngestDataChangeType.INSERT));
}

@Test
public void assertUpdateRowsEvent() {
public void assertUpdateRowsEvent() throws ReflectiveOperationException {
UpdateRowsEvent rowsEvent = new UpdateRowsEvent();
rowsEvent.setDatabaseName("");
rowsEvent.setTableName("t_order");
List<Serializable[]> beforeRows = new ArrayList<>(1);
beforeRows.add(new String[]{"1", "order_old"});
List<Serializable[]> afterRows = new ArrayList<>(1);
afterRows.add(new String[]{"1", "order_new"});
rowsEvent.setBeforeRows(beforeRows);
rowsEvent.setAfterRows(afterRows);
invokeMethod(incrementalDumper, "handleUpdateRowsEvent", new Class[]{UpdateRowsEvent.class, PipelineTableMetaData.class}, new Object[]{rowsEvent, pipelineTableMetaData});
List<Record> records = channel.fetchRecords(1, 0);
assertThat(records.size(), is(1));
assertThat(records.get(0), instanceOf(DataRecord.class));
assertThat(((DataRecord) records.get(0)).getType(), is(IngestDataChangeType.UPDATE));
rowsEvent.setBeforeRows(Collections.singletonList(new String[]{"1", "order_old"}));
rowsEvent.setAfterRows(Collections.singletonList(new String[]{"1", "order_new"}));
Plugins.getMemberAccessor().invoke(
MySQLIncrementalDumper.class.getDeclaredMethod("handleUpdateRowsEvent", UpdateRowsEvent.class, PipelineTableMetaData.class), incrementalDumper, rowsEvent, pipelineTableMetaData);
List<Record> actual = channel.fetchRecords(1, 0);
assertThat(actual.size(), is(1));
assertThat(actual.get(0), instanceOf(DataRecord.class));
assertThat(((DataRecord) actual.get(0)).getType(), is(IngestDataChangeType.UPDATE));
}

@Test
public void assertDeleteRowsEvent() {
public void assertDeleteRowsEvent() throws ReflectiveOperationException {
DeleteRowsEvent rowsEvent = new DeleteRowsEvent();
rowsEvent.setDatabaseName("");
rowsEvent.setTableName("t_order");
List<Serializable[]> rows = new ArrayList<>(1);
rows.add(new String[]{"1", "order"});
rowsEvent.setBeforeRows(rows);
invokeMethod(incrementalDumper, "handleDeleteRowsEvent", new Class[]{DeleteRowsEvent.class, PipelineTableMetaData.class}, new Object[]{rowsEvent, pipelineTableMetaData});
List<Record> records = channel.fetchRecords(1, 0);
assertThat(records.size(), is(1));
assertThat(records.get(0), instanceOf(DataRecord.class));
assertThat(((DataRecord) records.get(0)).getType(), is(IngestDataChangeType.DELETE));
rowsEvent.setBeforeRows(Collections.singletonList(new String[]{"1", "order"}));
Plugins.getMemberAccessor().invoke(
MySQLIncrementalDumper.class.getDeclaredMethod("handleDeleteRowsEvent", DeleteRowsEvent.class, PipelineTableMetaData.class), incrementalDumper, rowsEvent, pipelineTableMetaData);
List<Record> actual = channel.fetchRecords(1, 0);
assertThat(actual.size(), is(1));
assertThat(actual.get(0), instanceOf(DataRecord.class));
assertThat(((DataRecord) actual.get(0)).getType(), is(IngestDataChangeType.DELETE));
}

@Test
public void assertPlaceholderEvent() {
invokeHandleEvent(new PlaceholderEvent());
List<Record> records = channel.fetchRecords(1, 0);
assertThat(records.size(), is(1));
assertThat(records.get(0), instanceOf(PlaceholderRecord.class));
public void assertPlaceholderEvent() throws ReflectiveOperationException {
Plugins.getMemberAccessor().invoke(MySQLIncrementalDumper.class.getDeclaredMethod("handleEvent", AbstractBinlogEvent.class), incrementalDumper, new PlaceholderEvent());
List<Record> actual = channel.fetchRecords(1, 0);
assertThat(actual.size(), is(1));
assertThat(actual.get(0), instanceOf(PlaceholderRecord.class));
}

@Test
public void assertRowsEventFiltered() {
public void assertRowsEventFiltered() throws ReflectiveOperationException {
WriteRowsEvent rowsEvent = new WriteRowsEvent();
rowsEvent.setDatabaseName("unknown_database");
invokeHandleEvent(rowsEvent);
List<Record> records = channel.fetchRecords(1, 0);
assertThat(records.size(), is(1));
assertThat(records.get(0), instanceOf(PlaceholderRecord.class));
}

private void invokeHandleEvent(final AbstractBinlogEvent event) {
invokeMethod(incrementalDumper, "handleEvent", new Class[]{AbstractBinlogEvent.class}, new Object[]{event});
}

@SneakyThrows(ReflectiveOperationException.class)
private Object invokeMethod(final Object target, final String methodName, final Class<?>[] parameterTypes, final Object[] parameterValues) {
Method method = target.getClass().getDeclaredMethod(methodName, parameterTypes);
method.setAccessible(true);
return method.invoke(target, parameterValues);
Plugins.getMemberAccessor().invoke(MySQLIncrementalDumper.class.getDeclaredMethod("handleEvent", AbstractBinlogEvent.class), incrementalDumper, rowsEvent);
List<Record> actual = channel.fetchRecords(1, 0);
assertThat(actual.size(), is(1));
assertThat(actual.get(0), instanceOf(PlaceholderRecord.class));
}
}
Loading

0 comments on commit e3170a5

Please sign in to comment.