diff --git a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/MySQLBinlogRowsEventPacketTest.java b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/MySQLBinlogRowsEventPacketTest.java index 9257d09ce9e8d..6252745ef48a9 100644 --- a/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/MySQLBinlogRowsEventPacketTest.java +++ b/db-protocol/mysql/src/test/java/org/apache/shardingsphere/db/protocol/mysql/packet/binlog/row/MySQLBinlogRowsEventPacketTest.java @@ -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; @@ -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) { @@ -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); - } } diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/insert/values/InsertValueContextTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/insert/values/InsertValueContextTest.java index 8683b175caa0e..1862a7bb6140c 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/insert/values/InsertValueContextTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/insert/values/InsertValueContextTest.java @@ -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; @@ -36,24 +36,17 @@ 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 assignments = Collections.emptyList(); - List 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 getValueExpressionsResult = (List) 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 getParametersResult = (List) 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 @@ -61,8 +54,9 @@ public void assertGetLiteralValueWithParameterMarker() { Collection 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 valueFromInsertValueContext = insertValueContext.getLiteralValue(0); + assertTrue(valueFromInsertValueContext.isPresent()); + assertThat(valueFromInsertValueContext.get(), is(parameterValue)); } @Test @@ -83,8 +77,9 @@ public void assertGetLiteralValueWhenLiteralExpressionSegment() { Object literalObject = new Object(); Collection assignments = makeLiteralExpressionSegment(literalObject); InsertValueContext insertValueContext = new InsertValueContext(assignments, Collections.emptyList(), 0); - Object valueFromInsertValueContext = insertValueContext.getLiteralValue(0).get(); - assertThat(valueFromInsertValueContext, is(literalObject)); + Optional valueFromInsertValueContext = insertValueContext.getLiteralValue(0); + assertTrue(valueFromInsertValueContext.isPresent()); + assertThat(valueFromInsertValueContext.get(), is(literalObject)); } private Collection makeLiteralExpressionSegment(final Object literalObject) { diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/insert/values/OnDuplicateUpdateContextTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/insert/values/OnDuplicateUpdateContextTest.java index 521c224bfe145..09d42797e9c48 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/insert/values/OnDuplicateUpdateContextTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/insert/values/OnDuplicateUpdateContextTest.java @@ -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; @@ -40,21 +40,13 @@ public final class OnDuplicateUpdateContextTest { - @SuppressWarnings("unchecked") @Test public void assertInstanceConstructedOk() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { - Collection assignments = Collections.emptyList(); - List 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 getValueExpressionsResult = (List) 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 getParametersResult = (List) 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 @@ -63,8 +55,7 @@ public void assertGetValueWhenParameterMarker() { String parameterValue1 = "test1"; String parameterValue2 = "test2"; List 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); @@ -83,8 +74,7 @@ private Collection createParameterMarkerExpressionAssignmentS public void assertGetValueWhenLiteralExpressionSegment() { Object literalObject = new Object(); Collection assignments = createLiteralExpressionSegment(literalObject); - List 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)); } @@ -115,8 +105,7 @@ private AssignmentSegment createAssignmentSegment(final BinaryOperationExpressio public void assertGetColumn() { Object literalObject = new Object(); Collection assignments = createLiteralExpressionSegment(literalObject); - List 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))); } diff --git a/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/MySQLIncrementalDumperTest.java b/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/MySQLIncrementalDumperTest.java index 1e97aeaca6ce2..0f5397b9e4d5d 100644 --- a/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/MySQLIncrementalDumperTest.java +++ b/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/MySQLIncrementalDumperTest.java @@ -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; @@ -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() { @@ -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 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 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 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 beforeRows = new ArrayList<>(1); - beforeRows.add(new String[]{"1", "order_old"}); - List 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 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 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 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 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 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 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 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 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 actual = channel.fetchRecords(1, 0); + assertThat(actual.size(), is(1)); + assertThat(actual.get(0), instanceOf(PlaceholderRecord.class)); } } diff --git a/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/fixture/OpenGaussAuthenticationAlgorithm.java b/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/fixture/OpenGaussAuthenticationAlgorithm.java index 52df5558210d2..241208c2c11b8 100644 --- a/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/fixture/OpenGaussAuthenticationAlgorithm.java +++ b/proxy/frontend/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/fixture/OpenGaussAuthenticationAlgorithm.java @@ -21,8 +21,8 @@ import lombok.NoArgsConstructor; import lombok.SneakyThrows; import org.apache.shardingsphere.proxy.frontend.opengauss.authentication.OpenGaussAuthenticationHandler; +import org.mockito.internal.configuration.plugins.Plugins; -import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; @NoArgsConstructor(access = AccessLevel.PRIVATE) @@ -51,44 +51,31 @@ public static byte[] doRFC5802Algorithm(final String password, final String rand @SneakyThrows(ReflectiveOperationException.class) private static byte[] generateKFromPBKDF2(final String password, final String saltString, final int serverIteration) { - Method generateKFromPBKDF2Method = OpenGaussAuthenticationHandler.class.getDeclaredMethod("generateKFromPBKDF2", String.class, String.class, int.class); - generateKFromPBKDF2Method.setAccessible(true); - return (byte[]) generateKFromPBKDF2Method.invoke(null, password, saltString, serverIteration); + return (byte[]) Plugins.getMemberAccessor().invoke(OpenGaussAuthenticationHandler.class.getDeclaredMethod("generateKFromPBKDF2", String.class, String.class, int.class), + OpenGaussAuthenticationHandler.class, password, saltString, serverIteration); } @SneakyThrows(ReflectiveOperationException.class) private static byte[] getKeyFromHmac(final byte[] key, final byte[] data) { - Method getKeyFromHmacMethod = OpenGaussAuthenticationHandler.class.getDeclaredMethod("getKeyFromHmac", byte[].class, byte[].class); - getKeyFromHmacMethod.setAccessible(true); - return (byte[]) getKeyFromHmacMethod.invoke(null, key, data); + return (byte[]) Plugins.getMemberAccessor().invoke( + OpenGaussAuthenticationHandler.class.getDeclaredMethod("getKeyFromHmac", byte[].class, byte[].class), OpenGaussAuthenticationHandler.class, key, data); } @SneakyThrows(ReflectiveOperationException.class) private static byte[] sha256(final byte[] str) { - Method sha256Method = OpenGaussAuthenticationHandler.class.getDeclaredMethod("sha256", byte[].class); - sha256Method.setAccessible(true); - return (byte[]) sha256Method.invoke(null, str); + return (byte[]) Plugins.getMemberAccessor().invoke(OpenGaussAuthenticationHandler.class.getDeclaredMethod("sha256", byte[].class), OpenGaussAuthenticationHandler.class, new Object[]{str}); } @SneakyThrows(ReflectiveOperationException.class) private static byte[] hexStringToBytes(final String rawHexString) { - Method hexStringToBytesMethod = OpenGaussAuthenticationHandler.class.getDeclaredMethod("hexStringToBytes", String.class); - hexStringToBytesMethod.setAccessible(true); - return (byte[]) hexStringToBytesMethod.invoke(null, rawHexString); - } - - @SneakyThrows(ReflectiveOperationException.class) - private static byte[] calculateH2(final String password, final String random64code, final String token, final int serverIteration) { - Method calculateH2Method = OpenGaussAuthenticationHandler.class.getDeclaredMethod("calculateH2", String.class, String.class, String.class, int.class); - calculateH2Method.setAccessible(true); - return (byte[]) calculateH2Method.invoke(null, password, random64code, token, serverIteration); + return (byte[]) Plugins.getMemberAccessor().invoke( + OpenGaussAuthenticationHandler.class.getDeclaredMethod("hexStringToBytes", String.class), OpenGaussAuthenticationHandler.class, rawHexString); } @SneakyThrows(ReflectiveOperationException.class) private static byte[] xor(final byte[] value1, final byte[] value2) { - Method xorMethod = OpenGaussAuthenticationHandler.class.getDeclaredMethod("xor", byte[].class, byte[].class); - xorMethod.setAccessible(true); - return (byte[]) xorMethod.invoke(null, value1, value2); + return (byte[]) Plugins.getMemberAccessor().invoke( + OpenGaussAuthenticationHandler.class.getDeclaredMethod("xor", byte[].class, byte[].class), OpenGaussAuthenticationHandler.class, value1, value2); } private static void bytesToHex(final byte[] bytes, final byte[] hex, final int offset, final int length) { diff --git a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java index 0c9f6c405bd02..6e2f7727200f6 100644 --- a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java +++ b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java @@ -29,7 +29,6 @@ import org.apache.shardingsphere.db.protocol.CommonConstants; import org.apache.shardingsphere.db.protocol.payload.PacketPayload; import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.authentication.PostgreSQLMD5PasswordAuthenticationPacket; -import org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLIdentifierPacket; import org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload; import org.apache.shardingsphere.dialect.postgresql.exception.authority.EmptyUsernameException; import org.apache.shardingsphere.dialect.postgresql.exception.authority.InvalidPasswordException; @@ -53,11 +52,11 @@ import org.mockito.Answers; import org.mockito.ArgumentCaptor; import org.mockito.Mock; +import org.mockito.internal.configuration.plugins.Plugins; import org.mockito.internal.util.reflection.FieldReader; import org.mockito.internal.util.reflection.InstanceField; import org.mockito.junit.MockitoJUnitRunner; -import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.LinkedHashMap; @@ -135,12 +134,11 @@ public void assertLoginFailed() { @Test public void assertGetIdentifierPacket() throws ReflectiveOperationException { - Method method = PostgreSQLAuthenticationEngine.class.getDeclaredMethod("getIdentifierPacket", String.class); - method.setAccessible(true); - PostgreSQLIdentifierPacket packet = (PostgreSQLIdentifierPacket) method.invoke(new PostgreSQLAuthenticationEngine(), username); - assertThat(packet, instanceOf(PostgreSQLMD5PasswordAuthenticationPacket.class)); + assertThat(Plugins.getMemberAccessor().invoke(PostgreSQLAuthenticationEngine.class.getDeclaredMethod("getIdentifierPacket", String.class), new PostgreSQLAuthenticationEngine(), username), + instanceOf(PostgreSQLMD5PasswordAuthenticationPacket.class)); } + @SneakyThrows(ReflectiveOperationException.class) private void assertLogin(final String inputPassword) { PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(createByteBuf(16, 128), StandardCharsets.UTF_8); payload.writeInt4(64); @@ -158,7 +156,8 @@ private void assertLogin(final String inputPassword) { PostgreSQLMD5PasswordAuthenticationPacket md5PasswordPacket = argumentCaptor.getValue(); byte[] md5Salt = getMd5Salt(md5PasswordPacket); payload = new PostgreSQLPacketPayload(createByteBuf(16, 128), StandardCharsets.UTF_8); - String md5Digest = md5Encode(username, inputPassword, md5Salt); + String md5Digest = (String) Plugins.getMemberAccessor().invoke(PostgreSQLMD5PasswordAuthenticator.class.getDeclaredMethod("md5Encode", String.class, String.class, byte[].class), + new PostgreSQLMD5PasswordAuthenticator(), username, inputPassword, md5Salt); payload.writeInt1('p'); payload.writeInt4(4 + md5Digest.length() + 1); payload.writeStringNul(md5Digest); @@ -175,25 +174,18 @@ private ByteBuf createByteBuf(final int initialCapacity, final int maxCapacity) } private MetaDataContexts getMetaDataContexts(final ShardingSphereUser user) { - return new MetaDataContexts(mock(MetaDataPersistService.class), - new ShardingSphereMetaData(new LinkedHashMap<>(), buildGlobalRuleMetaData(user), new ConfigurationProperties(new Properties()))); + return new MetaDataContexts( + mock(MetaDataPersistService.class), new ShardingSphereMetaData(new LinkedHashMap<>(), buildGlobalRuleMetaData(user), new ConfigurationProperties(new Properties()))); } private ShardingSphereRuleMetaData buildGlobalRuleMetaData(final ShardingSphereUser user) { AuthorityRuleConfiguration ruleConfig = new AuthorityRuleConfiguration(Collections.singletonList(user), new AlgorithmConfiguration("ALL_PERMITTED", new Properties())); AuthorityRule rule = new AuthorityRuleBuilder().build(ruleConfig, Collections.emptyMap(), mock(InstanceContext.class), mock(ConfigurationProperties.class)); - return new ShardingSphereRuleMetaData(Collections.singletonList(rule)); + return new ShardingSphereRuleMetaData(Collections.singleton(rule)); } @SneakyThrows(NoSuchFieldException.class) private byte[] getMd5Salt(final PostgreSQLMD5PasswordAuthenticationPacket md5PasswordPacket) { return (byte[]) new FieldReader(md5PasswordPacket, PostgreSQLMD5PasswordAuthenticationPacket.class.getDeclaredField("md5Salt")).read(); } - - @SneakyThrows(ReflectiveOperationException.class) - private String md5Encode(final String username, final String password, final byte[] md5Salt) { - Method method = PostgreSQLMD5PasswordAuthenticator.class.getDeclaredMethod("md5Encode", String.class, String.class, byte[].class); - method.setAccessible(true); - return (String) method.invoke(new PostgreSQLMD5PasswordAuthenticator(), username, password, md5Salt); - } } diff --git a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandlerTest.java b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandlerTest.java index c8239ea6abdb4..b63bbaaf5f05e 100644 --- a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandlerTest.java +++ b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationHandlerTest.java @@ -48,8 +48,8 @@ import org.apache.shardingsphere.proxy.frontend.postgresql.authentication.authenticator.PostgreSQLMD5PasswordAuthenticator; import org.junit.Before; import org.junit.Test; +import org.mockito.internal.configuration.plugins.Plugins; -import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.HashMap; @@ -80,7 +80,7 @@ public final class PostgreSQLAuthenticationHandlerTest extends ProxyContextResto @Before public void init() { PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(createByteBuf(16, 128), StandardCharsets.UTF_8); - String md5Digest = md5Encode(username, password, md5Salt.getBytes(StandardCharsets.UTF_8)); + String md5Digest = md5Encode(md5Salt.getBytes(StandardCharsets.UTF_8)); payload.writeInt4(4 + md5Digest.length() + 1); payload.writeStringNul(md5Digest); passwordMessagePacket = new PostgreSQLPasswordMessagePacket(payload); @@ -154,9 +154,8 @@ private ShardingSphereRuleMetaData buildGlobalRuleMetaData(final ShardingSphereU } @SneakyThrows(ReflectiveOperationException.class) - private String md5Encode(final String username, final String password, final byte[] md5Salt) { - Method method = PostgreSQLMD5PasswordAuthenticator.class.getDeclaredMethod("md5Encode", String.class, String.class, byte[].class); - method.setAccessible(true); - return (String) method.invoke(new PostgreSQLMD5PasswordAuthenticator(), username, password, md5Salt); + private String md5Encode(final byte[] md5Salt) { + return (String) Plugins.getMemberAccessor().invoke(PostgreSQLMD5PasswordAuthenticator.class.getDeclaredMethod("md5Encode", String.class, String.class, byte[].class), + new PostgreSQLMD5PasswordAuthenticator(), username, password, md5Salt); } } diff --git a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/PostgreSQLMD5PasswordAuthenticatorTest.java b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/PostgreSQLMD5PasswordAuthenticatorTest.java index f5b4890afdcd6..591cf63888ec7 100644 --- a/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/PostgreSQLMD5PasswordAuthenticatorTest.java +++ b/proxy/frontend/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/authenticator/PostgreSQLMD5PasswordAuthenticatorTest.java @@ -22,12 +22,11 @@ import org.apache.shardingsphere.db.protocol.postgresql.packet.handshake.PostgreSQLRandomGenerator; import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser; import org.junit.Test; - -import java.lang.reflect.Method; +import org.mockito.internal.configuration.plugins.Plugins; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertFalse; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public final class PostgreSQLMD5PasswordAuthenticatorTest { @@ -47,15 +46,14 @@ public void assertGetAuthenticationMethodName() { public void assertAuthenticate() { ShardingSphereUser user = new ShardingSphereUser(username, password, ""); byte[] md5Salt = PostgreSQLRandomGenerator.getInstance().generateRandomBytes(4); - String md5Digest = md5Encode(username, password, md5Salt); + String md5Digest = md5Encode(md5Salt); assertTrue(authenticator.authenticate(user, new Object[]{md5Digest, md5Salt})); assertFalse(authenticator.authenticate(user, new Object[]{"wrong", md5Salt})); } @SneakyThrows(ReflectiveOperationException.class) - private String md5Encode(final String username, final String password, final byte[] md5Salt) { - Method method = PostgreSQLMD5PasswordAuthenticator.class.getDeclaredMethod("md5Encode", String.class, String.class, byte[].class); - method.setAccessible(true); - return (String) method.invoke(new PostgreSQLMD5PasswordAuthenticator(), username, password, md5Salt); + private String md5Encode(final byte[] md5Salt) { + return (String) Plugins.getMemberAccessor().invoke(PostgreSQLMD5PasswordAuthenticator.class.getDeclaredMethod("md5Encode", String.class, String.class, byte[].class), + new PostgreSQLMD5PasswordAuthenticator(), username, password, md5Salt); } } diff --git a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/scenario/consistencycheck/ConsistencyCheckJobTest.java b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/scenario/consistencycheck/ConsistencyCheckJobTest.java index 3eb9a5bc50d83..d2e71f1edf22c 100644 --- a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/scenario/consistencycheck/ConsistencyCheckJobTest.java +++ b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/scenario/consistencycheck/ConsistencyCheckJobTest.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.test.it.data.pipeline.scenario.consistencycheck; -import org.apache.shardingsphere.data.pipeline.api.config.job.ConsistencyCheckJobConfiguration; import org.apache.shardingsphere.data.pipeline.api.job.JobStatus; import org.apache.shardingsphere.data.pipeline.core.api.PipelineAPIFactory; import org.apache.shardingsphere.data.pipeline.core.job.AbstractPipelineJob; @@ -25,15 +24,14 @@ import org.apache.shardingsphere.data.pipeline.scenario.consistencycheck.ConsistencyCheckJob; import org.apache.shardingsphere.data.pipeline.scenario.consistencycheck.context.ConsistencyCheckJobItemContext; import org.apache.shardingsphere.data.pipeline.yaml.job.YamlConsistencyCheckJobConfiguration; -import org.apache.shardingsphere.data.pipeline.yaml.job.YamlConsistencyCheckJobConfigurationSwapper; import org.apache.shardingsphere.elasticjob.api.ShardingContext; import org.apache.shardingsphere.infra.util.yaml.YamlEngine; import org.apache.shardingsphere.test.it.data.pipeline.core.util.PipelineContextUtil; import org.junit.BeforeClass; import org.junit.Test; +import org.mockito.internal.configuration.plugins.Plugins; -import java.lang.reflect.Method; -import java.util.HashMap; +import java.util.Collections; import java.util.Map; import static org.hamcrest.MatcherAssert.assertThat; @@ -48,25 +46,27 @@ public static void beforeClass() { @Test public void assertBuildPipelineJobItemContext() throws ReflectiveOperationException { - YamlConsistencyCheckJobItemProgress jobItemProgress = new YamlConsistencyCheckJobItemProgress(); - jobItemProgress.setStatus(JobStatus.RUNNING.name()); - Map expectTableCheckPosition = new HashMap<>(); - expectTableCheckPosition.put("t_order", 100); - jobItemProgress.setTableCheckPositions(expectTableCheckPosition); String checkJobId = "j0201001"; - PipelineAPIFactory.getGovernanceRepositoryAPI().persistJobItemProgress(checkJobId, 0, YamlEngine.marshal(jobItemProgress)); - ConsistencyCheckJobConfiguration jobConfig = new ConsistencyCheckJobConfiguration(checkJobId, "", null, null); - YamlConsistencyCheckJobConfiguration yamlJobConfig = new YamlConsistencyCheckJobConfigurationSwapper().swapToYamlConfiguration(jobConfig); - ShardingContext shardingContext = new ShardingContext(checkJobId, "", 1, YamlEngine.marshal(yamlJobConfig), 0, ""); + Map expectTableCheckPosition = Collections.singletonMap("t_order", 100); + PipelineAPIFactory.getGovernanceRepositoryAPI().persistJobItemProgress(checkJobId, 0, YamlEngine.marshal(createYamlConsistencyCheckJobItemProgress(expectTableCheckPosition))); ConsistencyCheckJob consistencyCheckJob = new ConsistencyCheckJob(); - invokeSetJobId(checkJobId, consistencyCheckJob); - ConsistencyCheckJobItemContext actualItemContext = consistencyCheckJob.buildPipelineJobItemContext(shardingContext); - assertThat(actualItemContext.getProgressContext().getTableCheckPositions(), is(expectTableCheckPosition)); + Plugins.getMemberAccessor().invoke(AbstractPipelineJob.class.getDeclaredMethod("setJobId", String.class), consistencyCheckJob, checkJobId); + ConsistencyCheckJobItemContext actual = consistencyCheckJob.buildPipelineJobItemContext( + new ShardingContext(checkJobId, "", 1, YamlEngine.marshal(createYamlConsistencyCheckJobConfiguration(checkJobId)), 0, "")); + assertThat(actual.getProgressContext().getTableCheckPositions(), is(expectTableCheckPosition)); } - private void invokeSetJobId(final String checkJobId, final ConsistencyCheckJob consistencyCheckJob) throws ReflectiveOperationException { - Method method = AbstractPipelineJob.class.getDeclaredMethod("setJobId", String.class); - method.setAccessible(true); - method.invoke(consistencyCheckJob, checkJobId); + private YamlConsistencyCheckJobItemProgress createYamlConsistencyCheckJobItemProgress(final Map expectTableCheckPosition) { + YamlConsistencyCheckJobItemProgress result = new YamlConsistencyCheckJobItemProgress(); + result.setStatus(JobStatus.RUNNING.name()); + result.setTableCheckPositions(expectTableCheckPosition); + return result; + } + + private YamlConsistencyCheckJobConfiguration createYamlConsistencyCheckJobConfiguration(final String checkJobId) { + YamlConsistencyCheckJobConfiguration result = new YamlConsistencyCheckJobConfiguration(); + result.setJobId(checkJobId); + result.setParentJobId(""); + return result; } }