Skip to content

Commit

Permalink
Update Mockito and ByteBuddy to help with GraalVM integration (#17665)
Browse files Browse the repository at this point in the history
* Update Mockito to 4.5.1 to help with GraalVM integration.

* Update Mockito to interim version 3.5.0, and fix unit tests.

* Adjusted JUnits for a single class import.

* Update Mockito to interim version 3.12.4.

* Update Mockito to interim version 4.0.0.

* Update Mockito to latest version 4.5.1.

* Downgrade Mockito to interim version 4.4.0.

* Update ByteBuddy version to fix issues with unit tests.

* Re-update the version of Mockito to 4.5.1.

* Restored unit tests for the first version of ComposeConstructorAdviceTest to handle the limitations of the new version of Mockito.

* Restored ComposeConstructorAdviceTest's Unit test Name.

* Fix ComposeConstructorAdviceTest#assertOnConstructor.

* Fix CheckStyle.

* Fix ComposeConstructorAdviceTest#assertOnConstructor again.

* Remove ComposeConstructorAdviceTest#assertOnConstructor 's Mock.
  • Loading branch information
linghengqian committed May 16, 2022
1 parent ddb42e6 commit 403adb4
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 64 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<springframework.version>[4.3.6.RELEASE,5.0.0.M1)</springframework.version>
<spring-boot.version>[1.5.20.RELEASE,2.0.0.M1)</spring-boot.version>

<bytebuddy.version>1.11.18</bytebuddy.version>
<bytebuddy.version>1.12.10</bytebuddy.version>
<prometheus.version>0.11.0</prometheus.version>
<prometheus.jmx.version>0.16.1</prometheus.jmx.version>
<opentracing.version>0.31.0</opentracing.version>
Expand All @@ -121,7 +121,7 @@

<junit.version>4.12</junit.version>
<hamcrest.version>1.3</hamcrest.version>
<mockito.version>3.4.2</mockito.version>
<mockito.version>4.5.1</mockito.version>

<!-- Plugin versions -->
<takari-maven-plugin.version>0.6.1</takari-maven-plugin.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.util.reflection.FieldReader;
import org.mockito.internal.util.reflection.FieldSetter;
import org.mockito.plugins.MemberAccessor;

import java.util.HashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -89,7 +90,8 @@ public static void setup() {
.build()
.install();
interceptorPointMap.put(interceptorPointInTwice.getClassNameOfTarget(), interceptorPointInTwice);
FieldSetter.setField(LOADER, LOADER.getClass().getDeclaredField("interceptorPointMap"), interceptorPointMap);
MemberAccessor accessor = Plugins.getMemberAccessor();
accessor.set(LOADER.getClass().getDeclaredField("interceptorPointMap"), LOADER, interceptorPointMap);
byteBuddyAgent = new AgentBuilder.Default().with(new ByteBuddy().with(TypeValidation.ENABLED))
.ignore(ElementMatchers.isSynthetic()).or(ElementMatchers.nameStartsWith("org.apache.shardingsphere.agent.")
.and(ElementMatchers.not(ElementMatchers.nameStartsWith("org.apache.shardingsphere.agent.core.mock"))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@

import org.apache.shardingsphere.agent.api.advice.AdviceTargetObject;
import org.apache.shardingsphere.agent.api.advice.ConstructorAdvice;
import org.apache.shardingsphere.agent.core.mock.advice.MockConstructorAdvice;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Collections;
import java.util.LinkedList;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public final class ComposeConstructorAdviceTest {

@Mock
private ConstructorAdvice constructorAdvice;
private final ConstructorAdvice constructorAdvice = new MockConstructorAdvice();

private ComposeConstructorAdvice actual;

Expand All @@ -47,7 +46,7 @@ public void setUp() {
public void assertOnConstructor() {
AdviceTargetObject adviceTargetObject = mock(AdviceTargetObject.class);
Object[] args = new Object[2];
args[0] = new LinkedList<String>();
actual.onConstructor(adviceTargetObject, args);
verify(constructorAdvice).onConstructor(adviceTargetObject, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.util.reflection.FieldReader;
import org.mockito.internal.util.reflection.FieldSetter;
import org.mockito.plugins.MemberAccessor;

import java.util.Collections;
import java.util.Map;
Expand All @@ -51,7 +52,7 @@ public final class ApmPluginLoaderTest {

@BeforeClass
@SuppressWarnings("unchecked")
public static void setup() throws NoSuchFieldException {
public static void setup() throws NoSuchFieldException, IllegalAccessException {
FieldReader objectPoolReader = new FieldReader(LOADER, LOADER.getClass().getDeclaredField("objectPool"));
Map<String, Object> objectPool = (Map<String, Object>) objectPoolReader.read();
objectPool.put(MockConstructorAdvice.class.getTypeName(), new MockConstructorAdvice());
Expand All @@ -68,7 +69,8 @@ public static void setup() throws NoSuchFieldException {
.implement(MockConstructorAdvice.class.getTypeName())
.build()
.install();
FieldSetter.setField(LOADER, LOADER.getClass().getDeclaredField("interceptorPointMap"), Collections.singletonMap(interceptorPoint.getClassNameOfTarget(), interceptorPoint));
MemberAccessor accessor = Plugins.getMemberAccessor();
accessor.set(LOADER.getClass().getDeclaredField("interceptorPointMap"), LOADER, Collections.singletonMap(interceptorPoint.getClassNameOfTarget(), interceptorPoint));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sql92.dml.SQL92SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.dml.SQLServerSelectStatement;
import org.junit.Test;
import org.mockito.internal.util.reflection.FieldSetter;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.plugins.MemberAccessor;

import java.sql.SQLException;
import java.util.Arrays;
Expand All @@ -55,31 +56,31 @@
public final class OrderByValueTest {

@Test
public void assertCompareToForAscForMySQL() throws SQLException, NoSuchFieldException {
public void assertCompareToForAscForMySQL() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToForAsc(new MySQLSelectStatement());
}

@Test
public void assertCompareToForAscForOracle() throws SQLException, NoSuchFieldException {
public void assertCompareToForAscForOracle() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToForAsc(new OracleSelectStatement());
}

@Test
public void assertCompareToForAscForPostgreSQL() throws SQLException, NoSuchFieldException {
public void assertCompareToForAscForPostgreSQL() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToForAsc(new PostgreSQLSelectStatement());
}

@Test
public void assertCompareToForAscForSQL92() throws SQLException, NoSuchFieldException {
public void assertCompareToForAscForSQL92() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToForAsc(new SQL92SelectStatement());
}

@Test
public void assertCompareToForAscForSQLServer() throws SQLException, NoSuchFieldException {
public void assertCompareToForAscForSQLServer() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToForAsc(new SQLServerSelectStatement());
}

private void assertCompareToForAsc(final SelectStatement selectStatement) throws SQLException, NoSuchFieldException {
private void assertCompareToForAsc(final SelectStatement selectStatement) throws SQLException, NoSuchFieldException, IllegalAccessException {
ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
selectStatement.setProjections(projectionsSegment);
ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
Expand All @@ -92,46 +93,47 @@ private void assertCompareToForAsc(final SelectStatement selectStatement) throws
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)),
createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC))),
selectStatementContext, schema);
FieldSetter.setField(orderByValue1, OrderByValue.class.getDeclaredField("orderValuesCaseSensitive"), Arrays.asList(false, false));
MemberAccessor accessor = Plugins.getMemberAccessor();
accessor.set(OrderByValue.class.getDeclaredField("orderValuesCaseSensitive"), orderByValue1, Arrays.asList(false, false));
assertTrue(orderByValue1.next());
QueryResult queryResult2 = createQueryResult("3", "4");
OrderByValue orderByValue2 = new OrderByValue(queryResult2, Arrays.asList(
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)),
createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC))),
selectStatementContext, schema);
FieldSetter.setField(orderByValue2, OrderByValue.class.getDeclaredField("orderValuesCaseSensitive"), Arrays.asList(false, false));
accessor.set(OrderByValue.class.getDeclaredField("orderValuesCaseSensitive"), orderByValue2, Arrays.asList(false, false));
assertTrue(orderByValue2.next());
assertTrue(orderByValue1.compareTo(orderByValue2) < 0);
assertFalse(orderByValue1.getQueryResult().next());
assertFalse(orderByValue2.getQueryResult().next());
}

@Test
public void assertCompareToForDescForMySQL() throws SQLException, NoSuchFieldException {
public void assertCompareToForDescForMySQL() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToForDesc(new MySQLSelectStatement());
}

@Test
public void assertCompareToForDescForOracle() throws SQLException, NoSuchFieldException {
public void assertCompareToForDescForOracle() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToForDesc(new OracleSelectStatement());
}

@Test
public void assertCompareToForDescForPostgreSQL() throws SQLException, NoSuchFieldException {
public void assertCompareToForDescForPostgreSQL() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToForDesc(new PostgreSQLSelectStatement());
}

@Test
public void assertCompareToForDescForSQL92() throws SQLException, NoSuchFieldException {
public void assertCompareToForDescForSQL92() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToForDesc(new SQL92SelectStatement());
}

@Test
public void assertCompareToForDescForSQLServer() throws SQLException, NoSuchFieldException {
public void assertCompareToForDescForSQLServer() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToForDesc(new SQLServerSelectStatement());
}

private void assertCompareToForDesc(final SelectStatement selectStatement) throws SQLException, NoSuchFieldException {
private void assertCompareToForDesc(final SelectStatement selectStatement) throws SQLException, NoSuchFieldException, IllegalAccessException {
ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
selectStatement.setProjections(projectionsSegment);
ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
Expand All @@ -145,46 +147,47 @@ private void assertCompareToForDesc(final SelectStatement selectStatement) throw
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)),
createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))),
selectStatementContext, schema);
FieldSetter.setField(orderByValue1, OrderByValue.class.getDeclaredField("orderValuesCaseSensitive"), Arrays.asList(false, false));
MemberAccessor accessor = Plugins.getMemberAccessor();
accessor.set(OrderByValue.class.getDeclaredField("orderValuesCaseSensitive"), orderByValue1, Arrays.asList(false, false));
assertTrue(orderByValue1.next());
QueryResult queryResult2 = createQueryResult("3", "4");
OrderByValue orderByValue2 = new OrderByValue(queryResult2, Arrays.asList(
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)),
createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))),
selectStatementContext, schema);
FieldSetter.setField(orderByValue2, OrderByValue.class.getDeclaredField("orderValuesCaseSensitive"), Arrays.asList(false, false));
accessor.set(OrderByValue.class.getDeclaredField("orderValuesCaseSensitive"), orderByValue2, Arrays.asList(false, false));
assertTrue(orderByValue2.next());
assertTrue(orderByValue1.compareTo(orderByValue2) > 0);
assertFalse(orderByValue1.getQueryResult().next());
assertFalse(orderByValue2.getQueryResult().next());
}

@Test
public void assertCompareToWhenEqualForMySQL() throws SQLException, NoSuchFieldException {
public void assertCompareToWhenEqualForMySQL() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToWhenEqual(new MySQLSelectStatement());
}

@Test
public void assertCompareToWhenEqualForOracle() throws SQLException, NoSuchFieldException {
public void assertCompareToWhenEqualForOracle() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToWhenEqual(new OracleSelectStatement());
}

@Test
public void assertCompareToWhenEqualForPostgreSQL() throws SQLException, NoSuchFieldException {
public void assertCompareToWhenEqualForPostgreSQL() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToWhenEqual(new PostgreSQLSelectStatement());
}

@Test
public void assertCompareToWhenEqualForSQL92() throws SQLException, NoSuchFieldException {
public void assertCompareToWhenEqualForSQL92() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToWhenEqual(new SQL92SelectStatement());
}

@Test
public void assertCompareToWhenEqualForSQLServer() throws SQLException, NoSuchFieldException {
public void assertCompareToWhenEqualForSQLServer() throws SQLException, NoSuchFieldException, IllegalAccessException {
assertCompareToWhenEqual(new SQLServerSelectStatement());
}

private void assertCompareToWhenEqual(final SelectStatement selectStatement) throws SQLException, NoSuchFieldException {
private void assertCompareToWhenEqual(final SelectStatement selectStatement) throws SQLException, NoSuchFieldException, IllegalAccessException {
ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
selectStatement.setProjections(projectionsSegment);
ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
Expand All @@ -197,14 +200,15 @@ private void assertCompareToWhenEqual(final SelectStatement selectStatement) thr
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)),
createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))),
selectStatementContext, schema);
FieldSetter.setField(orderByValue1, OrderByValue.class.getDeclaredField("orderValuesCaseSensitive"), Arrays.asList(false, false));
MemberAccessor accessor = Plugins.getMemberAccessor();
accessor.set(OrderByValue.class.getDeclaredField("orderValuesCaseSensitive"), orderByValue1, Arrays.asList(false, false));
assertTrue(orderByValue1.next());
QueryResult queryResult2 = createQueryResult("1", "2");
OrderByValue orderByValue2 = new OrderByValue(queryResult2, Arrays.asList(
createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)),
createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))),
selectStatementContext, schema);
FieldSetter.setField(orderByValue2, OrderByValue.class.getDeclaredField("orderValuesCaseSensitive"), Arrays.asList(false, false));
accessor.set(OrderByValue.class.getDeclaredField("orderValuesCaseSensitive"), orderByValue2, Arrays.asList(false, false));
assertTrue(orderByValue2.next());
assertThat(orderByValue1.compareTo(orderByValue2), is(0));
assertFalse(orderByValue1.getQueryResult().next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.internal.util.reflection.FieldSetter;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.plugins.MemberAccessor;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
Expand Down Expand Up @@ -103,12 +104,14 @@ public void setUp() {
@SneakyThrows(ReflectiveOperationException.class)
private void setClient() {
mockClient();
FieldSetter.setField(repository, repository.getClass().getDeclaredField("client"), client);
MemberAccessor accessor = Plugins.getMemberAccessor();
accessor.set(repository.getClass().getDeclaredField("client"), repository, client);
}

@SneakyThrows(ReflectiveOperationException.class)
private void setProperties() {
FieldSetter.setField(repository, repository.getClass().getDeclaredField("etcdProps"), new EtcdProperties(new Properties()));
MemberAccessor accessor = Plugins.getMemberAccessor();
accessor.set(repository.getClass().getDeclaredField("etcdProps"), repository, new EtcdProperties(new Properties()));
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -264,7 +267,7 @@ public void assertGetChildrenKeysWhenThrowExecutionException() throws ExecutionE
}
}

@SneakyThrows({NoSuchFieldException.class, SecurityException.class})
@SneakyThrows({NoSuchFieldException.class, SecurityException.class, IllegalAccessException.class})
private WatchResponse buildWatchResponse(final WatchEvent.EventType eventType) {
WatchResponse result = new WatchResponse(mock(io.etcd.jetcd.api.WatchResponse.class), ByteSequence.EMPTY);
List<WatchEvent> events = new LinkedList<>();
Expand All @@ -273,7 +276,8 @@ private WatchResponse buildWatchResponse(final WatchEvent.EventType eventType) {
.setValue(ByteString.copyFromUtf8("value1")).build();
KeyValue keyValue = new KeyValue(keyValue1, ByteSequence.EMPTY);
events.add(new WatchEvent(keyValue, mock(KeyValue.class), eventType));
FieldSetter.setField(result, result.getClass().getDeclaredField("events"), events);
MemberAccessor accessor = Plugins.getMemberAccessor();
accessor.set(result.getClass().getDeclaredField("events"), result, events);
return result;
}
}

0 comments on commit 403adb4

Please sign in to comment.